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

Hi monks,

Could you please help me for my below question.
Is it possible to open a file for writing purpose with the full access permission (777) using open statement.
eq: -rwxrwxrwx 1 xxx abcd 1102777 Nov 14 01:42
thanks,
Perl User.

Replies are listed 'Best First'.
Re: Creating file with 777 permission
by johngg (Canon) on Nov 21, 2006 at 10:42 UTC
    Like Smaug, I am not sure why you would want to do this. Trying to do it via a single sysopen is going to be affected by the umask as reasonablekeith points out but you could do it within your script like this

    use strict; use warnings; my $newFile = q{spw585219.txt}; my $newFH; open $newFH, q{>}, $newFile or die qq{open: $newFile: $!\n}; close $newFH or die qq{close: $newFile: $!\n}; my $ls = `/usr/bin/ls -l $newFile`; print $ls; chmod 0777, $newFile or die qq{chmod: $newFile: $!\n}; $ls = `/usr/bin/ls -l $newFile`; print $ls; open $newFH, q{>}, $newFile or die qq{open: $newFile: $!\n}; print $newFH qq{The quick brown fox etc\n}; close $newFH or die qq{close: $newFile: $!\n}; $ls = `/usr/bin/ls -l $newFile`; print $ls;

    and here is the output showing the ls -l results

    -rw-r--r-- 1 johngg staff 0 Nov 21 10:29 spw585219.txt -rwxrwxrwx 1 johngg staff 0 Nov 21 10:29 spw585219.txt -rwxrwxrwx 1 johngg staff 24 Nov 21 10:29 spw585219.txt

    I hope this is of use.

    Cheers,

    JohnGG

Re: Creating file with 777 permission
by Smaug (Pilgrim) on Nov 21, 2006 at 09:25 UTC
    Hi,

    Can you give a little more information? I'm not sure why you want world to be able to execute or write to a file that is still being written?

    Thanks,
    Smaug.
    Update:
    Try:
    sysopen(FILEHANDLE, $filename, permissions, CHMOD);

    Just another update:
    Based on reasonablekeith comments I decided to try some tests. I ran the following code:
    #!/usr/bin/perl use strict; use Fcntl; my $var = 1; while ($var) { sysopen (FILE, '/home/smaug/testing/file.txt', O_RDWR|O_EXCL|O_CRE +AT, 0777); printf FILE "A value of n\n"; } close (FILE);


    This, of course, opens the file and keeps it open for reading, writing and executing...... or so it would seem. However an "ls -l" shows the following:
    -rwxr-xr-x 1 root root 13 2006-11-21 11:55 file.txt

    In short, sysopen calls the underlying operating system's open function. On my OS (Ubuntu Linux) the OS prevents writing to files that are already open for writing, so it seems that although it is still executable (why oh why!!) it is not writable.
    (The same is shown by trying to open the same file twice using "vi".)
    Again, looks can be deceiving... Trying to run the file while it is written shows:
    bash: ./file.txt: Text file busy
    Perhaps there are other OS's out there that allow this, but for now - I'm going with the "Cannot be done" answer.
    Thanks for pricking my curiosity reasonablekeith.
      That's still going to be affected by umask, and is unlikely to reliably give 777 as the permission mode of the file.

      You're probably best off creating the file first with your specific requirements, then opening it.

      ---
      my name's not Keith, and I'm not reasonable.
        Agreed, but you're also unlikely to want a file that is open for writing to be executed.

      On my OS (Ubuntu Linux) the OS prevents writing to files that are already open for writing,

      That's a very odd Unix. One I've never encountered before.

      Now, there are some OSses that won't allow you to write to pages binaries currently being executed by some process (giving you a 'text busy' message), but that's something else entirely.

      (The same is shown by trying to open the same file twice using "vi".)

      Ah, yes, that editor. The one that tries to be vi but isn't. I'm a vi-lover and have been using vi and vi-clones for 25+ years. But I loathe environments where 'vi file' starts up an editor that isn't quite 'vi'. By all means, if you call it 'vi', then have it act like 'vi'. If it acts otherwise, call it otherwise.

      As for that editor, that editor creates another file in the same directory, and if that file exists, it thinks the file is already edited and starts complaining. That has nothing to do with the OS - that's just a behavioural thing of the editor that isn't quite vi. Using another program, I can happily modify the same file.

        Thanks for that, although it really did not help me.
        Thanks johngg.
Re: Creating file with 777 permission
by calin (Deacon) on Nov 21, 2006 at 12:19 UTC
    #!/usr/bin/perl use strict; use warnings; use Fcntl; my $FILE = 'foobarbaz'; umask 0; -e $FILE && unlink $FILE; sysopen my $fh, $FILE, O_CREAT, 0777 or die "$!";

    I don't think it's possible to do this with open in an atomic manner (please correct me if I'm wrong). open has a built-in mask of 0666. Just open the file normally and chmod it immediately afterwards.

      Thanks Monks, Your suggestions are very usefull for me. thanks, Perl User.
Re: Creating file with 777 permission
by Anonymous Monk on Nov 21, 2006 at 10:53 UTC
    Yes, assuming the file doesn't already exists. First set the umask to 000, then use 'sysopen' to open the file, with '0777' as the fourth argument.

    I'd never do that myself. If I really want to create a file with 0777 permissions, I'd use chmod() after finishing writing the file.