On Tue, Jul 02, 2002 at 11:46:26AM -0400, James Baster wrote:
> I want to open a temporary file somwhere and I am facing the problem of 
> making sure I dont write to an already existing temporary file, thus 
> curropting some other process somewhere else.

Securely opening temporary files can be tricky.  If IO::File->new_tmpfile
isn't good enough, perhaps because you need an actual filename to move
around, then you should use the File::Temp module.  It should have been
included with your Perl distribution, but if not you should be able to
obtain it from CPAN.

You should look into information on secure programming practices.  Some good
sources of information are listed below.  I'm assuming you're on a Unix or
Unix-like system here; if you aren't then the information on such systems
will, obviously, be a little less useful.

    perldoc perlsec
    http://www.dwheeler.com/secure-programs/
    http://www.whitefang.com/sup/secure-faq.html
    http://www.shmoo.com/securecode/


> Is there a way of telling perl to open a new file for writing but to throw
> an error if the file already exists?

This is fairly easy, and a good way to start learning about finer-grained
control of opening files.

You can accomplish what you want with sysopen.  For example:

    use Fcntl qw(O_WRONLY O_EXCL O_CREAT);
    sysopen(FILE, $filename, O_WRONLY|O_EXCL|O_CREAT) || die ...

The O_WRONLY opens the file write-only, O_CREAT specifies it should be
created if it doesn't exist, and the O_EXCL in combination with the O_CREAT
specifies the file must not exist.  The documents I gave you above mention
this, and your man 2 open should as well (assuming you're on a Unix system).


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to