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

查看:115
本文介绍了创建文件时如何设置OTHERS_WRITE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 createFile ,其FileAttribute从 rw-rw-rw- 。我的文件在Fedora上被创建为 rw-rw-r-

I am attempting to use createFile with a FileAttribute derived from "rw-rw-rw-". My file is being created as "rw-rw-r--" on Fedora.

如何设置创建文件时是否使用OTHERS_WRITE?

How can I set OTHERS_WRITE when creating a file?

示例:

(->> "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--


推荐答案

在类似Unix的系统中,每个进程都有一个名为 umask 的属性。被子进程创建和继承的任何文件的权限所屏蔽。默认值为 0002 或关闭他人的写入。因此,很可能 Java设置了您寻求的权限,然后被屏蔽了。您可以在之后设置显式权限创建文件或在启动之前 更改Java进程的umask设置。

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. the default is 0002 or "turn off write for others". So it's most likely that Java is setting the permission you seek and then it's being masked out. You can explicitly set the permissions after creating the file or change the umask settings of the java process before you start it.

首先让我们看一下当前的umask:

First lets look at the current umask:

arthur@a:/tmp$ umask
0002

然后让每个人都可以读写文件(触摸会执行此操作以及您的Java代码)

then lets make a file with read and write for everyone (touch does this as well as your java code)

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

我们看到八进制 0002 已被屏蔽掉实际的文件权限。

因此我们可以删除将此umask设置为0000:

we see that the octal 0002 has been masked out of the actual file permissions.
So we can remove this umask by setting it to 0000:

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

我们看到foo仍保持更新时的状态,因为umask仅适用于 new 文件。

And we see that foo remains as it was when updating because umasks only apply to new files. 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这是习惯在Java中创建文件后显式设置权限,因为这在系统之间更容易实现*您可以通过在运行emacs / your-program之前在外壳程序中设置umask并检查文件权限之后。

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是写一次即可在任何地方运行是吗?

*Java is "write once run anywhere" eh?

这篇关于创建文件时如何设置OTHERS_WRITE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆