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

Hello All,

Something that really puzzles me. I got a neat script here from a fellow monk "nothingmuch" that works great to write a new line in another cgi script. The script that gets written to is originally chmod (ed) rwx-r x-r x, (755). However - once the script gets written to the chmod has become rw -r -r , that is, all the "executes" have been removed from the permissions and the original script will not run again till I go back to my host server and change it back manually. The code is below. Anyone know how to fix this?? Thanks so much for looking.....

#!/usr/local/bin/perl use CGI ':standard'; print "Content-type: text/html\n\n"; open (FILE,"writeline.cgi"); open (TEMP,">external_file.txt.tmp"); my $i = 0; while (<FILE>) { if ($i == 13){ $_ = "\$pageFile=\"webpage.html\";\n" if ($i == 13); } print TEMP $_; } continue { $i++; } close FILE; close TEMP; unlink ("write.cgi"); rename ("external_file.txt.tmp","writeline.cgi");

edited: Wed Oct 16 15:54:58 2002 by jeffa - code tags s/<br>//g

Replies are listed 'Best First'.
Re: chmod changes on server when script runs
by defyance (Curate) on Oct 15, 2002 at 20:13 UTC
    Look like what the script is rewriting the file with values saved in the .tmp file. When this is done, the server your running on will default permissions to 644. Might I suggest checking out chmod, or perldoc -f chmod for info on how to change the permissions after the file is re-written.

    Also, try wrapping your code in <code> tags next time you post. ;-)

    -- Can't never could do anything, so give me and inch, I'll make it a mile.

Re: chmod changes on server when script runs
by bnanaboy (Beadle) on Oct 15, 2002 at 20:17 UTC
    Looks like the original file is opened and read line by line into a temporary file, inserting the line that needs to be added. After all lines are read from the original file to the temp file, both files are closed and the temp file is renamed to the original filename. So the original file isn't having its permissions changed, it's being overwritten by a new file with the default permissions. Add a line at the bottom of the script after the rename:

    chmod 0755, 'writeline.cgi';

Re: chmod changes on server when script runs
by nothingmuch (Priest) on Oct 15, 2002 at 20:39 UTC
    Another approach, perhaps more 'correct' in the views of some, would be to do the following
    use Fcntl qw(O_RDWR O_CREAT); sysopen(TEMP,"external_file.txt.tmp",O_RDWR|O_CREAT,755);
    insted of the normal open.
    This has the effect of precreating the file with the correct permissions.

    You may also want to check the umask function in perlfunc aswell.

    Also of some importance, especially I expect, is the flock function, which you may also w ant to read about.

    -nuffin
    zz zZ Z Z #!perl
      Thanks once more for your words of wisdom. I am learning by great gobs and leaps! Great day when I found the Monastery!
Re: chmod changes on server when script runs
by thor (Priest) on Oct 16, 2002 at 13:35 UTC
    If you are writing to a new file, umask comes into play. You have a set of default permissions for new files, and the umask command manages that. Do a man umask on your system for details.

    thor