]>
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: 2005-08-01
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).
33 use base
qw(Exporter);
47 # Check if flock() is available
48 # I found this piece of code somewhere in the internet
50 $has_flock = eval { local $SIG{'__DIE__'}; flock(STDOUT
,0); 1 };
52 # Predeclaration of dir_copy()
60 # Params: 1. Directory to copy
63 # Return: Status code (Boolean)
67 my ($dir,$target) = @_;
69 return unless(-d
$dir);
71 my $entries = dir_read
($dir) or return;
73 my $dirs = $entries->{'dirs'};
74 my $files = $entries->{'files'};
76 mkdir($target,0777) unless(-d
$target);
78 foreach my $directory(@
$dirs)
80 unless(-d
$target.'/'.$directory)
82 mkdir($target.'/'.$directory,0777) or next;
85 if(-r
$target.'/'.$directory && -x
$target.'/'.$directory)
87 dir_copy
($dir.'/'.$directory,$target.'/'.$directory) or next;
91 foreach my $file(@
$files)
93 copy
($dir.'/'.$file,$target.'/'.$file) or next;
101 # Collect the files and directories in a directory
105 # Return: Hash reference: dirs => directories
106 # files => files and symbolic links
113 return unless(-d
$dir);
115 # Get all the entries in the directory
117 opendir(DIR
,$dir) or return;
118 my @entries = readdir(DIR
);
119 closedir(DIR
) or return;
123 @entries = sort {uc($a) cmp uc($b)} @entries;
128 foreach my $entry(@entries)
130 next if($entry eq '.' || $entry eq '..');
132 if(-d
$dir.'/'.$entry && not -l
$dir.'/'.$entry)
142 return {dirs
=> \
@dirs, files
=> \
@files};
147 # Create a file, but only if it doesn't already exist
149 # (I wanted to use O_EXCL for this, but `perldoc -f sysopen`
150 # doesn't say that it is available on every system - so I
151 # created this workaround using O_RDONLY and O_CREAT)
153 # Params: File to create
155 # Return: true on success;
156 # false on error or if the file already exists
165 sysopen(FILE
,$file,O_RDONLY
| O_CREAT
) or return;
166 close(FILE
) or return;
173 # System independent wrapper function for flock()
174 # On systems where flock() is not available, this function
175 # always returns true.
177 # Params: 1. Filehandle
180 # Return: Status code (Boolean)
184 my ($handle,$mode) = @_;
186 return 1 unless($has_flock);
187 return flock($handle,$mode);
192 # Read out a file completely
195 # 2. true => open in binary mode
196 # false => open in normal mode (default)
198 # Return: Contents of the file (Scalar Reference)
202 my ($file,$binary) = @_;
205 sysopen(FILE
,$file,O_RDONLY
) or return;
206 file_lock
(FILE
,LOCK_SH
) or do { close(FILE
); return };
207 binmode(FILE
) if($binary);
209 read(FILE
, my $content, -s
$file);
211 close(FILE
) or return;
221 # 2. File content as Scalar Reference
222 # 3. true => open in binary mode
223 # false => open in normal mode (default)
225 # Return: Status code (Boolean)
229 my ($file,$content,$binary) = @_;
232 sysopen(FILE
,$file,O_WRONLY
| O_CREAT
| O_TRUNC
) or return;
233 file_lock
(FILE
,LOCK_EX
) or do { close(FILE
); return };
234 binmode(FILE
) if($binary);
236 print FILE
$$content or do { close(FILE
); return };
238 close(FILE
) or return;
243 # it's true, baby ;-)
patrick-canterino.de