1 package Maasha::Filesys;
4 # Copyright (C) 2006-2008 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # This module contains routines for manipulation of files and directories.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 use vars qw( @ISA @EXPORT @EXPORT_OK );
42 @ISA = qw( Exporter ) ;
45 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FILES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
50 # Martin A. Hansen, January 2004.
52 # Read opens a file that may be gzipped and returns a filehandle.
54 my ( $path, # full path to file
61 if ( is_gzipped( $path ) ) {
62 $fh = new IO::File "zcat $path|" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
64 $fh = new IO::File $path, "r" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
73 # Martin A. Hansen, May 2009.
75 # Cats a number of files and returns a filehandle.
77 my ( $files, # full path to file
82 my ( $file, $fh, $type, %type_hash, $file_string );
84 foreach $file ( @{ $files } )
86 Maasha::Common::error( qq(No such file: $file) ) if not -f $file;
90 if ( $type =~ /gzip compressed/ ) {
91 $type_hash{ 'gzip' } = 1;
93 $type_hash{ 'ascii' } = 1;
97 Maasha::Common::error( qq(Mixture of zipped and unzipped files) ) if scalar keys %type_hash > 1;
99 $file_string = join " ", @{ $files };
101 if ( $type =~ /gzip compressed/ ) {
102 $fh = new IO::File "zcat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
104 $fh = new IO::File "cat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
113 # Martin A. Hansen, January 2004.
115 # write opens a file and returns a filehandle
117 my ( $path, # full path to file
118 $gzip, # flag if data is to be gzipped - OPRIONAL
126 $fh = new IO::File "|gzip -f>$path" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
128 $fh = new IO::File $path, "w" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
137 # Martin A. Hansen, February 2006.
139 # append opens file and returns a filehandle
141 my ( $path, # path to file
148 $fh = new IO::File $path, "a" or Maasha::Common::error( qq(Could not append-open file "$path": $!) );
156 # Martin A. Hansen, July 2007.
158 # Returns a filehandle to STDIN
162 $fh = new IO::File "<&STDIN" or Maasha::Common::error( qq(Could not read from STDIN: $!) );
170 # Martin A. Hansen, July 2007.
172 # Returns a filehandle to STDOUT
176 $fh = new IO::File ">&STDOUT" or Maasha::Common::error( qq(Could not write to STDOUT: $!) );
184 # Martin A. Hansen, December 2004.
186 # given a file, a seek beg position and
187 # length, returns the corresponding string.
189 my ( $fh, # file handle to file
190 $beg, # read start in file
191 $len, # read length of block
198 Maasha::Common::error( qq(Negative length: $len) ) if $len < 0;
200 sysseek $fh, $beg, 0;
201 sysread $fh, $string, $len;
209 # Martin A. Hansen, December 2004.
211 # writes a data structure to file.
213 my ( $path, # full path to file
214 $data, # data structure
217 Storable::store( $data, $path ) or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
223 # Martin A. Hansen, December 2004.
225 # retrieves hash data structure
226 # (this routines needs to test if its a hash, array or else)
228 my ( $path, # full path to data file
233 $data = Storable::retrieve( $path ) or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
235 return wantarray ? %{ $data } : $data;
241 # Martin A. Hansen, November 2008.
243 # Copy the content of a file from source path to
246 my ( $src, # source path
247 $dst, # destination path
252 my ( $fh_in, $fh_out, $line );
254 Maasha::Common::error( qq(copy failed: destination equals source "$src") ) if $src eq $dst;
256 $fh_in = file_read_open( $src );
257 $fh_out = file_write_open( $dst );
259 while ( $line = <$fh_in> ) {
270 # Martin A. Hansen, November 2008.
272 # Checks if a given file is gzipped.
273 # Currrently uses a call to the systems
274 # file tool. Returns 1 if gzipped otherwise
277 my ( $path, # path to file
284 $type = `file $path`;
286 if ( $type =~ /gzip compressed/ ) {
296 # Martin A. Hansen, March 2007
298 # returns the file size for a given file
300 my ( $path, # full path to file
305 my $file_size = ( stat ( $path ) )[ 7 ];
311 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DIRECTORIES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
316 # Martin A. Hansen, July 2007.
318 # Creates a directory.
320 my ( $path, # full path to dir
326 Maasha::Common::error( qq(Directory already exists "$path": $!) );
328 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
333 sub dir_create_if_not_exists
335 # Martin A. Hansen, May 2008.
337 # Creates a directory if it does not already exists.
339 my ( $path, # full path to dir
344 if ( not -d $path ) {
345 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
352 # Martin A. Hansen, April 2008.
354 # Removes a directory recursively.
356 my ( $path, # directory
359 Maasha::Common::run( "rm", "-rf $path" ) if -d $path;
365 # Martin A. Hansen, June 2007.
367 # returns all dirs in a given directory.
369 my ( $path, # full path to directory
372 # returns a list of filenames.
376 $dh = open_dir( $path );
378 @dirs = read_dir( $dh );
379 @dirs = grep { -d "$path/$_" } @dirs;
381 map { $_ = "$path/$_" } @dirs;
385 return wantarray ? @dirs : \@dirs;
391 # Martin A. Hansen, June 2007.
393 # returns all files in a given directory.
395 my ( $path, # full path to directory
398 # returns a list of filenames.
402 $dh = open_dir( $path );
404 @files = read_dir( $dh );
405 @files = grep { -f "$path/$_" } @files;
407 map { $_ = "$path/$_" } @files;
411 return wantarray ? @files : \@files;
417 # Martin A. Hansen, June 2007.
419 # open a directory and returns a directory handle
423 my ( $path, # full path to directory
430 $dh = IO::Dir->new( $path ) or Maasha::Common::error( qq(Could not open dir "$path": $!) );
438 # Martin A. Hansen, June 2007.
440 # read all files and directories from a directory.
442 my ( $dh, # directory handle object
447 my ( $elem, @elems );
449 while ( defined( $elem = $dh->read ) ) {
453 return wantarray ? @elems : \@elems;
457 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<