本文整理匯總了Java中java.nio.file.attribute.PosixFilePermission類的典型用法代碼示例。如果您正苦於以下問題:Java PosixFilePermission類的具體用法?Java PosixFilePermission怎麽用?Java PosixFilePermission使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PosixFilePermission類屬於java.nio.file.attribute包,在下文中一共展示了PosixFilePermission類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createLocalConfigFile
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
/**
* Create localconf folder and properties file necessary to run build task in order to check merge integrity.
* @param branchName
*/
public void createLocalConfigFile(String branchName) throws Exception {
String branchPath = SvnUtils.TEMP_FOLDER + "/" + branchName;
//create localconf directory
String newDirectoryPath = branchPath + "/localconf";
if ( !Files.exists( Paths.get(newDirectoryPath))) {
Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rwxrwxrwx");
FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions
.asFileAttribute(permissions);
Files.createDirectory(Paths.get(newDirectoryPath), fileAttributes);
}
//copy properties template
Files.copy(
Paths.get(branchPath + "/common/conf/worldnettps.properties.template"),
Paths.get(newDirectoryPath + "/worldnettps.properties"),
StandardCopyOption.REPLACE_EXISTING);
//setting glassfish directory in properties file
String appServerDir = PropertiesUtil.getString("appserver.dir");
String sedCommand = String.format(
"sed -i '/glassfish.dir/c\\glassfish.dir=%s' localconf/worldnettps.properties", appServerDir);
CommandExecutor.run( sedCommand, branchPath);
logger.info("worldnettps.properties file has been created in localconf folder.");
}
示例2: getPosixPerm
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
/**
* 獲取Posix權限
*/
public static Set<PosixFilePermission> getPosixPerm(int perm) {
StringBuilder permStr = new StringBuilder();
permStr.append(uCanRead(perm) ? "r" : "-");
permStr.append(uCanWrite(perm) ? "w" : "-");
permStr.append(uCanExec(perm) ? "x" : "-");
permStr.append(gCanRead(perm) ? "r" : "-");
permStr.append(gCanWrite(perm) ? "w" : "-");
permStr.append(gCanExec(perm) ? "x" : "-");
permStr.append(oCanRead(perm) ? "r" : "-");
permStr.append(oCanWrite(perm) ? "w" : "-");
permStr.append(oCanExec(perm) ? "x" : "-");
return PosixFilePermissions.fromString(permStr.toString());
}
示例3: testMissingWritePermissionOnShard
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
public void testMissingWritePermissionOnShard() throws IOException {
assumeTrue("posix filesystem", isPosix);
final String[] tempPaths = tmpPaths();
Path path = PathUtils.get(randomFrom(tempPaths));
Path fooIndex = path.resolve("nodes").resolve("0").resolve(NodeEnvironment.INDICES_FOLDER)
.resolve("foo");
Path fooShard = fooIndex.resolve("0");
Path fooShardIndex = fooShard.resolve("index");
Path fooShardTranslog = fooShard.resolve("translog");
Path fooShardState = fooShard.resolve("_state");
Path pick = randomFrom(fooShard, fooShardIndex, fooShardTranslog, fooShardState);
Files.createDirectories(pick);
try (PosixPermissionsResetter attr = new PosixPermissionsResetter(pick)) {
attr.setPermissions(new HashSet<>(Arrays.asList(PosixFilePermission.OTHERS_READ, PosixFilePermission.GROUP_READ,
PosixFilePermission.OWNER_READ)));
Settings build = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString())
.putArray(Environment.PATH_DATA_SETTING.getKey(), tempPaths).build();
IOException ioException = expectThrows(IOException.class, () -> {
new NodeEnvironment(build, new Environment(build));
});
assertTrue(ioException.getMessage(), ioException.getMessage().startsWith("failed to write in data directory"));
}
}
示例4: setFilePerm
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
/**
* 設置文件權限。前提:必須支持PosixFileAttributeView.
*/
public static void setFilePerm(File file, String perm) {
if (filePermSupported()) {
try {
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(perm);
PosixFileAttributes attr = Files.readAttributes(file.toPath(), PosixFileAttributes.class);
attr.permissions().clear();
Files.setPosixFilePermissions(file.toPath(), perms);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
示例5: testMissingWritePermission
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
public void testMissingWritePermission() throws IOException {
assumeTrue("posix filesystem", isPosix);
final String[] tempPaths = tmpPaths();
Path path = PathUtils.get(randomFrom(tempPaths));
try (PosixPermissionsResetter attr = new PosixPermissionsResetter(path)) {
attr.setPermissions(new HashSet<>(Arrays.asList(PosixFilePermission.OTHERS_READ, PosixFilePermission.GROUP_READ,
PosixFilePermission.OWNER_READ)));
Settings build = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString())
.putArray(Environment.PATH_DATA_SETTING.getKey(), tempPaths).build();
IOException ioException = expectThrows(IOException.class, () -> {
new NodeEnvironment(build, new Environment(build));
});
assertTrue(ioException.getMessage(), ioException.getMessage().startsWith(path.toString()));
}
}
示例6: testMissingWritePermissionOnIndex
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
public void testMissingWritePermissionOnIndex() throws IOException {
assumeTrue("posix filesystem", isPosix);
final String[] tempPaths = tmpPaths();
Path path = PathUtils.get(randomFrom(tempPaths));
Path fooIndex = path.resolve("nodes").resolve("0").resolve(NodeEnvironment.INDICES_FOLDER)
.resolve("foo");
Files.createDirectories(fooIndex);
try (PosixPermissionsResetter attr = new PosixPermissionsResetter(fooIndex)) {
attr.setPermissions(new HashSet<>(Arrays.asList(PosixFilePermission.OTHERS_READ, PosixFilePermission.GROUP_READ,
PosixFilePermission.OWNER_READ)));
Settings build = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString())
.putArray(Environment.PATH_DATA_SETTING.getKey(), tempPaths).build();
IOException ioException = expectThrows(IOException.class, () -> {
new NodeEnvironment(build, new Environment(build));
});
assertTrue(ioException.getMessage(), ioException.getMessage().startsWith("failed to write in data directory"));
}
}
示例7: testBinPermissions
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
public void testBinPermissions() throws Exception {
assumeTrue("posix filesystem", isPosix);
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
Path binDir = pluginDir.resolve("bin");
Files.createDirectory(binDir);
Files.createFile(binDir.resolve("somescript"));
String pluginZip = createPlugin("fake", pluginDir);
try (PosixPermissionsResetter binAttrs = new PosixPermissionsResetter(env.v2().binFile())) {
Set<PosixFilePermission> perms = binAttrs.getCopyPermissions();
// make sure at least one execute perm is missing, so we know we forced it during installation
perms.remove(PosixFilePermission.GROUP_EXECUTE);
binAttrs.setPermissions(perms);
installPlugin(pluginZip, env.v1());
assertPlugin("fake", pluginDir, env.v2());
}
}
示例8: setAsExecutable
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
public static void setAsExecutable(String filePath) throws IOException {
//using PosixFilePermission to set file permissions 755
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
//add owners permission
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_EXECUTE);
//add group permissions
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_EXECUTE);
//add others permissions
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(Paths.get(filePath), perms);
logger.info("Modified as executable " + filePath);
}
示例9: build
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
public TestWorkspaceBuilder build() throws IOException {
// mkdir -p
Files.createDirectories(path.getParent());
try (Writer out = Files.newBufferedWriter(path, Charset.defaultCharset())) {
out.write(contents);
}
Set<PosixFilePermission> permissions = Sets.newHashSet(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE);
if (isExecutable) {
permissions.add(PosixFilePermission.OWNER_EXECUTE);
}
Files.setPosixFilePermissions(path, permissions);
return workspaceBuilder;
}
示例10: execute
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
public int execute(String[] args, @Nullable Path workingDir, Map<String, String> addEnv) throws IOException {
if (!Files.isExecutable(file)) {
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(file, perms);
}
ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
CommandLine cmd = new CommandLine(file.toFile());
cmd.addArguments(args);
DefaultExecutor exec = new DefaultExecutor();
exec.setWatchdog(watchdog);
exec.setStreamHandler(createStreamHandler());
exec.setExitValues(null);
if (workingDir != null) {
exec.setWorkingDirectory(workingDir.toFile());
}
in.close();
LOG.info("Executing: {}", cmd.toString());
Map<String, String> env = new HashMap<>(System.getenv());
env.putAll(addEnv);
return exec.execute(cmd, env);
}
示例11: copyFilesAndApplyPermissions
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
public static void copyFilesAndApplyPermissions(Path sourceDir, Path targetDir, List<String> filenames) throws IOException {
for (String fileName : filenames) {
Files.copy(sourceDir.resolve(fileName),
targetDir.resolve(fileName));
try {
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_WRITE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OTHERS_WRITE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(targetDir.resolve(fileName), perms);
} catch (Throwable ignored) {
}
}
}
示例12: permissionToInt
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
private static int permissionToInt(PosixFilePermission permission) {
switch (permission) {
case OWNER_EXECUTE:
return 100;
case GROUP_EXECUTE:
return 10;
case OTHERS_EXECUTE:
return 1;
case OWNER_WRITE:
return 200;
case GROUP_WRITE:
return 20;
case OTHERS_WRITE:
return 2;
case OWNER_READ:
return 400;
case GROUP_READ:
return 40;
case OTHERS_READ:
return 4;
default:
return 0;
}
}
示例13: chmodFiles
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
private static void chmodFiles(final Args arg,
final HashMap<String, String> pairs,
final String[] chmodList) throws IOException {
final HashSet<PosixFilePermission> chmodSet = new HashSet<>();
chmodSet.add(PosixFilePermission.OWNER_EXECUTE);
chmodSet.add(PosixFilePermission.OWNER_READ);
chmodSet.add(PosixFilePermission.OWNER_WRITE);
System.out.println("Setting file permissions...");
for(final String filename: chmodList) {
System.out.println("\t" + arg.projectFolder.toString() + substitute(filename, pairs));
final String resourceName = WebFramework.getResourceName(filename);
final Path path =
FileSystems.getDefault().getPath(arg.projectFolder.toString() + substitute(filename, pairs));
Files.setPosixFilePermissions(path, chmodSet);
}
}
示例14: mkdirs
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
private boolean mkdirs(File file, Set<PosixFilePermission> permissions) throws IOException {
if (file.exists()) {
return false;
}
if (file.mkdir()) {
Files.setPosixFilePermissions(file.toPath(), permissions);
return true;
}
File parent = file.getParentFile();
if (parent == null) {
return false;
}
if (!mkdirs(parent, permissions) && !parent.exists()) {
return false;
}
if (file.mkdir()) {
Files.setPosixFilePermissions(file.toPath(), permissions);
return true;
}
return false;
}
示例15: assertFilePermissions
import java.nio.file.attribute.PosixFilePermission; //導入依賴的package包/類
public static void assertFilePermissions(Path testRoot) throws IOException {
Path file1 = testRoot.resolve(FILE_1); // mode 664
PosixFileAttributeView allAttributesFile1 =
Files.getFileAttributeView(file1, PosixFileAttributeView.class);
Assert.assertThat(
allAttributesFile1.readAttributes().permissions(),
Matchers.containsInAnyOrder(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.GROUP_READ));
Path file2 = testRoot.resolve(FILE_2); // mode 777
PosixFileAttributeView allAttributesFile2 =
Files.getFileAttributeView(file2, PosixFileAttributeView.class);
Assert.assertThat(
allAttributesFile2.readAttributes().permissions(),
Matchers.containsInAnyOrder(PosixFilePermission.values()));
}