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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 use vars qw( @ISA @EXPORT @EXPORT_OK );
40 @ISA = qw( Exporter ) ;
43 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FILES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
48 # Martin A. Hansen, January 2004.
50 # Read opens a file that may be gzipped and returns a filehandle.
52 my ( $path, # full path to file
59 if ( is_gzipped( $path ) ) {
60 $fh = new IO::File "zcat $path|" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
62 $fh = new IO::File $path, "r" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
71 # Martin A. Hansen, May 2009.
73 # Cats a number of files and returns a filehandle.
75 my ( $files, # full path to file
80 my ( $file, $fh, $type, %type_hash, $file_string );
82 foreach $file ( @{ $files } )
84 Maasha::Common::error( qq(No such file: $file) ) if not -f $file;
88 if ( $type =~ /gzip compressed/ ) {
89 $type_hash{ 'gzip' } = 1;
91 $type_hash{ 'ascii' } = 1;
95 Maasha::Common::error( qq(Mixture of zipped and unzipped files) ) if scalar keys %type_hash > 1;
97 $file_string = join " ", @{ $files };
99 if ( $type =~ /gzip compressed/ ) {
100 $fh = new IO::File "zcat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
102 $fh = new IO::File "cat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) );
111 # Martin A. Hansen, January 2004.
113 # write opens a file and returns a filehandle
115 my ( $path, # full path to file
116 $gzip, # flag if data is to be gzipped - OPRIONAL
124 $fh = new IO::File "|gzip -f>$path" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
126 $fh = new IO::File $path, "w" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
135 # Martin A. Hansen, February 2006.
137 # append opens file and returns a filehandle
139 my ( $path, # path to file
146 $fh = new IO::File $path, "a" or Maasha::Common::error( qq(Could not append-open file "$path": $!) );
154 # Martin A. Hansen, July 2007.
156 # Returns a filehandle to STDIN
160 $fh = new IO::File "<&STDIN" or Maasha::Common::error( qq(Could not read from STDIN: $!) );
168 # Martin A. Hansen, July 2007.
170 # Returns a filehandle to STDOUT
174 $fh = new IO::File ">&STDOUT" or Maasha::Common::error( qq(Could not write to STDOUT: $!) );
182 # Martin A. Hansen, December 2004.
184 # given a file, a seek beg position and
185 # length, returns the corresponding string.
187 my ( $fh, # file handle to file
188 $beg, # read start in file
189 $len, # read length of block
196 Maasha::Common::error( qq(Negative length: $len) ) if $len < 0;
198 sysseek $fh, $beg, 0;
199 sysread $fh, $string, $len;
207 # Martin A. Hansen, November 2008.
209 # Copy the content of a file from source path to
212 my ( $src, # source path
213 $dst, # destination path
218 my ( $fh_in, $fh_out, $line );
220 Maasha::Common::error( qq(copy failed: destination equals source "$src") ) if $src eq $dst;
222 $fh_in = file_read_open( $src );
223 $fh_out = file_write_open( $dst );
225 while ( $line = <$fh_in> ) {
236 # Martin A. Hansen, November 2008.
238 # Checks if a given file is gzipped.
239 # Currrently uses a call to the systems
240 # file tool. Returns 1 if gzipped otherwise
243 my ( $path, # path to file
250 $type = `file $path`;
252 if ( $type =~ /gzip compressed/ ) {
262 # Martin A. Hansen, March 2007
264 # returns the file size for a given file
266 my ( $path, # full path to file
271 my $file_size = ( stat ( $path ) )[ 7 ];
277 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DIRECTORIES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
282 # Martin A. Hansen, July 2007.
284 # Creates a directory.
286 my ( $path, # full path to dir
292 Maasha::Common::error( qq(Directory already exists "$path": $!) );
294 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
299 sub dir_create_if_not_exists
301 # Martin A. Hansen, May 2008.
303 # Creates a directory if it does not already exists.
305 my ( $path, # full path to dir
310 if ( not -d $path ) {
311 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
318 # Martin A. Hansen, April 2008.
320 # Removes a directory recursively.
322 my ( $path, # directory
325 Maasha::Common::run( "rm", "-rf $path" ) if -d $path;
331 # Martin A. Hansen, June 2007.
333 # returns all dirs in a given directory.
335 my ( $path, # full path to directory
338 # returns a list of filenames.
342 $dh = open_dir( $path );
344 @dirs = read_dir( $dh );
345 @dirs = grep { -d "$path/$_" } @dirs;
347 map { $_ = "$path/$_" } @dirs;
351 return wantarray ? @dirs : \@dirs;
357 # Martin A. Hansen, June 2007.
359 # returns all files in a given directory.
361 my ( $path, # full path to directory
364 # returns a list of filenames.
368 $dh = open_dir( $path );
370 @files = read_dir( $dh );
371 @files = grep { -f "$path/$_" } @files;
373 map { $_ = "$path/$_" } @files;
377 return wantarray ? @files : \@files;
383 # Martin A. Hansen, June 2007.
385 # open a directory and returns a directory handle
389 my ( $path, # full path to directory
396 $dh = IO::Dir->new( $path ) or Maasha::Common::error( qq(Could not open dir "$path": $!) );
404 # Martin A. Hansen, June 2007.
406 # read all files and directories from a directory.
408 my ( $dh, # directory handle object
413 my ( $elem, @elems );
415 while ( defined( $elem = $dh->read ) ) {
419 return wantarray ? @elems : \@elems;
423 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<