]>
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-07-05
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 };
48 # Params: 1. Directory to copy
51 # Return: Status code (Boolean)
55 my ($dir,$target) = @_;
57 return unless(-d
$dir);
59 my $entries = dir_read
($dir) or return;
61 my $dirs = $entries->{'dirs'};
62 my $files = $entries->{'files'};
64 mkdir($target,0777) unless(-d
$target);
66 foreach my $directory(@
$dirs)
68 unless(-d
$target.'/'.$directory)
70 mkdir($target.'/'.$directory,0777) or next;
73 if(-r
$target.'/'.$directory && -x
$target.'/'.$directory)
75 dir_copy
($dir.'/'.$directory,$target.'/'.$directory) or next;
79 foreach my $file(@
$files)
81 copy
($dir.'/'.$file,$target.'/'.$file) or next;
89 # Collect the files and directories in a directory
93 # Return: Hash reference: dirs => directories
94 # files => files and symbolic links
101 return unless(-d
$dir);
103 # Get all the entries in the directory
105 opendir(DIR
,$dir) or return;
106 my @entries = readdir(DIR
);
107 closedir(DIR
) or return;
111 @entries = sort {uc($a) cmp uc($b)} @entries;
116 foreach my $entry(@entries)
118 next if($entry eq '.' || $entry eq '..');
120 if(-d
$dir.'/'.$entry && not -l
$dir.'/'.$entry)
130 return {dirs
=> \
@dirs, files
=> \
@files};
135 # Create a file, but only if it doesn't already exist
137 # (I wanted to use O_EXCL for this, but `perldoc -f sysopen`
138 # doesn't say that it is available on every system - so I
139 # created this workaround using O_RDONLY and O_CREAT)
141 # Params: File to create
143 # Return: true on success;
144 # false on error or if the file already exists
153 sysopen(FILE
,$file,O_RDONLY
| O_CREAT
) or return;
154 close(FILE
) or return;
161 # System independent wrapper function for flock()
162 # On systems where flock() is not available, this function
163 # always returns true.
165 # Params: 1. Filehandle
168 # Return: Status code (Boolean)
172 my ($handle,$mode) = @_;
174 return 1 unless($has_flock);
175 return flock($handle,$mode);
180 # Read out a file completely
183 # 2. true => open in binary mode
184 # false => open in normal mode (default)
186 # Return: Contents of the file (Scalar Reference)
190 my ($file,$binary) = @_;
193 sysopen(FILE
,$file,O_RDONLY
) or return;
194 file_lock
(FILE
,LOCK_SH
) or do { close(FILE
); return };
195 binmode(FILE
) if($binary);
197 read(FILE
, my $content, -s
$file);
199 close(FILE
) or return;
209 # 2. File content as Scalar Reference
210 # 3. true => open in binary mode
211 # false => open in normal mode (default)
213 # Return: Status code (Boolean)
217 my ($file,$content,$binary) = @_;
220 sysopen(FILE
,$file,O_WRONLY
| O_CREAT
| O_TRUNC
) or return;
221 file_lock
(FILE
,LOCK_EX
) or do { close(FILE
); return };
222 binmode(FILE
) if($binary);
224 print FILE
$$content or do { close(FILE
); return };
226 close(FILE
) or return;
231 # it's true, baby ;-)
patrick-canterino.de