gyepisam/redux

Please respect umask

Opened this issue · 1 comments

Problem

Newly created files are given permissions 0600.

Expected behaviour

Newly created files should have permissions (0666 & ~umask), so if umask is 0022, new files should have permissions 0644.

$ umask 0022
$ touch 0022.txt  # 0666 & ~0022 = 0644
$ umask 0027
$ touch 0027.txt  # 0666 & ~0027 = 0640
$ ls -l
total 0
-rw-r--r-- 1 kas kas 0 Nov  9 10:23 0022.txt
-rw-r----- 1 kas kas 0 Nov  9 10:23 0027.txt

✔️

Observed behaviour

$ umask
022
$ mkdir /tmp/redux-test && cd /tmp/redux-test
$ redo-init
$ echo 'redo-ifchange target' >all.do
$ cat <<EOF >target.do
redo-ifchange source
cat source
EOF
$ date >source
$ ls -la
total 12
drwxr-xr-x  3 kas  kas  120 Nov  9 10:11 .
drwxrwxrwt 18 root root 460 Nov  9 10:12 ..
drwxr-xr-x  2 kas  kas   40 Nov  9 10:10 .redo
-rw-r--r--  1 kas  kas   21 Nov  9 10:10 all.do
-rw-r--r--  1 kas  kas   29 Nov  9 10:11 source
-rw-r--r--  1 kas  kas   32 Nov  9 10:11 target.do
$ # so far, so good
$ redo -v
all (all.do)
 all => target (target.do)
$ ls -la
total 16
drwxr-xr-x  3 kas  kas  140 Nov  9 10:13 .
drwxrwxrwt 18 root root 480 Nov  9 10:14 ..
drwxr-xr-x  4 kas  kas   80 Nov  9 10:13 .redo
-rw-r--r--  1 kas  kas   21 Nov  9 10:10 all.do
-rw-r--r--  1 kas  kas   29 Nov  9 10:11 source
-rw-------  1 kas  kas   29 Nov  9 10:13 target
-rw-r--r--  1 kas  kas   32 Nov  9 10:11 target.do

The .redo directory and the source and *.do files all get the permissions I expect from an umask of 0022. The target file, however, gets the unexpected permissions 0600.

I do not speak golang, but I suspect that tempfile creation is the reason for the erratic behaviour. In Linux glibc the mkstemp() function creates new files with permission 0600:

       The file is created with permissions 0600, that is, read plus write for
       owner  only.  The returned file descriptor provides both read and write
       access to the file.  The file is opened with the open(2)  O_EXCL  flag,
       guaranteeing that the caller is the process that creates the file.

Redo ought to make sure that newly created files get the permissions expected from the user's umask value: (0666 & ~umask) for files (and (0777 & ~umask) for directories).

Disclaimer

Please disregard my do files. I recently learned about redo/redux and I am currently assessing which implementation to use. The do files above is my first attempt at writing do files.

Addendum

This seems to happen only if the creator writes to standard output. If the creator writes to $3, umask is respected.