Search in sources :

Example 86 with PosixFilePermission

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);
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) HashSet(java.util.HashSet)

Example 87 with PosixFilePermission

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);
        }
    };
}
Also used : PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 88 with PosixFilePermission

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);
}
Also used : Set(java.util.Set) EnumSet(java.util.EnumSet) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 89 with PosixFilePermission

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);
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Test(org.junit.Test)

Example 90 with PosixFilePermission

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);
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Test(org.junit.Test)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)139 Path (java.nio.file.Path)69 IOException (java.io.IOException)41 File (java.io.File)40 HashSet (java.util.HashSet)40 Test (org.junit.Test)35 Set (java.util.Set)22 FileOutputStream (java.io.FileOutputStream)17 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)14 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)11 FileNotFoundException (java.io.FileNotFoundException)8 ArrayList (java.util.ArrayList)7 RandomAccessFile (java.io.RandomAccessFile)6 UserPrincipal (java.nio.file.attribute.UserPrincipal)6 OutputStream (java.io.OutputStream)5 SimpleDateFormat (java.text.SimpleDateFormat)5 BufferedWriter (java.io.BufferedWriter)4 InputStream (java.io.InputStream)4 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)4 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4