本文整理汇总了Java中java.nio.file.attribute.PosixFilePermissions类的典型用法代码示例。如果您正苦于以下问题:Java PosixFilePermissions类的具体用法?Java PosixFilePermissions怎么用?Java PosixFilePermissions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PosixFilePermissions类属于java.nio.file.attribute包,在下文中一共展示了PosixFilePermissions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: stashOriginalFilePermissions
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
@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);
}
}
示例2: createLocalConfigFile
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的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.");
}
示例3: getPosixPerm
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的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());
}
示例4: setFilePerm
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的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: flush
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
@Override
public void flush() throws IOException {
super.flush();
if (LOG.isDebugEnabled()) {
LOG.debug("Reseting permissions to '" + permissions + "'");
}
if (!Shell.WINDOWS) {
Files.setPosixFilePermissions(Paths.get(file.getCanonicalPath()),
permissions);
} else {
// FsPermission expects a 10-character string because of the leading
// directory indicator, i.e. "drwx------". The JDK toString method returns
// a 9-character string, so prepend a leading character.
FsPermission fsPermission = FsPermission.valueOf(
"-" + PosixFilePermissions.toString(permissions));
FileUtil.setPermission(file, fsPermission);
}
}
示例6: create
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
@Override
public int create(String path, @mode_t long mode, FuseFileInfo fi) {
try {
Set<OpenFlags> flags = bitMaskUtil.bitMaskToSet(OpenFlags.class, fi.flags.longValue());
LOG.info("createAndOpen {} with openOptions {}", path, flags);
Path node = resolvePath(path);
if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) {
FileAttribute<?> attrs = PosixFilePermissions.asFileAttribute(attrUtil.octalModeToPosixPermissions(mode));
return fileHandler.createAndOpen(node, fi, attrs);
} else {
return fileHandler.createAndOpen(node, fi);
}
} catch (RuntimeException e) {
LOG.error("create failed.", e);
return -ErrorCodes.EIO();
}
}
示例7: init
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
@PostConstruct
public void init() {
if (!createBaseDir) {
// Don't create dir in tests
return;
}
LOG.info("Initialize base directory {}", baseDir);
if (!Files.isDirectory(baseDir)) {
try {
Files.createDirectories(baseDir,
PosixFilePermissions.asFileAttribute(
PosixFilePermissions.fromString("rwx------")));
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}
}
示例8: testBrokenEnvironment
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
/**
* Doesn't appear to work in docker, will never work in windows.
* @throws IOException if any of the setup fails
*/
@Test
public void testBrokenEnvironment() throws IOException {
File root = testFolder.newFolder();
File workingDir = new File(root, FileStore.STANDARD.getPath() + File.separator);
DefinitionModel model = new DefinitionModel(SIMPLE_MODEL);
Files.createDirectories(workingDir.toPath(),
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(NO_PERMS)));
if (PosixFilePermissions.fromString(NO_PERMS).equals(Files.getPosixFilePermissions(workingDir.toPath()))) {
TestFileStore fileStore = new TestFileStore(root);
assertThatThrownBy(() -> fileStore.store(model)).isInstanceOf(IOException.class)
.hasMessage("could not write directories for model storage");
Files.setPosixFilePermissions(workingDir.toPath(), PosixFilePermissions.fromString("rwxrwxrwx"));
}
cleanUp(root);
}
示例9: failedStorage
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
/**
* Doesn't appear to work in docker, will never work in windows.
* @throws IOException if any of the setup fails
*/
@Test
public void failedStorage() throws IOException {
File root = testFolder.newFolder();
File workingDir = new File(root, FileStore.STANDARD.getPath() + File.separator);
Files.createDirectories(workingDir.toPath(),
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(NO_PERMS)));
if (PosixFilePermissions.fromString(NO_PERMS).equals(Files.getPosixFilePermissions(workingDir.toPath()))) {
DefinitionModel model1 = new DefinitionModel(SIMPLE_MODEL);
TestErrorListener el = new TestErrorListener();
TestFileStore fileStore = new TestFileStore(workingDir);
ResourceLoader loader = new ClasspathUrlResourceLoader(workingDir);
DefinitionModelStore store = new GsonDefinitionModelStore(loader, fileStore);
Set<DefinitionModel> models = new HashSet<>();
models.add(model1);
new DefinitionContentInspector()
.inspectDefinitionGraph(models, el, new ReflectionAssignabilityUtils(), store);
assertThat(el.getErrors()).hasSize(1);
assertThat(el.getErrors().get(0).getMessage()).isEqualTo(ErrorType.COULD_NOT_STORE);
Files.setPosixFilePermissions(workingDir.toPath(), PosixFilePermissions.fromString("rwxrwxrwx"));
}
cleanUp(root);
}
示例10: testConvert
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
@Test
public void testConvert() throws Exception {
final LocalSession session = new LocalSession(new Host(new LocalProtocol(), new LocalProtocol().getDefaultHostname()));
if(session.isPosixFilesystem()) {
session.open(new DisabledHostKeyCallback(), new DisabledLoginCallback());
session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
final Path file = new Path(new LocalHomeFinderFeature(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
new LocalTouchFeature(session).touch(file, new TransferStatus());
final java.nio.file.Path local = session.toPath(file);
final PosixFileAttributes posixAttributes = Files.readAttributes(local, PosixFileAttributes.class);
final LocalAttributesFinderFeature finder = new LocalAttributesFinderFeature(session);
assertEquals(PosixFilePermissions.toString(posixAttributes.permissions()), finder.find(file).getPermission().getSymbol());
Files.setPosixFilePermissions(local, PosixFilePermissions.fromString("rw-------"));
assertEquals("rw-------", finder.find(file).getPermission().getSymbol());
Files.setPosixFilePermissions(local, PosixFilePermissions.fromString("rwxrwxrwx"));
assertEquals("rwxrwxrwx", finder.find(file).getPermission().getSymbol());
Files.setPosixFilePermissions(local, PosixFilePermissions.fromString("rw-rw----"));
assertEquals("rw-rw----", finder.find(file).getPermission().getSymbol());
assertEquals(posixAttributes.size(), finder.find(file).getSize());
assertEquals(posixAttributes.lastModifiedTime().toMillis(), finder.find(file).getModificationDate());
assertEquals(posixAttributes.creationTime().toMillis(), finder.find(file).getCreationDate());
assertEquals(posixAttributes.lastAccessTime().toMillis(), finder.find(file).getAccessedDate());
new LocalDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
}
示例11: makeFileNonReadable
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
private static void makeFileNonReadable(String file) throws IOException {
Path filePath = Paths.get(file);
Set<String> supportedAttr = filePath.getFileSystem().supportedFileAttributeViews();
if (supportedAttr.contains("posix")) {
Files.setPosixFilePermissions(filePath, PosixFilePermissions.fromString("-w--w----"));
} else if (supportedAttr.contains("acl")) {
UserPrincipal fileOwner = Files.getOwner(filePath);
AclFileAttributeView view = Files.getFileAttributeView(filePath, AclFileAttributeView.class);
AclEntry entry = AclEntry.newBuilder()
.setType(AclEntryType.DENY)
.setPrincipal(fileOwner)
.setPermissions(AclEntryPermission.READ_DATA)
.build();
List<AclEntry> acl = view.getAcl();
acl.add(0, entry);
view.setAcl(acl);
}
}
示例12: setupEnvironment
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
/**
* creates and grants permission to daemon files directory
*/
private static void setupEnvironment() {
final File daemonFilePath = new File("/var/run/iofabric");
if (!daemonFilePath.exists()) {
try {
daemonFilePath.mkdirs();
UserPrincipalLookupService lookupservice = FileSystems.getDefault().getUserPrincipalLookupService();
final GroupPrincipal group = lookupservice.lookupPrincipalByGroupName("iofabric");
Files.getFileAttributeView(daemonFilePath.toPath(), PosixFileAttributeView.class,
LinkOption.NOFOLLOW_LINKS).setGroup(group);
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrwx---");
Files.setPosixFilePermissions(daemonFilePath.toPath(), perms);
} catch (Exception e) {
}
}
}
示例13: runChmodCheck
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
private void runChmodCheck(String routeSuffix, String expectedPermissions) throws Exception {
MockEndpoint mock = getMockEndpoint("mock:chmod" + routeSuffix);
mock.expectedMessageCount(1);
String testFileName = "chmod" + routeSuffix + ".txt";
String fullTestFileName = TEST_DIRECTORY + testFileName;
String testFileContent = "Writing file with chmod " + routeSuffix + " option at " + new Date();
mock.expectedFileExists(fullTestFileName, testFileContent);
template.sendBodyAndHeader("direct:write" + routeSuffix, testFileContent, Exchange.FILE_NAME, testFileName);
File f = new File(fullTestFileName);
Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(f.toPath(), LinkOption.NOFOLLOW_LINKS);
assertEquals(expectedPermissions, PosixFilePermissions.toString(permissions));
assertEquals(expectedPermissions.replace("-", "").length(), permissions.size());
assertMockEndpointsSatisfied();
}
示例14: getRuntimeDir
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
/**
* @return The single base directory relative to which user-specific
* non-essential runtime files and other file objects (such as sockets,
* named pipes, ...) should be stored.
*/
@Override
@NonNull
@EnsuresNonNull("this.runtimeDir")
public File getRuntimeDir() {
if (runtimeDir != null) return runtimeDir;
File dir = getBaseDir("XDG_RUNTIME_DIR", null);
if (dir == null) {
log.warn("Synthesizing runtime directory, as $XDG_RUNTIME_DIR is unset");
dir = new File(System.getProperty("java.io.tmpdir"));
dir = new File(dir, appName+"-"+System.getProperty("user.name"));
dir.mkdirs();
}
try {
Files.setPosixFilePermissions(dir.toPath(), PosixFilePermissions.fromString("rwx------"));
} catch (IOException | UnsupportedOperationException e) {
log.warn("Failed to set directory permissions on {} to owner-only", dir, e);
}
runtimeDir = dir;
Directories.deleteOnExit(dir);
return dir;
}
示例15: createUnixDirectories
import java.nio.file.attribute.PosixFilePermissions; //导入依赖的package包/类
public static void createUnixDirectories(
final Permission perm,
final Path... dirs) throws IOException {
FileAttribute<Set<PosixFilePermission>> attr
= PosixFilePermissions.asFileAttribute(POSIX_OWNrwx);
switch (perm) {
case OWNER_FULL_GROUP_EXEC:
attr = PosixFilePermissions.asFileAttribute(POSIX_OWNrwx_GRPr_x);
break;
case OWNER_FULL_GROUP_EXEC_OTHER_EXEC:
attr = PosixFilePermissions.asFileAttribute(POSIX_OWNrwx_GRPr_x_OTHr_x);
break;
}
for (Path dir : dirs) {
if (!java.nio.file.Files.exists(dir, LinkOption.NOFOLLOW_LINKS)) {
m_logger.debug("Creating directory: {} with permissions: {}", dir, perm);
java.nio.file.Files.createDirectories(dir, attr);
} else {
m_logger.debug("Directory exists: {}", dir);
}
}
}