本文整理汇总了Java中java.nio.file.attribute.PosixFileAttributes.permissions方法的典型用法代码示例。如果您正苦于以下问题:Java PosixFileAttributes.permissions方法的具体用法?Java PosixFileAttributes.permissions怎么用?Java PosixFileAttributes.permissions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.attribute.PosixFileAttributes
的用法示例。
在下文中一共展示了PosixFileAttributes.permissions方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isFileReadOnly
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
static final boolean isFileReadOnly(final File file) {
if (System.getProperty("os.name").startsWith("Windows")) {
return !file.canWrite();
}
//All Unix-like OSes
final Path path = Paths.get(file.getAbsolutePath());
PosixFileAttributes attributes = null;
try {
attributes = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes();
} catch (final IOException e) {
// File presence is guaranteed. Ignore
e.printStackTrace();
}
if (attributes == null) { return false; }
// A file is read-only in Linux-derived OSes only when it has 0444 permissions.
final Set<PosixFilePermission> permissions = attributes.permissions();
int bits = 0;
bits |= ((permissions.contains(PosixFilePermission.OWNER_READ)) ? 1 << 8 : 0);
bits |= ((permissions.contains(PosixFilePermission.OWNER_WRITE)) ? 1 << 7 : 0);
bits |= ((permissions.contains(PosixFilePermission.OWNER_EXECUTE)) ? 1 << 6 : 0);
bits |= ((permissions.contains(PosixFilePermission.GROUP_READ)) ? 1 << 5 : 0);
bits |= ((permissions.contains(PosixFilePermission.GROUP_WRITE)) ? 1 << 4 : 0);
bits |= ((permissions.contains(PosixFilePermission.GROUP_EXECUTE)) ? 1 << 3 : 0);
bits |= ((permissions.contains(PosixFilePermission.OTHERS_READ)) ? 1 << 2 : 0);
bits |= ((permissions.contains(PosixFilePermission.OTHERS_WRITE)) ? 1 << 1 : 0);
bits |= ((permissions.contains(PosixFilePermission.OTHERS_EXECUTE)) ? 1 : 0);
//System.out.println(Util.zeroPad(Integer.toBinaryString(bits), 32));
//System.out.println(Util.zeroPad(Integer.toOctalString(bits), 4));
// Here we are going to ignore the Owner Write & Execute bits to allow root/owner testing.
return ((bits & 0477) == 0444);
}
示例2: main
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
public static void main(String[] args) {
try {
Path path = Paths.get("metadata.txt");
Files.deleteIfExists(path);
Files.createFile(path);
BasicFileAttributeView basicView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
BasicFileAttributes basicAttributes = basicView.readAttributes();
boolean isDirectory = basicAttributes.isDirectory();
FileTime lastModifiedTime = basicAttributes.lastModifiedTime();
System.out.println(isDirectory);
System.out.println(lastModifiedTime);
PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
PosixFileAttributes posixAttributes = posixView.readAttributes();
GroupPrincipal group = posixAttributes.group();
Set<PosixFilePermission> permissions = posixAttributes.permissions();
permissions.add(PosixFilePermission.OWNER_EXECUTE);
posixView.setPermissions(permissions);
System.out.println(group);
} catch (IOException e) {
// Log and Handle exception
e.printStackTrace();
}
}
示例3: getPermissionOfFile_shouldContainOwnerRead
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
@Test
public void getPermissionOfFile_shouldContainOwnerRead() throws IOException {
writeToCache("/file.txt");
commitToMaster();
initGitFileSystem();
PosixFileAttributes attributes = readPosixAttributes("/file.txt");
Collection permissions = (Collection) attributes.permissions();
assertTrue(permissions.contains(PosixFilePermission.OWNER_READ));
}
示例4: getPermissionOfFile_shouldContainOwnerWrite
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
@Test
public void getPermissionOfFile_shouldContainOwnerWrite() throws IOException {
writeToCache("/file.txt");
commitToMaster();
initGitFileSystem();
PosixFileAttributes attributes = readPosixAttributes("/file.txt");
Collection permissions = (Collection) attributes.permissions();
assertTrue(permissions.contains(PosixFilePermission.OWNER_WRITE));
}
示例5: getPermissionOfFile_shouldNotContainOwnerExecute
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
@Test
public void getPermissionOfFile_shouldNotContainOwnerExecute() throws IOException {
writeToCache("/file.txt");
commitToMaster();
initGitFileSystem();
PosixFileAttributes attributes = readPosixAttributes("/file.txt");
Collection permissions = (Collection) attributes.permissions();
assertFalse(permissions.contains(OWNER_EXECUTE));
}
示例6: getPermissionOfExecutableFile_shouldContainOwnerExecute
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
@Test
public void getPermissionOfExecutableFile_shouldContainOwnerExecute() throws IOException {
writeToCache("/file.txt", someBytes(), EXECUTABLE_FILE);
commitToMaster();
initGitFileSystem();
PosixFileAttributes attributes = readPosixAttributes("/file.txt");
Collection permissions = (Collection) attributes.permissions();
assertTrue(permissions.contains(OWNER_EXECUTE));
}
示例7: pathIsWritable
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
public boolean pathIsWritable(final Path path) throws IOException {
Preconditions.checkNotNull(path, "path must not be null.");
if (Platform.isWindows()) {
return Files.isWritable(path);
} else {
final PosixFileAttributes attributes = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes();
final Set<PosixFilePermission> permissions = attributes.permissions();
return permissions.contains(PosixFilePermission.OWNER_WRITE) || permissions.contains(PosixFilePermission.GROUP_WRITE) ||
permissions.contains(PosixFilePermission.OTHERS_WRITE);
}
}
示例8: getPermissions
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
private String getPermissions(
String sourceFile ) {
int ownerPermissions = 0;
int groupPermissions = 0;
int othersPermissions = 0;
try {
Path path = Paths.get(sourceFile);
PosixFileAttributes attr;
attr = Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
Set<PosixFilePermission> filePermissions = attr.permissions();
if (filePermissions.contains(PosixFilePermission.OWNER_READ)) {
ownerPermissions += 4;
}
if (filePermissions.contains(PosixFilePermission.OWNER_WRITE)) {
ownerPermissions += 2;
}
if (filePermissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
ownerPermissions += 1;
}
if (filePermissions.contains(PosixFilePermission.GROUP_READ)) {
groupPermissions += 4;
}
if (filePermissions.contains(PosixFilePermission.GROUP_WRITE)) {
groupPermissions += 2;
}
if (filePermissions.contains(PosixFilePermission.GROUP_EXECUTE)) {
groupPermissions += 1;
}
if (filePermissions.contains(PosixFilePermission.OTHERS_READ)) {
othersPermissions += 4;
}
if (filePermissions.contains(PosixFilePermission.OTHERS_WRITE)) {
othersPermissions += 2;
}
if (filePermissions.contains(PosixFilePermission.OTHERS_EXECUTE)) {
othersPermissions += 1;
}
} catch (IOException ioe) {
throw new FileSystemOperationException("Could not get permissions for file '" + sourceFile + "'",
ioe);
}
return "0" + String.valueOf(ownerPermissions) + String.valueOf(groupPermissions)
+ String.valueOf(othersPermissions);
}
示例9: posixAttributesToMap
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
/**
* Convert POSIX attributes to property map.
*
* @param attrs Attributes view.
* @return IGFS properties map.
*/
public static Map<String, String> posixAttributesToMap(PosixFileAttributes attrs) {
if (attrs == null)
return null;
Map<String, String> props = U.newHashMap(3);
props.put(IgfsUtils.PROP_USER_NAME, attrs.owner().getName());
props.put(IgfsUtils.PROP_GROUP_NAME, attrs.group().getName());
int perm = 0;
for(PosixFilePermission p : attrs.permissions())
perm |= (1 << 8 - p.ordinal());
props.put(IgfsUtils.PROP_PERMISSION, '0' + Integer.toOctalString(perm));
return props;
}
示例10: toMode
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
private static int toMode(PosixFileAttributes attrs)
{
int mode = 0;
if (attrs.isDirectory()) {
mode |= FileOps.S_IFDIR;
} else if (attrs.isRegularFile()) {
mode |= FileOps.S_IFREG;
} else if (attrs.isSymbolicLink()) {
mode |= FileOps.S_IFLNK;
} else {
mode |= FileOps.S_IFUNK;
}
Set<PosixFilePermission> perms = attrs.permissions();
if (perms.contains(PosixFilePermission.OWNER_READ)) {
mode |= FileOps.S_IRUSR;
}
if (perms.contains(PosixFilePermission.OWNER_WRITE)) {
mode |= FileOps.S_IWUSR;
}
if (perms.contains(PosixFilePermission.OWNER_EXECUTE)) {
mode |= FileOps.S_IXUSR;
}
if (perms.contains(PosixFilePermission.GROUP_READ)) {
mode |= FileOps.S_IRGRP;
}
if (perms.contains(PosixFilePermission.GROUP_WRITE)) {
mode |= FileOps.S_IWGRP;
}
if (perms.contains(PosixFilePermission.GROUP_EXECUTE)) {
mode |= FileOps.S_IXGRP;
}
if (perms.contains(PosixFilePermission.OTHERS_READ)) {
mode |= FileOps.S_IROTH;
}
if (perms.contains(PosixFilePermission.OTHERS_WRITE)) {
mode |= FileOps.S_IWOTH;
}
if (perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
mode |= FileOps.S_IXOTH;
}
return mode;
}
示例11: testGettingMetadata
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
@Test
public void testGettingMetadata() throws IOException, InterruptedException {
final String tempPathPrefix = null;
final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);
final String fileName = "Gracie.txt";
try {
final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName));
if ( ! Platform.isWindows()) {
final PosixFileAttributes attributes = Files.readAttributes(filePath, PosixFileAttributes.class);
final Set<PosixFilePermission> permissions = attributes.permissions();
permissions.clear();
permissions.add(PosixFilePermission.OWNER_READ);
permissions.add(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(filePath, permissions);
}
final ImmutableMap.Builder<String, Path> fileMapper = ImmutableMap.builder();
fileMapper.put(filePath.toString(), filePath);
final Map<String, String> metadata = new MetadataAccessImpl(fileMapper.build()).getMetadataValue(filePath.toString());
if (Platform.isWindows()) {
assertEquals(metadata.size(), 13);
} else {
assertEquals(metadata.size(), 10);
}
if (Platform.isMac()) {
assertTrue(metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_OS).toLowerCase().startsWith(Platform.MAC_SYSTEM_NAME));
} else if (Platform.isLinux()) {
assertTrue(metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_OS).toLowerCase().startsWith(Platform.LINUX_SYSTEM_NAME));
} else if (Platform.isWindows()) {
assertTrue(metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_OS).toLowerCase().startsWith(Platform.WINDOWS_SYSTEM_NAME));
}
if (Platform.isWindows()) {
assertEquals("A", metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_FLAGS));
} else {
assertEquals("100600", metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_MODE));
assertEquals("600(rw-------)", metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_PERMISSION));
}
} finally {
FileUtils.deleteDirectory(tempDirectory.toFile());
}
}
示例12: testGettingMetadataFailureDoesntThrow
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
@Test
public void testGettingMetadataFailureDoesntThrow() throws IOException, InterruptedException {
Assume.assumeFalse(Platform.isWindows());
try {
final String tempPathPrefix = null;
final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);
final String fileName = "Gracie.txt";
final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName));
try {
// set permissions
if (!Platform.isWindows()) {
final PosixFileAttributes attributes = Files.readAttributes(filePath, PosixFileAttributes.class);
final Set<PosixFilePermission> permissions = attributes.permissions();
permissions.clear();
permissions.add(PosixFilePermission.OWNER_READ);
permissions.add(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(filePath, permissions);
}
// get permissions
final ImmutableMap.Builder<String, Path> fileMapper = ImmutableMap.builder();
fileMapper.put(filePath.toString(), filePath);
final Map<String, String> metadataFromFile = new MetadataAccessImpl(fileMapper.build()).getMetadataValue(filePath.toString());
FileUtils.deleteDirectory(tempDirectory.toFile());
// put old permissions back
final Metadata metadata = new MetadataImpl(new MockedHeadersReturningKeys(metadataFromFile));
new MetadataReceivedListenerImpl(tempDirectory.toString()).metadataReceived(fileName, metadata);
} finally {
FileUtils.deleteDirectory(tempDirectory.toFile());
}
} catch (final Throwable t) {
fail("Throwing exceptions from metadata est verbotten");
}
}
示例13: testGettingMetadataFailureHandler
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
@Test
public void testGettingMetadataFailureHandler() throws IOException, InterruptedException {
Assume.assumeFalse(Platform.isWindows());
try {
final String tempPathPrefix = null;
final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);
final String fileName = "Gracie.txt";
final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName));
try {
// set permissions
if (!Platform.isWindows()) {
final PosixFileAttributes attributes = Files.readAttributes(filePath, PosixFileAttributes.class);
final Set<PosixFilePermission> permissions = attributes.permissions();
permissions.clear();
permissions.add(PosixFilePermission.OWNER_READ);
permissions.add(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(filePath, permissions);
}
// get permissions
final ImmutableMap.Builder<String, Path> fileMapper = ImmutableMap.builder();
fileMapper.put(filePath.toString(), filePath);
final Map<String, String> metadataFromFile = new MetadataAccessImpl(fileMapper.build()).getMetadataValue(filePath.toString());
FileUtils.deleteDirectory(tempDirectory.toFile());
// put old permissions back
final Metadata metadata = new MetadataImpl(new MockedHeadersReturningKeys(metadataFromFile));
final AtomicInteger numTimesFailureHandlerCalled = new AtomicInteger(0);
new MetadataReceivedListenerImpl(tempDirectory.toString(),
new FailureEventListener() {
@Override
public void onFailure(final FailureEvent failureEvent) {
numTimesFailureHandlerCalled.incrementAndGet();
assertEquals(FailureEvent.FailureActivity.RestoringMetadata, failureEvent.doingWhat());
}
},"localhost")
.metadataReceived(fileName, metadata);
assertEquals(1, numTimesFailureHandlerCalled.get());
} finally {
FileUtils.deleteDirectory(tempDirectory.toFile());
}
} catch (final Throwable t) {
fail("Throwing exceptions from metadata est verbotten");
}
}
示例14: preVisitDirectory
import java.nio.file.attribute.PosixFileAttributes; //导入方法依赖的package包/类
@Override public FileVisitResult preVisitDirectory(Path directory, BasicFileAttributes basicAttribs) throws IOException
{
boolean skip = false;
for(int i = 0; i < excludes.size(); i++)
{
if(directory.toString().matches(excludes.get(i)))
skip = true;
}
if(skip)
{
System.out.println("Ignored excluded directory: " + directory);
return FileVisitResult.CONTINUE;
}
if(pathExists(directory))
{
System.out.println("Skipping existing directory: " + directory);
return FileVisitResult.CONTINUE;
}
System.out.print("Importing directory: " + directory);
PosixFileAttributes posixAttribs = Files.readAttributes(directory, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
SimpleDateFormat timestampFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date currentTime = new Date();
String timestamp = timestampFormat.format(currentTime);
System.out.println(" " + timestamp);
Set<PosixFilePermission> posixPerms = posixAttribs.permissions();
String perms = PosixFilePermissions.toString(posixPerms);
String inode = posixAttribs.fileKey().toString();
Pattern p = Pattern.compile("\\=(\\d+)\\)");
Matcher m = p.matcher(inode);
m.find();
inode = m.group(1);
long mtimeStamp = posixAttribs.lastModifiedTime().toMillis();
if(posixAttribs.isSymbolicLink())
System.out.println(Files.readSymbolicLink(directory).toString());
// Log this to file once in production
//System.out.println("Properties of directory: " + directory);
//System.out.println(inode + " " + perms + " " + posixAttribs.owner().getName() + ":" + posixAttribs.group().getName() + " " + posixAttribs.size() + " " + calcMtime(mtimeStamp) + "\n");
CloneFilesystem.Record entry = new CloneFilesystem.Record();
entry.path = directory.toString();
entry.name = new File(entry.path).getName();
entry.inode = Long.parseLong(inode);
entry.type = determineType(directory, posixAttribs);
entry.mode = calculateOctalPerms(perms);
entry.linkCount = 1; // link count is currently broken; set a default of one (itself)
entry.uid = users.get(posixAttribs.owner().getName()); // use containsKey() if this causes a null pointer exception
entry.gid = groups.get(posixAttribs.group().getName()); // same as above
entry.atime = posixAttribs.lastAccessTime().toMillis();
entry.ctime = posixAttribs.creationTime().toMillis();
entry.mtime = posixAttribs.lastModifiedTime().toMillis();
if(posixAttribs.isSymbolicLink())
entry.link = Files.readSymbolicLink(directory).toString();
else
entry.link = null;
entry.data = "";
writeDatabase(entry, false, true);
return FileVisitResult.CONTINUE;
}