# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+use warnings;
use strict;
use IO::File;
+use Storable;
+use Data::Dumper;
use Maasha::Common;
+use Digest::MD5;
use Exporter;
}
+sub files_read_open
+{
+ # Martin A. Hansen, May 2009.
+
+ # Cats a number of files and returns a filehandle.
+
+ my ( $files, # full path to file
+ ) = @_;
+
+ # returns filehandle
+
+ my ( $file, $fh, $type, %type_hash, $file_string );
+
+ foreach $file ( @{ $files } )
+ {
+ Maasha::Common::error( qq(No such file: $file) ) if not -f $file;
+
+ $type = `file $file`;
+
+ if ( $type =~ /gzip compressed/ ) {
+ $type_hash{ 'gzip' } = 1;
+ } else {
+ $type_hash{ 'ascii' } = 1;
+ }
+ }
+
+ Maasha::Common::error( qq(Mixture of zipped and unzipped files) ) if scalar keys %type_hash > 1;
+
+ $file_string = join " ", @{ $files };
+
+ if ( $type =~ /gzip compressed/ ) {
+ $fh = new IO::File "zcat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
+ } else {
+ $fh = new IO::File "cat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
+ }
+
+ return $fh;
+}
+
+
sub file_write_open
{
# Martin A. Hansen, January 2004.
}
+sub file_store
+{
+ # Martin A. Hansen, December 2004.
+
+ # writes a data structure to file.
+
+ my ( $path, # full path to file
+ $data, # data structure
+ ) = @_;
+
+ Storable::store( $data, $path ) or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
+}
+
+
+sub file_retrieve
+{
+ # Martin A. Hansen, December 2004.
+
+ # retrieves hash data structure
+ # (this routines needs to test if its a hash, array or else)
+
+ my ( $path, # full path to data file
+ ) = @_;
+
+ my ( $data );
+
+ $data = Storable::retrieve( $path ) or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
+
+ return wantarray ? %{ $data } : $data;
+}
+
+
sub file_copy
{
# Martin A. Hansen, November 2008.
# returns integer
- my $file_size = ( stat ( $path ) )[ 7 ];
+ my $file_size = -s $path;
return $file_size;
}
+sub file_md5
+{
+ # Martin A. Hansen, December 2009.
+
+ # Get an MD5 sum for a given file.
+
+ my ( $file, # file path
+ ) = @_;
+
+ # Returns a string.
+
+ my ( $fh, $md5 );
+
+ $fh = file_read_open( $file );
+
+ $md5 = Digest::MD5->new;
+
+ $md5->addfile( $fh );
+
+ close $fh;
+
+ return $md5->hexdigest;
+}
+
+
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DIRECTORIES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
my ( $path, # full path to dir
) = @_;
- # Returns nothing.
+ # Returns created path.
if ( -d $path ) {
- Maasha::Common::error( qq(Directory already exists "$path": $!) );
+ Maasha::Common::error( qq(Directory already exists "$path") );
} else {
mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
}
+
+ return $path;
}
my ( $path, # full path to dir
) = @_;
- # Returns nothing.
+ # Returns path.
if ( not -d $path ) {
mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
}
+
+ return $path;
}
close $dh;
+ @dirs = sort @dirs;
+
return wantarray ? @dirs : \@dirs;
}
+sub ls_dirs_base
+{
+ # Martin A. Hansen, November 2009.
+
+ # Returns all directory basenames execpt . and ..
+ # from a given directory.
+
+ my ( $path,
+ ) = @_;
+
+ # Returns a list.
+
+ my ( @dirs, $dir, @list );
+
+ @dirs = Maasha::Filesys::ls_dirs( $path );
+
+ foreach $dir ( @dirs )
+ {
+ next if $dir =~ /\/\.\.?$/;
+
+ push @list, ( split "/", $dir )[ -1 ];
+ }
+
+ return wantarray ? @list : \@list;
+}
+
+
sub ls_files
{
# Martin A. Hansen, June 2007.