use of java.nio.file.attribute.PosixFilePermission in project buck by facebook.
the class ProjectFilesystemTest method testCreateZipPreservesExecutablePermissions.
@Test
public void testCreateZipPreservesExecutablePermissions() throws IOException {
// Create a empty executable file.
Path exe = tmp.newFile("test.exe");
MoreFiles.makeExecutable(exe);
// Archive it into a zipfile using `ProjectFileSystem.createZip`.
Path zipFile = tmp.getRoot().resolve("test.zip");
filesystem.createZip(ImmutableList.of(exe), zipFile);
// permissions.
try (ZipFile zip = new ZipFile(zipFile.toFile())) {
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
assertTrue(entries.hasMoreElements());
ZipArchiveEntry entry = entries.nextElement();
Set<PosixFilePermission> permissions = MorePosixFilePermissions.fromMode(entry.getExternalAttributes() >> 16);
assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE));
assertFalse(entries.hasMoreElements());
}
}
use of java.nio.file.attribute.PosixFilePermission in project buck by facebook.
the class ZipStepTest method zipMaintainsExecutablePermissions.
@Test
public void zipMaintainsExecutablePermissions() throws IOException {
assumeTrue(Platform.detect() != Platform.WINDOWS);
Path parent = tmp.newFolder("zipstep");
Path toZip = tmp.newFolder("zipdir");
Path file = toZip.resolve("foo.sh");
ImmutableSet<PosixFilePermission> filePermissions = ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ, PosixFilePermission.OTHERS_READ);
Files.createFile(file, PosixFilePermissions.asFileAttribute(filePermissions));
Path outputZip = parent.resolve("output.zip");
ZipStep step = new ZipStep(filesystem, outputZip, ImmutableSet.of(), false, ZipCompressionLevel.MIN_COMPRESSION_LEVEL, Paths.get("zipdir"));
assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode());
Path destination = tmp.newFolder("output");
Unzip.extractZipFile(outputZip, destination, Unzip.ExistingFileMode.OVERWRITE);
assertTrue(Files.isExecutable(destination.resolve("foo.sh")));
}
use of java.nio.file.attribute.PosixFilePermission 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.PosixFilePermission in project vert.x by eclipse.
the class FileSystemImpl method chmodInternal.
protected BlockingAction<Void> chmodInternal(String path, String perms, String dirPerms, Handler<AsyncResult<Void>> handler) {
Objects.requireNonNull(path);
Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(perms);
Set<PosixFilePermission> dirPermissions = dirPerms == null ? null : PosixFilePermissions.fromString(dirPerms);
return new BlockingAction<Void>(handler) {
public Void perform() {
try {
Path target = vertx.resolveFile(path).toPath();
if (dirPermissions != null) {
Files.walkFileTree(target, new SimpleFileVisitor<Path>() {
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
//The directory entries typically have different permissions to the files, e.g. execute permission
//or can't cd into it
Files.setPosixFilePermissions(dir, dirPermissions);
return FileVisitResult.CONTINUE;
}
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.setPosixFilePermissions(file, permissions);
return FileVisitResult.CONTINUE;
}
});
} else {
Files.setPosixFilePermissions(target, permissions);
}
} catch (SecurityException e) {
throw new FileSystemException("Accessed denied for chmod on " + path);
} catch (IOException e) {
throw new FileSystemException(e);
}
return null;
}
};
}
use of java.nio.file.attribute.PosixFilePermission in project zeppelin by apache.
the class InterpreterSettingManager method saveToFile.
public void saveToFile() throws IOException {
String jsonString;
synchronized (interpreterSettings) {
InterpreterInfoSaving info = new InterpreterInfoSaving();
info.interpreterBindings = interpreterBindings;
info.interpreterSettings = interpreterSettings;
info.interpreterRepositories = interpreterRepositories;
jsonString = gson.toJson(info);
}
if (!Files.exists(interpreterBindingPath)) {
Files.createFile(interpreterBindingPath);
Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE);
Files.setPosixFilePermissions(interpreterBindingPath, permissions);
}
FileOutputStream fos = new FileOutputStream(interpreterBindingPath.toFile(), false);
OutputStreamWriter out = new OutputStreamWriter(fos);
out.append(jsonString);
out.close();
fos.close();
}
Aggregations