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

Hi All I'm creating a temp file and after checking it whether it is of valid size, i'm renaming/moving it to the actual place. Now i have to set the permissions to "umask 022" before moving the file. I can do this with chmod (after creating the file)but it has to be set using umask now. Pls let me know how should i use 'umask'??? Thanks Max

Replies are listed 'Best First'.
Re: umask settings in perl
by Fletch (Bishop) on Oct 20, 2005 at 12:34 UTC

    And what from perldoc -f umask is giving you problems? I mean we could look at the sample code you . . . oh, well . . . at least judging by the error message you poste. . . erm . . .

Re: umask settings in perl
by jfroebe (Parson) on Oct 20, 2005 at 13:44 UTC
    A quick explanation of umask (php, but it still applies)
    Jill Ramonsky Apr 2 2003
    Subject: RE: PHP-WIN how to use umask() ???

    The PHP manual (unfortunately) assumes some Unix knowledge here.

    The paramter to umask is a bitfield, passed as an integer. Within this bitfield:

    • bit 0 set implies that anyone in the world can execute the file
    • bit 1 set implies that anyone in the world can write to the file
    • bit 2 set implies that anyone in the world can read the file
    • bit 3 set implies that anyone in the file's group (a Unix concept) can execute the file
    • bit 4 set implies that anyone in the file's group (a Unix concept) can write to the file
    • bit 5 set implies that anyone in the file's group (a Unix concept) can read the file
    • bit 6 set implies that the owner of the file can execute the file
    • bit 7 set implies that the owner of the file can write to the file
    • bit 8 set implies that the owner of the file can read the file

    When accessing a file, the lower 9 bits of the permissions of the file (as stored on the server) are logically ANDed with the complement of the current "umask" - which you can set with the umask() function - and the resulting bitfield is inspected to see whether or not to grant permission for the specified action.

    Jill

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: umask settings in perl
by Moron (Curate) on Oct 20, 2005 at 13:42 UTC
    umask gets or sets the default file permissions. With a parameter it sets the default permissions to the binary complement of the mask (octal). This is simply 777 - x where x is the argument to umask, although be aware that umask can accept four octal digits to play the same game with the higher order flags as well. So in the example of the OP, umask 022 would set the default permissions to 755. After that a mv will then by default use the permissions implied by the umask that is in effect which in the example would be 755.

    -M

    Free your mind