use of java.nio.file.attribute.PosixFileAttributes in project elasticsearch by elastic.
the class InstallPluginCommandTests method assertPlugin.
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);
}
use of java.nio.file.attribute.PosixFileAttributes in project jimfs by google.
the class PosixAttributeProviderTest method testAttributes.
@Test
public void testAttributes() {
PosixFileAttributes attrs = provider.readAttributes(file);
assertThat(attrs.permissions()).isEqualTo(PosixFilePermissions.fromString("rw-r--r--"));
assertThat(attrs.group()).isEqualTo(createGroupPrincipal("group"));
assertThat(attrs.fileKey()).isEqualTo(0);
}
use of java.nio.file.attribute.PosixFileAttributes in project jimfs by google.
the class PosixAttributeProviderTest method testView.
@Test
public void testView() throws IOException {
file.setAttribute("owner", "owner", createUserPrincipal("user"));
PosixFileAttributeView view = provider.view(fileLookup(), ImmutableMap.of("basic", new BasicAttributeProvider().view(fileLookup(), NO_INHERITED_VIEWS), "owner", new OwnerAttributeProvider().view(fileLookup(), NO_INHERITED_VIEWS)));
assertNotNull(view);
assertThat(view.name()).isEqualTo("posix");
assertThat(view.getOwner()).isEqualTo(createUserPrincipal("user"));
PosixFileAttributes attrs = view.readAttributes();
assertThat(attrs.fileKey()).isEqualTo(0);
assertThat(attrs.owner()).isEqualTo(createUserPrincipal("user"));
assertThat(attrs.group()).isEqualTo(createGroupPrincipal("group"));
assertThat(attrs.permissions()).isEqualTo(PosixFilePermissions.fromString("rw-r--r--"));
view.setOwner(createUserPrincipal("root"));
assertThat(view.getOwner()).isEqualTo(createUserPrincipal("root"));
assertThat(file.getAttribute("owner", "owner")).isEqualTo(createUserPrincipal("root"));
view.setGroup(createGroupPrincipal("root"));
assertThat(view.readAttributes().group()).isEqualTo(createGroupPrincipal("root"));
assertThat(file.getAttribute("posix", "group")).isEqualTo(createGroupPrincipal("root"));
view.setPermissions(PosixFilePermissions.fromString("rwx------"));
assertThat(view.readAttributes().permissions()).isEqualTo(PosixFilePermissions.fromString("rwx------"));
assertThat(file.getAttribute("posix", "permissions")).isEqualTo(PosixFilePermissions.fromString("rwx------"));
}
use of java.nio.file.attribute.PosixFileAttributes in project neo4j by neo4j.
the class SelfSignedCertificatesIT method createSelfSignedCertificateWithCorrectPermissions.
@Test
public void createSelfSignedCertificateWithCorrectPermissions() throws Exception {
assumeTrue(!SystemUtils.IS_OS_WINDOWS);
Certificates certificates = new Certificates();
certificates.createSelfSignedCertificate(testDirectory.file("certificate"), testDirectory.file("privateKey"), "localhost");
PosixFileAttributes certificateAttributes = Files.getFileAttributeView(testDirectory.file("certificate").toPath(), PosixFileAttributeView.class).readAttributes();
assertTrue(certificateAttributes.permissions().contains(PosixFilePermission.OWNER_READ));
assertTrue(certificateAttributes.permissions().contains(PosixFilePermission.OWNER_WRITE));
assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.OWNER_EXECUTE));
assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.GROUP_READ));
assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.GROUP_WRITE));
assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.GROUP_EXECUTE));
assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.OTHERS_READ));
assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.OTHERS_WRITE));
assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.OTHERS_EXECUTE));
PosixFileAttributes privateKey = Files.getFileAttributeView(testDirectory.file("privateKey").toPath(), PosixFileAttributeView.class).readAttributes();
assertTrue(privateKey.permissions().contains(PosixFilePermission.OWNER_READ));
assertTrue(privateKey.permissions().contains(PosixFilePermission.OWNER_WRITE));
assertFalse(privateKey.permissions().contains(PosixFilePermission.OWNER_EXECUTE));
assertFalse(privateKey.permissions().contains(PosixFilePermission.GROUP_READ));
assertFalse(privateKey.permissions().contains(PosixFilePermission.GROUP_WRITE));
assertFalse(privateKey.permissions().contains(PosixFilePermission.GROUP_EXECUTE));
assertFalse(privateKey.permissions().contains(PosixFilePermission.OTHERS_READ));
assertFalse(privateKey.permissions().contains(PosixFilePermission.OTHERS_WRITE));
assertFalse(privateKey.permissions().contains(PosixFilePermission.OTHERS_EXECUTE));
}
use of java.nio.file.attribute.PosixFileAttributes in project syncany by syncany.
the class FileVersionComparator method captureFileProperties.
public FileProperties captureFileProperties(File file, FileChecksum knownChecksum, boolean forceChecksum) {
FileProperties fileProperties = new FileProperties();
fileProperties.relativePath = FileUtil.getRelativeDatabasePath(rootFolder, file);
Path filePath = null;
try {
filePath = Paths.get(file.getAbsolutePath());
fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
} catch (InvalidPathException e) {
// This throws an exception if the filename is invalid,
// e.g. colon in filename on windows "file:name"
logger.log(Level.FINE, "InvalidPath", e);
logger.log(Level.WARNING, "- Path '{0}' is invalid on this file system. It cannot exist. ", file.getAbsolutePath());
fileProperties.exists = false;
return fileProperties;
}
if (!fileProperties.exists) {
return fileProperties;
}
try {
// Read operating system dependent file attributes
BasicFileAttributes fileAttributes = null;
if (EnvironmentUtil.isWindows()) {
DosFileAttributes dosAttrs = Files.readAttributes(filePath, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
fileProperties.dosAttributes = FileUtil.dosAttrsToString(dosAttrs);
fileAttributes = dosAttrs;
} else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
PosixFileAttributes posixAttrs = Files.readAttributes(filePath, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
fileProperties.posixPermissions = PosixFilePermissions.toString(posixAttrs.permissions());
fileAttributes = posixAttrs;
} else {
fileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
}
fileProperties.lastModified = fileAttributes.lastModifiedTime().toMillis();
fileProperties.size = fileAttributes.size();
// Type
if (fileAttributes.isSymbolicLink()) {
fileProperties.type = FileType.SYMLINK;
fileProperties.linkTarget = FileUtil.readSymlinkTarget(file);
} else if (fileAttributes.isDirectory()) {
fileProperties.type = FileType.FOLDER;
fileProperties.linkTarget = null;
} else {
fileProperties.type = FileType.FILE;
fileProperties.linkTarget = null;
}
// Checksum
if (knownChecksum != null) {
fileProperties.checksum = knownChecksum;
} else {
if (fileProperties.type == FileType.FILE && forceChecksum) {
try {
if (fileProperties.size > 0) {
fileProperties.checksum = new FileChecksum(FileUtil.createChecksum(file, checksumAlgorithm));
} else {
fileProperties.checksum = null;
}
} catch (NoSuchAlgorithmException | IOException e) {
logger.log(Level.FINE, "Failed create checksum", e);
logger.log(Level.SEVERE, "SEVERE: Unable to create checksum for file {0}", file);
fileProperties.checksum = null;
}
} else {
fileProperties.checksum = null;
}
}
// Must be last (!), used for vanish-test later
fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
fileProperties.locked = fileProperties.exists && FileUtil.isFileLocked(file);
return fileProperties;
} catch (IOException e) {
logger.log(Level.FINE, "Failed to read file", e);
logger.log(Level.SEVERE, "SEVERE: Cannot read file {0}. Assuming file is locked.", file);
fileProperties.exists = true;
fileProperties.locked = true;
return fileProperties;
}
}
Aggregations