From: Rob H. <for...@us...> - 2002-07-14 08:31:41
|
Update of /cvsroot/sandweb/sandweb/lib/SandWeb/File In directory usw-pr-cvs1:/tmp/cvs-serv6833/lib/SandWeb/File Added Files: Unix.pm Log Message: all file ops now use a module. currently, the only supported module is File/Unix.pm --- NEW FILE --- #lib/SandWeb/File/Unix.pm # # This class handles all file viewing and operations on Unix systems. # It is only intended to be called by the File class. # # SandWeb (Web-based VCS client) # # Copyright (C) 2002 Nick Jennings # Copyright (C) 2002 Robert Helmer # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # package SandWeb::File::Unix; use SandWeb::Shell; sub new { my $class = shift; my %args = @_; my $filename = $args{'filename'}; # Security check, no "/.." or "../" allowed mister! if ($filename) { $filename =~ s:/\.\.::g; $filename =~ s:\.\./::g; } my $log_obj = $args{'log_obj'}; my $location = $args{'location'}; my $raw_file_info = _shell( method => 'execute', command => "ls -lad $location/$filename", ); my @file_info = split(' ', $raw_file_info); my $perms = $file_info[0]; my $inodes = $file_info[1]; my $owner = $file_info[2]; my $group = $file_info[3]; my $size = $file_info[4]; my $month = $file_info[5]; my $day = $file_info[6]; my $time = $file_info[7]; my $self = bless { 'filename' => $filename, 'location' => $location, 'log_obj' => $log_obj, 'perms' => $perms, 'owner' => $owner, 'group' => $group, 'size' => $size, 'age' => "$month $day $time", }, $class; return $self; } sub get_owner { my $self = shift; return $self->{'owner'}; } sub get_group { my $self = shift; return $self->{'group'}; } sub get_filename { my $self = shift; return $self->{'filename'}; } sub get_location { my $self = shift; return $self->{'filename'}; } sub get_permissions { my $self = shift; return $self->{'perms'}; } sub get_file_type { my $self = shift; my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $file_scan = _shell( method => 'execute', command => "file $location/$filename", ); if ($file_scan =~ /directory/) { $file_type = 'Directory'; } elsif ($file_scan =~ /text/) { $file_type = 'Text'; } elsif ($file_scan =~ /empty/) { $file_type = 'Text'; } elsif (($file_scan =~ /data/) || ($file_scan =~ /executable/)) { $file_type = 'Binary'; } else { $file_type = 'Unknown'; } return $file_type; } sub get_size { my $self = shift; return $self->{'size'}; } sub get_age { my $self = shift; return $self->{'age'}; } sub upload { 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 @file = <$filehandle>; chomp @file; my $contents = join("\n", @file); _do_file_write( 'location' => $location, 'filename' => $filename, 'contents' => "$contents", 'log_obj' => $log, ); } sub download { my $self = shift; my %args = @_; my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $log = $self->{'log_obj'}; my $mime_type = $args{'mime_type'}; print "content-type: $mime_type\n\n"; my $file_contents = file_read(); print "$file_contents"; close FILE; } sub create_file { my $self = shift; my %args = @_; my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $contents = $args{'contents'}; my $log = $self->{'log_obj'}; $log->debug("creating file : $location/$filename"); my $create_file = _shell( method => 'execute', command => "touch $location/$filename", ); return $return; } sub create_folder { my $self = shift; my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $log = $self->{'log_obj'}; $log->debug("creating folder : $location/$filename"); my $create_folder = _shell( method => 'execute', command => "mkdir $location/$filename", ); return 1; } sub delete { my $self = shift; my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $log = $self->{'log_obj'}; my $return; $log->debug("removing file : $location/$filename"); my $result = _shell( method => 'execute', command => "rm $location/$filename", log_obj => $log, ); $log->debug("error code : $result"); return $result; } sub delete_folder { my $self = shift; my %args = @_; my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $log = $self->{'log_obj'}; my $recurse = $args{'recurse'}; my $return; $log->debug("removing folder : $location/$filename"); my $result = _shell( method => 'execute', command => "rm -rf $location/$filename", log_obj => $log, ); $log->debug("error code : $result"); return 1; } sub copy { my $self = shift; my %args = @_; my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $log = $self->{'log_obj'}; my $tofile = $args{'tofile'}; $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, ); return 1; } sub rename { my $self = shift; my %args = @_; my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $log = $self->{'log_obj'}; my $tofile = $args{'tofile'}; $log->debug("renaming file : $location/$filename to $location/$tofile"); my $file_scan = _shell( method => 'execute', command => "mv $location/$filename $location/$tofile", ); return 1; } sub file_read { my $self = shift; my $location = $self->{'location'}; my $filename = $self->{'filename'}; return _do_file_read( 'location' => "$location", 'filename' => "$filename", ); } sub file_write { my $self = shift; my %args = @_; my $location = $self->{'location'}; my $filename = $self->{'filename'}; my $log = $self->{'log_obj'}; my $contents = $args{'contents'}; my $return = _do_file_write( 'location' => "$location", 'filename' => "$filename", 'contents' => "$contents", 'log_obj' => $log, ); return 1; } sub get_file_commands { my $self = shift; return ( 'create_file', 'create_folder', 'delete', 'rename', 'upload', ); } sub _do_file_write { my %args = @_; my $location = $args{'location'}; my $filename = $args{'filename'}; my $contents = $args{'contents'}; my $log = $args{'log_obj'}; my $filehandle = _shell( method => 'pipe', command => "dd of=$location/$filename 1>/dev/null 2>/dev/null", ); #open (FILEHANDLE,"|`dd of=$location/$filename 1>/dev/null 2>/dev/null`"); print $filehandle $contents; close $filehandle; return 1; } sub _do_file_read { my %args = @_; my $location = $args{'location'}; my $filename = $args{'filename'}; my $log = $args{'log_obj'}; my $file_contents = _shell( method => 'execute', command => "cat $location/$filename", ); return join('', $file_contents); } sub _shell { my %args = @_; my $log = $args{'log_obj'}; my $command = $args{'command'}; my $method = $args{'method'}; my $shell = SandWeb::Shell->new( log_obj => $log ); if ($method eq 'execute') { my %result = $shell->execute( command => "$command" ); return $result{'output'}; } elsif ($method eq 'pipe') { my $result = $shell->pipe( command => "$command" ); return $result; } $log->error("error - invalid execute method : $method"); return 0; } 1; |