Exemplo n.º 1
0
  public FileAttrs getAttrs() throws IOException {
    FileAttrs result;
    BasicFileAttributes attr;
    DosFileAttributes dosAttr;
    PosixFileAttributes posixAttr;

    result = new FileAttrs();
    attr = Files.readAttributes(this.path.toPath(), BasicFileAttributes.class);

    result.setCtime(attr.creationTime().toMillis());
    result.setMtime(attr.lastModifiedTime().toMillis());
    // result.append("symlink", attr.isSymbolicLink()); //Redundant
    result.setSize(attr.size());

    if (System.getProperty("os.name").startsWith("Windows")) {
      dosAttr = Files.readAttributes(this.path.toPath(), DosFileAttributes.class);

      result.setDosArchive(dosAttr.isArchive());
      result.setDosHidden(dosAttr.isHidden());
      result.setDosReadonly(dosAttr.isReadOnly());
      result.setDosSystem(dosAttr.isSystem());
    } else {
      posixAttr = Files.readAttributes(this.path.toPath(), PosixFileAttributes.class);

      result.setPosixSymlink(this.isSymlink());

      if (result.getPosixSymlink()) {
        result.setLinkTo(Files.readSymbolicLink(this.path.toPath()).toString());
      }

      result.setPosixOwner(posixAttr.owner().getName());
      result.setPosixGroup(posixAttr.group().getName());
      result.setPosixPermission(PosixFilePermissions.toString(posixAttr.permissions()));
    }

    return result;
  }
Exemplo n.º 2
0
 static void checkPosixAttributes(PosixFileAttributes attrs1, PosixFileAttributes attrs2) {
   assertTrue(attrs1.permissions().equals(attrs2.permissions()));
   assertTrue(attrs1.owner().equals(attrs2.owner()));
   assertTrue(attrs1.group().equals(attrs2.group()));
 }
 /**
  * Invoked by readAttributes or sub-classes to add all matching posix attributes to the builder
  */
 final void addRequestedPosixAttributes(PosixFileAttributes attrs, AttributesBuilder builder) {
   addRequestedBasicAttributes(attrs, builder);
   if (builder.match(PERMISSIONS_NAME)) builder.add(PERMISSIONS_NAME, attrs.permissions());
   if (builder.match(OWNER_NAME)) builder.add(OWNER_NAME, attrs.owner());
   if (builder.match(GROUP_NAME)) builder.add(GROUP_NAME, attrs.group());
 }
  void assertPlugin(String name, Path original, Environment env) throws IOException {
    Path got = env.pluginsFile().resolve(name);
    assertTrue("dir " + name + " exists", Files.exists(got));

    if (isPosix) {
      Set<PosixFilePermission> perms = Files.getPosixFilePermissions(got);
      assertThat(
          perms,
          containsInAnyOrder(
              PosixFilePermission.OWNER_READ,
              PosixFilePermission.OWNER_WRITE,
              PosixFilePermission.OWNER_EXECUTE,
              PosixFilePermission.GROUP_READ,
              PosixFilePermission.GROUP_EXECUTE,
              PosixFilePermission.OTHERS_READ,
              PosixFilePermission.OTHERS_EXECUTE));
    }

    assertTrue("jar was copied", Files.exists(got.resolve("plugin.jar")));
    assertFalse("bin was not copied", Files.exists(got.resolve("bin")));
    assertFalse("config was not copied", Files.exists(got.resolve("config")));
    if (Files.exists(original.resolve("bin"))) {
      Path binDir = env.binFile().resolve(name);
      assertTrue("bin dir exists", Files.exists(binDir));
      assertTrue("bin is a dir", Files.isDirectory(binDir));
      PosixFileAttributes binAttributes = null;
      if (isPosix) {
        binAttributes = Files.readAttributes(env.binFile(), PosixFileAttributes.class);
      }
      try (DirectoryStream<Path> stream = Files.newDirectoryStream(binDir)) {
        for (Path file : stream) {
          assertFalse("not a dir", Files.isDirectory(file));
          if (isPosix) {
            PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
            assertEquals(InstallPluginCommand.BIN_FILES_PERMS, attributes.permissions());
          }
        }
      }
    }
    if (Files.exists(original.resolve("config"))) {
      Path configDir = env.configFile().resolve(name);
      assertTrue("config dir exists", Files.exists(configDir));
      assertTrue("config is a dir", Files.isDirectory(configDir));

      UserPrincipal user = null;
      GroupPrincipal group = null;

      if (isPosix) {
        PosixFileAttributes configAttributes =
            Files.getFileAttributeView(env.configFile(), PosixFileAttributeView.class)
                .readAttributes();
        user = configAttributes.owner();
        group = configAttributes.group();

        PosixFileAttributes attributes =
            Files.getFileAttributeView(configDir, PosixFileAttributeView.class).readAttributes();
        assertThat(attributes.owner(), equalTo(user));
        assertThat(attributes.group(), equalTo(group));
      }

      try (DirectoryStream<Path> stream = Files.newDirectoryStream(configDir)) {
        for (Path file : stream) {
          assertFalse("not a dir", Files.isDirectory(file));

          if (isPosix) {
            PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
            if (user != null) {
              assertThat(attributes.owner(), equalTo(user));
            }
            if (group != null) {
              assertThat(attributes.group(), equalTo(group));
            }
          }
        }
      }
    }
    assertInstallCleaned(env);
  }