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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41 use vars qw( @ISA @EXPORT @EXPORT_OK );
43 @ISA = qw( Exporter ) ;
46 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FILES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
51 # Martin A. Hansen, January 2004.
53 # Read opens a file that may be gzipped and returns a filehandle.
55 my ( $path, # full path to file
62 if ( is_gzipped( $path ) ) {
63 $fh = new IO::File "zcat $path|" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
65 $fh = new IO::File $path, "r" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
74 # Martin A. Hansen, May 2009.
76 # Cats a number of files and returns a filehandle.
78 my ( $files, # full path to file
83 my ( $file, $fh, $type, %type_hash, $file_string );
85 foreach $file ( @{ $files } )
87 Maasha::Common::error( qq(No such file: $file) ) if not -f $file;
91 if ( $type =~ /gzip compressed/ ) {
92 $type_hash{ 'gzip' } = 1;
94 $type_hash{ 'ascii' } = 1;
98 Maasha::Common::error( qq(Mixture of zipped and unzipped files) ) if scalar keys %type_hash > 1;
100 $file_string = join " ", @{ $files };
102 if ( $type =~ /gzip compressed/ ) {
103 $fh = new IO::File "zcat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
105 $fh = new IO::File "cat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
114 # Martin A. Hansen, January 2004.
116 # write opens a file and returns a filehandle
118 my ( $path, # full path to file
119 $gzip, # flag if data is to be gzipped - OPRIONAL
127 $fh = new IO::File "|gzip -f>$path" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
129 $fh = new IO::File $path, "w" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
138 # Martin A. Hansen, February 2006.
140 # append opens file and returns a filehandle
142 my ( $path, # path to file
149 $fh = new IO::File $path, "a" or Maasha::Common::error( qq(Could not append-open file "$path": $!) );
157 # Martin A. Hansen, July 2007.
159 # Returns a filehandle to STDIN
163 $fh = new IO::File "<&STDIN" or Maasha::Common::error( qq(Could not read from STDIN: $!) );
171 # Martin A. Hansen, July 2007.
173 # Returns a filehandle to STDOUT
177 $fh = new IO::File ">&STDOUT" or Maasha::Common::error( qq(Could not write to STDOUT: $!) );
185 # Martin A. Hansen, December 2004.
187 # given a file, a seek beg position and
188 # length, returns the corresponding string.
190 my ( $fh, # file handle to file
191 $beg, # read start in file
192 $len, # read length of block
199 Maasha::Common::error( qq(Negative length: $len) ) if $len < 0;
201 sysseek $fh, $beg, 0;
202 sysread $fh, $string, $len;
210 # Martin A. Hansen, December 2004.
212 # writes a data structure to file.
214 my ( $path, # full path to file
215 $data, # data structure
218 Storable::store( $data, $path ) or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
224 # Martin A. Hansen, December 2004.
226 # retrieves hash data structure
227 # (this routines needs to test if its a hash, array or else)
229 my ( $path, # full path to data file
234 $data = Storable::retrieve( $path ) or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
236 return wantarray ? %{ $data } : $data;
242 # Martin A. Hansen, November 2008.
244 # Copy the content of a file from source path to
247 my ( $src, # source path
248 $dst, # destination path
253 my ( $fh_in, $fh_out, $line );
255 Maasha::Common::error( qq(copy failed: destination equals source "$src") ) if $src eq $dst;
257 $fh_in = file_read_open( $src );
258 $fh_out = file_write_open( $dst );
260 while ( $line = <$fh_in> ) {
271 # Martin A. Hansen, November 2008.
273 # Checks if a given file is gzipped.
274 # Currrently uses a call to the systems
275 # file tool. Returns 1 if gzipped otherwise
278 my ( $path, # path to file
285 $type = `file $path`;
287 if ( $type =~ /gzip compressed/ ) {
297 # Martin A. Hansen, March 2007
299 # returns the file size for a given file
301 my ( $path, # full path to file
306 my $file_size = -s $path;
312 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DIRECTORIES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
317 # Martin A. Hansen, July 2007.
319 # Creates a directory.
321 my ( $path, # full path to dir
327 Maasha::Common::error( qq(Directory already exists "$path": $!) );
329 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
334 sub dir_create_if_not_exists
336 # Martin A. Hansen, May 2008.
338 # Creates a directory if it does not already exists.
340 my ( $path, # full path to dir
345 if ( not -d $path ) {
346 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
353 # Martin A. Hansen, April 2008.
355 # Removes a directory recursively.
357 my ( $path, # directory
360 Maasha::Common::run( "rm", "-rf $path" ) if -d $path;
366 # Martin A. Hansen, June 2007.
368 # returns all dirs in a given directory.
370 my ( $path, # full path to directory
373 # returns a list of filenames.
377 $dh = open_dir( $path );
379 @dirs = read_dir( $dh );
380 @dirs = grep { -d "$path/$_" } @dirs;
382 map { $_ = "$path/$_" } @dirs;
386 return wantarray ? @dirs : \@dirs;
392 # Martin A. Hansen, June 2007.
394 # returns all files in a given directory.
396 my ( $path, # full path to directory
399 # returns a list of filenames.
403 $dh = open_dir( $path );
405 @files = read_dir( $dh );
406 @files = grep { -f "$path/$_" } @files;
408 map { $_ = "$path/$_" } @files;
412 return wantarray ? @files : \@files;
418 # Martin A. Hansen, June 2007.
420 # open a directory and returns a directory handle
424 my ( $path, # full path to directory
431 $dh = IO::Dir->new( $path ) or Maasha::Common::error( qq(Could not open dir "$path": $!) );
439 # Martin A. Hansen, June 2007.
441 # read all files and directories from a directory.
443 my ( $dh, # directory handle object
448 my ( $elem, @elems );
450 while ( defined( $elem = $dh->read ) ) {
454 return wantarray ? @elems : \@elems;
458 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<