在 Java 中创建文件时如何设置文件 owner/group
How to set file owner/group when creating a file in Java
我想设置从 Java 创建的文件的 (unix) 所有者和组。我想要 this:
Path file = ...;
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file, attr);
-- 这是一个如何设置权限的示例,但我找不到如何对 owner/group 执行相同的操作。
请注意,我对在 文件创建后 更改所有者不感兴趣(这已在 SO [1] [2] 上得到回答),但是 当 文件创建时。
这个问题的动机是我需要确保我正在创建的文件在我设置适当的所有者和权限时没有被其他用户修改。
Oracle-Documentation 描述了如何设置和获取 posix 符合所有者。
Path path = ...
UserPrincipalLookupService lookupService =
provider(path).getUserPrincipalLookupService();
UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
Files.setOwner(path, joe);
函数原型如下所示:
public static Path setOwner(Path path,
UserPrincipal owner)
throws IOException
参数:
path
- 定位文件的文件引用
owner
- 新的文件所有者
文档中确实没有提到该组:
检索文件的组所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
设置文件的组所有者
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
似乎无法在创建文件时设置所有权。当你查看open()
system call的文档时,它描述了如何设置文件权限,但唯一提到的所有者是:
If the file does not exist it will be created. The owner (user ID) of the file is set to the effective user ID of the process. The group ownership (group ID) is set either to the effective group ID of the process or to the group ID of the parent directory
另见 this answer。
我最后的解决方案是这样的:
创建具有默认所有者但权限受限的文件000
:
Path file = ...;
Set<PosixFilePermission> perms = Collections.<PosixFilePermissions>emptySet();
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file, attr);
将owner/group更改为目标用户
- 然后目标用户根据需要设置权限。
这应该确保任何其他用户都不能在任何时间点修改该文件。
我想设置从 Java 创建的文件的 (unix) 所有者和组。我想要 this:
Path file = ...;
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file, attr);
-- 这是一个如何设置权限的示例,但我找不到如何对 owner/group 执行相同的操作。
请注意,我对在 文件创建后 更改所有者不感兴趣(这已在 SO [1] [2] 上得到回答),但是 当 文件创建时。
这个问题的动机是我需要确保我正在创建的文件在我设置适当的所有者和权限时没有被其他用户修改。
Oracle-Documentation 描述了如何设置和获取 posix 符合所有者。
Path path = ...
UserPrincipalLookupService lookupService =
provider(path).getUserPrincipalLookupService();
UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
Files.setOwner(path, joe);
函数原型如下所示:
public static Path setOwner(Path path,
UserPrincipal owner)
throws IOException
参数:
path
- 定位文件的文件引用owner
- 新的文件所有者
文档中确实没有提到该组:
检索文件的组所有者
File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
设置文件的组所有者
File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);
似乎无法在创建文件时设置所有权。当你查看open()
system call的文档时,它描述了如何设置文件权限,但唯一提到的所有者是:
If the file does not exist it will be created. The owner (user ID) of the file is set to the effective user ID of the process. The group ownership (group ID) is set either to the effective group ID of the process or to the group ID of the parent directory
另见 this answer。
我最后的解决方案是这样的:
创建具有默认所有者但权限受限的文件
000
:Path file = ...; Set<PosixFilePermission> perms = Collections.<PosixFilePermissions>emptySet(); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); Files.createFile(file, attr);
将owner/group更改为目标用户
- 然后目标用户根据需要设置权限。
这应该确保任何其他用户都不能在任何时间点修改该文件。