use of java.nio.file.attribute.PosixFilePermission in project ha-bridge by bwssytems.
the class BridgeSettings method configWriter.
private synchronized void configWriter(String content, Path filePath) {
if (Files.exists(filePath) && !Files.isWritable(filePath)) {
log.error("Error file is not writable: " + filePath);
return;
}
if (Files.notExists(filePath.getParent())) {
try {
Files.createDirectories(filePath.getParent());
} catch (IOException e) {
log.error("Error creating the directory: " + filePath + " message: " + e.getMessage(), e);
}
}
try {
Path target = null;
if (Files.exists(filePath)) {
target = FileSystems.getDefault().getPath(filePath.getParent().toString(), "habridge.config.old." + getCurrentDate());
Files.move(filePath, target);
}
Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE);
// set attributes to be for user only
// using PosixFilePermission to set file permissions
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
// add owners permission
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
try {
String osName = System.getProperty("os.name");
if (osName.toLowerCase().indexOf("win") < 0)
Files.setPosixFilePermissions(filePath, perms);
} catch (UnsupportedOperationException e) {
log.info("Cannot set permissions for config file on this system as it is not supported. Continuing");
}
if (target != null)
Files.delete(target);
} catch (IOException e) {
log.error("Error writing the file: " + filePath + " message: " + e.getMessage(), e);
}
}
use of java.nio.file.attribute.PosixFilePermission in project zeppelin by apache.
the class InterpreterSettingManager method saveToFile.
public void saveToFile() throws IOException {
String jsonString;
synchronized (interpreterSettings) {
InterpreterInfoSaving info = new InterpreterInfoSaving();
info.interpreterBindings = interpreterBindings;
info.interpreterSettings = interpreterSettings;
info.interpreterRepositories = interpreterRepositories;
jsonString = gson.toJson(info);
}
if (!Files.exists(interpreterBindingPath)) {
Files.createFile(interpreterBindingPath);
Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE);
Files.setPosixFilePermissions(interpreterBindingPath, permissions);
}
FileOutputStream fos = new FileOutputStream(interpreterBindingPath.toFile(), false);
OutputStreamWriter out = new OutputStreamWriter(fos);
out.append(jsonString);
out.close();
fos.close();
}
use of java.nio.file.attribute.PosixFilePermission in project vert.x by eclipse.
the class FileSystemImpl method chmodInternal.
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() {
try {
Path target = vertx.resolveFile(path).toPath();
if (dirPermissions != null) {
Files.walkFileTree(target, new SimpleFileVisitor<Path>() {
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
//The directory entries typically have different permissions to the files, e.g. execute permission
//or can't cd into it
Files.setPosixFilePermissions(dir, dirPermissions);
return FileVisitResult.CONTINUE;
}
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.setPosixFilePermissions(file, permissions);
return FileVisitResult.CONTINUE;
}
});
} else {
Files.setPosixFilePermissions(target, permissions);
}
} catch (SecurityException e) {
throw new FileSystemException("Accessed denied for chmod on " + path);
} catch (IOException e) {
throw new FileSystemException(e);
}
return null;
}
};
}
use of java.nio.file.attribute.PosixFilePermission in project elasticsearch by elastic.
the class InstallPluginCommandTests method assertPlugin.
void assertPlugin(String name, Path original, Environment env) throws IOException {
Path got = env.pluginsFile().resolve(name);
assertTrue("dir " + name + " exists", Files.exists(got));
if (isPosix) {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(got);
assertThat(perms, containsInAnyOrder(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_EXECUTE));
}
assertTrue("jar was copied", Files.exists(got.resolve("plugin.jar")));
assertFalse("bin was not copied", Files.exists(got.resolve("bin")));
assertFalse("config was not copied", Files.exists(got.resolve("config")));
if (Files.exists(original.resolve("bin"))) {
Path binDir = env.binFile().resolve(name);
assertTrue("bin dir exists", Files.exists(binDir));
assertTrue("bin is a dir", Files.isDirectory(binDir));
PosixFileAttributes binAttributes = null;
if (isPosix) {
binAttributes = Files.readAttributes(env.binFile(), PosixFileAttributes.class);
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(binDir)) {
for (Path file : stream) {
assertFalse("not a dir", Files.isDirectory(file));
if (isPosix) {
PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
assertEquals(InstallPluginCommand.BIN_FILES_PERMS, attributes.permissions());
}
}
}
}
if (Files.exists(original.resolve("config"))) {
Path configDir = env.configFile().resolve(name);
assertTrue("config dir exists", Files.exists(configDir));
assertTrue("config is a dir", Files.isDirectory(configDir));
UserPrincipal user = null;
GroupPrincipal group = null;
if (isPosix) {
PosixFileAttributes configAttributes = Files.getFileAttributeView(env.configFile(), PosixFileAttributeView.class).readAttributes();
user = configAttributes.owner();
group = configAttributes.group();
PosixFileAttributes attributes = Files.getFileAttributeView(configDir, PosixFileAttributeView.class).readAttributes();
assertThat(attributes.owner(), equalTo(user));
assertThat(attributes.group(), equalTo(group));
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(configDir)) {
for (Path file : stream) {
assertFalse("not a dir", Files.isDirectory(file));
if (isPosix) {
PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
if (user != null) {
assertThat(attributes.owner(), equalTo(user));
}
if (group != null) {
assertThat(attributes.group(), equalTo(group));
}
}
}
}
}
assertInstallCleaned(env);
}
use of java.nio.file.attribute.PosixFilePermission in project che by eclipse.
the class JGitConnection method writePrivateKeyFile.
/**
* Writes private SSH key into file.
*
* @return file that contains SSH key
* @throws GitException
* if other error occurs
*/
private File writePrivateKeyFile(String url, File keyDirectory) throws GitException {
final File keyFile = new File(keyDirectory, "identity");
try (FileOutputStream fos = new FileOutputStream(keyFile)) {
byte[] sshKey = sshKeyProvider.getPrivateKey(url);
fos.write(sshKey);
} catch (IOException | ServerException exception) {
String errorMessage = "Can't store ssh key. ".concat(exception.getMessage());
LOG.error(errorMessage, exception);
throw new GitException(errorMessage, ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY);
}
Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE);
try {
java.nio.file.Files.setPosixFilePermissions(keyFile.toPath(), permissions);
} catch (IOException exception) {
throw new GitException(exception.getMessage(), exception);
}
return keyFile;
}
Aggregations