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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39 use vars qw( @ISA @EXPORT @EXPORT_OK );
41 @ISA = qw( Exporter ) ;
44 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FILES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
49 # Martin A. Hansen, January 2004.
51 # Read opens a file that may be gzipped and returns a filehandle.
53 my ( $path, # full path to file
60 if ( is_gzipped( $path ) ) {
61 $fh = new IO::File "zcat $path|" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
63 $fh = new IO::File $path, "r" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
72 # Martin A. Hansen, May 2009.
74 # Cats a number of files and returns a filehandle.
76 my ( $files, # full path to file
81 my ( $file, $fh, $type, %type_hash, $file_string );
83 foreach $file ( @{ $files } )
85 Maasha::Common::error( qq(No such file: $file) ) if not -f $file;
89 if ( $type =~ /gzip compressed/ ) {
90 $type_hash{ 'gzip' } = 1;
92 $type_hash{ 'ascii' } = 1;
96 Maasha::Common::error( qq(Mixture of zipped and unzipped files) ) if scalar keys %type_hash > 1;
98 $file_string = join " ", @{ $files };
100 if ( $type =~ /gzip compressed/ ) {
101 $fh = new IO::File "zcat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
103 $fh = new IO::File "cat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
112 # Martin A. Hansen, January 2004.
114 # write opens a file and returns a filehandle
116 my ( $path, # full path to file
117 $gzip, # flag if data is to be gzipped - OPRIONAL
125 $fh = new IO::File "|gzip -f>$path" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
127 $fh = new IO::File $path, "w" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
136 # Martin A. Hansen, February 2006.
138 # append opens file and returns a filehandle
140 my ( $path, # path to file
147 $fh = new IO::File $path, "a" or Maasha::Common::error( qq(Could not append-open file "$path": $!) );
155 # Martin A. Hansen, July 2007.
157 # Returns a filehandle to STDIN
161 $fh = new IO::File "<&STDIN" or Maasha::Common::error( qq(Could not read from STDIN: $!) );
169 # Martin A. Hansen, July 2007.
171 # Returns a filehandle to STDOUT
175 $fh = new IO::File ">&STDOUT" or Maasha::Common::error( qq(Could not write to STDOUT: $!) );
183 # Martin A. Hansen, December 2004.
185 # given a file, a seek beg position and
186 # length, returns the corresponding string.
188 my ( $fh, # file handle to file
189 $beg, # read start in file
190 $len, # read length of block
197 Maasha::Common::error( qq(Negative length: $len) ) if $len < 0;
199 sysseek $fh, $beg, 0;
200 sysread $fh, $string, $len;
208 # Martin A. Hansen, December 2004.
210 # writes a data structure to file.
212 my ( $path, # full path to file
213 $data, # data structure
216 Storable::store( $data, $path ) or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
222 # Martin A. Hansen, December 2004.
224 # retrieves hash data structure
225 # (this routines needs to test if its a hash, array or else)
227 my ( $path, # full path to data file
232 $data = Storable::retrieve( $path ) or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
234 return wantarray ? %{ $data } : $data;
240 # Martin A. Hansen, November 2008.
242 # Copy the content of a file from source path to
245 my ( $src, # source path
246 $dst, # destination path
251 my ( $fh_in, $fh_out, $line );
253 Maasha::Common::error( qq(copy failed: destination equals source "$src") ) if $src eq $dst;
255 $fh_in = file_read_open( $src );
256 $fh_out = file_write_open( $dst );
258 while ( $line = <$fh_in> ) {
269 # Martin A. Hansen, November 2008.
271 # Checks if a given file is gzipped.
272 # Currrently uses a call to the systems
273 # file tool. Returns 1 if gzipped otherwise
276 my ( $path, # path to file
283 $type = `file $path`;
285 if ( $type =~ /gzip compressed/ ) {
295 # Martin A. Hansen, March 2007
297 # returns the file size for a given file
299 my ( $path, # full path to file
304 my $file_size = ( stat ( $path ) )[ 7 ];
310 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DIRECTORIES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
315 # Martin A. Hansen, July 2007.
317 # Creates a directory.
319 my ( $path, # full path to dir
325 Maasha::Common::error( qq(Directory already exists "$path": $!) );
327 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
332 sub dir_create_if_not_exists
334 # Martin A. Hansen, May 2008.
336 # Creates a directory if it does not already exists.
338 my ( $path, # full path to dir
343 if ( not -d $path ) {
344 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
351 # Martin A. Hansen, April 2008.
353 # Removes a directory recursively.
355 my ( $path, # directory
358 Maasha::Common::run( "rm", "-rf $path" ) if -d $path;
364 # Martin A. Hansen, June 2007.
366 # returns all dirs in a given directory.
368 my ( $path, # full path to directory
371 # returns a list of filenames.
375 $dh = open_dir( $path );
377 @dirs = read_dir( $dh );
378 @dirs = grep { -d "$path/$_" } @dirs;
380 map { $_ = "$path/$_" } @dirs;
384 return wantarray ? @dirs : \@dirs;
390 # Martin A. Hansen, June 2007.
392 # returns all files in a given directory.
394 my ( $path, # full path to directory
397 # returns a list of filenames.
401 $dh = open_dir( $path );
403 @files = read_dir( $dh );
404 @files = grep { -f "$path/$_" } @files;
406 map { $_ = "$path/$_" } @files;
410 return wantarray ? @files : \@files;
416 # Martin A. Hansen, June 2007.
418 # open a directory and returns a directory handle
422 my ( $path, # full path to directory
429 $dh = IO::Dir->new( $path ) or Maasha::Common::error( qq(Could not open dir "$path": $!) );
437 # Martin A. Hansen, June 2007.
439 # read all files and directories from a directory.
441 my ( $dh, # directory handle object
446 my ( $elem, @elems );
448 while ( defined( $elem = $dh->read ) ) {
452 return wantarray ? @elems : \@elems;
456 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<