From: Rob H. <for...@us...> - 2003-01-12 05:13:50
|
Update of /cvsroot/sandweb/sandweb/lib/SandWeb/File In directory sc8-pr-cvs1:/tmp/cvs-serv16255/SandWeb/File Modified Files: Unix.pm Log Message: better formatting; now the API doc section for each method is right above that method in the code. Index: Unix.pm =================================================================== RCS file: /cvsroot/sandweb/sandweb/lib/SandWeb/File/Unix.pm,v retrieving revision 1.8 retrieving revision 1.9 diff -U2 -r1.8 -r1.9 --- Unix.pm 6 Jan 2003 09:49:30 -0000 1.8 +++ Unix.pm 12 Jan 2003 05:13:47 -0000 1.9 @@ -2,9 +2,56 @@ =head1 -# lib/SandWeb/File/Unix.pm +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. + +=cut + +# SandWeb (Web-based VCS client) # -# This class handles all file viewing and operations on Unix systems. -# It is only intended to be called by the File class. --------------------------------------------------------------------------------- +# 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 +# + +=head1 + +part of the SandWeb::File::Unix package + +=cut + +package SandWeb::File::Unix; + +=head1 + +uses SandWeb::Shell and SandWeb::Security + +=cut + +use SandWeb::Shell; +use SandWeb::Security; + +=head1 + +Methods + +=cut + +=head1 + METHOD new @@ -49,5 +96,57 @@ read the files specified, or they did not exist. --------------------------------------------------------------------------------- +=cut + +sub new { + my $class = shift; + my %args = @_; + + my $unsafe_filename = $args{'filename'}; + my $unsafe_location = $args{'location'}; + + # Security check + my $secure = SandWeb::Security->new(); + + my $filename = $secure->path( + filename => "$unsafe_filename" + ); + + my $location = $secure->path( + filename => "$unsafe_location" + ); + + my $log_obj = $args{'log_obj'}; + + 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; +} + +=head1 METHOD @@ -76,6 +175,15 @@ read the files specified, or they did not exist. --------------------------------------------------------------------------------- +=cut + +sub get_owner { + my $self = shift; + return $self->{'owner'}; +} +=head1 + +get_group + returns the group of the current file METHOD get_group @@ -105,4 +213,12 @@ -------------------------------------------------------------------------------- +=cut + +sub get_group { + my $self = shift; + return $self->{'group'}; +} +=head1 + METHOD get_filename @@ -132,4 +248,13 @@ -------------------------------------------------------------------------------- +=cut + +sub get_filename { + my $self = shift; + return $self->{'filename'}; +} + +=head1 + METHOD get_location @@ -160,4 +285,13 @@ -------------------------------------------------------------------------------- +=cut + +sub get_location { + my $self = shift; + return $self->{'filename'}; +} + +=head1 + METHOD get_permissions @@ -188,4 +322,12 @@ -------------------------------------------------------------------------------- +=cut + +sub get_permissions { + my $self = shift; + return $self->{'perms'}; +} +=head1 + METHOD get_file_type @@ -217,4 +359,35 @@ -------------------------------------------------------------------------------- +=cut + +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; +} +=head1 + METHOD get_size @@ -244,4 +417,12 @@ -------------------------------------------------------------------------------- +=cut + +sub get_size { + my $self = shift; + return $self->{'size'}; +} +=head1 + METHOD get_age @@ -272,4 +453,12 @@ -------------------------------------------------------------------------------- +=cut + +sub get_age { + my $self = shift; + return $self->{'age'}; +} +=head1 + METHOD create_file @@ -299,422 +488,50 @@ -------------------------------------------------------------------------------- -METHOD - create_folder - -SYNOPSIS +=cut - my $return = $file->create_folder(); +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'}; -DESCRIPTION +# begin workaround for IE/Windows upload behaviour + while ( $filename =~ /\\/ ) { + $filename = $'; + } +# end workaround for IE/Windows upload behaviour - Creates a folder ( also known as a directory ) using the - filename passed to the constructor. + my @file = <$filehandle>; + chomp @file; -PARAMETERS + my $contents = join("\n", @file); - None. + _do_file_write( + 'location' => $location, + 'filename' => $filename, + 'contents' => "$contents", + 'log_obj' => $log, + ); +} +=head1 -RETURN CODES +download - 1 = The operation completed successfully. - - 0 = This means that the method got an error proccessing your request. - Perhaps an invalid parameter? - - -1 = This return value means that there was not sufficient permision to - read the files specified, or they did not exist. + downloads ( sends ) the file to the user --------------------------------------------------------------------------------- +=cut -METHOD - copy +sub download { + my $self = shift; + my %args = @_; -SYNOPSIS + my $location = $self->{'location'}; + my $filename = $self->{'filename'}; + my $log = $self->{'log_obj'}; - my $return = $file->copy( tofile => "alternate_name.c" ); - -DESCRIPTION - - Creates an exact copy of the file. - -PARAMETERS - - tocopy (type: string) (required) - Contains the full path and filename of the copy. - -RETURN CODES - - 1 = The operation completed successfully. - - 0 = This means that the method got an error proccessing your request. - Perhaps an invalid parameter? - - -1 = This return value means that there was not sufficient permision to - read the files specified, or they did not exist. - --------------------------------------------------------------------------------- - -METHOD - rename - -SYNOPSIS - - my $return = $file->rename( tofile => "alternate_name.c" ); - -DESCRIPTION - - Renames an existing file. This is the same thing as a move, - as far as the file system and operating system are concerned. - -PARAMETERS - - tocopy (type: string) (required) - Contains the full path and filename of the new file. - -RETURN CODES - - 1 = The operation completed successfully. - - 0 = This means that the method got an error proccessing your request. - Perhaps an invalid parameter? - - -1 = This return value means that there was not sufficient permision to - read the files specified, or they did not exist. - --------------------------------------------------------------------------------- - -METHOD - remove_file - -SYNOPSIS - - my $return = $file->remove_file(); - -DESCRIPTION - - Removes the file specified in the constructor. - -PARAMETERS - - none. - -RETURN CODES - - 1 = The operation completed successfully. - - 0 = This means that the method got an error proccessing your request. - Perhaps an invalid parameter? - - -1 = This return value means that there was not sufficient permision to - read the files specified, or they did not exist. - --------------------------------------------------------------------------------- - -METHOD - remove_folder - -SYNOPSIS - - my $return = $file->remove_folder(); - -DESCRIPTION - - Removes the folder specified in the constructor. - -PARAMETERS - - none. - -RETURN CODES - - 1 = The operation completed successfully. - - 0 = This means that the method got an error proccessing your request. - Perhaps an invalid parameter? - - -1 = This return value means that there was not sufficient permision to - read the files specified, or they did not exist. - -#lib/SandWeb/File/Unix.pm -# - -SandWeb::File::Unix - -This class handles all file viewing and operations on Unix systems. -It is only intended to be called by the File class. - -=cut - -# 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 -# - -=head1 - -part of the SandWeb::File::Unix package - -=cut - -package SandWeb::File::Unix; - -=head1 - -uses SandWeb::Shell and SandWeb::Security - -=cut - -use SandWeb::Shell; -use SandWeb::Security; - -=head1 - -Methods - - -new - - instantiates a new object - -=cut - -sub new { - my $class = shift; - my %args = @_; - - my $unsafe_filename = $args{'filename'}; - my $unsafe_location = $args{'location'}; - - # Security check - my $secure = SandWeb::Security->new(); - - my $filename = $secure->path( - filename => "$unsafe_filename" - ); - - my $location = $secure->path( - filename => "$unsafe_location" - ); - - my $log_obj = $args{'log_obj'}; - - 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; -} - -=head1 - -get_owner - - returns the owner of the current file - -=cut - -sub get_owner { - my $self = shift; - return $self->{'owner'}; -} -=head1 - -get_group - - returns the group of the current file - -=cut - -sub get_group { - my $self = shift; - return $self->{'group'}; -} -=head1 - -get_filename - - returns the filename of the current file - -=cut - -sub get_filename { - my $self = shift; - return $self->{'filename'}; -} - -=head1 - -get_location - - returns the location (directory) of the current file - -=cut - -sub get_location { - my $self = shift; - return $self->{'filename'}; -} - -=head1 - -get_permissions - - returns the permissions of the current file - -=cut - -sub get_permissions { - my $self = shift; - return $self->{'perms'}; -} -=head1 - -get_file_type - - returns the file_type of the current file - -=cut - -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; -} -=head1 - -get_size - - returns the size of the current file - -=cut - -sub get_size { - my $self = shift; - return $self->{'size'}; -} -=head1 - -get_age - - returns the age of the current file - -=cut - -sub get_age { - my $self = shift; - return $self->{'age'}; -} -=head1 - -upload - - uploads ( receives ) a file from the user - -=cut - -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, - ); -} -=head1 - -download - - downloads ( sends ) the file to the user - -=cut - -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'}; + my $mime_type = $args{'mime_type'}; print "content-type: $mime_type\n\n"; @@ -764,7 +581,31 @@ =head1 -create_file +METHOD + create_folder - creates ( touches ) an empty file with the current filename +SYNOPSIS + + my $return = $file->create_folder(); + +DESCRIPTION + + Creates a folder ( also known as a directory ) using the + filename passed to the constructor. + +PARAMETERS + + None. + +RETURN CODES + + 1 = The operation completed successfully. + + 0 = This means that the method got an error proccessing your request. + Perhaps an invalid parameter? + + -1 = This return value means that there was not sufficient permision to + read the files specified, or they did not exist. + +-------------------------------------------------------------------------------- =cut @@ -813,7 +654,30 @@ =head1 -delete +METHOD + remove_file + +SYNOPSIS - deletes ( rm ) the current file + my $return = $file->remove_file(); + +DESCRIPTION + + Removes the file specified in the constructor. + +PARAMETERS + + none. + +RETURN CODES + + 1 = The operation completed successfully. + + 0 = This means that the method got an error proccessing your request. + Perhaps an invalid parameter? + + -1 = This return value means that there was not sufficient permision to + read the files specified, or they did not exist. + +-------------------------------------------------------------------------------- =cut @@ -842,7 +706,29 @@ =head1 -delete_folder +METHOD + remove_folder + +SYNOPSIS + + my $return = $file->remove_folder(); + +DESCRIPTION + + Removes the folder specified in the constructor. + +PARAMETERS + + none. + +RETURN CODES + + 1 = The operation completed successfully. + + 0 = This means that the method got an error proccessing your request. + Perhaps an invalid parameter? + + -1 = This return value means that there was not sufficient permision to + read the files specified, or they did not exist. - deletes ( rm -rf ) the current folder =cut @@ -870,7 +756,31 @@ =head1 -copy +METHOD + copy + +SYNOPSIS - copy ( cp ) the current file/folder + my $return = $file->copy( tofile => "alternate_name.c" ); + +DESCRIPTION + + Creates an exact copy of the file. + +PARAMETERS + + tocopy (type: string) (required) + Contains the full path and filename of the copy. + +RETURN CODES + + 1 = The operation completed successfully. + + 0 = This means that the method got an error proccessing your request. + Perhaps an invalid parameter? + + -1 = This return value means that there was not sufficient permision to + read the files specified, or they did not exist. + +-------------------------------------------------------------------------------- =cut @@ -904,7 +814,32 @@ =head1 -rename +METHOD + rename + +SYNOPSIS + + my $return = $file->rename( tofile => "alternate_name.c" ); + +DESCRIPTION + + Renames an existing file. This is the same thing as a move, + as far as the file system and operating system are concerned. + +PARAMETERS + + tocopy (type: string) (required) + Contains the full path and filename of the new file. - renames ( mv ) the current file +RETURN CODES + + 1 = The operation completed successfully. + + 0 = This means that the method got an error proccessing your request. + Perhaps an invalid parameter? + + -1 = This return value means that there was not sufficient permision to + read the files specified, or they did not exist. + +-------------------------------------------------------------------------------- =cut |