Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

open() and permissions not quite as documented?

by mpersico (Monk)
on May 27, 2016 at 18:29 UTC ( [id://1164347]=perlquestion: print w/replies, xml ) Need Help??

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

Is it a known behavior that if you 'open' a file for writing that does not already exist, umask settings are properly applied, yet if a file that exists is opened for (over)writing, it keeps the existing permissions, ignoring the umask settings? This drove me nuts for an hour this morning on a Linux box with Perl 5.16.
  • Comment on open() and permissions not quite as documented?

Replies are listed 'Best First'.
Re: open() and permissions not quite as documented?
by Marshall (Canon) on May 27, 2016 at 18:37 UTC
    That is exactly what is supposed to happen. umask applies only to creating new files. Opening an existing file for "re-write" is not the same as creating a new file. You would need to delete the existing file, then create a new one for the umask to apply.

    On Linux and other Unix-like operating systems, new files are created with a default set of permissions. Specifically, a new file's permissions may be restricted in a specific way by applying a permissions "mask" called the umask. The umask command is used to set this mask, or to show you its current value.

Re: open() and permissions not quite as documented?
by Discipulus (Canon) on May 28, 2016 at 13:16 UTC
    as side note, using perl you can also trap the occurence of this behaviour, if it is important in your application.

    sysopen MODE and combination of them, can be used in order to track the overwrite:

    O_WRONLY|O_CREAT # possible rewrite of an existing file O_WRONLY|O_CREAT|O_EXCL # only create new file (fails if file exists +)

    With sysopen you can also specify PERM in octal to be applied to the new created file. If not specified 0666 is the default. Then umask will be applied.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1164347]
Approved by Marshall
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2024-04-19 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found