]>
git.p6c8.net - devedit.git/blob - modules/File/Access.pm
248eb35ee852473a87b8286058e8a7f687794568
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
25 use base
qw(Exporter);
39 # Check if flock() is available
40 # I found this piece of code somewhere in the internet
42 $has_flock = eval { local $SIG{'__DIE__'}; flock(STDOUT
,0); 1 };
44 # Predeclaration of dir_copy()
52 # Params: 1. Directory to copy
55 # Return: Status code (Boolean)
59 my ($dir,$target) = @_;
61 return unless(-d
$dir);
63 my $entries = dir_read
($dir) or return;
65 my $dirs = $entries->{'dirs'};
66 my $files = $entries->{'files'};
68 mkdir($target,0777) unless(-d
$target);
70 foreach my $directory(@
$dirs)
72 unless(-d
$target.'/'.$directory)
74 mkdir($target.'/'.$directory,0777) or next;
77 if(-r
$target.'/'.$directory && -x
$target.'/'.$directory)
79 dir_copy
($dir.'/'.$directory,$target.'/'.$directory) or next;
83 foreach my $file(@
$files)
85 copy
($dir.'/'.$file,$target.'/'.$file) or next;
93 # Collect the files and directories in a directory
97 # Return: Hash reference: dirs => directories
98 # files => files and symbolic links
105 return unless(-d
$dir);
107 # Get all the entries in the directory
109 opendir(DIR
,$dir) or return;
110 my @entries = readdir(DIR
);
111 closedir(DIR
) or return;
115 @entries = sort {uc($a) cmp uc($b)} @entries;
120 foreach my $entry(@entries)
122 next if($entry eq '.' || $entry eq '..');
124 if(-d
$dir.'/'.$entry && not -l
$dir.'/'.$entry)
134 return {dirs
=> \
@dirs, files
=> \
@files};
139 # Create a file, but only if it doesn't already exist
141 # (I wanted to use O_EXCL for this, but `perldoc -f sysopen`
142 # doesn't say that it is available on every system - so I
143 # created this workaround using O_RDONLY and O_CREAT)
145 # Params: File to create
147 # Return: true on success;
148 # false on error or if the file already exists
157 sysopen(FILE
,$file,O_RDONLY
| O_CREAT
) or return;
158 close(FILE
) or return;
165 # System independent wrapper function for flock()
166 # On systems where flock() is not available, this function
167 # always returns true.
169 # Params: 1. Filehandle
172 # Return: Status code (Boolean)
176 my ($handle,$mode) = @_;
178 return 1 unless($has_flock);
179 return flock($handle,$mode);
184 # Read out a file completely
187 # 2. true => open in binary mode
188 # false => open in normal mode (default)
190 # Return: Contents of the file (Scalar Reference)
194 my ($file,$binary) = @_;
197 sysopen(FILE
,$file,O_RDONLY
) or return;
198 file_lock
(FILE
,LOCK_SH
) or do { close(FILE
); return };
199 binmode(FILE
) if($binary);
201 read(FILE
, my $content, -s
$file);
203 close(FILE
) or return;
213 # 2. File content as Scalar Reference
214 # 3. true => open in binary mode
215 # false => open in normal mode (default)
217 # Return: Status code (Boolean)
221 my ($file,$content,$binary) = @_;
224 sysopen(FILE
,$file,O_WRONLY
| O_CREAT
| O_TRUNC
) or return;
225 file_lock
(FILE
,LOCK_EX
) or do { close(FILE
); return };
226 binmode(FILE
) if($binary);
228 print FILE
$$content or do { close(FILE
); return };
230 close(FILE
) or return;
235 # it's true, baby ;-)
patrick-canterino.de