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, January 2004.
73 # write opens a file and returns a filehandle
75 my ( $path, # full path to file
76 $gzip, # flag if data is to be gzipped - OPRIONAL
84 $fh = new IO::File "|gzip -f>$path" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
86 $fh = new IO::File $path, "w" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
95 # Martin A. Hansen, February 2006.
97 # append opens file and returns a filehandle
99 my ( $path, # path to file
106 $fh = new IO::File $path, "a" or Maasha::Common::error( qq(Could not append-open file "$path": $!) );
114 # Martin A. Hansen, July 2007.
116 # Returns a filehandle to STDIN
120 $fh = new IO::File "<&STDIN" or Maasha::Common::error( qq(Could not read from STDIN: $!) );
128 # Martin A. Hansen, July 2007.
130 # Returns a filehandle to STDOUT
134 $fh = new IO::File ">&STDOUT" or Maasha::Common::error( qq(Could not write to STDOUT: $!) );
142 # Martin A. Hansen, December 2004.
144 # given a file, a seek beg position and
145 # length, returns the corresponding string.
147 my ( $fh, # file handle to file
148 $beg, # read start in file
149 $len, # read length of block
156 Maasha::Common::error( qq(Negative length: $len) ) if $len < 0;
158 sysseek $fh, $beg, 0;
159 sysread $fh, $string, $len;
167 # Martin A. Hansen, November 2008.
169 # Copy the content of a file from source path to
172 my ( $src, # source path
173 $dst, # destination path
178 my ( $fh_in, $fh_out, $line );
180 Maasha::Common::error( qq(copy failed: destination equals source "$src") ) if $src eq $dst;
182 $fh_in = file_read_open( $src );
183 $fh_out = file_write_open( $dst );
185 while ( $line = <$fh_in> ) {
196 # Martin A. Hansen, November 2008.
198 # Checks if a given file is gzipped.
199 # Currrently uses a call to the systems
200 # file tool. Returns 1 if gzipped otherwise
203 my ( $path, # path to file
210 $type = `file $path`;
212 if ( $type =~ /gzip compressed/ ) {
222 # Martin A. Hansen, March 2007
224 # returns the file size for a given file
226 my ( $path, # full path to file
231 my $file_size = ( stat ( $path ) )[ 7 ];
237 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DIRECTORIES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
242 # Martin A. Hansen, July 2007.
244 # Creates a directory.
246 my ( $path, # full path to dir
252 Maasha::Common::error( qq(Directory already exists "$path": $!) );
254 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
259 sub dir_create_if_not_exists
261 # Martin A. Hansen, May 2008.
263 # Creates a directory if it does not already exists.
265 my ( $path, # full path to dir
270 if ( not -d $path ) {
271 mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
278 # Martin A. Hansen, April 2008.
280 # Removes a directory recursively.
282 my ( $path, # directory
285 Maasha::Common::run( "rm", "-rf $path" ) if -d $path;
291 # Martin A. Hansen, June 2007.
293 # returns all dirs in a given directory.
295 my ( $path, # full path to directory
298 # returns a list of filenames.
302 $dh = open_dir( $path );
304 @dirs = read_dir( $dh );
305 @dirs = grep { -d "$path/$_" } @dirs;
307 map { $_ = "$path/$_" } @dirs;
311 return wantarray ? @dirs : \@dirs;
317 # Martin A. Hansen, June 2007.
319 # returns all files in a given directory.
321 my ( $path, # full path to directory
324 # returns a list of filenames.
328 $dh = open_dir( $path );
330 @files = read_dir( $dh );
331 @files = grep { -f "$path/$_" } @files;
333 map { $_ = "$path/$_" } @files;
337 return wantarray ? @files : \@files;
343 # Martin A. Hansen, June 2007.
345 # open a directory and returns a directory handle
349 my ( $path, # full path to directory
356 $dh = IO::Dir->new( $path ) or Maasha::Common::error( qq(Could not open dir "$path": $!) );
364 # Martin A. Hansen, June 2007.
366 # read all files and directories from a directory.
368 my ( $dh, # directory handle object
373 my ( $elem, @elems );
375 while ( defined( $elem = $dh->read ) ) {
379 return wantarray ? @elems : \@elems;
383 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<