當前位置: 首頁>>代碼示例>>Java>>正文


Java PosixFileAttributes.permissions方法代碼示例

本文整理匯總了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);
}
 
開發者ID:DataSketches,項目名稱:memory,代碼行數:33,代碼來源:AllocateDirectMap.java

示例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();
	}
}
 
開發者ID:ldebello,項目名稱:javacuriosities,代碼行數:34,代碼來源:Lesson06FileAttributes.java

示例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));
}
 
開發者ID:beijunyi,項目名稱:ParallelGit,代碼行數:11,代碼來源:PosixFileAttributesTest.java

示例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));
}
 
開發者ID:beijunyi,項目名稱:ParallelGit,代碼行數:11,代碼來源:PosixFileAttributesTest.java

示例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));
}
 
開發者ID:beijunyi,項目名稱:ParallelGit,代碼行數:11,代碼來源:PosixFileAttributesTest.java

示例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));
}
 
開發者ID:beijunyi,項目名稱:ParallelGit,代碼行數:11,代碼來源:PosixFileAttributesTest.java

示例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);
    }
}
 
開發者ID:SpectraLogic,項目名稱:ds3_java_sdk,代碼行數:14,代碼來源:FileSystemHelperImpl.java

示例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);
}
 
開發者ID:Axway,項目名稱:ats-framework,代碼行數:50,代碼來源:LocalFileSystemOperations.java

示例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;
}
 
開發者ID:apache,項目名稱:ignite,代碼行數:25,代碼來源:LocalFileSystemUtils.java

示例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;
}
 
開發者ID:perlundq,項目名稱:yajsync,代碼行數:46,代碼來源:PosixFileAttributeManager.java

示例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());
    }
}
 
開發者ID:SpectraLogic,項目名稱:ds3_java_sdk,代碼行數:48,代碼來源:MetadataAccessImpl_Test.java

示例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");
    }
}
 
開發者ID:SpectraLogic,項目名稱:ds3_java_sdk,代碼行數:42,代碼來源:MetadataReceivedListenerImpl_Test.java

示例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");
    }
}
 
開發者ID:SpectraLogic,項目名稱:ds3_java_sdk,代碼行數:54,代碼來源:MetadataReceivedListenerImpl_Test.java

示例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;
}
 
開發者ID:ldilley,項目名稱:linulator,代碼行數:67,代碼來源:CloneFilesystem.java


注:本文中的java.nio.file.attribute.PosixFileAttributes.permissions方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。