use of java.nio.file.attribute.PosixFilePermission in project embulk by embulk.
the class EmbulkNew method setExecutable.
private void setExecutable(String targetFileName) throws IOException {
final Path targetPath = this.pluginBasePath.resolve(targetFileName);
final Set<PosixFilePermission> permissions = new HashSet<PosixFilePermission>(Files.getPosixFilePermissions(targetPath));
permissions.add(PosixFilePermission.OWNER_EXECUTE);
permissions.add(PosixFilePermission.GROUP_EXECUTE);
permissions.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(targetPath, permissions);
}
use of java.nio.file.attribute.PosixFilePermission in project Bytecoder by mirkosertic.
the class PosixFilePermissions method asFileAttribute.
/**
* Creates a {@link FileAttribute}, encapsulating a copy of the given file
* permissions, suitable for passing to the {@link java.nio.file.Files#createFile
* createFile} or {@link java.nio.file.Files#createDirectory createDirectory}
* methods.
*
* @param perms
* the set of permissions
*
* @return an attribute encapsulating the given file permissions with
* {@link FileAttribute#name name} {@code "posix:permissions"}
*
* @throws ClassCastException
* if the set contains elements that are not of type {@code
* PosixFilePermission}
*/
public static FileAttribute<Set<PosixFilePermission>> asFileAttribute(Set<PosixFilePermission> perms) {
// copy set and check for nulls (CCE will be thrown if an element is not
// a PosixFilePermission)
perms = new HashSet<>(perms);
for (PosixFilePermission p : perms) {
if (p == null)
throw new NullPointerException();
}
final Set<PosixFilePermission> value = perms;
return new FileAttribute<>() {
@Override
public String name() {
return "posix:permissions";
}
@Override
public Set<PosixFilePermission> value() {
return Collections.unmodifiableSet(value);
}
};
}
use of java.nio.file.attribute.PosixFilePermission in project spf4j by zolyfarkas.
the class FileBasedLockTest method testFileLockPermissions.
@Test
@SuppressFBWarnings("UAC_UNNECESSARY_API_CONVERSION_FILE_TO_PATH")
public void testFileLockPermissions() throws IOException {
File tmpDir = Files.createTempDir();
Set<PosixFilePermission> reqPermissions = EnumSet.of(PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE);
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(reqPermissions);
File file = new File(tmpDir, "file.lock");
FileBasedLock lock = FileBasedLock.getLock(file, attr);
Assert.assertTrue(lock.tryLock());
lock.unlock();
Set<PosixFilePermission> actualPermissions = java.nio.file.Files.getPosixFilePermissions(file.toPath());
Assert.assertEquals(reqPermissions, actualPermissions);
}
use of java.nio.file.attribute.PosixFilePermission in project copybara by google.
the class GitOriginTest method testGitOriginWithHookExitError.
@Test
public void testGitOriginWithHookExitError() throws Exception {
Path hook = Files.createTempFile("script", "script");
Files.write(hook, "exit 1".getBytes(UTF_8));
Files.setPosixFilePermissions(hook, ImmutableSet.<PosixFilePermission>builder().addAll(Files.getPosixFilePermissions(hook)).add(PosixFilePermission.OWNER_EXECUTE).build());
options.gitOrigin.originCheckoutHook = hook.toAbsolutePath().toString();
origin = origin();
Reader<GitRevision> reader = newReader();
thrown.expect(RepoException.class);
thrown.expectMessage("Error executing the git checkout hook");
reader.checkout(origin.resolve("master"), checkoutDir);
}
use of java.nio.file.attribute.PosixFilePermission in project copybara by google.
the class FolderOriginTest method testChangesFilePermissions.
@Test
public void testChangesFilePermissions() throws Exception {
Path localFolder = Files.createTempDirectory("local_folder");
Path regularFile = localFolder.resolve("foo/regular_file");
Path cannotWrite = localFolder.resolve("foo/file_cannot_write");
Path executableCannotWrite = localFolder.resolve("foo/executable_cannot_write");
touch(regularFile, "one");
touch(cannotWrite, "two");
touch(executableCannotWrite, "three");
// Regular file already has [GROUP_READ, OWNER_WRITE, OWNER_READ, OTHERS_READ]
// Removing write permissions from the second file
Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(cannotWrite);
permissions.removeAll(ImmutableSet.of(OWNER_WRITE, GROUP_WRITE, OTHERS_WRITE));
Files.setPosixFilePermissions(cannotWrite, permissions);
// Setting executable and removing write for third one
Set<PosixFilePermission> secondPermissions = Files.getPosixFilePermissions(executableCannotWrite);
secondPermissions.removeAll(ImmutableSet.of(OWNER_WRITE, GROUP_WRITE, OTHERS_WRITE));
secondPermissions.add(OWNER_EXECUTE);
Files.setPosixFilePermissions(executableCannotWrite, secondPermissions);
FolderOrigin origin = skylark.eval("f", "f = folder.origin()");
Reader<FolderRevision> reader = origin.newReader(Glob.ALL_FILES, authoring);
FolderRevision ref = origin.resolve(localFolder.toString());
reader.checkout(ref, workdir);
assertThatPath(workdir).containsFile("foo/regular_file", "one").containsFile("foo/file_cannot_write", "two").containsFile("foo/executable_cannot_write", "three").containsNoMoreFiles();
Path cannotWriteDestination = workdir.resolve("foo/file_cannot_write");
assertThat(Files.getPosixFilePermissions(cannotWriteDestination)).containsAllOf(OWNER_READ, OWNER_WRITE);
Path executableCannotWriteDestination = workdir.resolve("foo/executable_cannot_write");
assertThat(Files.getPosixFilePermissions(executableCannotWriteDestination)).containsAllOf(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE);
}
Aggregations