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
324 # Returns created path.
327 Maasha::Common::error( qq(Directory already exists "$path") );
329 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
336 sub dir_create_if_not_exists
338 # Martin A. Hansen, May 2008.
340 # Creates a directory if it does not already exists.
342 my ( $path, # full path to dir
347 if ( not -d $path ) {
348 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
357 # Martin A. Hansen, April 2008.
359 # Removes a directory recursively.
361 my ( $path, # directory
364 Maasha::Common::run( "rm", "-rf $path" ) if -d $path;
370 # Martin A. Hansen, June 2007.
372 # returns all dirs in a given directory.
374 my ( $path, # full path to directory
377 # returns a list of filenames.
381 $dh = open_dir( $path );
383 @dirs = read_dir( $dh );
384 @dirs = grep { -d "$path/$_" } @dirs;
386 map { $_ = "$path/$_" } @dirs;
390 return wantarray ? @dirs : \@dirs;
396 # Martin A. Hansen, June 2007.
398 # returns all files in a given directory.
400 my ( $path, # full path to directory
403 # returns a list of filenames.
407 $dh = open_dir( $path );
409 @files = read_dir( $dh );
410 @files = grep { -f "$path/$_" } @files;
412 map { $_ = "$path/$_" } @files;
416 return wantarray ? @files : \@files;
422 # Martin A. Hansen, June 2007.
424 # open a directory and returns a directory handle
428 my ( $path, # full path to directory
435 $dh = IO::Dir->new( $path ) or Maasha::Common::error( qq(Could not open dir "$path": $!) );
443 # Martin A. Hansen, June 2007.
445 # read all files and directories from a directory.
447 my ( $dh, # directory handle object
452 my ( $elem, @elems );
454 while ( defined( $elem = $dh->read ) ) {
458 return wantarray ? @elems : \@elems;
462 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<