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

A cgi script writes a file to the server. Because it's a perl file with a .pl extension, i'm wondering if anyone knows how to write the file and set permissions all at once? -Lisa

Replies are listed 'Best First'.
Re: set permissions automatically
by Chady (Priest) on Nov 16, 2002 at 09:26 UTC

    sysopen might be what you want:

    use Fcntl qw( O_CREAT O_RDWR ); sysopen(FILE, "$file", O_RDWR|O_CREAT, 0755) ...

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: set permissions automatically
by jdporter (Paladin) on Nov 16, 2002 at 15:35 UTC
    In addition to the above advice, you should learn about umask.

    umask determines what mode bits will be set on a newly created file by default. What happens is, the systems starts with bits 0777 (full permissions), and subtracts the bits that are set in your umask. The result is used as the default mode for new files.

    So, basically, if you want to have all the execute bits set, make sure the corresponding bits are unset (0) in your umask.

    For example, if you want a mode of 0751 by default, then set your umask to 0026.

    jdporter
    ...porque es dificil estar guapo y blanco.

Re: set permissions automatically
by BrowserUk (Patriarch) on Nov 16, 2002 at 09:37 UTC

    In addition to looking at the sysopen docs as indicated above, you might also find it useful to read perlopentut section "Open à la C" and particularly the next section "Permissions à la mode".


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: set permissions automatically
by sauoq (Abbot) on Nov 16, 2002 at 19:46 UTC

    I'm not sure what you mean by "all at once." As others have pointed out, you can use sysopen() but you have to concern yourself with the process umask when you do. It's probably better just to open the file, write it, close it, and then set the permissions with Perl's chmod() function.

    -sauoq
    "My two cents aren't worth a dime.";
    
      yea.. that's what i'm looking for.. guess i was tired. :)-Lisa
Re: set permissions automatically
by Dr. Mu (Hermit) on Nov 17, 2002 at 04:43 UTC
    Okay, wolverina, now that you know the how, it's time to deal with the whether. Please consider the security implications of letting someone upload a file and, just because it has a .pl extension, automatically making it executable. Who gets to upload such a file? Where does it get written? Who gets to execute the code once uploaded? What if the code is malicious? It seems like such a small thing, but setting permissions in such a way can open a Pandora's box of tribulation. Please be careful.