public void testParserCygwin() throws Exception {
    final Map map = checkStream("CYGWIN_NT-5.1");
    final FileAttributes o =
        (FileAttributes)
            map.get(
                "src/main/java/org/codehaus/plexus/components/io/attributes/AttributeConstants.java");

    // -rw-r--r--  1 1020 1030   11108 Mar 16 22:42 build.xml
    assertEquals("-rw-r--r--", new String(o.getLsModeParts()));
    assertEquals(203222, o.getUserId().intValue());
    assertEquals(10513, o.getGroupId().intValue());
  }
  public void testParserUbuntu10_04_en() throws Exception {
    final Map map = checkStream("Linux");

    final FileAttributes o =
        (FileAttributes)
            map.get(
                "src/main/java/org/codehaus/plexus/components/io/attributes/AttributeConstants.java");

    // -rw-r--r--  1 1020 1030   11108 Mar 16 22:42 build.xml
    assertEquals("-rw-rw-r--", new String(o.getLsModeParts()));
    assertEquals(1020, o.getUserId().intValue());
    assertEquals(1030, o.getGroupId().intValue());
    // Should probably test pass 2 too...
  }
  public static boolean delete(@NotNull File file) {
    FileAttributes attributes = FileSystemUtil.getAttributes(file);
    if (attributes == null) return true;

    if (attributes.isDirectory() && !attributes.isSymLink()) {
      File[] files = file.listFiles();
      if (files != null) {
        for (File child : files) {
          if (!delete(child)) return false;
        }
      }
    }

    return deleteFile(file);
  }
  public void testReversedMonthDayOrder() throws Exception {
    String output = //
        "-rw-r--r--   1 501  80  7683 31 May 10:06 pom_newer.xml\n"
            + //
            "-rwxr--r--   1 502  81  7683  1 Jun 2010  pom_older.xml";
    InputStream byteArrayInputStream = new ByteArrayInputStream(output.getBytes());
    NumericUserIDAttributeParser parser = getNumericParser();
    parse(byteArrayInputStream, parser);
    Map<String, PlexusIoResourceAttributes> map = parser.getAttributesByPath();

    // 6 months or newer ls date format
    FileAttributes newerFileAttr = (FileAttributes) map.get("pom_newer.xml");
    assertNotNull(newerFileAttr);
    assertEquals("-rw-r--r--", new String(newerFileAttr.getLsModeParts()));
    assertEquals(501, newerFileAttr.getUserId().intValue());
    assertEquals(80, newerFileAttr.getGroupId().intValue());

    // older than 6 months ls date format
    FileAttributes olderFileAttr = (FileAttributes) map.get("pom_older.xml");
    assertNotNull(olderFileAttr);
    assertEquals("-rwxr--r--", new String(olderFileAttr.getLsModeParts()));
    assertEquals(502, olderFileAttr.getUserId().intValue());
    assertEquals(81, olderFileAttr.getGroupId().intValue());
  }
示例#5
0
  @Override
  @Nullable
  public VirtualFileSystemEntry findRoot(
      @NotNull String basePath, @NotNull NewVirtualFileSystem fs) {
    if (basePath.isEmpty()) {
      LOG.error("Invalid root, fs=" + fs);
      return null;
    }

    String rootUrl = normalizeRootUrl(basePath, fs);

    myRootsLock.readLock().lock();
    try {
      VirtualFileSystemEntry root = myRoots.get(rootUrl);
      if (root != null) return root;
    } finally {
      myRootsLock.readLock().unlock();
    }

    final VirtualFileSystemEntry newRoot;
    int rootId = FSRecords.findRootRecord(rootUrl);

    VfsData.Segment segment = VfsData.getSegment(rootId, true);
    VfsData.DirectoryData directoryData = new VfsData.DirectoryData();
    if (fs instanceof ArchiveFileSystem) {
      String parentPath =
          basePath.substring(0, basePath.indexOf(ArchiveFileSystem.ARCHIVE_SEPARATOR));
      VirtualFile parentFile = LocalFileSystem.getInstance().findFileByPath(parentPath);
      if (parentFile == null) return null;
      FileType type = FileTypeRegistry.getInstance().getFileTypeByFileName(parentFile.getName());
      if (!(type instanceof ArchiveFileType)) return null;
      newRoot = new ArchiveRoot(fs, rootId, segment, directoryData, parentFile);
    } else {
      newRoot = new FsRoot(fs, rootId, segment, directoryData, basePath);
    }

    FileAttributes attributes =
        fs.getAttributes(
            new StubVirtualFile() {
              @NotNull
              @Override
              public String getPath() {
                return newRoot.getPath();
              }

              @Nullable
              @Override
              public VirtualFile getParent() {
                return null;
              }
            });
    if (attributes == null || !attributes.isDirectory()) {
      return null;
    }

    boolean mark = false;

    myRootsLock.writeLock().lock();
    try {
      VirtualFileSystemEntry root = myRoots.get(rootUrl);
      if (root != null) return root;

      VfsData.initFile(rootId, segment, -1, directoryData);
      mark = writeAttributesToRecord(rootId, 0, newRoot, fs, attributes);

      myRoots.put(rootUrl, newRoot);
      myRootsById.put(rootId, newRoot);
    } finally {
      myRootsLock.writeLock().unlock();
    }

    if (!mark && attributes.lastModified != FSRecords.getTimestamp(rootId)) {
      newRoot.markDirtyRecursively();
    }

    LOG.assertTrue(
        rootId == newRoot.getId(),
        "root=" + newRoot + " expected=" + rootId + " actual=" + newRoot.getId());

    return newRoot;
  }
  @Override
  @Nullable
  public VirtualFileSystemEntry findRoot(
      @NotNull String basePath, @NotNull NewVirtualFileSystem fs) {
    String rootUrl = normalizeRootUrl(basePath, fs);
    boolean isFakeRoot = basePath.isEmpty();
    VirtualFileSystemEntry root;

    myRootsLock.readLock().lock();
    try {
      root = isFakeRoot ? mySuperRoot : myRoots.get(rootUrl);
      if (root != null) return root;
    } finally {
      myRootsLock.readLock().unlock();
    }

    myRootsLock.writeLock().lock();
    try {
      root = isFakeRoot ? mySuperRoot : myRoots.get(rootUrl);
      if (root != null) return root;

      int rootId = FSRecords.findRootRecord(rootUrl);
      root = myRootsById.get(rootId);
      if (root != null) return root;

      if (isFakeRoot) {
        // fake super-root
        root = new FakeRoot(fs, rootId);
      } else if (fs instanceof JarFileSystem) {
        // optimization: for jar roots do not store base path in the myName field, use local FS
        // file's getPath()
        String parentPath = basePath.substring(0, basePath.indexOf(JarFileSystem.JAR_SEPARATOR));
        VirtualFile parentLocalFile = LocalFileSystem.getInstance().findFileByPath(parentPath);
        if (parentLocalFile == null) return null;

        // check one more time since the findFileByPath could have created the root (by reentering
        // the findRoot)
        root = myRoots.get(rootUrl);
        if (root != null) return root;
        root = myRootsById.get(rootId);
        if (root != null) return root;

        root = new JarRoot(fs, rootId, parentLocalFile);
      } else {
        root = new FsRoot(fs, rootId, basePath);
      }

      if (isFakeRoot) {
        mySuperRoot = root;
      } else {
        FileAttributes attributes = fs.getAttributes(root);
        if (attributes == null || !attributes.isDirectory()) {
          return null;
        }
        final boolean newRoot = writeAttributesToRecord(rootId, 0, root, fs, attributes);
        if (!newRoot && attributes.lastModified != FSRecords.getTimestamp(rootId)) {
          root.markDirtyRecursively();
        }

        myRoots.put(rootUrl, root);
        myRootsById.put(rootId, root);

        if (rootId != root.getId()) throw new AssertionError();
      }

      return root;
    } finally {
      myRootsLock.writeLock().unlock();
    }
  }