in reply to Setting permissions as text file is created

See also perldoc -f sysopen, or perldoc POSIX then search for open.

Replies are listed 'Best First'.
Re: Re: Setting permissions as text file is created
by Hissingsid (Sexton) on Mar 17, 2004 at 13:05 UTC
    Hi,

    So is this the correct syntax for sysopen to do the following. If filepath does not exist create a file with the name contained in the filepath with permissions 666, print some stuff to the file and then close it?

    sysopen (FILEHANDLE, ">>$filepath",O_CREAT, 0666)||&ErrorMessage;
    print FILEHANDLE "$pageoutput";
    close FILEHANDLE;

    Thanks

    Sid
      A couple of points:
      1) chmod and the sysopen* call above set the file mode not the permission
      2) A file mode of 0666 is generally bad as it is generally allows more access to the file than is needed (do you really want everyone to be able to read/change/delete your file?)
      3) If you need to modify owner or the group membership, use chown

      CC

      Updated: Ok, you can't actually delete the file unless you have write access to the directory whihch holds the file. You can, however, blank out the file. For example:

      > my.file
      will reduce the file to zero bytes without actually having write access to the directory. The file isn't deleted but it certainly isn't very useful now.

      * if your umask is sane (ie 022) then the sysopen (FILEHANDLE, ">>$filepath",O_CREAT, 0666) will not be inherently insecure.

        A file mode of 0666 is generally bad
        Perhaps, but a mode of 0666 on sysopen is generally good, because it gives the user of the program the most control. Remember that the mode given to sysopen is masked with the umask of the user. A mode of 0666 gives the user the option to say who can read/write the file, by using commonly used umasks of 022, 027 and 077. If you use a mode of 0644 or 0600, you rob the user of options. For executables, use a mode of 0777.

        Abigail

        Not to be terribly nitpicky but the file permissions (at least on the *nixes I've used) don't have any control over wether or not you can delete the file. You need write permissions on the directory the file is in to delete it. i.e.
        % ls -al total 12 4 drwxr-xr-x 2 notme notmygroup 4096 Mar 17 11:12 ./ 4 drwxr-xr-x 101 me mygroup 4096 Mar 17 11:12 ../ 4 -rw-rw-rw- 1 me mygroup 42 Mar 17 11:12 foo.foo % rm foo.foo rm: cannot remove `foo.foo': Permission denied % sudo chmod 777 . % rm foo.foo

      What happens when you try it?

      (And you probably should also read perldoc perlopentut which gives examples of correct use of sysopen().)