From: Rob H. <for...@us...> - 2002-07-14 08:31:40
|
Update of /cvsroot/sandweb/sandweb/lib/SandWeb In directory usw-pr-cvs1:/tmp/cvs-serv6833/lib/SandWeb Modified Files: File.pm Shell.pm Log Message: all file ops now use a module. currently, the only supported module is File/Unix.pm Index: File.pm =================================================================== RCS file: /cvsroot/sandweb/sandweb/lib/SandWeb/File.pm,v retrieving revision 1.58 retrieving revision 1.59 diff -U2 -r1.58 -r1.59 --- File.pm 19 Jun 2002 07:09:00 -0000 1.58 +++ File.pm 14 Jul 2002 08:31:37 -0000 1.59 @@ -28,9 +28,6 @@ package SandWeb::File; -# standard modules -# -# POSIX is for the get_age() method - -use POSIX qw(strftime); +# file handling module(s) +use SandWeb::File::Unix; sub new { @@ -38,15 +35,14 @@ my %args = @_; - my $filename = $args{'filename'}; - # Security check, no "/.." or "../" allowed mister! - $filename =~ s:/\.\.::g; - $filename =~ s:\.\./::g; - my $log_obj = $args{'log_obj'}; - my $config = $args{'config_obj'}; - my $location = $args{'location'}; - - my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, - $atime,$mtime,$ctime,$blksize,$blocks) - = stat("$location/$filename"); + my $filename = $args{'filename'} || ''; + my $log_obj = $args{'log_obj'} || ''; + my $config = $args{'config_obj'} || ''; + my $location = $args{'location'} || ''; + + my $module = SandWeb::File::Unix->new( + filename => $args{'filename'}, + log_obj => $log_obj, + location => $args{'location'}, + ); my $self = bless { @@ -55,9 +51,9 @@ 'log_obj' => $log_obj, 'config_obj' => $config, - 'mode' => $mode, - 'uid' => $uid, - 'gid' => $gid, - 'size' => $size, - 'mtime' => $mtime, + 'perms' => $module->get_permissions(), + 'owner' => $module->get_owner(), + 'group' => $module->get_group(), + 'size' => $module->get_size(), + 'age' => $module->get_age(), }, $class; @@ -87,6 +83,5 @@ sub get_permissions { my $self = shift; - my $mode = $self->{'mode'}; - return $mode & 0777; + return $self->{'perms'}; } @@ -95,16 +90,13 @@ my $location = $self->{'location'}; my $filename = $self->{'filename'}; + my $log = $self->{'log_obj'}; - if (-d "$location/$filename") { - $file_type = 'Directory'; - } - elsif (-T "$location/$filename") { - $file_type = 'Text'; - } - elsif (-B "$location/$filename") { - $file_type = 'Binary'; - } else { - $file_type = 'Unknown'; - } + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, + ); + + my $file_type = $module->get_file_type(); return $file_type; @@ -139,8 +131,5 @@ sub get_age { my $self = shift; - my $mtime = $self->{'mtime'}; - my $time = strftime "%m/%e/%Y %H:%M:%S", localtime $mtime; - - return $time; + return $self->{'age'}; } @@ -148,22 +137,18 @@ my $self = shift; my %args = @_; + my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $log = $self->{'log_obj'}; - # passed from CGI->upload - FileHandle of incoming file. - my $filehandle = $args{'filehandle'}; -# begin workaround for IE/Windows upload behaviour - while ( $filename =~ /\\/ ) { - $filename = $'; - } -# end workaround for IE/Windows upload behaviour + my $filehandle = $args{'filehandle'}; - my @file = <$filehandle>; - chomp @file; + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, + ); - open UPLOADFILE, ">$location/$filename"; - print UPLOADFILE join("\n", @file); - close UPLOADFILE; + return $module->upload( filehandle => $filehandle ); } @@ -173,36 +158,29 @@ my $filename = $self->{'filename'}; my $log = $self->{'log_obj'}; - my $config = $self->{'config_obj'}; - my $mime_location = $config->{'webserver'}->{'mime-types_location'}; - my $mime_file = $config->{'webserver'}->{'mime-types_file'}; - - $log->debug("downloading file : $location/$filename"); + my $mime_type = $self->get_mime_type() || ''; - my $mime_type = _get_mime_type( - 'location' => "$location", - 'filename' => "$filename", - 'mime_file' => "$mime_file", - 'mime_location' => "$mime_location", - 'log_obj' => "$log", + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, ); - $log->debug("MIME type looks like : $mime_type"); + return $module->download( mime_type => $mime_type ); +} - unless ($mime_type) { - $mime_type = "binary/octet-stream"; - } +sub exists { + my $self = shift; + my $location = $self->{'location'}; + my $filename = $self->{'filename'}; + my $log = $self->{'log_obj'}; - print "content-type: $mime_type\n\n"; + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, + ); - if (open FILE,"< $location/$filename") { - while (<FILE>) { - print $_; - } - } else { - $log->debug("Can't download $location/$filename : $!"); - } - close FILE; - + return $module->exists(); } @@ -216,12 +194,11 @@ $log->debug("creating file : $location/$filename"); - my $return = _do_file_write( - 'location' => $location, - 'filename' => $filename, - 'contents' => "$contents", - 'log_obj' => $log, + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, ); - return $return; + return $module->create_file(); } @@ -233,13 +210,11 @@ $log->debug("creating folder : $location/$filename"); + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, + ); - if (mkdir( "$location/$filename", 0750 )) { - $return = 1; - } else { - $log->debug("could not create folder $location/$filename : $!"); - $return = 0; - } - - return $return; + return $module->create_folder(); } @@ -252,13 +227,11 @@ $log->debug("removing file : $location/$filename"); + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, + ); - if ( unlink("$location/$filename") ) { - $return = 1; - } else { - $log->debug("could not delete $location/$filename : $!"); - $return = 0; - } - - return $return; + return $module->delete(); } @@ -273,16 +246,11 @@ $log->debug("removing folder : $location/$filename"); + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, + ); - if ($recurse) { - $return = _delete_tree( - 'location' => "$location", - 'filename' => "$filename", - 'log_obj' => "$log", - ); - } else { - $return = rmdir("$location/$filename"); - } - - return $return; + return $module->delete_folder(); } @@ -296,20 +264,11 @@ $log->debug("copying file : $location/$filename to $tofile"); - - my $file = SandWeb::File->new( - 'location' => "$location", - 'filename' => "$filename", - ); - - my @fromfile = _do_file_read(); - - _do_file_write( - 'filename' => $filename, - 'location' => $location, - 'contents' => "@fromfile", - 'log_obj' => $log, + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, ); - return $return; + return $module->copy( tofile => $tofile ); } @@ -323,11 +282,11 @@ $log->debug("renaming file : $location/$filename to $location/$tofile"); - - my $return = rename( "$location/$filename", "$location/$tofile" ); - unless ($return) { - $log->debug("renaming failed : $!"); - } + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, + ); - return $return; + return $module->rename( tofile => $tofile ); } @@ -337,9 +296,13 @@ my $location = $self->{'location'}; my $filename = $self->{'filename'}; + my $log = $self->{'log_obj'}; - return _do_file_read( - 'location' => "$location", - 'filename' => "$filename", + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, ); + + return $module->file_read(); } @@ -353,12 +316,11 @@ my $contents = $args{'contents'}; - my $return = _do_file_write( - 'location' => "$location", - 'filename' => "$filename", - 'contents' => "$contents", - 'log_obj' => $log, + my $module = SandWeb::File::Unix->new( + filename => $filename, + log_obj => $log, + location => $location, ); - return $return; + return $module->file_write(contents => $contents); } @@ -386,8 +348,10 @@ my $mime_type; - my $mime_output = _do_file_read( + my $module = SandWeb::File::Unix->new( 'location' => "$mime_location", 'filename' => "$mime_file", ); + + my $mime_output = $module->file_read(); my @mimes = split('\n',$mime_output); @@ -404,85 +368,4 @@ } return $mime_type; -} - -sub _do_file_write { - my %args = @_; - my $location = $args{'location'}; - my $filename = $args{'filename'}; - my $contents = $args{'contents'}; - my $log = $args{'log_obj'}; - my $return; - - if (open (FILE, ">$location/$filename")) { - print FILE join('', $contents); - } else { - $log->debug("error opening $location/$filename for writing: $!"); - } - if (close FILE) { - $return = 1; - } else { - $log->debug("error closing $location/$filename from writing: $!"); - $return = 0; - } - - return $return; -} - -sub _do_file_read { - my %args = @_; - my $location = $args{'location'}; - my $filename = $args{'filename'}; - my $log = $args{'log_obj'}; - - open (FILE, "<$location/$filename"); - my @contents = <FILE>; - close FILE; - - return join('', @contents); -} - -sub _delete_tree { - my %args = @_; - my $location = $args{'location'}; - my $filename = $args{'filename'}; - my $log = $args{'log_obj'}; - my $return; - - if ( -d "$location/$filename" ) { - opendir(DIR, "$location/$filename"); - my @files = readdir(DIR); - closedir(DIR); - foreach my $file (@files) { - next if ($file eq "."); - next if ($file eq ".."); - if ( -d "$location/$filename/$file" ) { - $return = _delete_tree( - 'location' => "$location/$filename", - 'filename' => "$file", - 'log_obj' => "$log", - ); - $return = rmdir("$location/$filename/$file"); - unless ($return) { -# -# XXX - log not working here -# $log->debug("error deleting dir $location/$filename/$file: $!"); - } - } else { - $return = unlink("$location/$filename/$file"); - unless ($return) { -# -# XXX - log not working here -# $log->debug("error deleting file $location/$filename/$file: $!"); - } - } - } - $return = rmdir("$location/$filename"); - unless ($return) { -# -# XXX - log not working here -# $log->debug("error deleting dir $location/$filename/$file: $!"); - } - } - return $return; } Index: Shell.pm =================================================================== RCS file: /cvsroot/sandweb/sandweb/lib/SandWeb/Shell.pm,v retrieving revision 1.17 retrieving revision 1.18 diff -U2 -r1.17 -r1.18 --- Shell.pm 24 Jun 2002 20:39:25 -0000 1.17 +++ Shell.pm 14 Jul 2002 08:31:37 -0000 1.18 @@ -157,3 +157,19 @@ } +sub pipe { + my $self = shift; + my %args = @_; + + my $log = $self->{'log'} || ''; + + my $command = $args{'command'} || ''; + + if ($command) { + open (FILEHANDLE, "|`$command`"); + return \*FILEHANDLE; + } + + return 0; +} + 1; |