.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Paranoid::Filesystem 3pm" .TH Paranoid::Filesystem 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Paranoid::Filesystem \- Filesystem Functions .SH VERSION .IX Header "VERSION" \&\f(CW$Id:\fR lib/Paranoid/Filesystem.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $ .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Paranoid::Filesystem; \& \& $rv = pmkdir("/foo/{a1,b2}"); \& \& $rv = preadDir("/tmp", @entries); \& $rv = psubdirs("/etc", @dirList); \& $rv = pfiles("/etc", @filesList); \& \& $rv = ptouch("/foo/*", $tstamp); \& $rv = ptouchR("/foo", $tstamp, $follow, %errors); \& $rv = pchmod("/foo", $perms); \& $rv = pchmodR("/foo", $perms, $follow, %errors); \& $rv = pchown("/foo", $user, $group); \& $rv = pchownR("/foo", $user, $group, $follow, %errors); \& \& $rv = prm("/foo"); \& $rv = prmR("/foo", 1, %errors); \& \& $fullname = pwhich(\*(Aqls\*(Aq); \& $cleaned = pcleanPath($filename); \& $noLinks = ptranslateLink("/etc/foo/bar.conf"); \& $rv = ptranslatePerms("ug+rwx"); \& \& $filename = pwhich(\*(Aqls\*(Aq); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This module provides a few functions to make accessing the filesystem a little easier, while instituting some safety checks. If you want to enable debug tracing into each function you must set \fBPDEBUG\fR to at least 9. .PP \&\fBpcleanPath\fR, \fBptranslateLink\fR, and \fBptranslatePerms\fR are only exported if this module is used with the \fB:all\fR target. .SH "IMPORT LISTS" .IX Header "IMPORT LISTS" This module exports the following symbols by default: .PP .Vb 2 \& preadDir psubdirs pfiles pmkdir prm prmR ptouch \& ptouchR pchmod pchmodR pchown pchownR pwhich .Ve .PP The following specialized import lists also exist: .PP .Vb 4 \& List Members \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& all @defaults ptranslateLink pcleanPath \& ptranslatePerms .Ve .SH SUBROUTINES/METHODS .IX Header "SUBROUTINES/METHODS" .SS pmkdir .IX Subsection "pmkdir" .Vb 3 \& $rv = pmkdir("/foo/{a1,b2}"); \& $rv = pmkdir("/foo", 0750); \& $rv = pmkdir("/foo", 0750, %errors); .Ve .PP This function simulates a 'mkdir \-p {path}', returning false if it fails for any reason other than the directory already being present. The second argument (permissions) is optional, but if present should be an octal number. Shell-style globs are supported as the path argument. .PP If you need to make a directory that includes characters which would normally be interpreted as shell expansion characters you can offer a \fBParanoid::Glob\fR object as the path argument instead. Creating such an object while passing it a \fIliteral\fR value will prevent any shell expansion from happening. .PP This method also allows you to call \fBpmkdir\fR with a list of directories to create, rather than just relying upon shell expansion to construct the list. .SS prm .IX Subsection "prm" .Vb 2 \& $rv = prm("/foo"); \& $rv = prm("/foo", %errors); .Ve .PP This function unlinks non-directories and rmdir's directories. .PP File arguments are processed through Paranoid::Glob and expanded into multiple targets if globs are detected. You can also use a Paranoid::Glob object with a multitude of entities to delete instead of a string. .PP The optional second argument is a hash in which any error messages is stored (with the file/directory name as the key). Attempting to delete something that's not present is not considered a failure. .SS prmR .IX Subsection "prmR" .Vb 3 \& $rv = prmR("/foo"); \& $rv = prmR("/foo", 1); \& $rv = prmR("/foo", 1, %errors); .Ve .PP This function works the same as \fBprm\fR but performs a recursive delete, similar to "rm \-r" on the command line. An optional second argument determines if symbolic links are followed and the targets also recursively deleted. .SS preadDir .IX Subsection "preadDir" .Vb 2 \& $rv = preadDir("/tmp", @entries); \& $rv = preadDir("/tmp", @entries, 1); .Ve .PP This function populates the passed array with the contents of the specified directory. If there are any problems reading the directory the return value will be false and a string explaining the error will be stored in \&\fBParanoid::ERROR\fR. .PP All entries in the returned list will be prefixed with the directory name. An optional third boolean argument can be given to filter out symlinks from the results. .SS psubdirs .IX Subsection "psubdirs" .Vb 1 \& $rv = psubdirs("/etc", @dirList); .Ve .PP This function calls \fBpreadDir\fR in the background and filters the list for directory (or symlinks to) entries. It also returns a true if the command was processed with no problems, and false otherwise. .PP Like \fBpreadDir\fR an optional third boolean argument can be passed that causes symlinks to be filtered out. .SS pfiles .IX Subsection "pfiles" .Vb 1 \& $rv = pfiles("/etc", @filesList); .Ve .PP This function calls \fBpreadDir\fR in the background and filters the list for file (or symlinks to) entries. It also returns a true if the command was processed with no problems, and false otherwise. .PP Like \fBpreadDir\fR an optional third boolean argument can be passed that causes symlinks to be filtered out. .SS pcleanPath .IX Subsection "pcleanPath" .Vb 1 \& $cleaned = pcleanPath($filename); .Ve .PP This function takes a filename and cleans out any '.', '..', and '//+' occurrences within the path. It does not remove '.' or '..' as the first path element, however, in order to preserve the root of the relative path. .PP \&\fBNOTE:\fR this function does not do any checking to see if the passed filename/path actually exists or is valid in any way. It merely removes the unnecessary artifacts from the string. .PP If you're resolving an existing filename and want symlinks resolved to the real path as well you might be interested in \fBCwd\fR's \fBrealpath\fR function instead. .SS ptranslateLink .IX Subsection "ptranslateLink" .Vb 1 \& $noLinks = ptranslateLink("/etc/foo/bar.conf"); .Ve .PP This functions tests if passed filename is a symlink, and if so, translates it to the final target. If a second argument is passed and evaluates to true it will check every element in the path and do a full translation to the final target. .PP The final target is passed through pcleanPath beforehand to remove any unneeded path artifacts. If an error occurs (like circular link references or the target being nonexistent) this function will return undef. You can retrieve the reason for failure from \fBParanoid::ERROR\fR. .PP Obviously, testing for symlinks requires testing against the filesystem, so the target must be valid and present. .PP \&\fBNote:\fR because of the possibility that relative links are being used (including levels of '..') all links are translated fully qualified from /. .SS ptouch .IX Subsection "ptouch" .Vb 3 \& $rv = ptouch("/foo/*"); \& $rv = ptouch("/foo/*", $tstamp); \& $rv = ptouch("/foo/*", $tstamp, %errors); .Ve .PP Simulates the UNIX touch command. Like the UNIX command this will create zero-byte files if they don't exist. The time stamp is an integer denoting the time in UNIX epoch seconds. .PP Shell-style globs are supported, as are Paranoid::Glob objects. .PP The error message from each failed operation will be placed into the passed hash using the file name as the key. .SS ptouchR .IX Subsection "ptouchR" .Vb 4 \& $rv = ptouchR("/foo"); \& $rv = ptouchR("/foo", $tstamp); \& $rv = ptouchR("/foo", $tstamp, $follow); \& $rv = ptouchR("/foo", $tstamp, $follow, %errors); .Ve .PP This function works the same as \fBptouch\fR, but offers one additional argument (the third argument), boolean, which indicates whether or not the command should follow symlinks. .PP You cannot use this function to create new, non-existant files, this only works to update an existing directory heirarchy's mtime. .SS ptranslatePerms .IX Subsection "ptranslatePerms" .Vb 1 \& $rv = ptranslatePerms("ug+rwx"); .Ve .PP This translates symbolic mode notation into an octal number. It fed invalid permissions it will return undef. It understands the following symbols: .PP .Vb 8 \& u permissions apply to user \& g permissions apply to group \& o permissions apply to all others \& r read privileges \& w write privileges \& x execute privileges \& s setuid/setgid (depending on u/g) \& t sticky bit .Ve .PP \&\fBEXAMPLES\fR .PP .Vb 3 \& # Add user executable privileges \& $perms = (stat "./foo")[2]; \& chmod $perms | ptranslatePerms("u+x"), "./foo"; \& \& # Remove all world privileges \& $perms = (stat "./bar")[2]; \& chmod $perms ^ ptranslatePerms("o\-rwx"), "./bar"; .Ve .PP \&\fBNOTE:\fR If this function is called with a numeric representation of permissions, it will return them as-is. This allows for this function to be called indiscriminately where you might be given permissions in either format, but ultimately want them only in numeric presentation. .SS pchmod .IX Subsection "pchmod" .Vb 2 \& $rv = pchmod("/foo", $perms); \& $rv = pchmod("/foo", $perms, %errors); .Ve .PP This function takes a given permission and applies it to every file given to it. The permission can be an octal number or symbolic notation (see \&\fIptranslatePerms\fR for specifics). If symbolic notation is used the permissions will be applied relative to the current permissions on each file. In other words, it acts exactly like the \fBchmod\fR program. .PP File arguments are processed through Paranoid::Glob and expanded into multiple targets if globs are detected. or you can hand it a glob object directly. .PP The error message from each failed operation will be placed into the passed hash using the filename as the key. .PP The return value will be true unless any errors occur during the actual chmod operation including attempting to set permissions on non-existent files. .SS pchmodR .IX Subsection "pchmodR" .Vb 3 \& $rv = pchmodR("/foo", $perms); \& $rv = pchmodR("/foo", $perms, $follow); \& $rv = pchmodR("/foo", $perms, $follow, %errors); .Ve .PP This function works the same as \fBpchmod\fR, but offers one additional argument (the third argument), boolean, which indicates whether or not the command should follow symlinks. .SS pchown .IX Subsection "pchown" .Vb 3 \& $rv = pchown("/foo", $user); \& $rv = pchown("/foo", $user, $group); \& $rv = pchown("/foo", $user, $group, %errors); .Ve .PP This function takes a user and/or a named group or ID and applies it to every file given to it. If either the user or group is undefined it leaves that portion of ownership unchanged. .PP File arguments are processed through Paranoid::Glob and expanded into multiple targets if globs are detected, or you can hand it a populated glob object directly. .PP The error message from each failed operation will be placed into the passed hash using the filename as the key. .PP The return value will be true unless any errors occur during the actual chown operation including attempting to set permissions on non-existent files. .SS pchownR .IX Subsection "pchownR" .Vb 4 \& $rv = pchownR("/foo", $user); \& $rv = pchownR("/foo", $user, $group); \& $rv = pchownR("/foo", $user, $group, $follow); \& $rv = pchownR("/foo", $user, $group, $follow, %errors); .Ve .PP This function works the same as \fBpchown\fR, but requires one additional argument (the fourth argument), boolean, which indicates whether or not the command should follow symlinks. .SS pwhich .IX Subsection "pwhich" .Vb 1 \& $fullname = pwhich(\*(Aqls\*(Aq); .Ve .PP This function tests each directory in your path for a binary that's both readable and executable by the effective user. It will return only one match, stopping the search on the first match. If no matches are found it will return undef. .SH DEPENDENCIES .IX Header "DEPENDENCIES" .IP o 4 .IX Item "o" Cwd .IP o 4 .IX Item "o" Errno .IP o 4 .IX Item "o" Fcntl .IP o 4 .IX Item "o" Paranoid .IP o 4 .IX Item "o" Paranoid::Debug .IP o 4 .IX Item "o" Paranoid::Glob .IP o 4 .IX Item "o" Paranoid::Input .IP o 4 .IX Item "o" Paranoid::IO .IP o 4 .IX Item "o" Paranoid::Process .SH "BUGS AND LIMITATIONS" .IX Header "BUGS AND LIMITATIONS" \&\fBptranslateLink\fR is probably pointless for 99% of the uses out there, you're better off using \fBCwd\fR's \fBrealpath\fR function instead. The only thing it can do differently is translating a single link itself, without translating any additional symlinks found in the preceding path. But, again, you probably won't want that in most circumstances. .PP All of the \fB*R\fR recursive functions have the potential to be very expensive in terms of memory usage. In an attempt to be fast (and reduce excessive function calls and stack depth) it utilizes Paranoid::Glob's \fBrecurse\fR method. In essence, this means that the entire directory tree is loaded into memory at once before any operations are performed. .PP For the most part functions meant to simulate userland programs try to act just as those programs would in a shell environment. That includes filtering arguments through shell globbing expansion, etc. Should you have a filename that should be treated as a literal string you should put it into a Paranoid::Glob object as a literal first, and then hand the glob to the functions. .SH AUTHOR .IX Header "AUTHOR" Arthur Corliss (corliss@digitalmages.com) .SH "LICENSE AND COPYRIGHT" .IX Header "LICENSE AND COPYRIGHT" This software is free software. Similar to Perl, you can redistribute it and/or modify it under the terms of either: .PP .Vb 7 \& a) the GNU General Public License \& as published by the \& Free Software Foundation ; either version 1 \& , or any later version \& , or \& b) the Artistic License 2.0 \& , .Ve .PP subject to the following additional term: No trademark rights to "Paranoid" have been or are conveyed under any of the above licenses. However, "Paranoid" may be used fairly to describe this unmodified software, in good faith, but not as a trademark. .PP (c) 2005 \- 2020, Arthur Corliss (corliss@digitalmages.com) (tm) 2008 \- 2020, Paranoid Inc. (www.paranoid.com)