我尝试使用公共写入权限创建目录。为简化起见,我希望它具有777
权限。这是我的代码:
private static FileAttribute<Set<PosixFilePermission>> DIR_PERMISSIONS;
static {
Set<PosixFilePermission> perms = new HashSet<>();
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_WRITE);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_WRITE);
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
DIR_PERMISSIONS = PosixFilePermissions.asFileAttribute(perms);
}
private Path ensurePath(LocalDate localDate) throws IOException {
String year = String.valueOf(localDate.getYear());
String month = String.format("%02d", localDate.getMonthValue());
Path path = Paths.get(rootDirectory, year, month);
return Files.createDirectories(path, DIR_PERMISSIONS);
}
使用rootDirectory=/tmp/data
,这应该会创建/tmp/data/2016/01
这样的文件夹,每个文件夹都有777
个权限。相反,文件夹具有775
权限(drwxrwxr-x。),因此它们缺少公共写入。为什么这样工作?也许JVM需要特殊的参数才能设置这样的权限?
我的系统是Fedora 24,app是标准的Spring Boot应用程序,由maven插件启动。
答案 0 :(得分:0)
好的,这让我很好奇,所以这里有:
Google导致了这一点:https://stackoverflow.com/a/25557947/6768037(“Java正在设置您所寻求的权限,然后将其屏蔽掉。”)然后返回@OlivierGrégoire最初提供的链接。 umask 是一个苛刻的情妇。
Spring Boot与此行为无关。以下是一个简单的概念证明。我的默认umask是0002.我的/ tmp目录最初是空的。
public class Test {
public static void main(String[] args) {
Set<PosixFilePermission> fullPermission = new HashSet<PosixFilePermission>();
fullPermission.add(PosixFilePermission.OWNER_EXECUTE);
fullPermission.add(PosixFilePermission.OWNER_READ);
fullPermission.add(PosixFilePermission.OWNER_WRITE);
fullPermission.add(PosixFilePermission.GROUP_EXECUTE);
fullPermission.add(PosixFilePermission.GROUP_READ);
fullPermission.add(PosixFilePermission.GROUP_WRITE);
fullPermission.add(PosixFilePermission.OTHERS_EXECUTE);
fullPermission.add(PosixFilePermission.OTHERS_READ);
fullPermission.add(PosixFilePermission.OTHERS_WRITE);
Path path = Paths.get("/tmp/data/", "01/26");
try {
Files.createDirectories(path, PosixFilePermissions.asFileAttribute(fullPermission));
outputLS(path);
Files.setPosixFilePermissions(path, fullPermission);
outputLS(path);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void outputLS(Path path) throws IOException {
System.out.println(new java.util.Scanner(Runtime.getRuntime().exec("ls -alt " + path.toAbsolutePath() + "/..").getInputStream()).useDelimiter("\\A").next());
}
}
收率:
total 12
drwxrwxr-x 3 jsampson jsampson 4096 Jan 26 21:53 .
drwxrwxr-x 2 jsampson jsampson 4096 Jan 26 21:53 26
drwxrwxr-x 3 jsampson jsampson 4096 Jan 26 21:53 ..
total 12
drwxrwxr-x 3 jsampson jsampson 4096 Jan 26 21:53 .
drwxrwxrwx 2 jsampson jsampson 4096 Jan 26 21:53 26
drwxrwxr-x 3 jsampson jsampson 4096 Jan 26 21:53 ..
umask 只负责所有文件/目录创建权限。创建后,可以进行更改。