简体   繁体   English

创建文件时如何设置OTHERS_WRITE?

[英]How can I set OTHERS_WRITE when creating a file?

I am attempting to use createFile with a FileAttribute derived from "rw-rw-rw-" . 我试图将createFile与从"rw-rw-rw-"派生的FileAttribute一起使用。 My file is being created as "rw-rw-r--" on Fedora. 我的文件在Fedora上被创建为"rw-rw-r--"

How can I set OTHERS_WRITE when creating a file? 创建文件时如何设置OTHERS_WRITE?

Example: 例:

(->> "rw-rw-rw-"
     PosixFilePermissions/fromString
     PosixFilePermissions/asFileAttribute
     vector
     into-array
     (Files/createFile (Paths/get "temp" (into-array String []))))
;;; temp is created as rw-rw-r--

In unix like systems every process has a property called umask which is masked onto the permissions of any file created and inherited by child processes. 在类似Unix的系统中,每个进程都有一个称为umask的属性,该属性被屏蔽到子进程创建和继承的任何文件的权限上。 the default is 0002 or "turn off write for others". 默认值为0002或“关闭他人的写操作”。 So it's most likely that Java is setting the permission you seek and then it's being masked out . 因此, Java很可能会设置您寻求的权限,然后将其屏蔽掉 You can explicitly set the permissions after creating the file or change the umask settings of the java process before you start it. 您可以创建文件显式设置权限也可以启动更改java进程的umask设置。

First lets look at the current umask: 首先让我们看一下当前的umask:

arthur@a:/tmp$ umask
0002

then lets make a file with read and write for everyone (touch does this as well as your java code) 然后让每个人都可以读写的文件(touch和Java代码一样)

arthur@a:/tmp$ touch foo
arthur@a:/tmp$ ls -l foo
-rw-rw-r-- 1 arthur arthur 0 Aug 28 13:58 foo

we see that the octal 0002 has been masked out of the actual file permissions. 我们看到八进制的0002已被屏蔽掉实际的文件权限。
So we can remove this umask by setting it to 0000: 因此,我们可以通过将其设置为0000来删除该umask:

arthur@a:/tmp$ umask 0000
arthur@a:/tmp$ touch foo

And we see that foo remains as it was when updating because umasks only apply to new files. 而且我们看到foo仍保持更新时的状态,因为umask仅适用于文件。 and a new file bar is created with the read-other permission. 并创建一个具有其他读取权限的新文件栏。

arthur@a:/tmp$ ls -l foo
-rw-rw-r-- 1 arthur arthur 0 Aug 28 14:00 foo
arthur@a:/tmp$ touch bar
arthur@a:/tmp$ ls -l bar
-rw-rw-rw- 1 arthur arthur 0 Aug 28 14:00 bar

I'm in the habit of setting permissions explicitly after creating a file in Java because this carries more easily from system to system* You can prove this to your self by setting the umask in your shell before you run emacs/your-program and checking the file permissions after. 我习惯在Java中创建文件后明确设置权限,因为这在系统之间更容易实现*您可以通过在运行emacs / your-program并检查之前在shell中设置umask来证明这一点之后的文件权限。

*Java is "write once run anywhere" eh? * Java是“写一次即可在任何地方运行”是吗?

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

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