moderated >> Safe Temp Files

by Mock, George » Wed, 06 Dec 2006 07:06:00 GMT

From the Perl Cookbook (2nd Edition) recipe 7.11 I see that there
two main ways to create a temporary file. Use the File::Temp
module or pass undef as the filename to open(), like this ...

open($fh, "+>", undef) or die ...

Is there any reason to believe that one method is more safe and/or
reliable than the other?

Thanks!

-George

Similar Threads

1. can i change file content without using a temp file

Now i create a temp file to store changed content.
is there a simple way to change file content without creating temp
files?

ie:

while(<>)
{
  tr/[a-e]/[1-5]/g
  print TEMP, $_
}

2. Storing files in temp internet files, won't delete properly

3. File::Temp: opening the temp. file in "r+" mode? Also "man in the middle"

Hi,

I'm working at a CGI-script which would receive a 20 MB
big file via HTTP-upload, then change few bytes in it,
then calculate an MD5 hash over a region of that file
and save that value into the file as well.

At the moment I do the first task (change few bytes) like 
this (the $flash is coming from the CGI.pm's filefield):

  my ($fh, $filename) = tempfile(DIR => UPLOADDIR);

  while (sysread $flash, $chunk, KEYSIZE) {
          $both = $prev . $chunk;
          if ($both =~ s/$SEARCH/$REPLACE/o) {
                  print $fh $both;
                  undef $prev;
          } else {
                  print $fh $prev;
                  $prev = $chunk;
          }
  }
  # don't forget to print the last line
  print $fh $prev;

Now I need to calculate the MD5 value and store it
in the file. Is there a safe way to reuse the $fh ?
Maybe somehow by using:

  open(FILEHANDLE, "<&=$fh")

? But how do I specify the mode above? I need "rb+"
so that I can store the MD5 hash into the temp.file.

Also do I really need the new temp.file or is there
a nice way to work with CGI.pm's temp files?

And also actually the whole task I'm trying to solve
is that the file is being sent by TCP-connection to
a daemon. I'm looking for a way to intercept that
connection and to insert the bytes and the MD5 hash
there (kind of "man in the middle attack", but it's
not an attack :-) I could find the way to do it sofar,
that's why I stick with a CGI-script at the moment.

Regards
Alex

4. Where are anonymous temp files created?

5. Perldoc error: "Could not create temp file"

Greetings, groups.  I'm running Windows 2000 and djgpp, and trying
to read the built-in perl documentation via "perldoc", but it's
not working.  Perldoc fails both when invoked from the Windows
command processor (cmd.exe) or from Bash.  In either case, I get
an error like this:

wd=C:\djgpp\bin
%bash
bash-2.04$ perldoc perl
Error in tempfile() using c:/djgpp/tmp/XXXXXXXXXX: Could not create temp file c:
/djgpp/tmp/zkQ06tqlcH: Invalid argument (EINVAL) at /dev/env/DJDIR/lib/perl5/Pod
/Perldoc.pm line 1483
bash-2.04$ exit
exit
wd=C:\djgpp\bin
%perl perldoc perl
Error in tempfile() using C:/TEMP/XXXXXXXXXX: Could not create temp file C:/TEMP
/0mfgo64w9W: Invalid argument (EINVAL) at /dev/env/DJDIR/lib/perl5/Pod/Perldoc.p
m line 1483
wd=C:\djgpp\bin
%

Now, when I look at perldoc.pm line 1483, I see:

#..........................................................................
sub new_tempfile {    # $self->new_tempfile( [$suffix, [$infix] ] )
  my $self = shift;

  ++$Temp_Files_Created;

  if( IS_MSWin32 ) {
    my @out = $self->MSWin_perldoc_tempfile(@_);
    return @out if @out;
    # otherwise fall thru to the normal stuff below...
  }

  require File::Temp;
  return File::Temp::tempfile(UNLINK => 1); ######### LINE 1483 #########
}
#..........................................................................

So, why would the parameters of File::Temp::tempfile be "invalid"?
And what the heck is "UNLINK"?  That doesn't look like a variable.

I'm getting this problem both on my computer at work, and here at home.
Both computers are running MS Windows 2000, and both are using djgpp
and Perl version 5.8.8 .

I tried changing environment variable "tmpdir" to "C:\Temp", but I get
that same "Invalid argument on line 1483" error.

I'm not sure if this is a Perl issue or a djgpp issue (or both), so I'm
posting this in both comp.lang.perl.misc and comp.os.msdos.djgpp .

-- 
Cheers,
Robbie Hatley
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
perl -le 'print "\150ttp\72//\167ww.\167ell.\143om/~\154onewolf/"'


6. perl cgi.pm and temp files

7. search and replace with a temp file at a certain substr position

All,

I need to search a file for a string then set a variable using substr b/c
it is at a certain position, then finally replace the string with another
string.
Here is my test code:


                        my $gpg=qq(/home/gpghrp/.gnupg);
                        open (GPG, "gpg --fingerprint |") or die "unable to
open pipe sys call (1)... Broken? $!";
                        open(OLD,"+<$gpg/scripts/gpg_encr.saved") or die
$!;
                        open(NEW, "+<$gpg/scripts/gpg_encr.saved.new") or
die $!

                        for (;<OLD>;)
                        {
                                if ( /(?)readonly tlx=/ ) {
                                        my $ns =substr($_,13,8);
                                        print "$_\n";
                                        print "$ns\n";
                    print "Now substituting\n";
                                        print NEW s/$ns/"blah"/;
                                }

                        }
        close (GPG);
        close (SYS);

__END__CODE__

__BEGIN__DATA__

#PUBLIC KEYS

readonly an=809D16DD                    # Anthem's Key
readonly af=2ADCCA83                    # Aflac's Key
readonly cb=79A05028                    # Cenben's Key
readonly ci=FB0E9610                    # CitiStreet's key
readonly co=DBBFAB1C                    # Concierge's key
readonly daw=CB0E1CA5                   # Dawson's key
readonly df=935A53B4                    # Definity's key
readonly dl=D357304B                    # Delta's Key
readonly flxb=31412625                  # Flexbank's key
readonly fp=57931F1E                    # Fireproof's key
readonly hnb=E8D02D63                   # Huntington's key
# EF2AA234 32BCF762                     # HealthStream's keys.  Set up as a
group
readonly hum=9564F2E1                   # Humamarc's key --- non psoft
readonly ohg=D334EB59                   # OhioHealth Group's key
readonly merc=0D3C3955                  # Mercer's key
readonly ncb=165AAEBD                   # National City's key
readonly prvd=2EC1720B                  # UnumProvident's key
readonly tlx=886EC5AB                   # TALX's key
readonly vsp=03844E3A                   # VSP's key
readonly ws=68F0D515                    # Workscape's key



The coobook CD give an example but not with a substr.
In both files the position od $ns will always be the same.

thank you,
derek




Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams


8. reading from scalar File::Temp handle - Perl