>From 82c6de2211a590b9652262d35642b11fa43f2cb9 Mon Sep 17 00:00:00 2001 From: Evan Hanson Date: Wed, 13 May 2020 17:50:39 +1200 Subject: [PATCH] Use 0666 as default mode for `file-open' There doesn't seem to be a default mode for the open(2) system call specified anywhere, so let's make `file-open' behave like fopen(3). This way, the permissions of files created using the posix unit will match those created with the normal `open-output-file' procedure, and such files will no longer be executable by default. This closes #1698. --- NEWS | 3 +++ posixunix.scm | 2 +- posixwin.scm | 2 +- tests/posix-tests.scm | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 67b81974..f10c242e 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,9 @@ On UNIX, the values returned will be precise to the millisecond instead of rounded down to the nearest second at startup (which would result in erratic startup times). + - The default mode for files created by `file-open' has been changed + to 0666 (plus whatever change the effective umask applies), rather + than 0744 (see #1698). - Runtime system - Sleeping primordial thread doesn't forget mutations made to diff --git a/posixunix.scm b/posixunix.scm index 23ff157e..1bddc429 100644 --- a/posixunix.scm +++ b/posixunix.scm @@ -335,7 +335,7 @@ static int set_file_mtime(char *filename, C_word atime, C_word mtime) res ) ) ) ) ) (set! chicken.file.posix#file-open - (let ((defmode (bitwise-ior _s_irwxu (bitwise-ior _s_irgrp _s_iroth))) ) + (let ((defmode (bitwise-ior _s_irusr _s_iwusr _s_irgrp _s_iwgrp _s_iroth _s_iwoth))) (lambda (filename flags . mode) (let ([mode (if (pair? mode) (car mode) defmode)]) (##sys#check-string filename 'file-open) diff --git a/posixwin.scm b/posixwin.scm index c279c58e..cb31a07b 100644 --- a/posixwin.scm +++ b/posixwin.scm @@ -517,7 +517,7 @@ static int set_file_mtime(char *filename, C_word atime, C_word mtime) (set! chicken.file.posix#open/noinherit _o_noinherit) (set! chicken.file.posix#file-open - (let ((defmode (bitwise-ior _s_irwxu (fxior _s_irgrp _s_iroth)))) + (let ((defmode (bitwise-ior _s_irusr _s_iwusr _s_irgrp _s_iwgrp _s_iroth _s_iwoth))) (lambda (filename flags . mode) (let ([mode (if (pair? mode) (car mode) defmode)]) (##sys#check-string filename 'file-open) diff --git a/tests/posix-tests.scm b/tests/posix-tests.scm index 807730e6..361f55c1 100644 --- a/tests/posix-tests.scm +++ b/tests/posix-tests.scm @@ -87,3 +87,43 @@ (assert (equal? (get-environment-variable "FOO") "bar")) (unset-environment-variable! "FOO") (assert (not (get-environment-variable "FOO"))) + +;; file creation and umask interaction +(letrec-syntax ((test (syntax-rules () + ((test umask expected) + (test umask "expected" expected "given")) + ((test umask given expected) + (test umask "expected" expected "given" given)) + ((test umask "expected" expected "given" given ...) + (let ((mode (file-creation-mode))) + (set! (file-creation-mode) umask) + (delete-file* "posix-tests.out") + (file-open "posix-tests.out" open/creat given ...) + (assert (equal? (file-permissions "posix-tests.out") expected)) + (set! (file-creation-mode) mode)))))) + ;; default file mode + (test #o000 #o666) + (test #o002 #o664) + (test #o020 #o646) + (test #o022 #o644) + (test #o027 #o640) + (test #o072 #o604) + (test #o077 #o600) + (test #o777 #o000) + ;; explicit file mode argument + (test #o000 #o644 #o644) + (test #o002 #o644 #o644) + (test #o020 #o644 #o644) + (test #o022 #o644 #o644) + (test #o027 #o644 #o640) + (test #o072 #o644 #o604) + (test #o077 #o644 #o600) + (test #o777 #o644 #o000) + (test #o000 #o777 #o777) + (test #o002 #o777 #o775) + (test #o020 #o777 #o757) + (test #o022 #o777 #o755) + (test #o027 #o777 #o750) + (test #o072 #o777 #o705) + (test #o077 #o777 #o700) + (test #o777 #o777 #o000)) -- 2.25.1