use of java.nio.file.attribute.PosixFileAttributes in project java-chassis by ServiceComb.
the class FilePerm method setFilePerm.
/**
* 设置文件权限。前提:必须支持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);
}
}
}
use of java.nio.file.attribute.PosixFileAttributes in project alluxio by Alluxio.
the class SimpleFileManagerTest method addMultipleFileSameName.
@Test
public void addMultipleFileSameName() throws Exception {
assertTrue(mFileManager.addFile("test", "644", "test".getBytes()));
List<String> files = mFileManager.listFile();
assertEquals(1, files.size());
assertEquals("test", files.get(0));
Path file = Paths.get(mFileManager.getNextFilePath("test"));
PosixFileAttributes attrs = Files.getFileAttributeView(file, PosixFileAttributeView.class).readAttributes();
assertEquals(mUser, attrs.owner().getName());
assertEquals(mGroup, attrs.group().getName());
verifyPerms(attrs.permissions(), "644");
// Create a new file with the same name, but different content and permissions
assertTrue(mFileManager.addFile("test", "777", "test 2".getBytes()));
files = mFileManager.listFile();
assertEquals(1, files.size());
assertEquals("test", files.get(0));
file = Paths.get(mFileManager.getNextFilePath("test"));
attrs = Files.getFileAttributeView(file, PosixFileAttributeView.class).readAttributes();
assertEquals(mUser, attrs.owner().getName());
assertEquals(mGroup, attrs.group().getName());
verifyPerms(attrs.permissions(), "777");
}
use of java.nio.file.attribute.PosixFileAttributes in project alluxio by Alluxio.
the class LocalUnderFileSystem method getStatus.
@Override
public UfsStatus getStatus(String path) throws IOException {
String tpath = stripPath(path);
File file = new File(tpath);
try {
PosixFileAttributes attr = Files.readAttributes(Paths.get(file.getPath()), PosixFileAttributes.class);
if (file.isFile()) {
// Return file status.
String contentHash = UnderFileSystemUtils.approximateContentHash(file.length(), file.lastModified());
return new UfsFileStatus(path, contentHash, file.length(), file.lastModified(), attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), mUfsConf.getBytes(PropertyKey.USER_BLOCK_SIZE_BYTES_DEFAULT));
}
// Return directory status.
return new UfsDirectoryStatus(path, attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), file.lastModified());
} catch (FileSystemException e) {
throw new FileNotFoundException(e.getMessage());
}
}
use of java.nio.file.attribute.PosixFileAttributes in project alluxio by Alluxio.
the class LocalUnderFileSystem method getFileStatus.
@Override
public UfsFileStatus getFileStatus(String path) throws IOException {
String tpath = stripPath(path);
File file = new File(tpath);
try {
PosixFileAttributes attr = Files.readAttributes(Paths.get(file.getPath()), PosixFileAttributes.class);
if (attr.isDirectory()) {
throw new IOException(String.format("path %s is not a file", path));
}
String contentHash = UnderFileSystemUtils.approximateContentHash(file.length(), file.lastModified());
return new UfsFileStatus(path, contentHash, file.length(), file.lastModified(), attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), mUfsConf.getBytes(PropertyKey.USER_BLOCK_SIZE_BYTES_DEFAULT));
} catch (FileSystemException e) {
throw new FileNotFoundException(e.getMessage());
}
}
use of java.nio.file.attribute.PosixFileAttributes in project alluxio by Alluxio.
the class LocalUnderFileSystem method getDirectoryStatus.
@Override
public UfsDirectoryStatus getDirectoryStatus(String path) throws IOException {
String tpath = stripPath(path);
File file = new File(tpath);
try {
PosixFileAttributes attr = Files.readAttributes(Paths.get(file.getPath()), PosixFileAttributes.class);
if (!attr.isDirectory()) {
throw new IOException(String.format("path %s is not directory", path));
}
return new UfsDirectoryStatus(path, attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), file.lastModified());
} catch (FileSystemException e) {
throw new FileNotFoundException(e.getMessage());
}
}
Aggregations