Refine search
/** * 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 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; }
@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); } }
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); } } }; }
/** * 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); } }
final Path copyFile = rootDirPath.resolve(flowFile.getAttribute(CoreAttributes.FILENAME.key())); if (!Files.exists(rootDirPath)) { if (context.getProperty(CREATE_DIRS).asBoolean()) { Files.createDirectories(rootDirPath); } else { flowFile = session.penalize(flowFile); if (Files.exists(finalCopyFileDir) && maxDestinationFiles != null) { // check if too many files already final int numFiles = finalCopyFileDir.toFile().list().length; String perms = stringPermissions(permissions); if (!perms.isEmpty()) { Files.setPosixFilePermissions(dotCopyFile, PosixFilePermissions.fromString(perms));
@Test public void testCreateFiles() throws IOException { Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rwx------"); Files.createDirectories(commandFile.getParent()); Files.createFile(commandFile, PosixFilePermissions.asFileAttribute(permissions)); Files.write(commandFile, "#bin\necho hello world".getBytes()); assertTrue(Files.exists(commandFile)); Files.deleteIfExists(commandFile); }
private static List<FileAttribute<Set<PosixFilePermission>>> getPosixAttributes(Path file) throws IOException { if (Files.exists(file) && supportsFileOwnerAttributeView(file, PosixFileAttributeView.class)) { PosixFileAttributeView posixView = Files.getFileAttributeView(file, PosixFileAttributeView.class); if (posixView != null) { return Collections.singletonList(PosixFilePermissions.asFileAttribute(posixView.readAttributes().permissions())); } } return Collections.emptyList(); }
@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(); }
@Override public void close() throws IOException { if (outputPath != null) { log.info("Persisting authentication tokens to disk."); if (!Files.exists(outputPath)) { Files.createFile(outputPath, PosixFilePermissions.asFileAttribute(OWNER_READ_WRITE)); } else { Files.setPosixFilePermissions(outputPath, OWNER_READ_WRITE); } mapper.writeValue(outputPath.toFile(), cache); } } }
fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS); DosFileAttributes dosAttrs = Files.readAttributes(filePath, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS); fileProperties.dosAttributes = FileUtil.dosAttrsToString(dosAttrs); PosixFileAttributes posixAttrs = Files.readAttributes(filePath, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS); fileProperties.posixPermissions = PosixFilePermissions.toString(posixAttrs.permissions());
public static void createPrivateDirectories(String directoryPath) throws IOException { final File file = new File(directoryPath); if (file.exists()) { return; } final Path path = file.toPath(); try { Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE); Files.createDirectories(path, PosixFilePermissions.asFileAttribute(perms)); } catch (UnsupportedOperationException e) { Files.createDirectories(path); } }
private SegmentHandle doCreate(String streamSegmentName) throws IOException { long traceId = LoggerHelpers.traceEnter(log, "create", streamSegmentName); FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(READ_WRITE_PERMISSION); Path path = Paths.get(config.getRoot(), streamSegmentName); Path parent = path.getParent(); assert parent != null; Files.createDirectories(parent); Files.createFile(path, fileAttributes); LoggerHelpers.traceLeave(log, "create", traceId); FileSystemMetrics.CREATE_COUNT.inc(); return FileSystemSegmentHandle.writeHandle(streamSegmentName); }
final File destFile = new File(directory, inFile.getName()); final Path targetPath = destFile.toPath(); Files.copy(inPath, targetPath); Set<PosixFilePermission> perms = PosixFilePermissions.fromString("r--r-----"); Files.setPosixFilePermissions(targetPath, perms); Set<PosixFilePermission> permsAfterSet = Files.getPosixFilePermissions(targetPath); if (perms.equals(permsAfterSet)) { verifyPermissions = true;
private static void walk(Path current, boolean inExecDir) throws IOException { if (Files.isDirectory(current)) { List<Path> list = Files.list(current).collect(Collectors.toList()); for (Path child : list) { boolean next = inExecDir || Optional.ofNullable(child.getFileName()) .map(Path::toString) .filter(TRIGGER_NAME::contains) .isPresent(); walk(child, next); } } else if (isExecutableTarget(current, inExecDir)) { PosixFileAttributeView attr = Files.getFileAttributeView(current, PosixFileAttributeView.class); Set<PosixFilePermission> perms = attr.readAttributes().permissions(); if (perms.containsAll(EXECUTABLES) == false) { System.out.printf("set executable: %s (%s)%n", current, PosixFilePermissions.toString(perms)); perms.addAll(EXECUTABLES); attr.setPermissions(perms); } } }
@Override public String invoke(File dir, VirtualChannel channel) throws IOException { Path tempPath; final boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix"); if (isPosix) { tempPath = Files.createTempDirectory(Util.fileToPath(dir), name, PosixFilePermissions.asFileAttribute(EnumSet.allOf(PosixFilePermission.class))); } else { tempPath = Files.createTempDirectory(Util.fileToPath(dir), name, new FileAttribute<?>[] {}); } if (tempPath.toFile() == null) { throw new IOException("Failed to obtain file from path " + dir + " on " + remote); } return tempPath.toFile().getName(); } }
@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); } }
private Path createSensitiveKeyFile(File confDir) { Path sensitiveKeyFile = Paths.get(confDir+"/sensitive.key"); final boolean isPosixSupported = FileSystems.getDefault().supportedFileAttributeViews().contains("posix"); try { if (isPosixSupported) { // Initially create file with the empty permission set (so nobody can get a file descriptor on it): Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); sensitiveKeyFile = Files.createFile(sensitiveKeyFile, attr); // Then, once created, add owner-only rights: perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.OWNER_READ); attr = PosixFilePermissions.asFileAttribute(perms); Files.setPosixFilePermissions(sensitiveKeyFile, perms); } else { // If Posix is not supported (e.g. Windows) then create the key file without permission settings. cmdLogger.info("Current file system does not support Posix, using default permission settings."); sensitiveKeyFile = Files.createFile(sensitiveKeyFile); } } catch (final FileAlreadyExistsException faee) { cmdLogger.error("The sensitive.key file {} already exists. That shouldn't have been. Aborting.", sensitiveKeyFile); System.exit(1); } catch (final Exception e) { cmdLogger.error("Other failure relating to setting permissions on {}. " + "(so that only the owner can read it). " + "This is fatal to the bootstrap process for security reasons. Exception was: {}", sensitiveKeyFile, e); System.exit(1); } return sensitiveKeyFile; }
protected static Path createTempDir() throws IOException { Path tmpDir = Files.createTempDirectory("test"); Files.setPosixFilePermissions(tmpDir, PosixFilePermissions.fromString("rwxr-xr-x")); return tmpDir; }
public HttpJarDownloader() throws Exception { FileAttribute<Set<PosixFilePermission>> perms = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x")); Path createdPath = Files.createTempDirectory(null, perms); this.tmpDirectory = createdPath.toAbsolutePath().toString(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(20); httpClient = HttpClients.custom().setConnectionManager(cm).build(); }