简体   繁体   English

在Java中创建文件时如何设置文件所有者/组

[英]How to set file owner/group when creating a file in Java

I would like to set a (unix) owner and group of a file created from Java.我想设置从 Java 创建的文件的(unix)所有者和组。 I would like something like this :我想类似这样

Path file = ...;
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createFile(file, attr);

-- it is an example how to set permissions, but I cannot find how to do the same with owner/group. -- 这是如何设置权限的示例,但我找不到如何对所有者/组执行相同操作。

Please note that I'm not interested in changing the owner after the file is created (this has already been answered on SO [1] [2] ), but when the file is created.请注意,我对在创建文件更改所有者不感兴趣(这已经在 SO [1] [2]上得到了回答),但是创建文件

Motivation for this question is that I need to make sure that the file I'm creating is not modified by other users while I set proper owner and permissions.这个问题的动机是我需要确保我创建的文件在我设置正确的所有者和权限时不被其他用户修改。

Setting ownership on file creation does not seem to be possible.似乎不可能在文件创建时设置所有权。 When you look at the documentation of open() system call , it describes how to set file permissions, but the only mention of owner is:当您查看open()系统调用的文档时,它描述了如何设置文件权限,但唯一提到的所有者是:

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.文件的所有者(用户 ID)设置为进程的有效用户 ID。 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组所有权(组 ID)设置为进程的有效组 ID 或父目录的组 ID

See also this answer .另请参阅此答案

The solution I went for in the end was this:我最终寻求的解决方案是这样的:

  1. Create the file with default owner but restrictive permissions 000 :创建具有默认所有者但限制性权限000的文件:

     Path file = ...; Set<PosixFilePermission> perms = Collections.<PosixFilePermissions>emptySet(); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); Files.createFile(file, attr);
  2. Change the owner/group to the target user将所有者/组更改为目标用户

  3. The target user then sets permissions to what it needs.目标用户然后根据需要设置权限。

This should ensure that no other user can modify the file at any point in time.这应该确保没有其他用户可以在任何时间点修改该文件。

The Oracle-Documentation describes how to set and get posix conform owner. Oracle 文档描述了如何设置和获取符合 posix 的所有者。

Path path = ...
 UserPrincipalLookupService lookupService =
     provider(path).getUserPrincipalLookupService();
 UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
 Files.setOwner(path, joe);

Function Prototype looks like this:函数原型看起来像这样:

public static Path setOwner(Path path,
        UserPrincipal owner)
                 throws IOException

Parameters :参数

  • path - A file reference that locates the file path - 定位文件的文件引用
  • owner - The new file owner owner - 新的文件所有者

The group is indeed not mentioned in the Docs:文档中确实没有提到该组:

Retrieve the group owner of a file检索文件的组所有者

File originalFile = new File("original.jpg"); // just as an example
GroupPrincipal group = Files.readAttributes(originalFile.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();

Set the group owner of a file设置文件的组所有者

File targetFile = new File("target.jpg");
Files.getFileAttributeView(targetFile.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM