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

I'm after a quick way to check the permissions of a file, and then make sure that all other files created by my program have the same permissions.
I know that I "do stuff" with the output of stat, but it seems like a lot of work for what I want to do.
For the record, perldoc -q permissions, perldoc -q mode and perldoc -q file have not been of any help here.

setantae@eidosnet.co.uk|setantae|www.setantae.uklinux.net

Replies are listed 'Best First'.
Re: Mode of created files
by plaid (Chaplain) on Mar 28, 2000 at 03:18 UTC
    There are several options here. Pretty much any way you go about it, you'll need to get the existing file's permissions from stat, mentioned above. Setting your umask to those permissions is one way to go about it, but a umask is only a mask applied to newly created files, not an absolute setting of permissions. To set permissions, you could create the file first, then use chmod. Or you could use the built-in POSIX module and its creat() and open() modules, in which case you'd be working with file descriptors instead of file handles. The best way I can see, though, is through something like:
    use POSIX; my $perms = (stat "file")[2]; sysopen(OUT, "newfile", O_CREAT | O_WRONLY, $perms) or die "$!\n"; print OUT "whatever\n"; close(OUT);
    This lets you create a file with given permissions, and work with filehandles. The O_CREAT is needed to create the file if it doesn't already exist, and the O_WRONLY is needed to write to the file in case it doesn't have write perms (don't worry, if the file doesn't have write perms, O_WRONLY won't affect that. It'll write to the file and leave the perms untouched).
Re: Mode of created files
by chromatic (Archbishop) on Mar 28, 2000 at 02:25 UTC
    Why not use umask? The only other built-in way I know of that might be of use is perldoc -f -X, but I'm not sure that's what you want. You might look at Stat::lsMode from CPAN if you would rather deal with permissions as -rwxr-xr-x (as ls -l would show you).
      I'm not sure that I do want umask:
       To clarify, the mode I need the files created with is unknown.
      What I'm trying to do is to check the mode of a file that already exists, and then create other files that have the same mode (i.e., I don't know what mode the files need to be created with yet).
      So my logic would possibly go something like:
      1) get mode of file1; 2) create files file2, file3,..., fileN; 3) chmod files file2,..., fileN to the same mode as file1.
      If that makes any more sense...
        You could do that, sure... in that case you just need to use stat to get the mode of the file.
        my $mode = (stat "foo")[2];
        But I think you could use umask, still... and it may make it easier. Try something like this:
        my $mode = (stat "foo")[2]; my $mask = 777 - join '', (($mode&0700)>>6, ($mode&0070)>>3, ($mode&0007)); umask oct $mask;
        I'm not sure if this is absolutely right (please someone correct me if it isn't), but it seemed to work for the test cases I tried.
Re: Mode of created files
by btrott (Parson) on Mar 28, 2000 at 02:33 UTC
    You want umask.

    To *get* the permissions of a file, I assume you'd want to use stat, yes, or File::stat, which gives a nice by-name interface to the stat fields. So, for example,

    use File::stat; my $st = stat "foo"; print "file is executable" if $st->mode & 0111;
    I'm not quite sure how you'd go about turning file permissions into a file creation mask (as used by umask), other than a big if/then block.