]>
git.p6c8.net - devedit.git/blob - modules/File/Access.pm
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: 2010-12-27
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).
25 $has_archive_extract);
34 use base
qw(Exporter);
36 @EXPORT = qw(archive_unpack
49 # Check if flock() is available
50 # I found this piece of code somewhere in the internet
52 $has_flock = eval { local $SIG{'__DIE__'}; flock(STDOUT
,0); 1 };
54 # Check if Archive::Extract is available
56 $has_archive_extract = eval { local $SIG{'__DIE__'}; require Archive
::Extract
; 1 };
58 # Predeclaration of dir_copy()
65 # (archive type must be supported by Archive::Extract)
67 # Params: 1. Archive path
68 # 2. Path to extract (optional)
70 # Return: - Status code (Boolean)
71 # - undef if Archive::Extract is not available
73 sub archive_unpack
($;$)
75 my ($archive,$path) = @_;
77 return undef unless($has_archive_extract);
79 return unless(-f
$archive);
80 return if($path && not -d
$path);
82 my $ae = Archive
::Extract
->new(archive
=> $archive);
87 return $ae->extract(to
=> $path);
99 # Params: 1. Directory to copy
102 # Return: Status code (Boolean)
106 my ($dir,$target) = @_;
108 return unless(-d
$dir);
110 my $entries = dir_read
($dir) or return;
112 my $dirs = $entries->{'dirs'};
113 my $files = $entries->{'files'};
115 mkdir($target,0777) unless(-d
$target);
117 foreach my $directory(@
$dirs)
119 unless(-d
$target.'/'.$directory)
121 mkdir($target.'/'.$directory,0777) or next;
124 if(-r
$target.'/'.$directory && -x
$target.'/'.$directory)
126 dir_copy
($dir.'/'.$directory,$target.'/'.$directory) or next;
130 foreach my $file(@
$files)
132 copy
($dir.'/'.$file,$target.'/'.$file) or next;
140 # Collect the files and directories in a directory
144 # Return: Hash reference: dirs => directories
145 # files => files and symbolic links
152 return unless(-d
$dir);
154 # Get all the entries in the directory
156 opendir(DIR
,$dir) or return;
157 my @entries = readdir(DIR
);
158 closedir(DIR
) or return;
162 @entries = sort {uc($a) cmp uc($b)} @entries;
167 foreach my $entry(@entries)
169 next if($entry eq '.' || $entry eq '..');
171 if(-d
$dir.'/'.$entry && not -l
$dir.'/'.$entry)
181 return {dirs
=> \
@dirs, files
=> \
@files};
186 # Create a file, but only if it doesn't already exist
188 # (I wanted to use O_EXCL for this, but `perldoc -f sysopen`
189 # doesn't say that it is available on every system - so I
190 # created this workaround using O_RDONLY and O_CREAT)
192 # Params: File to create
194 # Return: true on success;
195 # false on error or if the file already exists
204 sysopen(FILE
,$file,O_RDONLY
| O_CREAT
) or return;
205 close(FILE
) or return;
212 # System independent wrapper function for flock()
213 # On systems where flock() is not available, this function
214 # always returns true.
216 # Params: 1. Filehandle
219 # Return: Status code (Boolean)
223 my ($handle,$mode) = @_;
225 return 1 unless($has_flock);
226 return flock($handle,$mode);
231 # Read out a file completely
234 # 2. true => open in binary mode
235 # false => open in normal mode (default)
237 # Return: Contents of the file (Scalar Reference)
241 my ($file,$binary) = @_;
244 sysopen(FILE
,$file,O_RDONLY
) or return;
245 file_lock
(FILE
,LOCK_SH
) or do { close(FILE
); return };
246 binmode(FILE
) if($binary);
248 read(FILE
, my $content, -s
$file);
250 close(FILE
) or return;
260 # 2. File content as Scalar Reference
261 # 3. true => open in binary mode
262 # false => open in normal mode (default)
264 # Return: Status code (Boolean)
268 my ($file,$content,$binary) = @_;
271 sysopen(FILE
,$file,O_WRONLY
| O_CREAT
| O_TRUNC
) or return;
272 file_lock
(FILE
,LOCK_EX
) or do { close(FILE
); return };
273 binmode(FILE
) if($binary);
275 print FILE
$$content or do { close(FILE
); return };
277 close(FILE
) or return;
282 # it's true, baby ;-)
patrick-canterino.de