Commit | Line | Data |
270d1e39 |
1 | package File::Spec::Unix; |
2 | |
270d1e39 |
3 | use strict; |
270d1e39 |
4 | |
c27914c9 |
5 | use Cwd; |
6 | |
270d1e39 |
7 | =head1 NAME |
8 | |
9 | File::Spec::Unix - methods used by File::Spec |
10 | |
11 | =head1 SYNOPSIS |
12 | |
cbc7acb0 |
13 | require File::Spec::Unix; # Done automatically by File::Spec |
270d1e39 |
14 | |
15 | =head1 DESCRIPTION |
16 | |
17 | Methods for manipulating file specifications. |
18 | |
19 | =head1 METHODS |
20 | |
21 | =over 2 |
22 | |
23 | =item canonpath |
24 | |
25 | No physical check on the filesystem, but a logical cleanup of a |
26 | path. On UNIX eliminated successive slashes and successive "/.". |
27 | |
c27914c9 |
28 | $cpath = File::Spec->canonpath( $path ) ; |
c27914c9 |
29 | |
270d1e39 |
30 | =cut |
31 | |
32 | sub canonpath { |
0994714a |
33 | my ($self,$path) = @_; |
4fabb596 |
34 | $path =~ s|/+|/|g unless($^O eq 'cygwin'); # xx////xx -> xx/xx |
cbc7acb0 |
35 | $path =~ s|(/\.)+/|/|g; # xx/././xx -> xx/xx |
270d1e39 |
36 | $path =~ s|^(\./)+|| unless $path eq "./"; # ./xx -> xx |
c27914c9 |
37 | $path =~ s|^/(\.\./)+|/|; # /../../xx -> xx |
270d1e39 |
38 | $path =~ s|/$|| unless $path eq "/"; # xx/ -> xx |
cbc7acb0 |
39 | return $path; |
270d1e39 |
40 | } |
41 | |
42 | =item catdir |
43 | |
44 | Concatenate two or more directory names to form a complete path ending |
45 | with a directory. But remove the trailing slash from the resulting |
46 | string, because it doesn't look good, isn't necessary and confuses |
47 | OS2. Of course, if this is the root directory, don't cut off the |
48 | trailing slash :-) |
49 | |
50 | =cut |
51 | |
270d1e39 |
52 | sub catdir { |
cbc7acb0 |
53 | my $self = shift; |
270d1e39 |
54 | my @args = @_; |
cbc7acb0 |
55 | foreach (@args) { |
270d1e39 |
56 | # append a slash to each argument unless it has one there |
cbc7acb0 |
57 | $_ .= "/" if $_ eq '' || substr($_,-1) ne "/"; |
270d1e39 |
58 | } |
cbc7acb0 |
59 | return $self->canonpath(join('', @args)); |
270d1e39 |
60 | } |
61 | |
62 | =item catfile |
63 | |
64 | Concatenate one or more directory names and a filename to form a |
65 | complete path ending with a filename |
66 | |
67 | =cut |
68 | |
69 | sub catfile { |
cbc7acb0 |
70 | my $self = shift; |
270d1e39 |
71 | my $file = pop @_; |
72 | return $file unless @_; |
73 | my $dir = $self->catdir(@_); |
cbc7acb0 |
74 | $dir .= "/" unless substr($dir,-1) eq "/"; |
270d1e39 |
75 | return $dir.$file; |
76 | } |
77 | |
78 | =item curdir |
79 | |
cbc7acb0 |
80 | Returns a string representation of the current directory. "." on UNIX. |
270d1e39 |
81 | |
82 | =cut |
83 | |
84 | sub curdir { |
cbc7acb0 |
85 | return "."; |
270d1e39 |
86 | } |
87 | |
99804bbb |
88 | =item devnull |
89 | |
cbc7acb0 |
90 | Returns a string representation of the null device. "/dev/null" on UNIX. |
99804bbb |
91 | |
92 | =cut |
93 | |
94 | sub devnull { |
95 | return "/dev/null"; |
96 | } |
97 | |
270d1e39 |
98 | =item rootdir |
99 | |
cbc7acb0 |
100 | Returns a string representation of the root directory. "/" on UNIX. |
270d1e39 |
101 | |
102 | =cut |
103 | |
104 | sub rootdir { |
105 | return "/"; |
106 | } |
107 | |
cbc7acb0 |
108 | =item tmpdir |
109 | |
110 | Returns a string representation of the first writable directory |
111 | from the following list or "" if none are writable: |
112 | |
113 | $ENV{TMPDIR} |
114 | /tmp |
115 | |
116 | =cut |
117 | |
118 | my $tmpdir; |
119 | sub tmpdir { |
120 | return $tmpdir if defined $tmpdir; |
121 | foreach ($ENV{TMPDIR}, "/tmp") { |
122 | next unless defined && -d && -w _; |
123 | $tmpdir = $_; |
124 | last; |
125 | } |
126 | $tmpdir = '' unless defined $tmpdir; |
127 | return $tmpdir; |
128 | } |
129 | |
270d1e39 |
130 | =item updir |
131 | |
cbc7acb0 |
132 | Returns a string representation of the parent directory. ".." on UNIX. |
270d1e39 |
133 | |
134 | =cut |
135 | |
136 | sub updir { |
137 | return ".."; |
138 | } |
139 | |
140 | =item no_upwards |
141 | |
142 | Given a list of file names, strip out those that refer to a parent |
143 | directory. (Does not strip symlinks, only '.', '..', and equivalents.) |
144 | |
145 | =cut |
146 | |
147 | sub no_upwards { |
cbc7acb0 |
148 | my $self = shift; |
270d1e39 |
149 | return grep(!/^\.{1,2}$/, @_); |
150 | } |
151 | |
152 | =item file_name_is_absolute |
153 | |
154 | Takes as argument a path and returns true, if it is an absolute path. |
155 | |
156 | =cut |
157 | |
158 | sub file_name_is_absolute { |
cbc7acb0 |
159 | my ($self,$file) = @_; |
160 | return scalar($file =~ m:^/:); |
270d1e39 |
161 | } |
162 | |
163 | =item path |
164 | |
165 | Takes no argument, returns the environment variable PATH as an array. |
166 | |
167 | =cut |
168 | |
169 | sub path { |
cbc7acb0 |
170 | my @path = split(':', $ENV{PATH}); |
171 | foreach (@path) { $_ = '.' if $_ eq '' } |
172 | return @path; |
270d1e39 |
173 | } |
174 | |
175 | =item join |
176 | |
177 | join is the same as catfile. |
178 | |
179 | =cut |
180 | |
181 | sub join { |
cbc7acb0 |
182 | my $self = shift; |
183 | return $self->catfile(@_); |
270d1e39 |
184 | } |
185 | |
c27914c9 |
186 | =item splitpath |
187 | |
188 | ($volume,$directories,$file) = File::Spec->splitpath( $path ); |
189 | ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file ); |
190 | |
191 | Splits a path in to volume, directory, and filename portions. On systems |
192 | with no concept of volume, returns undef for volume. |
193 | |
194 | For systems with no syntax differentiating filenames from directories, |
195 | assumes that the last file is a path unless $no_file is true or a |
196 | trailing separator or /. or /.. is present. On Unix this means that $no_file |
197 | true makes this return ( '', $path, '' ). |
198 | |
199 | The directory portion may or may not be returned with a trailing '/'. |
200 | |
201 | The results can be passed to L</catpath()> to get back a path equivalent to |
202 | (usually identical to) the original path. |
203 | |
204 | =cut |
205 | |
206 | sub splitpath { |
207 | my ($self,$path, $nofile) = @_; |
208 | |
209 | my ($volume,$directory,$file) = ('','',''); |
210 | |
211 | if ( $nofile ) { |
212 | $directory = $path; |
213 | } |
214 | else { |
215 | $path =~ m|^ ( (?: .* / (?: \.\.?$ )? )? ) ([^/]*) |x; |
216 | $directory = $1; |
217 | $file = $2; |
218 | } |
219 | |
220 | return ($volume,$directory,$file); |
221 | } |
222 | |
223 | |
224 | =item splitdir |
225 | |
226 | The opposite of L</catdir()>. |
227 | |
228 | @dirs = File::Spec->splitdir( $directories ); |
229 | |
230 | $directories must be only the directory portion of the path on systems |
231 | that have the concept of a volume or that have path syntax that differentiates |
232 | files from directories. |
233 | |
234 | Unlike just splitting the directories on the separator, leading empty and |
235 | trailing directory entries can be returned, because these are significant |
236 | on some OSs. So, |
237 | |
238 | File::Spec->splitdir( "/a/b/c" ); |
239 | |
240 | Yields: |
241 | |
242 | ( '', 'a', 'b', '', 'c', '' ) |
243 | |
244 | =cut |
245 | |
246 | sub splitdir { |
247 | my ($self,$directories) = @_ ; |
248 | # |
249 | # split() likes to forget about trailing null fields, so here we |
250 | # check to be sure that there will not be any before handling the |
251 | # simple case. |
252 | # |
253 | if ( $directories !~ m|/$| ) { |
254 | return split( m|/|, $directories ); |
255 | } |
256 | else { |
257 | # |
258 | # since there was a trailing separator, add a file name to the end, |
259 | # then do the split, then replace it with ''. |
260 | # |
261 | my( @directories )= split( m|/|, "${directories}dummy" ) ; |
262 | $directories[ $#directories ]= '' ; |
263 | return @directories ; |
264 | } |
265 | } |
266 | |
267 | |
268 | =item catpath |
269 | |
270 | Takes volume, directory and file portions and returns an entire path. Under |
0994714a |
271 | Unix, $volume is ignored, and directory and file are catenated. A '/' is |
272 | inserted if need be. On other OSs, $volume is significant. |
c27914c9 |
273 | |
274 | =cut |
275 | |
276 | sub catpath { |
277 | my ($self,$volume,$directory,$file) = @_; |
278 | |
279 | if ( $directory ne '' && |
280 | $file ne '' && |
281 | substr( $directory, -1 ) ne '/' && |
282 | substr( $file, 0, 1 ) ne '/' |
283 | ) { |
284 | $directory .= "/$file" ; |
285 | } |
286 | else { |
287 | $directory .= $file ; |
288 | } |
289 | |
290 | return $directory ; |
291 | } |
292 | |
293 | =item abs2rel |
294 | |
295 | Takes a destination path and an optional base path returns a relative path |
296 | from the base path to the destination path: |
297 | |
298 | $rel_path = File::Spec->abs2rel( $destination ) ; |
299 | $rel_path = File::Spec->abs2rel( $destination, $base ) ; |
300 | |
301 | If $base is not present or '', then L<cwd()> is used. If $base is relative, |
302 | then it is converted to absolute form using L</rel2abs()>. This means that it |
303 | is taken to be relative to L<cwd()>. |
304 | |
305 | On systems with the concept of a volume, this assumes that both paths |
306 | are on the $destination volume, and ignores the $base volume. |
307 | |
308 | On systems that have a grammar that indicates filenames, this ignores the |
309 | $base filename as well. Otherwise all path components are assumed to be |
310 | directories. |
311 | |
312 | If $path is relative, it is converted to absolute form using L</rel2abs()>. |
313 | This means that it is taken to be relative to L<cwd()>. |
314 | |
315 | Based on code written by Shigio Yamaguchi. |
316 | |
317 | No checks against the filesystem are made. |
318 | |
319 | =cut |
320 | |
321 | sub abs2rel { |
322 | my($self,$path,$base) = @_; |
323 | |
324 | # Clean up $path |
325 | if ( ! $self->file_name_is_absolute( $path ) ) { |
326 | $path = $self->rel2abs( $path ) ; |
327 | } |
328 | else { |
329 | $path = $self->canonpath( $path ) ; |
330 | } |
331 | |
332 | # Figure out the effective $base and clean it up. |
333 | if ( !defined( $base ) || $base eq '' ) { |
334 | $base = cwd() ; |
335 | } |
336 | elsif ( ! $self->file_name_is_absolute( $base ) ) { |
337 | $base = $self->rel2abs( $base ) ; |
338 | } |
339 | else { |
340 | $base = $self->canonpath( $base ) ; |
341 | } |
342 | |
343 | # Now, remove all leading components that are the same |
344 | my @pathchunks = $self->splitdir( $path); |
345 | my @basechunks = $self->splitdir( $base); |
346 | |
347 | while (@pathchunks && @basechunks && $pathchunks[0] eq $basechunks[0]) { |
348 | shift @pathchunks ; |
349 | shift @basechunks ; |
350 | } |
351 | |
352 | $path = CORE::join( '/', @pathchunks ); |
353 | $base = CORE::join( '/', @basechunks ); |
354 | |
355 | # $base now contains the directories the resulting relative path |
356 | # must ascend out of before it can descend to $path_directory. So, |
357 | # replace all names with $parentDir |
358 | $base =~ s|[^/]+|..|g ; |
359 | |
360 | # Glue the two together, using a separator if necessary, and preventing an |
361 | # empty result. |
362 | if ( $path ne '' && $base ne '' ) { |
363 | $path = "$base/$path" ; |
364 | } else { |
365 | $path = "$base$path" ; |
366 | } |
367 | |
368 | return $self->canonpath( $path ) ; |
369 | } |
370 | |
371 | =item rel2abs |
372 | |
373 | Converts a relative path to an absolute path. |
374 | |
375 | $abs_path = $File::Spec->rel2abs( $destination ) ; |
376 | $abs_path = $File::Spec->rel2abs( $destination, $base ) ; |
377 | |
378 | If $base is not present or '', then L<cwd()> is used. If $base is relative, |
379 | then it is converted to absolute form using L</rel2abs()>. This means that it |
380 | is taken to be relative to L<cwd()>. |
381 | |
382 | On systems with the concept of a volume, this assumes that both paths |
383 | are on the $base volume, and ignores the $destination volume. |
384 | |
385 | On systems that have a grammar that indicates filenames, this ignores the |
386 | $base filename as well. Otherwise all path components are assumed to be |
387 | directories. |
388 | |
389 | If $path is absolute, it is cleaned up and returned using L</canonpath()>. |
390 | |
391 | Based on code written by Shigio Yamaguchi. |
392 | |
393 | No checks against the filesystem are made. |
394 | |
395 | =cut |
396 | |
397 | sub rel2abs($;$;) { |
398 | my ($self,$path,$base ) = @_; |
399 | |
400 | # Clean up $path |
401 | if ( ! $self->file_name_is_absolute( $path ) ) { |
402 | # Figure out the effective $base and clean it up. |
403 | if ( !defined( $base ) || $base eq '' ) { |
404 | $base = cwd() ; |
405 | } |
406 | elsif ( ! $self->file_name_is_absolute( $base ) ) { |
407 | $base = $self->rel2abs( $base ) ; |
408 | } |
409 | else { |
410 | $base = $self->canonpath( $base ) ; |
411 | } |
412 | |
413 | # Glom them together |
414 | $path = $self->catdir( $base, $path ) ; |
415 | } |
416 | |
417 | return $self->canonpath( $path ) ; |
418 | } |
419 | |
420 | |
270d1e39 |
421 | =back |
422 | |
423 | =head1 SEE ALSO |
424 | |
425 | L<File::Spec> |
426 | |
427 | =cut |
428 | |
429 | 1; |