]>
git.p6c8.net - devedit.git/blob - modules/File/Access.pm
c6b1d60348df8d651452cad66827dd6f4de91a9e
4 # Dev-Editor - Module File::Access
6 # Some simple routines for doing things with files by
7 # using only one command
9 # Author: Patrick Canterino <patrick@patshaping.de>
10 # Last modified: 2011-01-05
12 # Copyright (C) 1999-2000 Roland Bluethgen, Frank Schoenmann
13 # Copyright (C) 2003-2009 Patrick Canterino
14 # All Rights Reserved.
16 # This file can be distributed and/or modified under the terms of
17 # of the Artistic License 1.0 (see also the LICENSE file found at
18 # the top level of the Dev-Editor distribution).
26 $archive_extract_error);
35 use base
qw(Exporter);
37 @EXPORT = qw(archive_unpack
50 # Check if flock() is available
51 # I found this piece of code somewhere in the internet
53 $has_flock = eval { local $SIG{'__DIE__'}; flock(STDOUT
,0); 1 };
55 # Check if Archive::Extract is available
57 $has_archive_extract = eval { local $SIG{'__DIE__'}; require Archive
::Extract
; 1 };
59 # Predeclaration of dir_copy()
66 # (archive type must be supported by Archive::Extract)
68 # Params: 1. Archive path
69 # 2. Path to extract (optional)
71 # Return: - Status code (Boolean)
72 # - undef if Archive::Extract is not available
74 sub archive_unpack
($;$)
76 my ($archive,$path) = @_;
78 return undef unless($has_archive_extract);
80 return unless(-f
$archive);
81 return if($path && not -d
$path);
83 my $ae = Archive
::Extract
->new(archive
=> $archive);
88 if($ae->extract(to
=> $path))
94 $archive_extract_error = $ae->error;
106 $archive_extract_error = $ae->error;
116 # Params: 1. Directory to copy
119 # Return: Status code (Boolean)
123 my ($dir,$target) = @_;
125 return unless(-d
$dir);
127 my $entries = dir_read
($dir) or return;
129 my $dirs = $entries->{'dirs'};
130 my $files = $entries->{'files'};
132 mkdir($target,0777) unless(-d
$target);
134 foreach my $directory(@
$dirs)
136 unless(-d
$target.'/'.$directory)
138 mkdir($target.'/'.$directory,0777) or next;
141 if(-r
$target.'/'.$directory && -x
$target.'/'.$directory)
143 dir_copy
($dir.'/'.$directory,$target.'/'.$directory) or next;
147 foreach my $file(@
$files)
149 copy
($dir.'/'.$file,$target.'/'.$file) or next;
157 # Collect the files and directories in a directory
161 # Return: Hash reference: dirs => directories
162 # files => files and symbolic links
169 return unless(-d
$dir);
171 # Get all the entries in the directory
173 opendir(DIR
,$dir) or return;
174 my @entries = readdir(DIR
);
175 closedir(DIR
) or return;
179 @entries = sort {uc($a) cmp uc($b)} @entries;
184 foreach my $entry(@entries)
186 next if($entry eq '.' || $entry eq '..');
188 if(-d
$dir.'/'.$entry && not -l
$dir.'/'.$entry)
198 return {dirs
=> \
@dirs, files
=> \
@files};
203 # Create a file, but only if it doesn't already exist
205 # (I wanted to use O_EXCL for this, but `perldoc -f sysopen`
206 # doesn't say that it is available on every system - so I
207 # created this workaround using O_RDONLY and O_CREAT)
209 # Params: File to create
211 # Return: true on success;
212 # false on error or if the file already exists
221 sysopen(FILE
,$file,O_RDONLY
| O_CREAT
) or return;
222 close(FILE
) or return;
229 # System independent wrapper function for flock()
230 # On systems where flock() is not available, this function
231 # always returns true.
233 # Params: 1. Filehandle
236 # Return: Status code (Boolean)
240 my ($handle,$mode) = @_;
242 return 1 unless($has_flock);
243 return flock($handle,$mode);
248 # Read out a file completely
251 # 2. true => open in binary mode
252 # false => open in normal mode (default)
254 # Return: Contents of the file (Scalar Reference)
258 my ($file,$binary) = @_;
261 sysopen(FILE
,$file,O_RDONLY
) or return;
262 file_lock
(FILE
,LOCK_SH
) or do { close(FILE
); return };
263 binmode(FILE
) if($binary);
265 read(FILE
, my $content, -s
$file);
267 close(FILE
) or return;
277 # 2. File content as Scalar Reference
278 # 3. true => open in binary mode
279 # false => open in normal mode (default)
281 # Return: Status code (Boolean)
285 my ($file,$content,$binary) = @_;
288 sysopen(FILE
,$file,O_WRONLY
| O_CREAT
| O_TRUNC
) or return;
289 file_lock
(FILE
,LOCK_EX
) or do { close(FILE
); return };
290 binmode(FILE
) if($binary);
292 print FILE
$$content or do { close(FILE
); return };
294 close(FILE
) or return;
299 # it's true, baby ;-)
patrick-canterino.de