/** * Changes local file's permission. * * @param filePath that will change permission * @param perms the permission, e.g. "rwxr--r--" */ public static void changeLocalFilePermission(String filePath, String perms) throws IOException { Files.setPosixFilePermissions(Paths.get(filePath), PosixFilePermissions.fromString(perms)); }
private static void setExec(Path path) { try { path.toFile().setExecutable(true); Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxr-xr-x")); } catch (Exception ex) { // ignored } } }
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() {
protected BlockingAction<Void> createFileInternal(String p, String perms, Handler<AsyncResult<Void>> handler) { Objects.requireNonNull(p); FileAttribute<?> attrs = perms == null ? null : PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(perms)); return new BlockingAction<Void>(handler) { public Void perform() { try { Path target = vertx.resolveFile(p).toPath(); if (attrs != null) { Files.createFile(target, attrs); } else { Files.createFile(target); } } catch (IOException e) { throw new FileSystemException(e); } return null; } }; }
private synchronized void ensurePaths() throws IOException { File directory = mergedConfigDirectory.toFile(); if (directory.mkdirs()) { Files.setPosixFilePermissions( mergedConfigDirectory, PosixFilePermissions.fromString("rwx------")); logger.info("Set up PriamConfigurationPersister directory successfully"); } }
protected BlockingAction<String> createTempDirectoryInternal(String parentDir, String prefix, String perms, Handler<AsyncResult<String>> handler) { FileAttribute<?> attrs = perms == null ? null : PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(perms)); return new BlockingAction<String>(handler) { public String perform() { try { Path tmpDir; if (parentDir != null) { Path dir = vertx.resolveFile(parentDir).toPath(); if (attrs != null) { tmpDir = Files.createTempDirectory(dir, prefix, attrs); } else { tmpDir = Files.createTempDirectory(dir, prefix); } } else { if (attrs != null) { tmpDir = Files.createTempDirectory(prefix, attrs); } else { tmpDir = Files.createTempDirectory(prefix); } } return tmpDir.toFile().getAbsolutePath(); } catch (IOException e) { throw new FileSystemException(e); } } }; }
protected BlockingAction<String> createTempFileInternal(String parentDir, String prefix, String suffix, String perms, Handler<AsyncResult<String>> handler) { FileAttribute<?> attrs = perms == null ? null : PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(perms)); return new BlockingAction<String>(handler) { public String perform() { try { Path tmpFile; if (parentDir != null) { Path dir = vertx.resolveFile(parentDir).toPath(); if (attrs != null) { tmpFile = Files.createTempFile(dir, prefix, suffix, attrs); } else { tmpFile = Files.createTempFile(dir, prefix, suffix); } } else { if (attrs != null) { tmpFile = Files.createTempFile(prefix, suffix, attrs); } else { tmpFile = Files.createTempFile(prefix, suffix); } } return tmpFile.toFile().getAbsolutePath(); } catch (IOException e) { throw new FileSystemException(e); } } }; }
protected BlockingAction<Void> mkdirInternal(String path, String perms, boolean createParents, Handler<AsyncResult<Void>> handler) { Objects.requireNonNull(path); FileAttribute<?> attrs = perms == null ? null : PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(perms)); return new BlockingAction<Void>(handler) { public Void perform() { try { Path source = vertx.resolveFile(path).toPath(); if (createParents) { if (attrs != null) { Files.createDirectories(source, attrs); } else { Files.createDirectories(source); } } else { if (attrs != null) { Files.createDirectory(source, attrs); } else { Files.createDirectory(source); } } } catch (IOException e) { throw new FileSystemException(e); } return null; } }; }
private static File copyToLocal(FileSystem fs, Path keyPath) throws IOException { java.nio.file.Path tmpKeyPath = Files.createTempFile(GoogleCommon.class.getSimpleName(), "tmp", PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------"))); File copied = tmpKeyPath.toFile(); copied.deleteOnExit(); fs.copyToLocalFile(keyPath, new Path(copied.getAbsolutePath())); return copied; }
/** * Formats the worker data folder. * * @param folder folder path */ private static void formatWorkerDataFolder(String folder) throws IOException { Path path = Paths.get(folder); if (Files.exists(path)) { FileUtils.deletePathRecursively(folder); } Files.createDirectory(path); // For short-circuit read/write to work, others needs to be able to access this directory. // Therefore, default is 777 but if the user specifies the permissions, respect those instead. String permissions = ServerConfiguration.get(PropertyKey.WORKER_DATA_FOLDER_PERMISSIONS); Set<PosixFilePermission> perms = PosixFilePermissions.fromString(permissions); Files.setPosixFilePermissions(path, perms); FileUtils.setLocalDirStickyBit(path.toAbsolutePath().toString()); }
private void setPerms(Path path, String perms) { if (Utils.isWindows() == false) { try { Files.setPosixFilePermissions( path, PosixFilePermissions.fromString(perms) ); } catch(IOException e) { throw new RuntimeException(e.getMessage()); } } }
/** * Sets the permissions to look for. * @param permissions the permissions string (rwxrwxrwx or octal) */ public void setPermissions(String permissions) { if (permissions.length() == 3 && permissions.matches("^[0-7]+$")) { this.permissions = PosixFilePermissions.toString( PermissionUtils.permissionsFromMode(Integer.parseInt(permissions, 8))); return; } try { this.permissions = PosixFilePermissions.toString(PosixFilePermissions.fromString(permissions)); } catch (IllegalArgumentException ex) { throw new BuildException("the permissions attribute " + permissions + " is invalid", ex); } }
@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); } }
@Override public void execute() throws Exception { ensurePaths(); Path tempPath = null; try { File output = File.createTempFile( structuredPath.getFileName().toString(), ".tmp", mergedConfigDirectory.toFile()); tempPath = output.toPath(); // The configuration might contain sensitive information, so ... don't let non Priam // users read it // Theoretically createTempFile creates the file with the right permissions, but I want // to be explicit Files.setPosixFilePermissions(tempPath, PosixFilePermissions.fromString("rw-------")); Map<String, Object> structuredConfiguration = config.getStructuredConfiguration("all"); ObjectMapper mapper = new ObjectMapper(); ObjectWriter structuredPathTmpWriter = mapper.writer(new MinimalPrettyPrinter()); structuredPathTmpWriter.writeValue(output, structuredConfiguration); // Atomically swap out the new config for the old config. if (!output.renameTo(structuredPath.toFile())) logger.error("Failed to persist structured Priam configuration"); } finally { if (tempPath != null) Files.deleteIfExists(tempPath); } }
permissions = Sets.immutableEnumSet( PosixFilePermissions.fromString((String) userProvidedPermissions)); } else if (userProvidedPermissions instanceof Set) { permissions = toPermissions((Set<?>) userProvidedPermissions);
try { if (options.getPerms() != null) { FileAttribute<?> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(options.getPerms())); ch = AsynchronousFileChannel.open(file, opts, vertx.getWorkerPool(), attrs); } else {
logger.log(Level.INFO, " - Setting POSIX permissions: " + reconstructedFileVersion.getPosixPermissions() + " ..."); Set<PosixFilePermission> posixPerms = PosixFilePermissions.fromString(reconstructedFileVersion.getPosixPermissions());
/** * 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())); }
@BeforeClass public static void setUp() throws Exception { if (!tmpDir.toFile().exists()) { System.out.println("Creating directory " + tmpDir); Files.createDirectories(tmpDir, PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x"))); } HiveConf conf = new HiveConf(); jsonReportFile = File.createTempFile("TestCodahaleMetrics", ".json"); System.out.println("Json metrics saved in " + jsonReportFile.getAbsolutePath()); conf.setVar(HiveConf.ConfVars.HIVE_METRICS_CLASS, CodahaleMetrics.class.getCanonicalName()); conf.setVar(HiveConf.ConfVars.HIVE_CODAHALE_METRICS_REPORTER_CLASSES, "org.apache.hadoop.hive.common.metrics.metrics2.JsonFileMetricsReporter, " + "org.apache.hadoop.hive.common.metrics.metrics2.JmxMetricsReporter"); conf.setVar(HiveConf.ConfVars.HIVE_METRICS_JSON_FILE_LOCATION, jsonReportFile.getAbsolutePath()); conf.setTimeVar(HiveConf.ConfVars.HIVE_METRICS_JSON_FILE_INTERVAL, REPORT_INTERVAL_MS, TimeUnit.MILLISECONDS); MetricsFactory.init(conf); metricRegistry = ((CodahaleMetrics) MetricsFactory.getInstance()).getMetricRegistry(); }
Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("r--r--r--"));