]> git.p6c8.net - devedit.git/blob - modules/File/Access.pm
Typo
[devedit.git] / modules / File / Access.pm
1 package File::Access;
2
3 #
4 # Dev-Editor - Module File::Access
5 #
6 # Some simple routines for doing things with files by
7 # using only one command
8 #
9 # Author: Patrick Canterino <patrick@patshaping.de>
10 # Last modified: 2005-02-16
11 #
12
13 use strict;
14
15 use vars qw(@EXPORT
16 $has_flock);
17
18 use Fcntl qw(:DEFAULT
19 :flock);
20
21 ### Export ###
22
23 use base qw(Exporter);
24
25 @EXPORT = qw(dir_read
26 file_create
27 file_lock
28 file_read
29 file_save
30 file_unlock
31
32 LOCK_SH
33 LOCK_EX
34 LOCK_UN
35 LOCK_NB);
36
37 # Check if flock() is available
38 # I found this piece of code somewhere in the internet
39
40 $has_flock = eval { local $SIG{'__DIE__'}; flock(STDOUT,0); 1 };
41
42 # dir_read()
43 #
44 # Collect the files and directories in a directory
45 #
46 # Params: Directory
47 #
48 # Return: Hash reference: dirs => directories
49 # files => files and symbolic links
50
51 sub dir_read($)
52 {
53 my $dir = shift;
54 local *DIR;
55
56 return unless(-d $dir);
57
58 # Get all the entries in the directory
59
60 opendir(DIR,$dir) or return;
61 my @entries = readdir(DIR);
62 closedir(DIR) or return;
63
64 # Sort the entries
65
66 @entries = sort {uc($a) cmp uc($b)} @entries;
67
68 my @files;
69 my @dirs;
70
71 foreach my $entry(@entries)
72 {
73 next if($entry eq '.' || $entry eq '..');
74
75 if(-d $dir.'/'.$entry && not -l $dir.'/'.$entry)
76 {
77 push(@dirs,$entry);
78 }
79 else
80 {
81 push(@files,$entry);
82 }
83 }
84
85 return {dirs => \@dirs, files => \@files};
86 }
87
88 # file_create()
89 #
90 # Create a file, but only if it doesn't already exist
91 #
92 # (I wanted to use O_EXCL for this, but `perldoc -f sysopen`
93 # doesn't say that it is available on every system - so I
94 # created this workaround using O_RDONLY and O_CREAT)
95 #
96 # Params: File to create
97 #
98 # Return: true on success;
99 # false on error or if the file already exists
100
101 sub file_create($)
102 {
103 my $file = shift;
104 local *FILE;
105
106 return if(-e $file);
107
108 sysopen(FILE,$file,O_RDONLY | O_CREAT) or return;
109 close(FILE) or return;
110
111 return 1;
112 }
113
114 # file_lock()
115 #
116 # System independent wrapper function for flock()
117 # On systems where flock() is not available, this function
118 # always returns true.
119 #
120 # Params: 1. Filehandle
121 # 2. Locking mode
122 #
123 # Return: Status code (Boolean)
124
125 sub file_lock(*$)
126 {
127 my ($handle,$mode) = @_;
128
129 return 1 unless($has_flock);
130 return flock($handle,$mode);
131 }
132
133 # file_read()
134 #
135 # Read out a file completely
136 #
137 # Params: 1. File
138 # 2. true => open in binary mode
139 # false => open in normal mode (default)
140 #
141 # Return: Contents of the file (Scalar Reference)
142
143 sub file_read($;$)
144 {
145 my ($file,$binary) = @_;
146 local *FILE;
147
148 sysopen(FILE,$file,O_RDONLY) or return;
149 file_lock(FILE,LOCK_SH) or do { close(FILE); return };
150 binmode(FILE) if($binary);
151
152 read(FILE, my $content, -s $file);
153
154 close(FILE) or return;
155
156 return \$content;
157 }
158
159 # file_save()
160 #
161 # Save a file
162 #
163 # Params: 1. File
164 # 2. File content as Scalar Reference
165 # 3. true => open in binary mode
166 # false => open in normal mode (default)
167 #
168 # Return: Status code (Boolean)
169
170 sub file_save($$;$)
171 {
172 my ($file,$content,$binary) = @_;
173 local *FILE;
174
175 sysopen(FILE,$file,O_WRONLY | O_CREAT | O_TRUNC) or return;
176 file_lock(FILE,LOCK_EX) or do { close(FILE); return };
177 binmode(FILE) if($binary);
178
179 print FILE $$content or do { close(FILE); return };
180
181 close(FILE) or return;
182
183 return 1;
184 }
185
186 # file_unlock()
187 #
188 # Remove a file from the list of files in use
189 #
190 # Params: 1. File::UseList object
191 # 2. File to remove
192 #
193 # Return: Status code (Boolean)
194
195 sub file_unlock($$)
196 {
197 my ($uselist,$file) = @_;
198
199 $uselist->remove_file($file) or return;
200 $uselist->save or return;
201
202 return 1;
203 }
204
205 # it's true, baby ;-)
206
207 1;
208
209 #
210 ### End ###

patrick-canterino.de