Forum Moderators: coopster & phranque

Message Too Old, No Replies

Setting permissions as file is created

         

Hissingsid

10:31 am on Mar 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I have a script which writes text files like this.

open (FILEHANDLE, ">>$filepath")¦¦&ErrorMessage;
print FILEHANDLE "$pageoutput";
close FILEHANDLE;

This prints a string that I've stuffed into $pageoutput.

What I need to know is how can I set the permissions I need at the same time.

Can anyone help?

Best wishes

Sid

tombola

12:46 pm on Mar 17, 2004 (gmt 0)

10+ Year Member



After close FILEHANDLE:

`chmod XXX $filepath`;

Replace XXX with the permissions you'd like to set (for example: 755).

Hissingsid

1:51 pm on Mar 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Tombola!

I'll try that.

best wishes

Sid

SeanW

1:57 pm on Mar 17, 2004 (gmt 0)

10+ Year Member



Gaak... If you're going to spawn a subshell for every mudane task, you may as well write it in shell ;)

Perl has a chmod function:

chmod 0755, @files;

There are some caveats, ie the mode should be an octal number (use that leading zero), and not a string:

[rocketaware.com...]

Sean

Hissingsid

5:08 pm on Mar 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I asked the same question on Perlmonks and got this suggestion which is what I think I will use.

my $umask = umask 0;
open (FILEHANDLE, ">>$filepath")¦¦&ErrorMessage;
umask $umask;
print FILEHANDLE "$pageoutput";
close FILEHANDLE;

I can then adjust permissions by simply changing the value of $umask.

The alternative is apparently to use sysopen which has switches for various attributes including what to do if the file can't be found and permissions.

Best wishes

Sid

SeanW

5:18 pm on Mar 17, 2004 (gmt 0)

10+ Year Member



Hi, Sid

That's an even more elegant way of doing it... I'll have to remember that for the future.

Just remember that you're dealing with a umask, not a file permission. umask = 777 - permission (in octal)

Sean

Hissingsid

5:45 pm on Mar 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

There's a good explanation of how to use the mask here. [perldoc.com ]

In effect the umask numbers are simply subtracted from the existing permissions. I think that i may get into the habit of using sysopen to create my files with a creation mode of 0666 and then use the mask to tweak these to suit my requirements.

I must confess that until recently non of my CGIs created files on the server they just sent HTML to the browser and emails to me. Reading and writing files opens up a whole new world of possibilities (and dangers!). As a consequence of not creating files in the past and having access to some excellent GUI FTP clients and permissions utilities I had never had to know that RWX = 7 I just selected the relevant check boxes and submitted.

Thanks for everyones help on this.

Best wishes

Sid