File::Path(3)
π File::Path(3) π
okay so this is File::Path, and it is literally the βmake a whole tree of directoriesβ and βdelete a whole tree of directoriesβ module. Bestie, it's giving filesystem main character energy. π
πΈ NAME πΈ
File::Path β Create or remove directory trees. That's the whole vibe. β¨
β VERSION β
2.18 - released November 4 2020.
π SYNOPSIS π
use File::Path qw(make_path remove_tree);
@created = make_path('foo/bar/baz', '/zug/zwang');
@created = make_path('foo/bar/baz', '/zug/zwang', {
verbose => 1,
mode => 0711,
});
make_path('foo/bar/baz', '/zug/zwang', {
chmod => 0777,
});
$removed_count = remove_tree('foo/bar/baz', '/zug/zwang', {
verbose => 1,
error => \my $err_list,
safe => 1,
});
# legacy (interface promoted before v2.00)
@created = mkpath('/foo/bar/baz');
@created = mkpath('/foo/bar/baz', 1, 0711);
@created = mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);
$removed_count = rmtree('foo/bar/baz', 1, 1);
$removed_count = rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1);
# legacy (interface promoted before v2.06)
@created = mkpath('foo/bar/baz', '/zug/zwang', { verbose => 1, mode => 0711 });
$removed_count = rmtree('foo/bar/baz', '/zug/zwang', { verbose => 1, mode => 0711 });
π¦ DESCRIPTION π¦
This module provides a convenient way to create directories of arbitrary depth and to delete an entire directory subtree from the filesystem.
The following functions are provided:
make_path( $dir1, $dir2, .... )
make_path( $dir1, $dir2, ...., \%opts )
The make_path function creates the given directories if they don't exist before, much like the Unix command mkdir -p.
The function accepts a list of directories to be created. Its behaviour may be tuned by an optional hashref appearing as the last parameter on the call.
The function returns the list of directories actually created during the call; in scalar context the number of directories created.
The following keys are recognised in the option hash:
mode => $numβ The numeric permissions mode to apply to each created directory (defaults to0777), to be modified by the currentumask. If the directory already exists (and thus does not need to be created), the permissions will not be modified.
mask is recognised as an alias for this parameter.
chmod => $numβ Takes a numeric mode to apply to each created directory (not modified by the currentumask). If the directory already exists (and thus does not need to be created), the permissions will not be modified.verbose => $boolβ If present, will causemake_pathto print the name of each directory as it is created. By default nothing is printed.error => \$errβ If present, it should be a reference to a scalar. This scalar will be made to reference an array, which will be used to store any errors that are encountered. See theERROR HANDLINGsection for more information.
If this parameter is not used, certain error conditions may raise a fatal error that will cause the program to halt, unless trapped in an eval block.
owner => $ownerβ If present, will cause any created directory to be owned by$owner. If the value is numeric, it will be interpreted as a uid; otherwise a username is assumed. An error will be issued if the username cannot be mapped to a uid, the uid does not exist or the process lacks the privileges to change ownership.
Ownership of directories that already exist will not be changed.
user and uid are aliases of owner.
user => $ownerβ Alias ofowner; same ownership vibes, same rules. πuid => $ownerβ Also an alias ofowner; same ownership vibes, same rules. πgroup => $groupβ If present, will cause any created directory to be owned by the group$group. If the value is numeric, it will be interpreted as a gid; otherwise a group name is assumed. An error will be issued if the group name cannot be mapped to a gid, the gid does not exist or the process lacks the privileges to change group ownership.
Group ownership of directories that already exist will not be changed.
make_path '/var/tmp/webcache', {owner=>'nobody', group=>'nogroup'};
mkpath( $dir )
mkpath( $dir, $verbose, $mode )
mkpath( [$dir1, $dir2,...], $verbose, $mode )
mkpath( $dir1, $dir2,..., \%opt )
The mkpath() function provide the legacy interface of make_path() with a different interpretation of the arguments passed. The behaviour and return value of the function is otherwise identical to make_path().
remove_tree( $dir1, $dir2, .... )
remove_tree( $dir1, $dir2, ...., \%opts )
The remove_tree function deletes the given directories and any files and subdirectories they might contain, much like the Unix command rm -rf or the Windows commands rmdir /s and rd /s.
The function accepts a list of directories to be removed. (In point of fact, it will also accept filesystem entries which are not directories, such as regular files and symlinks. But, as its name suggests, its intent is to remove trees rather than individual files.)
remove_tree()'s behaviour may be tuned by an optional hashref appearing as the last parameter on the call. If an empty string is passed to remove_tree, an error will occur.
NOTE: For security reasons, we strongly advise use of the hashref-as-final-argument syntax -- specifically, with a setting of the safe element to a true value.
remove_tree( $dir1, $dir2, ....,
{
safe => 1,
... # other key-value pairs
},
);
The function returns the number of files successfully deleted.
The following keys are recognised in the option hash:
verbose => $boolβ If present, will causeremove_treeto print the name of each file as it is unlinked. By default nothing is printed. π£οΈsafe => $boolβ When set to a true value, will causeremove_treeto skip the files for which the process lacks the required privileges needed to delete files, such as delete privileges on VMS. In other words, the code will make no attempt to alter file permissions. Thus, if the process is interrupted, no filesystem object will be left in a more permissive mode. π‘οΈkeep_root => $boolβ When set to a true value, will cause all files and subdirectories to be removed, except the initially specified directories. This comes in handy when cleaning out an application's scratch directory.
remove_tree( '/tmp', {keep_root => 1} );
result => \$resβ If present, it should be a reference to a scalar. This scalar will be made to reference an array, which will be used to store all files and directories unlinked during the call. If nothing is unlinked, the array will be empty.
remove_tree( '/tmp', {result => \my $list} );
print "unlinked $_\n" for @$list;
This is a useful alternative to the verbose key.
error => \$errβ If present, it should be a reference to a scalar. This scalar will be made to reference an array, which will be used to store any errors that are encountered. See theERROR HANDLINGsection for more information.
Removing things is a much more dangerous proposition than creating things. As such, there are certain conditions that remove_tree may encounter that are so dangerous that the only sane action left is to kill the program.
Use error to trap all that is reasonable (problems with permissions and the like), and let it die if things get out of hand. This is the safest course of action.
rmtree( $dir )
rmtree( $dir, $verbose, $safe )
rmtree( [$dir1, $dir2,...], $verbose, $safe )
rmtree( $dir1, $dir2,..., \%opt )
The rmtree() function provide the legacy interface of remove_tree() with a different interpretation of the arguments passed. The behaviour and return value of the function is otherwise identical to remove_tree().
NOTE: For security reasons, we strongly advise use of the hashref-as-final-argument syntax, specifically with a setting of the safe element to a true value.
rmtree( $dir1, $dir2, ....,
{
safe => 1,
... # other key-value pairs
},
);
π ERROR HANDLING π
NOTE: The following error handling mechanism is consistent throughout all code paths EXCEPT in cases where the ROOT node is nonexistent. In version 2.11 the maintainers attempted to rectify this inconsistency but too many downstream modules encountered problems. In such case, if you require root node evaluation or error checking prior to callingmake_pathorremove_tree, you should take additional precautions.
If make_path or remove_tree encounters an error, a diagnostic message will be printed to STDERR via carp (for non-fatal errors) or via croak (for fatal errors).
If this behaviour is not desirable, the error attribute may be used to hold a reference to a variable, which will be used to store the diagnostics. The variable is made a reference to an array of hash references. Each hash contain a single key/value pair where the key is the name of the file, and the value is the error message (including the contents of $! when appropriate). If a general error is encountered the diagnostic key will be empty.
An example usage looks like:
remove_tree( 'foo/bar', 'bar/rat', {error => \my $err} );
if ($err && @$err) {
for my $diag (@$err) {
my ($file, $message) = %$diag;
if ($file eq '') {
print "general error: $message\n";
}
else {
print "problem unlinking $file: $message\n";
}
}
}
else {
print "No error encountered\n";
}
Note that if no errors are encountered, $err will reference an empty array. This means that $err will always end up TRUE; so you need to test @$err to determine if errors occurred.
π«Ά NOTES π«Ά
File::Path blindly exports mkpath and rmtree into the current namespace. These days, this is considered bad style, but to change it now would break too much code. Nonetheless, you are invited to specify what it is you are expecting to use:
use File::Path 'rmtree';
...and okay so the plot twist nobody warned you about: make_path and remove_tree are not exported by default. Like, at all. You literally have to tell Perl which ones you want. No auto-magic, no freebies, no nothing. π
use File::Path 'remove_tree';
Heads up though bestie β a side-effect of the line above is thatmkpathandrmtreeare no longer exported at all. That's just how theExportermodule works, it's not a bug, it's not personal. If you're migrating a codebase over to the new interface, you'll have to list every single thing explicitly. But honestly? That's just good practice anyway. π
use File::Path qw(remove_tree rmtree);
π¦ API CHANGES π¦
So the API got a glow-up in the 2.0 branch. For a hot minute there, mkpath and rmtree tried β unsuccessfully, bless them β to deal with the two different calling mechanisms at once. That whole approach was later considered a failure. RIP to that era. π
The new semantics are now only available via make_path and remove_tree. The old semantics are only available through mkpath and rmtree. Users are strongly encouraged to upgrade to at least 2.08 so you don't get jumpscared by surprises. fr fr, just upgrade.
π SECURITY CONSIDERATIONS π
Okay this is the serious part, so like, pay attention bestie. π
There were race conditions in the 1.x implementations of File::Path's rmtree function (although sometimes they got patched, depending on the OS distribution or platform). The 2.0 version contains code to avoid the problem mentioned in CVE-2002-0435.
For more info, peep these pages:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=286905
http://www.nntp.perl.org/group/perl.perl5.porters/2005/01/msg97623.html
http://www.debian.org/security/2005/dsa-696
π¨ Also, unless thesafeparameter is set (or the third parameter in the traditional interface isTRUE), if aremove_treegets interrupted, files that were originally in read-only mode may now have their permissions set to a read-write (or "delete OK") mode. So like, yeah. Setsafe. Please. π
The following CVE reports were previously filed against File-Path and are believed to have been addressed:
In February 2017 the cPanel Security Team reported an additional vulnerability in File-Path. The chmod() logic to make directories traversable can be abused to set the mode on an attacker-chosen file to an attacker-chosen value. This is due to the time-of-check-to-time-of-use (TOCTTOU) race condition (<https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use>) between the stat() that decides the inode is a directory and the chmod() that tries to make it user-rwx. CPAN versions 2.13 and later incorporate a patch provided by John Lightsey to address this problem. This vulnerability has been reported as CVE-2017-6512.
π DIAGNOSTICS π
FATAL errors will cause the program to halt (croak), because the problem is so severe that continuing would be straight-up dangerous. (You can always trap it with eval, but like⦠it's not a good idea. Under the circumstances, dying is the best thing to do. dramatic but true.)
SEVERE errors may be trapped using the modern interface. If they're not trapped, or if the old interface is used, such an error will cause the program to halt.
All other errors may be trapped using the modern interface. Otherwise they get carped about. Program execution will not be halted.
mkdir [path]: [errmsg] (SEVERE)βmake_pathwas unable to create the path. Probably some sort of permissions error at the point of departure, or insufficient resources (like free inodes on Unix). πNo root path(s) specifiedβmake_pathwasn't given any paths to create. This message is only emitted if the routine is called with the traditional interface. The modern interface stays silent if it's given nothing to do. iconic behavior honestly.No such file or directoryβ On Windows, ifmake_pathgives you this warning, it may mean you've exceeded your filesystem's maximum path length. πcannot fetch initial working directory: [errmsg]βremove_treeattempted to determine the initial directory by callingCwd::getcwd, but the call failed for some reason. No attempt will be made to delete anything.cannot stat initial working directory: [errmsg]βremove_treeattempted to stat the initial directory (after having successfully obtained its name viagetcwd), however the call failed for some reason. No attempt will be made to delete anything.cannot chdir to [dir]: [errmsg]βremove_treeattempted to set the working directory so it could start deleting the objects in there, but it was unsuccessful. This is usually a permissions issue. The routine will continue to delete other things, but this directory will be left intact.directory [dir] changed before chdir, expected dev=[n] ino=[n], actual dev=[n] ino=[n], aborting. (FATAL)βremove_treerecorded the device and inode of a directory, then moved into it. It then performed astaton the current directory and detected that the device and inode were no longer the same. Since this is at the heart of the race condition problem, the program will die right there. πͺ¦cannot make directory [dir] read+writeable: [errmsg]βremove_treeattempted to change the permissions on the current directory to make sure subsequent unlinkings wouldn't run into problems, but was unable to do so. The permissions remain as they were, and the program carries on doing the best it can. π«Άcannot read [dir]: [errmsg]βremove_treetried to read the contents of the directory to acquire the names of the directory entries to be unlinked, but was unsuccessful. Usually a permissions issue. The program will continue, but the files in this directory will remain after the call.cannot reset chmod [dir]: [errmsg]βremove_tree, after having deleted everything in a directory, attempted to restore its permissions to the original state but failed. The directory may wind up being left behind. π¬cannot remove [dir] when cwd is [dir]β The current working directory of the program is/some/path/to/hereand you're attempting to remove an ancestor, such as/some/path. The directory tree is left untouched.
The solution is to chdir out of the child directory to a place outside the directory tree to be removed.
cannot chdir to [parent-dir] from [child-dir]: [errmsg], aborting. (FATAL)βremove_tree, after having deleted everything and restored the permissions of a directory, was unable tochdirback to the parent. The program halts to avoid a race condition from occurring.cannot stat prior working directory [dir]: [errmsg], aborting. (FATAL)βremove_treewas unable to stat the parent directory after having returned from the child. Since there's no way of knowing if we returned to where we think we should be (by comparing device and inode), the only way out is tocroak.previous directory [parent-dir] changed before entering [child-dir], expected dev=[n] ino=[n], actual dev=[n] ino=[n], aborting. (FATAL)β Whenremove_treereturned from deleting files in a child directory, a check revealed that the parent directory it returned to wasn't the one it started out from. This is considered a sign of malicious activity. π±cannot make directory [dir] writeable: [errmsg]β Just before removing a directory (after having successfully removed everything it contained),remove_treeattempted to set the permissions on the directory to make sure it could be removed, and failed. Program execution continues, but the directory may possibly not be deleted.cannot remove directory [dir]: [errmsg]βremove_treeattempted to remove a directory but failed. This may be because some objects that were unable to be removed remain in the directory, or it could be a permissions issue. The directory will be left behind.cannot restore permissions of [dir] to [0nnn]: [errmsg]β After having failed to remove a directory,remove_treewas unable to restore its permissions from a permissive state back to a possibly more restrictive setting. (Permissions given in octal.)cannot make file [file] writeable: [errmsg]βremove_treeattempted to force the permissions of a file to make sure it could be deleted, but failed to do so. It will, however, still attempt to unlink the file. determined queen behavior.cannot unlink file [file]: [errmsg]βremove_treefailed to remove a file. Probably a permissions issue.cannot restore permissions of [file] to [0nnn]: [errmsg]β After having failed to remove a file,remove_treewas also unable to restore the permissions on the file to a possibly less permissive setting. (Permissions given in octal.)unable to map [owner] to a uid, ownership not changed");βmake_pathwas instructed to give the ownership of created directories to the symbolic name[owner], butgetpwnamdid not return the corresponding numeric uid. The directory will be created, but ownership will not be changed.unable to map [group] to a gid, group ownership not changedβmake_pathwas instructed to give the group ownership of created directories to the symbolic name[group], butgetgrnamdid not return the corresponding numeric gid. The directory will be created, but group ownership will not be changed.
π SEE ALSO π
- File::Remove β Allows files and directories to be moved to the Trashcan/Recycle Bin (where they may later be restored if necessary) if the operating system supports such functionality. This feature may one day be made available directly in
File::Path. π manifesting it. - File::Find::Rule β When removing directory trees, if you want to examine each file to decide whether to delete it (and possibly leaving large swathes alone), File::Find::Rule offers a convenient and flexible approach to examining directory trees. β¨
π BUGS AND LIMITATIONS π
the following describes File::Path limitations and how to report bugs. buckle up, bestie. π«Ά
π§΅ MULTITHREADED APPLICATIONS π§΅
so File::Path's rmtree and remove_tree will not work with multithreaded applications due to its use of chdir. at this time, no warning or error is generated in this situation. you will certainly encounter unexpected results. π
the implementation that surfaces this limitation will not be changed. periodt. if you need functionality similar to File::Path but which does not chdir, see the File::Path::Tiny module. β¨
π NFS Mount Points π
File::Path is not responsible for triggering the automounts, mirror mounts, and the contents of network mounted filesystems. nope, not its job. π ββοΈ
if your NFS implementation requires an action to be performed on the filesystem in order for File::Path to perform operations, it is strongly suggested you assure filesystem availability by reading the root of the mounted filesystem. consider that your vibe check. π«Ά
π REPORTING BUGS π
please report all bugs on the RT queue, either via the web interface:
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Path>
or by email:
bug-File-Path@rt.cpan.org
in either case, please attach patches to the bug report rather than including them inline in the web post or the body of the email. we're begging. π
you can also send pull requests to the Github repository:
<https://github.com/rpcme/File-Path>
π«Ά ACKNOWLEDGEMENTS π«Ά
Paul Szabo identified the race condition originally, and Brendan O'Dea wrote an implementation for Debian that addressed the problem. that code was used as a basis for the current code. their efforts are greatly appreciated. iconic behavior, honestly. π
Gisle Aas made a number of improvements to the documentation for 2.07 and his advice and assistance is also greatly appreciated. we love a docs glow-up. π
π AUTHORS π
prior authors and maintainers: Tim Bunce, Charles Bailey, and David Landgren <david@landgren.net>.
current maintainers are Richard Elberger <riche@cpan.org> and James (Jim) Keenan <jkeenan@cpan.org>.
π CONTRIBUTORS π
contributors to File::Path, in alphabetical order by first name. slay, alphabetical order is self-care. π
<bulkdd@cpan.org>- Charlie Gonzalez
<itcharlie@cpan.org> - Craig A. Berry
<craigberry@mac.com> - James E Keenan
<jkeenan@cpan.org> - John Lightsey
<john@perlsec.org> - Nigel Horne
<njh@bandsman.co.uk> - Richard Elberger
<riche@cpan.org> - Ryan Yee
<ryee@cpan.org> - Skye Shaw
<shaw@cpan.org> - Tom Lutz
<tommylutz@gmail.com> - Will Sheppard
<willsheppard@github>
Β©οΈ COPYRIGHT Β©οΈ
This module is copyright (C) Charles Bailey, Tim Bunce, David Landgren, James Keenan and Richard Elberger 1995-2020. All rights reserved.
π LICENSE π
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page, bestie. this one comes from perl v5.42.0, dated 2020-11-05 β mwah πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-File-Path 2.180000-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms