1Path(3)               User Contributed Perl Documentation              Path(3)
2
3
4

NAME

6       File::Path - Create or remove directory trees
7

VERSION

9       This document describes version 2.09 of File::Path, released
10       2013-01-17.
11

SYNOPSIS

13         use File::Path qw(make_path remove_tree);
14
15         make_path('foo/bar/baz', '/zug/zwang');
16         make_path('foo/bar/baz', '/zug/zwang', {
17             verbose => 1,
18             mode => 0711,
19         });
20
21         remove_tree('foo/bar/baz', '/zug/zwang');
22         remove_tree('foo/bar/baz', '/zug/zwang', {
23             verbose => 1,
24             error  => \my $err_list,
25         });
26
27         # legacy (interface promoted before v2.00)
28         mkpath('/foo/bar/baz');
29         mkpath('/foo/bar/baz', 1, 0711);
30         mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);
31         rmtree('foo/bar/baz', 1, 1);
32         rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1);
33
34         # legacy (interface promoted before v2.06)
35         mkpath('foo/bar/baz', '/zug/zwang', { verbose => 1, mode => 0711 });
36         rmtree('foo/bar/baz', '/zug/zwang', { verbose => 1, mode => 0711 });
37

DESCRIPTION

39       This module provide a convenient way to create directories of arbitrary
40       depth and to delete an entire directory subtree from the filesystem.
41
42       The following functions are provided:
43
44       make_path( $dir1, $dir2, .... )
45       make_path( $dir1, $dir2, ...., \%opts )
46           The "make_path" function creates the given directories if they
47           don't exists before, much like the Unix command "mkdir -p".
48
49           The function accepts a list of directories to be created. Its
50           behaviour may be tuned by an optional hashref appearing as the last
51           parameter on the call.
52
53           The function returns the list of directories actually created
54           during the call; in scalar context the number of directories
55           created.
56
57           The following keys are recognised in the option hash:
58
59           mode => $num
60               The numeric permissions mode to apply to each created directory
61               (defaults to 0777), to be modified by the current "umask". If
62               the directory already exists (and thus does not need to be
63               created), the permissions will not be modified.
64
65               "mask" is recognised as an alias for this parameter.
66
67           verbose => $bool
68               If present, will cause "make_path" to print the name of each
69               directory as it is created. By default nothing is printed.
70
71           error => \$err
72               If present, it should be a reference to a scalar.  This scalar
73               will be made to reference an array, which will be used to store
74               any errors that are encountered.  See the "ERROR HANDLING"
75               section for more information.
76
77               If this parameter is not used, certain error conditions may
78               raise a fatal error that will cause the program will halt,
79               unless trapped in an "eval" block.
80
81           owner => $owner
82           user => $owner
83           uid => $owner
84               If present, will cause any created directory to be owned by
85               $owner.  If the value is numeric, it will be interpreted as a
86               uid, otherwise as username is assumed. An error will be issued
87               if the username cannot be mapped to a uid, or the uid does not
88               exist, or the process lacks the privileges to change ownership.
89
90               Ownwership of directories that already exist will not be
91               changed.
92
93               "user" and "uid" are aliases of "owner".
94
95           group => $group
96               If present, will cause any created directory to be owned by the
97               group $group.  If the value is numeric, it will be interpreted
98               as a gid, otherwise as group name is assumed. An error will be
99               issued if the group name cannot be mapped to a gid, or the gid
100               does not exist, or the process lacks the privileges to change
101               group ownership.
102
103               Group ownwership of directories that already exist will not be
104               changed.
105
106                   make_path '/var/tmp/webcache', {owner=>'nobody', group=>'nogroup'};
107
108       mkpath( $dir )
109       mkpath( $dir, $verbose, $mode )
110       mkpath( [$dir1, $dir2,...], $verbose, $mode )
111       mkpath( $dir1, $dir2,..., \%opt )
112           The mkpath() function provide the legacy interface of make_path()
113           with a different interpretation of the arguments passed.  The
114           behaviour and return value of the function is otherwise identical
115           to make_path().
116
117       remove_tree( $dir1, $dir2, .... )
118       remove_tree( $dir1, $dir2, ...., \%opts )
119           The "remove_tree" function deletes the given directories and any
120           files and subdirectories they might contain, much like the Unix
121           command "rm -r" or "del /s" on Windows.
122
123           The function accepts a list of directories to be removed. Its
124           behaviour may be tuned by an optional hashref appearing as the last
125           parameter on the call.
126
127           The functions returns the number of files successfully deleted.
128
129           The following keys are recognised in the option hash:
130
131           verbose => $bool
132               If present, will cause "remove_tree" to print the name of each
133               file as it is unlinked. By default nothing is printed.
134
135           safe => $bool
136               When set to a true value, will cause "remove_tree" to skip the
137               files for which the process lacks the required privileges
138               needed to delete files, such as delete privileges on VMS. In
139               other words, the code will make no attempt to alter file
140               permissions. Thus, if the process is interrupted, no filesystem
141               object will be left in a more permissive mode.
142
143           keep_root => $bool
144               When set to a true value, will cause all files and
145               subdirectories to be removed, except the initially specified
146               directories. This comes in handy when cleaning out an
147               application's scratch directory.
148
149                 remove_tree( '/tmp', {keep_root => 1} );
150
151           result => \$res
152               If present, it should be a reference to a scalar.  This scalar
153               will be made to reference an array, which will be used to store
154               all files and directories unlinked during the call. If nothing
155               is unlinked, the array will be empty.
156
157                 remove_tree( '/tmp', {result => \my $list} );
158                 print "unlinked $_\n" for @$list;
159
160               This is a useful alternative to the "verbose" key.
161
162           error => \$err
163               If present, it should be a reference to a scalar.  This scalar
164               will be made to reference an array, which will be used to store
165               any errors that are encountered.  See the "ERROR HANDLING"
166               section for more information.
167
168               Removing things is a much more dangerous proposition than
169               creating things. As such, there are certain conditions that
170               "remove_tree" may encounter that are so dangerous that the only
171               sane action left is to kill the program.
172
173               Use "error" to trap all that is reasonable (problems with
174               permissions and the like), and let it die if things get out of
175               hand. This is the safest course of action.
176
177       rmtree( $dir )
178       rmtree( $dir, $verbose, $safe )
179       rmtree( [$dir1, $dir2,...], $verbose, $safe )
180       rmtree( $dir1, $dir2,..., \%opt )
181           The rmtree() function provide the legacy interface of remove_tree()
182           with a different interpretation of the arguments passed. The
183           behaviour and return value of the function is otherwise identical
184           to remove_tree().
185
186   ERROR HANDLING
187       NOTE:
188           The following error handling mechanism is considered experimental
189           and is subject to change pending feedback from users.
190
191       If "make_path" or "remove_tree" encounter an error, a diagnostic
192       message will be printed to "STDERR" via "carp" (for non-fatal errors),
193       or via "croak" (for fatal errors).
194
195       If this behaviour is not desirable, the "error" attribute may be used
196       to hold a reference to a variable, which will be used to store the
197       diagnostics. The variable is made a reference to an array of hash
198       references.  Each hash contain a single key/value pair where the key is
199       the name of the file, and the value is the error message (including the
200       contents of $! when appropriate).  If a general error is encountered
201       the diagnostic key will be empty.
202
203       An example usage looks like:
204
205         remove_tree( 'foo/bar', 'bar/rat', {error => \my $err} );
206         if (@$err) {
207             for my $diag (@$err) {
208                 my ($file, $message) = %$diag;
209                 if ($file eq '') {
210                     print "general error: $message\n";
211                 }
212                 else {
213                     print "problem unlinking $file: $message\n";
214                 }
215             }
216         }
217         else {
218             print "No error encountered\n";
219         }
220
221       Note that if no errors are encountered, $err will reference an empty
222       array.  This means that $err will always end up TRUE; so you need to
223       test @$err to determine if errors occured.
224
225   NOTES
226       "File::Path" blindly exports "mkpath" and "rmtree" into the current
227       namespace. These days, this is considered bad style, but to change it
228       now would break too much code. Nonetheless, you are invited to specify
229       what it is you are expecting to use:
230
231         use File::Path 'rmtree';
232
233       The routines "make_path" and "remove_tree" are not exported by default.
234       You must specify which ones you want to use.
235
236         use File::Path 'remove_tree';
237
238       Note that a side-effect of the above is that "mkpath" and "rmtree" are
239       no longer exported at all. This is due to the way the "Exporter" module
240       works. If you are migrating a codebase to use the new interface, you
241       will have to list everything explicitly. But that's just good practice
242       anyway.
243
244         use File::Path qw(remove_tree rmtree);
245
246       API CHANGES
247
248       The API was changed in the 2.0 branch. For a time, "mkpath" and
249       "rmtree" tried, unsuccessfully, to deal with the two different calling
250       mechanisms. This approach was considered a failure.
251
252       The new semantics are now only available with "make_path" and
253       "remove_tree". The old semantics are only available through "mkpath"
254       and "rmtree". Users are strongly encouraged to upgrade to at least 2.08
255       in order to avoid surprises.
256
257       SECURITY CONSIDERATIONS
258
259       There were race conditions 1.x implementations of File::Path's "rmtree"
260       function (although sometimes patched depending on the OS distribution
261       or platform). The 2.0 version contains code to avoid the problem
262       mentioned in CVE-2002-0435.
263
264       See the following pages for more information:
265
266         http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=286905
267         http://www.nntp.perl.org/group/perl.perl5.porters/2005/01/msg97623.html
268         http://www.debian.org/security/2005/dsa-696
269
270       Additionally, unless the "safe" parameter is set (or the third
271       parameter in the traditional interface is TRUE), should a "remove_tree"
272       be interrupted, files that were originally in read-only mode may now
273       have their permissions set to a read-write (or "delete OK") mode.
274

DIAGNOSTICS

276       FATAL errors will cause the program to halt ("croak"), since the
277       problem is so severe that it would be dangerous to continue. (This can
278       always be trapped with "eval", but it's not a good idea. Under the
279       circumstances, dying is the best thing to do).
280
281       SEVERE errors may be trapped using the modern interface. If the they
282       are not trapped, or the old interface is used, such an error will cause
283       the program will halt.
284
285       All other errors may be trapped using the modern interface, otherwise
286       they will be "carp"ed about. Program execution will not be halted.
287
288       mkdir [path]: [errmsg] (SEVERE)
289           "make_path" was unable to create the path. Probably some sort of
290           permissions error at the point of departure, or insufficient
291           resources (such as free inodes on Unix).
292
293       No root path(s) specified
294           "make_path" was not given any paths to create. This message is only
295           emitted if the routine is called with the traditional interface.
296           The modern interface will remain silent if given nothing to do.
297
298       No such file or directory
299           On Windows, if "make_path" gives you this warning, it may mean that
300           you have exceeded your filesystem's maximum path length.
301
302       cannot fetch initial working directory: [errmsg]
303           "remove_tree" attempted to determine the initial directory by
304           calling "Cwd::getcwd", but the call failed for some reason. No
305           attempt will be made to delete anything.
306
307       cannot stat initial working directory: [errmsg]
308           "remove_tree" attempted to stat the initial directory (after having
309           successfully obtained its name via "getcwd"), however, the call
310           failed for some reason. No attempt will be made to delete anything.
311
312       cannot chdir to [dir]: [errmsg]
313           "remove_tree" attempted to set the working directory in order to
314           begin deleting the objects therein, but was unsuccessful. This is
315           usually a permissions issue. The routine will continue to delete
316           other things, but this directory will be left intact.
317
318       directory [dir] changed before chdir, expected dev=[n] ino=[n], actual
319       dev=[n] ino=[n], aborting. (FATAL)
320           "remove_tree" recorded the device and inode of a directory, and
321           then moved into it. It then performed a "stat" on the current
322           directory and detected that the device and inode were no longer the
323           same. As this is at the heart of the race condition problem, the
324           program will die at this point.
325
326       cannot make directory [dir] read+writeable: [errmsg]
327           "remove_tree" attempted to change the permissions on the current
328           directory to ensure that subsequent unlinkings would not run into
329           problems, but was unable to do so. The permissions remain as they
330           were, and the program will carry on, doing the best it can.
331
332       cannot read [dir]: [errmsg]
333           "remove_tree" tried to read the contents of the directory in order
334           to acquire the names of the directory entries to be unlinked, but
335           was unsuccessful. This is usually a permissions issue. The program
336           will continue, but the files in this directory will remain after
337           the call.
338
339       cannot reset chmod [dir]: [errmsg]
340           "remove_tree", after having deleted everything in a directory,
341           attempted to restore its permissions to the original state but
342           failed. The directory may wind up being left behind.
343
344       cannot remove [dir] when cwd is [dir]
345           The current working directory of the program is /some/path/to/here
346           and you are attempting to remove an ancestor, such as /some/path.
347           The directory tree is left untouched.
348
349           The solution is to "chdir" out of the child directory to a place
350           outside the directory tree to be removed.
351
352       cannot chdir to [parent-dir] from [child-dir]: [errmsg], aborting.
353       (FATAL)
354           "remove_tree", after having deleted everything and restored the
355           permissions of a directory, was unable to chdir back to the parent.
356           The program halts to avoid a race condition from occurring.
357
358       cannot stat prior working directory [dir]: [errmsg], aborting. (FATAL)
359           "remove_tree" was unable to stat the parent directory after have
360           returned from the child. Since there is no way of knowing if we
361           returned to where we think we should be (by comparing device and
362           inode) the only way out is to "croak".
363
364       previous directory [parent-dir] changed before entering [child-dir],
365       expected dev=[n] ino=[n], actual dev=[n] ino=[n], aborting. (FATAL)
366           When "remove_tree" returned from deleting files in a child
367           directory, a check revealed that the parent directory it returned
368           to wasn't the one it started out from. This is considered a sign of
369           malicious activity.
370
371       cannot make directory [dir] writeable: [errmsg]
372           Just before removing a directory (after having successfully removed
373           everything it contained), "remove_tree" attempted to set the
374           permissions on the directory to ensure it could be removed and
375           failed. Program execution continues, but the directory may possibly
376           not be deleted.
377
378       cannot remove directory [dir]: [errmsg]
379           "remove_tree" attempted to remove a directory, but failed. This may
380           because some objects that were unable to be removed remain in the
381           directory, or a permissions issue. The directory will be left
382           behind.
383
384       cannot restore permissions of [dir] to [0nnn]: [errmsg]
385           After having failed to remove a directory, "remove_tree" was unable
386           to restore its permissions from a permissive state back to a
387           possibly more restrictive setting. (Permissions given in octal).
388
389       cannot make file [file] writeable: [errmsg]
390           "remove_tree" attempted to force the permissions of a file to
391           ensure it could be deleted, but failed to do so. It will, however,
392           still attempt to unlink the file.
393
394       cannot unlink file [file]: [errmsg]
395           "remove_tree" failed to remove a file. Probably a permissions
396           issue.
397
398       cannot restore permissions of [file] to [0nnn]: [errmsg]
399           After having failed to remove a file, "remove_tree" was also unable
400           to restore the permissions on the file to a possibly less
401           permissive setting. (Permissions given in octal).
402
403       unable to map [owner] to a uid, ownership not changed");
404           "make_path" was instructed to give the ownership of created
405           directories to the symbolic name [owner], but "getpwnam" did not
406           return the corresponding numeric uid. The directory will be
407           created, but ownership will not be changed.
408
409       unable to map [group] to a gid, group ownership not changed
410           "make_path" was instructed to give the group ownership of created
411           directories to the symbolic name [group], but "getgrnam" did not
412           return the corresponding numeric gid. The directory will be
413           created, but group ownership will not be changed.
414

SEE ALSO

416       ·   File::Remove
417
418           Allows files and directories to be moved to the Trashcan/Recycle
419           Bin (where they may later be restored if necessary) if the
420           operating system supports such functionality. This feature may one
421           day be made available directly in "File::Path".
422
423       ·   File::Find::Rule
424
425           When removing directory trees, if you want to examine each file to
426           decide whether to delete it (and possibly leaving large swathes
427           alone), File::Find::Rule offers a convenient and flexible approach
428           to examining directory trees.
429

BUGS

431       Please report all bugs on the RT queue:
432
433       <http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Path>
434
435       You can also send pull requests to the Github repository:
436
437       <https://github.com/dland/File-Path>
438

ACKNOWLEDGEMENTS

440       Paul Szabo identified the race condition originally, and Brendan O'Dea
441       wrote an implementation for Debian that addressed the problem.  That
442       code was used as a basis for the current code. Their efforts are
443       greatly appreciated.
444
445       Gisle Aas made a number of improvements to the documentation for 2.07
446       and his advice and assistance is also greatly appreciated.
447

AUTHORS

449       Tim Bunce and Charles Bailey. Currently maintained by David Landgren
450       <david@landgren.net>.
451
453       This module is copyright (C) Charles Bailey, Tim Bunce and David
454       Landgren 1995-2013. All rights reserved.
455

LICENSE

457       This library is free software; you can redistribute it and/or modify it
458       under the same terms as Perl itself.
459
460
461
462perl v5.16.3                      2013-01-16                           Path(3)
Impressum