private static boolean canExecuteExecutable(File file) throws IOException { if (PlatformDependent.javaVersion() < 7) { // Pre-JDK7, the Java API did not directly support POSIX permissions; instead of implementing a custom // work-around, assume true, which disables the check. return true; } // If we can already execute, there is nothing to do. if (file.canExecute()) { return true; } // On volumes, with noexec set, even files with the executable POSIX permissions will fail to execute. // The File#canExecute() method honors this behavior, probaby via parsing the noexec flag when initializing // the UnixFileStore, though the flag is not exposed via a public API. To find out if library is being // loaded off a volume with noexec, confirm or add executalbe permissions, then check File#canExecute(). // Note: We use FQCN to not break when netty is used in java6 Set<java.nio.file.attribute.PosixFilePermission> existingFilePermissions = java.nio.file.Files.getPosixFilePermissions(file.toPath()); Set<java.nio.file.attribute.PosixFilePermission> executePermissions = EnumSet.of(java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE, java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE, java.nio.file.attribute.PosixFilePermission.OTHERS_EXECUTE); if (existingFilePermissions.containsAll(executePermissions)) { return false; } Set<java.nio.file.attribute.PosixFilePermission> newPermissions = EnumSet.copyOf(existingFilePermissions); newPermissions.addAll(executePermissions); java.nio.file.Files.setPosixFilePermissions(file.toPath(), newPermissions); return file.canExecute(); }
private boolean canWritePosixFile(File file) throws IOException { try { Set<PosixFilePermission> permissions = Files .getPosixFilePermissions(file.toPath()); for (PosixFilePermission permission : WRITE_PERMISSIONS) { if (permissions.contains(permission)) { return true; } } return false; } catch (UnsupportedOperationException ex) { // Assume that we can return true; } }
@Override public boolean isSelected(File basedir, String filename, File file) { if (permissions == null) { throw new BuildException("the permissions attribute is required"); } try { return PosixFilePermissions.toString(followSymlinks ? Files.getPosixFilePermissions(file.toPath()) : Files.getPosixFilePermissions(file.toPath(), LinkOption.NOFOLLOW_LINKS)) .equals(permissions); } catch (IOException e) { // => not the expected permissions } return false; } }
private void setExecutableIfAvailable(final String targetFileName) throws IOException { final Path targetPath = this.pluginBasePath.resolve(targetFileName); final FileSystem fileSystem = targetPath.getFileSystem(); if (fileSystem.supportedFileAttributeViews().contains("posix")) { // NTFS does not support PosixFilePermissions, for example. 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); } }
private static boolean canExecuteExecutable(File file) throws IOException { if (PlatformDependent.javaVersion() < 7) { // Pre-JDK7, the Java API did not directly support POSIX permissions; instead of implementing a custom // work-around, assume true, which disables the check. return true; } // If we can already execute, there is nothing to do. if (file.canExecute()) { return true; } // On volumes, with noexec set, even files with the executable POSIX permissions will fail to execute. // The File#canExecute() method honors this behavior, probaby via parsing the noexec flag when initializing // the UnixFileStore, though the flag is not exposed via a public API. To find out if library is being // loaded off a volume with noexec, confirm or add executalbe permissions, then check File#canExecute(). // Note: We use FQCN to not break when netty is used in java6 Set<java.nio.file.attribute.PosixFilePermission> existingFilePermissions = java.nio.file.Files.getPosixFilePermissions(file.toPath()); Set<java.nio.file.attribute.PosixFilePermission> executePermissions = EnumSet.of(java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE, java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE, java.nio.file.attribute.PosixFilePermission.OTHERS_EXECUTE); if (existingFilePermissions.containsAll(executePermissions)) { return false; } Set<java.nio.file.attribute.PosixFilePermission> newPermissions = EnumSet.copyOf(existingFilePermissions); newPermissions.addAll(executePermissions); java.nio.file.Files.setPosixFilePermissions(file.toPath(), newPermissions); return file.canExecute(); }
private void setExecutableIfAvailable(final String targetFileName) throws IOException { final Path targetPath = this.basePath.resolve(targetFileName); final FileSystem fileSystem = targetPath.getFileSystem(); if (fileSystem.supportedFileAttributeViews().contains("posix")) { // NTFS does not support PosixFilePermissions, for example. 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); } }
public static Closeable withPermissions( Path file, Set<PosixFilePermission> permissions ) throws IOException { Set<PosixFilePermission> originalPermissions = Files.getPosixFilePermissions( file ); Files.setPosixFilePermissions( file, permissions ); return () -> Files.setPosixFilePermissions( file, originalPermissions ); } }
perms = Files.getPosixFilePermissions(p, LinkOption.NOFOLLOW_LINKS); } catch (Exception e) { if(log.isDebugEnabled()) {
private static boolean canExecuteExecutable(File file) throws IOException { if (PlatformDependent.javaVersion() < 7) { // Pre-JDK7, the Java API did not directly support POSIX permissions; instead of implementing a custom // work-around, assume true, which disables the check. return true; } // If we can already execute, there is nothing to do. if (file.canExecute()) { return true; } // On volumes, with noexec set, even files with the executable POSIX permissions will fail to execute. // The File#canExecute() method honors this behavior, probaby via parsing the noexec flag when initializing // the UnixFileStore, though the flag is not exposed via a public API. To find out if library is being // loaded off a volume with noexec, confirm or add executalbe permissions, then check File#canExecute(). // Note: We use FQCN to not break when netty is used in java6 Set<java.nio.file.attribute.PosixFilePermission> existingFilePermissions = java.nio.file.Files.getPosixFilePermissions(file.toPath()); Set<java.nio.file.attribute.PosixFilePermission> executePermissions = EnumSet.of(java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE, java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE, java.nio.file.attribute.PosixFilePermission.OTHERS_EXECUTE); if (existingFilePermissions.containsAll(executePermissions)) { return false; } Set<java.nio.file.attribute.PosixFilePermission> newPermissions = EnumSet.copyOf(existingFilePermissions); newPermissions.addAll(executePermissions); java.nio.file.Files.setPosixFilePermissions(file.toPath(), newPermissions); return file.canExecute(); }
private String getPerms(String fileName) { try { Set<PosixFilePermission> perms = Files.getPosixFilePermissions(Paths.get(testDir + pathSep + fileName)); return PosixFilePermissions.toString(perms); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
@Override protected void stashOriginalFilePermissions() throws IOException { // save off permissions in case we need to // rewrite the keystore in flush() if (!Shell.WINDOWS) { Path path = Paths.get(file.getCanonicalPath()); permissions = Files.getPosixFilePermissions(path); } else { // On Windows, the JDK does not support the POSIX file permission APIs. // Instead, we can do a winutils call and translate. String[] cmd = Shell.getGetPermissionCommand(); String[] args = new String[cmd.length + 1]; System.arraycopy(cmd, 0, args, 0, cmd.length); args[cmd.length] = file.getCanonicalPath(); String out = Shell.execCommand(args); StringTokenizer t = new StringTokenizer(out, Shell.TOKEN_SEPARATOR_REGEX); // The winutils output consists of 10 characters because of the leading // directory indicator, i.e. "drwx------". The JDK parsing method expects // a 9-character string, so remove the leading character. String permString = t.nextToken().substring(1); permissions = PosixFilePermissions.fromString(permString); } }
/** * Gets the mode of a file/directory, if appropriate. Only includes read, write, and * execute permissions for the owner, group, and others, i.e. the max return value * is 0777. Consider using {@link Files#getPosixFilePermissions} instead if you only * care about access permissions. * <p>If the file is symlink, the mode is that of the link target, not the link itself. * @return a file mode, or -1 if not on Unix * @throws PosixException if the file could not be statted, e.g. broken symlink */ public static int mode(File f) throws PosixException { if(Functions.isWindows()) return -1; try { if (Util.NATIVE_CHMOD_MODE) { return PosixAPI.jnr().stat(f.getPath()).mode(); } else { return Util.permissionsToMode(Files.getPosixFilePermissions(fileToPath(f))); } } catch (IOException cause) { PosixException e = new PosixException("Unable to get file permissions", null); e.initCause(cause); throw e; } }
@Test public void testFilePermissions() throws Exception { final Logger logger = loggerContextRule.getLogger(FileAppenderPermissionsTest.class); for (int i = 0; i < 1000; ++i) { final String message = "This is test message number " + i; logger.debug(message); } assertEquals("rw-------", PosixFilePermissions.toString( Files.getPosixFilePermissions(Paths.get("target/permissions1/AppenderTest-1.log")))); }
/** * Tests {@link FileUtils#createBlockPath} method when storage dir exists or doesn't exist. */ @Test public void createStorageDirPath() throws IOException { File storageDir = new File(mTestFolder.getRoot(), "storageDir"); File blockFile = new File(storageDir, "200"); // When storage dir doesn't exist FileUtils.createBlockPath(blockFile.getAbsolutePath(), mWorkerDataFolderPerms); assertTrue(FileUtils.exists(storageDir.getAbsolutePath())); assertEquals( PosixFilePermissions.fromString("rwxrwxrwx"), Files.getPosixFilePermissions(Paths.get(storageDir.getAbsolutePath()))); // When storage dir exists FileUtils.createBlockPath(blockFile.getAbsolutePath(), mWorkerDataFolderPerms); assertTrue(FileUtils.exists(storageDir.getAbsolutePath())); }
@Test public void testCreateTempFileWithPermsBlocking() throws Exception { Assume.assumeFalse(Utils.isWindows()); FileSystem fs = vertx.fileSystem(); String tempFile = fs.createTempFileBlocking("project", ".tmp", DEFAULT_FILE_PERMS); Path path = Paths.get(tempFile); assertTrue(Files.exists(path)); String perms = PosixFilePermissions.toString(Files.getPosixFilePermissions(path)); assertEquals(perms, DEFAULT_FILE_PERMS); } }
@Test public void testCreateTempDirectoryWithPermsBlocking() throws Exception { Assume.assumeFalse(Utils.isWindows()); FileSystem fs = vertx.fileSystem(); String tempDirectory = fs.createTempDirectoryBlocking("project", DEFAULT_DIR_PERMS); String perms = PosixFilePermissions.toString(Files.getPosixFilePermissions(Paths.get(tempDirectory))); assertEquals(perms, DEFAULT_DIR_PERMS); }
gzippedFiles1++; assertEquals("rw-------", PosixFilePermissions.toString(Files.getPosixFilePermissions(file.toPath()))); } else { gzippedFiles2++; assertEquals("r--r--r--", PosixFilePermissions.toString(Files.getPosixFilePermissions(file.toPath()))); PosixFilePermissions.toString(Files.getPosixFilePermissions(file.toPath()))); } else { assertEquals("rwx------", PosixFilePermissions.toString(Files.getPosixFilePermissions(file.toPath())));
@Test public void testCreateTempFileWithPerms() { Assume.assumeFalse(Utils.isWindows()); FileSystem fs = vertx.fileSystem(); fs.createTempFile("project", ".tmp", DEFAULT_FILE_PERMS, onSuccess(tempFile -> { Path path = Paths.get(tempFile); assertTrue(Files.exists(path)); try { String perms = PosixFilePermissions.toString(Files.getPosixFilePermissions(path)); assertEquals(perms, DEFAULT_FILE_PERMS); } catch (IOException e) { fail(e); } })); }
vertx.<Set<PosixFilePermission>>executeBlocking(fut -> { try { fut.complete(Files.getPosixFilePermissions(new File(testDir, target).toPath(), LinkOption.NOFOLLOW_LINKS)); } catch (IOException e) { fut.fail(e);
@Test public void testCreateTempDirectoryWithPerms() { Assume.assumeFalse(Utils.isWindows()); FileSystem fs = vertx.fileSystem(); fs.createTempDirectory("project", DEFAULT_DIR_PERMS, onSuccess(tempDirectory -> { try { String perms = PosixFilePermissions.toString(Files.getPosixFilePermissions(Paths.get(tempDirectory))); assertEquals(perms, DEFAULT_DIR_PERMS); } catch (IOException e) { fail(e); } complete(); })); await(); }