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; } }; }
@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(); } }
/** * Creates a new temporary directory. */ public static File createTempDir() throws IOException { // The previously used approach of creating a temporary file, deleting // it, and making a new directory having the same name in its place is // potentially problematic: // https://stackoverflow.com/questions/617414/how-to-create-a-temporary-directory-folder-in-java // We can use the Java 7 Files.createTempDirectory() API, but note that // by default, the permissions of the created directory are 0700&(~umask) // whereas the old approach created a temporary directory with permissions // 0777&(~umask). // To avoid permissions problems like https://issues.jenkins-ci.org/browse/JENKINS-48407 // we can pass POSIX file permissions as an attribute (see, for example, // https://github.com/jenkinsci/jenkins/pull/3161 ) final Path tempPath; final String tempDirNamePrefix = "jenkins"; if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) { tempPath = Files.createTempDirectory(tempDirNamePrefix, PosixFilePermissions.asFileAttribute(EnumSet.allOf(PosixFilePermission.class))); } else { tempPath = Files.createTempDirectory(tempDirNamePrefix); } return tempPath.toFile(); }
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; } }; }
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); } } }; }
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; }
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; }
try { if (options.getPerms() != null) { FileAttribute<?> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(options.getPerms())); ch = AsynchronousFileChannel.open(file, opts, vertx.getWorkerPool(), attrs); } else {
@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(); }
public static void createPrivateFile(String path) throws IOException { final Path file = new File(path).toPath(); try { Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE); Files.createFile(file, PosixFilePermissions.asFileAttribute(perms)); } catch (UnsupportedOperationException e) { Files.createFile(file); } }
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); } }
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; } }; }
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; } }; }
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<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); } } }; }
try { if (options.getPerms() != null) { FileAttribute<?> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(options.getPerms())); ch = AsynchronousFileChannel.open(file, opts, vertx.getWorkerPool(), attrs); } else {
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(); }
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); }
@Test public void testGetFileStatus() throws IOException { File subFolder = temporaryFolder.newFolder(); java.nio.file.Path nativePath = Files.createFile( new File(subFolder, "foo").toPath(), PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r-xr--r--"))); FileStatus status = sabotFS.getFileStatus(new Path(toPathString(nativePath))); assertEquals(toPathString(nativePath), toPathString(status.getPath())); assertEquals(FsPermission.createImmutable((short) 0544), status.getPermission()); }