本文整理了Java中java.nio.file.Files.getOwner()
方法的一些代码示例,展示了Files.getOwner()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.getOwner()
方法的具体详情如下:
包路径:java.nio.file.Files
类名称:Files
方法名:getOwner
暂无
代码示例来源:origin: apache/storm
public static String getFileOwner(String path) throws IOException {
return Files.getOwner(FileSystems.getDefault().getPath(path)).getName();
}
代码示例来源:origin: org.apache.ant/ant
@Override
public boolean isSelected(File basedir, String filename, File file) {
if (owner == null) {
throw new BuildException("the owner attribute is required");
}
if (file != null) {
try {
UserPrincipal user = followSymlinks ? Files.getOwner(file.toPath())
: Files.getOwner(file.toPath(), LinkOption.NOFOLLOW_LINKS);
return user != null && owner.equals(user.getName());
} catch (UnsupportedOperationException | IOException ex) {
// => not the expected owner
}
}
return false;
}
代码示例来源:origin: apache/storm
@Override
public void deleteIfExists(File path, String user, String logPrefix) throws IOException {
String absolutePath = path.getAbsolutePath();
if (Utils.checkFileExists(absolutePath)) {
LOG.info("Deleting path (runAsUser) {}", absolutePath);
if (user == null) {
user = Files.getOwner(path.toPath()).getName();
}
List<String> commands = new ArrayList<>();
commands.add("rmr");
commands.add(absolutePath);
ClientSupervisorUtils.processLauncherAndWait(_conf, user, commands, null, logPrefix);
if (Utils.checkFileExists(absolutePath)) {
// It's possible that permissions were not set properly on the directory, and
// the user who is *supposed* to own the dir does not. In this case, try the
// delete as the supervisor user.
Utils.forceDelete(absolutePath);
if (Utils.checkFileExists(absolutePath)) {
throw new RuntimeException(path + " was not deleted.");
}
}
}
}
代码示例来源:origin: apache/storm
/**
* @return the user that some operations should be done as.
*
* @throws IOException on any error
*/
protected String getWorkerUser() throws IOException {
LOG.info("GET worker-user for {}", _workerId);
File file = new File(ConfigUtils.workerUserFile(_conf, _workerId));
if (_ops.fileExists(file)) {
return _ops.slurpString(file).trim();
} else if (_assignment != null && _assignment.is_set_owner()) {
return _assignment.get_owner();
}
if (ConfigUtils.isLocalMode(_conf)) {
return System.getProperty("user.name");
} else {
File f = new File(ConfigUtils.workerArtifactsRoot(_conf));
if (f.exists()) {
return Files.getOwner(f.toPath()).getName();
}
throw new IllegalStateException("Could not recover the user for " + _workerId);
}
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testChownToOwnUser() throws Exception {
String file1 = "some-file.dat";
createFileWithJunk(file1, 100);
String fullPath = testDir + pathSep + file1;
Path path = Paths.get(fullPath);
UserPrincipal owner = Files.getOwner(path);
String user = owner.getName();
vertx.fileSystem().chown(fullPath, user, null, ar -> {
deleteFile(file1);
assertTrue(ar.succeeded());
testComplete();
});
await();
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
assertEquals(user, Files.getOwner(path).getName());
assertEquals(group, Files.readAttributes(path, PosixFileAttributes.class).group().getName());
} finally {
代码示例来源:origin: io.vertx/vertx-core
@Test
public void testChownToOwnUser() throws Exception {
String file1 = "some-file.dat";
createFileWithJunk(file1, 100);
String fullPath = testDir + pathSep + file1;
Path path = Paths.get(fullPath);
UserPrincipal owner = Files.getOwner(path);
String user = owner.getName();
vertx.fileSystem().chown(fullPath, user, null, ar -> {
deleteFile(file1);
assertTrue(ar.succeeded());
testComplete();
});
await();
}
代码示例来源:origin: org.eclipse.dirigible/dirigible-api-io
/**
* Get the name of teh principal representing the file owner
* @param path path to a file
* @return the name of the principal representing the file owner
* @throws IOException in case of failure in underlying layer
*/
public static final String getOwner(String path) throws IOException {
return Files.getOwner(Paths.get(path)).getName();
}
代码示例来源:origin: org.apache.twill/twill-common
@Override
public String getOwner() throws IOException {
return Files.getOwner(file.toPath()).getName();
}
代码示例来源:origin: org.apache.storm/storm-core
public static String getFileOwner(String path) throws IOException {
return Files.getOwner(FileSystems.getDefault().getPath(path)).getName();
}
代码示例来源:origin: apache/twill
@Override
public String getOwner() throws IOException {
return Files.getOwner(file.toPath()).getName();
}
代码示例来源:origin: ModeShape/modeshape
private String ownerFor( File file ) {
Path filePath = Paths.get(file.toURI());
try {
return Files.getOwner(filePath).getName();
} catch (IOException e) {
log().debug(e, "Unable to read the owner of '{0}'", filePath);
return null;
}
}
代码示例来源:origin: org.fcrepo/modeshape-jcr
private String ownerFor( File file ) {
Path filePath = Paths.get(file.toURI());
try {
return Files.getOwner(filePath).getName();
} catch (IOException e) {
log().debug(e, "Unable to read the owner of '{0}'", filePath);
return null;
}
}
代码示例来源:origin: de.pfabulist.lindwurm/niotest
@Test
@Category( { Posix.class, Unix.class, Principals.class } )
public void testFilesHaveOwners() throws IOException {
assertThat( Files.getOwner( getFile() ) ).isNotNull();
}
代码示例来源:origin: org.unix4j/unix4j-command
@Override
protected String getOwner(File file, LsArguments args) {
try {
final String owner = Files.getOwner(file.toPath()).getName();
return StringUtil.fixSizeString(7, true, owner);
} catch (IOException e) {
return super.getOwner(file, args);
}
}
代码示例来源:origin: de.pfabulist.lindwurm/niotest
@Test
@Category( { Principals.class, Attributes.class, FileOwnerView.class } )
public void testFindOwner() throws IOException {
UserPrincipalLookupService lookupService = FS.getUserPrincipalLookupService();
UserPrincipal owner = Files.getOwner( FS.getPath( "" ).toAbsolutePath() );
assertThat( lookupService.lookupPrincipalByName( owner.getName() ) ).isEqualTo( owner );
}
代码示例来源:origin: tools4j/unix4j
@Override
protected String getOwner(File file, LsArguments args) {
try {
final String owner = Files.getOwner(file.toPath()).getName();
return StringUtil.fixSizeString(7, true, owner);
} catch (IOException e) {
return super.getOwner(file, args);
}
}
代码示例来源:origin: de.pfabulist.lindwurm/niotest
@Test
@Category( { Attributes.class, Posix.class, OwnerView.class } )
public void testOwnerByTwoMethods() throws IOException {
assertThat( Files.getOwner( pathDefault() ) ).isEqualTo(
Files.readAttributes( pathDefault(), PosixFileAttributes.class ).owner() );
}
代码示例来源:origin: shrinkwrap/shrinkwrap
@Test(expected = UnsupportedOperationException.class)
public void getOwner() throws IOException {
Files.getOwner(fs.getPath("path"), (LinkOption) null);
}
代码示例来源:origin: de.pfabulist.lindwurm/memoryfs
@Override
public void checkAccess( EightyPath path, AccessMode... modes ) {
checkContract( path );
// can not be moved to Eighty because Files.exists calls checkAccess first -> loop
if( !isDirectory( path ) && !isFile( path ) ) {
throw u( new NoSuchFileException( "files does not exist " + path ) );
}
// todo generalize
if( Arrays.asList( modes ).contains( AccessMode.WRITE ) ) {
UserPrincipal fileOwner = Unchecked.u( () -> Files.getOwner( path ) );
if( !principals.getCurrentUser().equals( fileOwner ) ) {
throw u( new AccessDeniedException( "file is not writable " ) );
}
}
}
内容来源于网络,如有侵权,请联系作者删除!