Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Creating a file with Open

by Newbie Ed (Initiate)
on Oct 03, 2000 at 11:27 UTC ( [id://35070]=perlquestion: print w/replies, xml ) Need Help??

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

I'm writing a CGI program in Perl. I want to create a file 'on the fly' using the users name so I can write stuff to it. Here's my code: open (UPLOAD, "+>$user_name" ) || print_error_page ("Can't open your submit directory."); flock(UPLOAD, $LOCK_EX) || print_error_page("Can't flock your submit directory."); My problem is that the open command isn't creating a new file for me. Is this a Unix problem? Thanks in advance for any help. Ed Kuehnel Chicago

Replies are listed 'Best First'.
Re: Creating a file with Open
by Jouke (Curate) on Oct 03, 2000 at 11:32 UTC
    brrr...next time try to format the code like this:
    open (UPLOAD, "+>$user_name" ) || print_error_page ("Can't open your s +ubmit directory."); flock(UPLOAD, $LOCK_EX) || print_error_page("Can't flock your submit d +irectory.");
    It's possible that the permissions on the directory you are writing in are not sufficient. It's a good idea to include $! (the system-errormessage) in the string you're printing with print_error_page(). In that case you can tell why the file is not created. (it should be possible to do this...)

    HTH

    Jouke Visser, Perl 'Adept'
(jcwren) Re: Creating a file with Open
by jcwren (Prior) on Oct 03, 2000 at 15:28 UTC
    Most likely it's a problem with file permissions. The webserver typically runs with a userid of 'nobody', and is not permitted to write most places. If you're trying to write a file to the user's home directory, for example, it's highly unlikely (and would be highly improper) if the webserver were allowed to write there.

    Often, people create a directory off of /home/httpd/, and give 'nobody' special permission to write there.

    --Chris

    e-mail jcwren
RE (tilly) 1: Creating a file with Open
by tilly (Archbishop) on Oct 03, 2000 at 16:04 UTC
    Everyone has already said to include $! in the error message. This is important but it is only a start.

    You should also do as perlstyle says and include $user_name.

    And personally I would begin sweating at trusting user input for filenames. Have you verified that the name looks like what you think it should? See perlsec for more on that issue.

    And btw that code for flocking will not give you the protection you want, open it "+<$user_name" or else it gets wiped out before you try to lock it.

      And btw that code for flocking will not give you the protection you want, open it "+<$user_name" or else it gets wiped out before you try to lock it.
      I'm confused here. Everyone else has been saying "+>$user_name" which makes sense to me. Is your suggestion a typo, or am I missing something fundamental here?

      Guildenstern
      Negaterd character class uber alles!
        My point is not a typo.

        Both "+<$user_name" and "+>$user_name" open the file for both reading and writing, but the first opens it for reading, and you can also write, while the second opens it for writing, but you can also read. Therefore the second wipes out the possibly existing file for you, while the first does not.

        Wiping out the file before you lock it and ensure it is not in use is unlikely to be desired. I call that, ERACE. (Sorry, didn't even try to hold that one back. :-)

        If this confuses you, just use sentinel files as in Simple Locking since I already dealt with all of this there.

        EDIT
        Somehow I said "suggestion" where I meant "typo". Fixed.

        EDIT 2
        Some days I should not post. Thanks isotope for pointing out I meant "while the first does not". *sigh*

        I'm confused here. Everyone else has been saying "+>$user_name" which makes sense to me. Is your suggestion a typo, or am I missing something fundamental here?
        No, not a typo. If you open something with +>, you are saying "zero out this file" before anything else happens. At that point, if someone else has the flock already, you are now erasing their work. You must open in a way that preserves any existing data, but if you're also going to write to it, you must use two-way mode, like +< or +>>.

        -- Randal L. Schwartz, Perl hacker

        Opening the file "+>" will wipe it out, so flocking it is then pretty pointless - you destroy the file before you've gained the lock on it.

        You need to open the file "+<", if that failed then open it "+>" then flock it then truncate it if you want to be safe with your flocking in the presence of other readers.

        Note that if you want to read the file back in with flocking you need to use "+<" so that you open the file with write intent otherwise the flocking may not work properly (I don't understand the reasons for this but that is what it says in the man pages!)

Re: Creating a file with Open
by c-era (Curate) on Oct 03, 2000 at 15:32 UTC
    If you want to get the error change your code to this:
    open (UPLOAD, "+>$user_name" ) || print_error_page ("Can't open your s +ubmit directory.<br>$!"); flock(UPLOAD, $LOCK_EX) || print_error_page("Can't flock your submit d +irectory.<br>$!");
    The $! will print out the error for you.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://35070]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found