gpt4 book ai didi

模仿 chmod -R 755 的 java walkFileTree 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 01:31:39 25 4
gpt4 key购买 nike

我想确保目录树中的正确权限(目录为 0755,文件为 644)。这是我的类(class):

package NRF_Utils;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import static java.nio.file.FileVisitResult.*;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.logging.Logger;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;

public class DirectoryTreeOperations {
private static final Logger log = Logger.getLogger("spv." + DirectoryTreeOperations.class.getName());

public static boolean setPermissions(String root, String directoryPermissions, String filePermissions) {
boolean result = true;
//declaring the path to delete
final Path path = Paths.get(root);
final Set<PosixFilePermission> dirPermissions = PosixFilePermissions.fromString(directoryPermissions);
final Set<PosixFilePermission> filPermissions = PosixFilePermissions.fromString(filePermissions);

try {
Files.walkFileTree(path, new FileVisitor<Path>() {

@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
System.out.println("setting dir permission on " + dir);
Files.setPosixFilePermissions(dir, dirPermissions);
return CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
System.out.println("setting file permission on " + file);
Files.setPosixFilePermissions(file, filPermissions);
return CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
log.severe("visitFileFailed failed on " + file + " : " + exc);
return CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir,
IOException exc) throws IOException {
return CONTINUE;
}

});
} catch (IOException e) {
log.severe("setPermissions failed " + e);
result = false;
}
return result;
}

// TODO remove and provide a proper unit test
public static void main(String [] arg) {
System.out.println ("setPermissions returned: " + setPermissions("/tmp/x", "rwxr-xr-x", "rw-r--r--"));
}
}

为了进行测试,我做了:

mkdir -p /tmp/x/y/z;touch /tmp/x/y/z/f;chmod 000 /tmp/x/y/z

输出是:

setting dir permission on /tmp/x
setting dir permission on /tmp/x/y
setPermissions returned: true
nov. 26, 2017 2:45:00 PM NRF_Utils.DirectoryTreeOperations$1 visitFileFailed
GRAVE: visitFileFailed failed on /tmp/x/y/z : java.nio.file.AccessDeniedException: /tmp/x/y/z

java似乎不想探索目录树,因为权限不对,但我的目的是探索目录树以修复权限。 API 疯了吗?

最佳答案

它的这种行为方式看起来确实很奇怪,所以也许不是一个疯狂的 API,但实现肯定不适合使用 preVisitDirectory() 使目录可访问的特定情况。

Files.walkFileTree() 文档说:

Where the file is a directory, and the directory could not be opened, then the visitFileFailed method is invoked with the I/O exception, after which, the file tree walk continues, by default, at the next sibling of the directory.

并查看实现,它实际上在调用 visitor.preVisitDirectory() 之前尝试打开目录,这解释了您遇到的问题。

作为解决方案,您可以获取 FileTreeWalker.java 源代码,并在您自己的本地版本的 FileTreeWalker 中进行细微调整 - 直接查找对 visitor.preVisitDirectory() 的调用即可> 并将打开的目录代码移到之后发生。然后将您的代码更改为:

  new FileTreeWalker(EnumSet.noneOf(FileVisitOption.class), new FileVisitor<Path>() {
...
}, Integer.MAX_VALUE).walk(path);

当你运行时,你会得到:

setting dir permission on /tmp/x
setting dir permission on /tmp/x/y
setting dir permission on /tmp/x/y/z
setting file permission on /tmp/x/y/z/f
setPermissions returned: true

这表明您对 FileVisitor 的实现没有问题。

希望对您有所帮助。

关于模仿 chmod -R 755 的 java walkFileTree 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47496853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com