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, November 2008.
116 # Copy the content of a file from source path to
119 my ( $src, # source path
120 $dst, # destination path
125 my ( $fh_in, $fh_out, $line );
127 Maasha::Common::error( qq(copy failed: destination equals source "$src") ) if $src eq $dst;
129 $fh_in = file_read_open( $src );
130 $fh_out = file_write_open( $dst );
132 while ( $line = <$fh_in> ) {
143 # Martin A. Hansen, November 2008.
145 # Checks if a given file is gzipped.
146 # Currrently uses a call to the systems
147 # file tool. Returns 1 if gzipped otherwise
150 my ( $path, # path to file
157 $type = `file $path`;
159 if ( $type =~ /gzip compressed/ ) {
169 # Martin A. Hansen, March 2007
171 # returns the file size for a given file
173 my ( $path, # full path to file
178 my $file_size = ( stat ( $path ) )[ 7 ];
184 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<