How can I set OTHERS_WRITE when creating a file?
Asked Answered
T

1

3

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.

How can I set OTHERS_WRITE when creating a file?

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--
Trivet answered 28/8, 2014 at 19:10 Comment(0)
S
4

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.

First lets look at the current 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)

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.
So we can remove this umask by setting it to 0000:

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. 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 is "write once run anywhere" eh?

Soilasoilage answered 28/8, 2014 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.