Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've been using sysopen with 0666 to get -rw-rw-rw- and found that the file ended up as 0644...turns out that the default umask was 0222 and that was combing with my 0666 to get 0644...we ended up defining umask as 0000 just before the sysopen and the file privileges ended up as the desired 0666...is there a better way of doing this?
# Set the umask to 0's so if we have to create the file, we do it w/0 +666 mask. 323 # NOTE: The default umask for users is 0022, so creating a + file will mask it with 0666 &* 0022 = 0644 324 umask 0000; 325 sysopen( FH, $log_file_name, O_WRONLY | O_CREAT | O_APPEND +, 0666 ) 326 or die "\n\tcan't open $log_file_name....$!\n";

Replies are listed 'Best First'.
Re: umask with 0666
by almut (Canon) on Aug 04, 2010 at 12:14 UTC
    is there a better way of doing this?

    You might store the current/previous umask (as returned by umask), and set it back when done... (in case you're doing other things later in the same proccess where you want the default umask to apply).

    (BTW, your default umask likely was 0022 (not 0222), otherwise you'd have gotten 0444.)

      thanks...yes 0222 was a typo
Re: umask with 0666
by ikegami (Patriarch) on Aug 04, 2010 at 15:55 UTC
    Instead of
    1. Get and change umask.
    2. Create file
    3. Reset umask.
    You could
    1. Create file
    2. Change permissions

    If you do so, there will be a short period where the file will have a more restrictive mask than desired. I don't know if that matters in your case.