| 1 | # -*-perl-*- | ||||||||||
| 2 | # Creation date: 2003-09-01 22:23:46 | ||||||||||
| 3 | # Authors: Don | ||||||||||
| 4 | # Change log: | ||||||||||
| 5 | # $Id: UploadFile.pm,v 1.6 2004/10/24 10:33:08 don Exp $ | ||||||||||
| 6 | |||||||||||
| 7 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | { package CGI::Utils::UploadFile; | ||||||||||
| 10 | |||||||||||
| 11 | 6 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 6 | $VERSION = do { my @r=(q$Revision: 1.6 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r }; | |||||||||
| 13 | |||||||||||
| 14 | 6 | ▶ | use vars qw($FH_COUNT $Have_File_Temp $Open_Flags); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 6 | $FH_COUNT = 0; | |||||||||
| 16 | |||||||||||
| 17 | 6 | ▶ | use Fcntl (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 6 | ▶ | use overload '""' => '_asString', cmp => '_compareAsString', fallback => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 6 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.2
| |||||||||||
| 22 | 6 | ▶ | local $SIG{__DIE__} = sub {}; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 6 | ▶ | local $SIG{__WARN__} = sub {}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||
| 24 | 6 | $Have_File_Temp = eval 'require File::Temp; 1'; | |||||||||
| 25 | 6 | $Open_Flags = Fcntl::O_RDWR()|Fcntl::O_CREAT(); | |||||||||
| 26 | # Fcntl::O_EXCL(); - leave this out for now cuz it breaks File::Temp usage | ||||||||||
| 27 | |||||||||||
| 28 | # idea taken from File::Temp | ||||||||||
| 29 | 6 | ▶ | unless ($^O eq 'MacOS') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 6 | my $bit = 0; | |||||||||
| 31 | 6 | ▶ | $Open_Flags |= $bit if eval '$bit = Fcntl::O_TEMPORARY()'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | } | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=2 SCAR=13.7
Pod
undocumented
| |||||||||||
| 36 | 0 | my ($proto, $name) = @_; | |||||||||
| 37 | 6 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 0 | (my $safe_name = $name) =~ s/([^a-zA-Z0-9_])/sprintf("%%%02x", ord($1))/eg; | |||||||||
| 39 | 0 | $FH_COUNT++; | |||||||||
| 40 | 0 | my $sub_name = "fh" . $FH_COUNT . "_" . $safe_name; | |||||||||
| 41 | 0 | my $ref = \*{"CGI::Utils::UploadFile::$sub_name"}; | |||||||||
| 42 | 0 | my $self = bless $ref, $proto; | |||||||||
| 43 | 0 | ▶ | return wantarray ? ($self, $sub_name) : $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | sub new_from_handle { | ||||||||
|
Subroutine
new_from_handle: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 47 | 0 | my ($proto, $file_name, $old_fh) = @_; | |||||||||
| 48 | 0 | my ($fh, $name_space) = $proto->new($file_name); | |||||||||
| 49 | |||||||||||
| 50 | # dup the old file handle | ||||||||||
| 51 | 0 | open($fh, ">&", $old_fh); | |||||||||
| 52 | 0 | return $fh; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | sub new_tmpfile { | ||||||||
|
Subroutine
new_tmpfile: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 56 | 0 | my ($proto, $file_name) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 0 | my ($fh, $name_space) = $proto->new($file_name); | |||||||||
| 59 | |||||||||||
| 60 | 0 | my $tmp_file = ''; | |||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | if ($Have_File_Temp) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | my $tmp_fh = File::Temp->new(UNLINK => 0); | |||||||||
| 64 | 0 | $tmp_file = $tmp_fh->filename; | |||||||||
| 65 | } else { | ||||||||||
| 66 | 0 | my $tmp_dir = "/tmp"; | |||||||||
| 67 | 0 | $tmp_file = $tmp_dir . | |||||||||
| 68 | "/_cgi_utils_" . sprintf("%x%x%x", 10000 + int rand(10000), time(), $$); | ||||||||||
| 69 | 0 | for my $i (1 .. 20) { | |||||||||
| 70 | 0 | ▶ | last unless -e $tmp_file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | $tmp_file = $tmp_dir . | |||||||||
| 72 | "/_cgi_utils_" . sprintf("%x%x%x", 10000 + int rand(10000), time(), $$); | ||||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | sysopen($fh, $tmp_file, $Open_Flags, 0600) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | or return undef; | ||||||||||
| 78 | |||||||||||
| 79 | 0 | unlink $tmp_file; | |||||||||
| 80 | 0 | delete $CGI::Utils::UploadFile::{$name_space}; | |||||||||
| 81 | |||||||||||
| 82 | 0 | return $fh; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub filename { | ||||||||
|
Subroutine
filename: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 86 | 0 | return shift()->_asString; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 0 | ▶ | sub _asString { | ||||||||
|
Subroutine
_asString: not called CC=1 SCAR=6.9
| |||||||||||
| 90 | 0 | my ($self) = @_; | |||||||||
| 91 | |||||||||||
| 92 | 0 | (my $safe_name = $$self) =~ s/^.+::fh\d+_([^:]+)$/$1/; | |||||||||
| 93 | 0 | $safe_name =~ s/%([a-f0-9]{2})/chr(hex($1))/eg; | |||||||||
| 94 | 0 | return $safe_name; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub _compareAsString { | ||||||||
|
Subroutine
_compareAsString: not called CC=1 SCAR=6.9
| |||||||||||
| 98 | 0 | my ($self, $val) = @_; | |||||||||
| 99 | 0 | return "$self" cmp $val; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||
| 103 | 0 | my ($self) = @_; | |||||||||
| 104 | 0 | close $self; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 6 | 1; | |||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||