PATHCHK(1POSIX)

🌸 PROLOG 🌸

Okay bestie, before we even get into it β€” this page is part of the POSIX Programmer's Manual (the POSIX.1-2017 era, she's a classic πŸ’…).

The Linux implementation of this interface may differ (so like, go read the matching Linux manual page if you need the Linux-flavored tea), or honestly the interface may not even be implemented on Linux at all. πŸ™ˆ


✨ NAME ✨

pathchk β€” check pathnames


πŸ¦‹ SYNOPSIS πŸ¦‹

pathchk [-p] [-P] pathname...

πŸ’– DESCRIPTION πŸ’–

So what does pathchk actually do, you ask? It checks that one or more pathnames are valid β€” meaning they could be used to access or create a file without causing syntax errors β€” and portable, meaning no filename truncation happens. ✨

And if you want the deeper portability checks? That's literally what the -p and -P options are for, bestie.

By default, pathchk checks each component of each pathname operand based on the underlying file system. It writes a diagnostic for each pathname operand that:

  • Is longer than {PATH_MAX} bytes (see Pathname Variable Values in the Base Definitions volume of POSIX.1‐2017, <limits.h>)
  • Contains any component longer than {NAME_MAX} bytes in its containing directory
  • Contains any component in a directory that is not searchable
  • Contains any byte sequence that is not valid in its containing directory

The format of the diagnostic message is not specified, but it shall indicate the error detected and the corresponding pathname operand. (So like, it tells you what went wrong and where. Iconic of her.)

And here's the plot twist 🫢: it is not considered an error if one or more components of a pathname operand do not exist β€” as long as a file matching the pathname specified by the missing components could be created without violating any of the checks listed above. Not me being chill about missing files 😭

πŸŽ€ OPTIONS πŸŽ€

pathchk conforms to the Base Definitions volume of POSIX.1‐2017, Section 12.2, Utility Syntax Guidelines. Periodt.

The following option shall be supported:

  • -p β€” Instead of performing checks based on the underlying file system, write a diagnostic for each pathname operand that:
    • Is longer than {_POSIX_PATH_MAX} bytes (see Minimum Values in the Base Definitions volume of POSIX.1‐2017, <limits.h>)
    • Contains any component longer than {_POSIX_NAME_MAX} bytes
    • Contains any character in any component that is not in the portable filename character set
  • -P β€” Write a diagnostic for each pathname operand that:
    • Contains a component whose first character is the <hyphen-minus> character
    • Is empty

πŸ’… OPERANDS πŸ’…

The following operand shall be supported:

  • pathname β€” A pathname to be checked. (That's the whole vibe. Just hand her a path and she'll vibe check it. βœ…)

🌈 STDIN 🌈

Not used. She doesn't read from stdin, she's not about that life.


πŸ“ INPUT FILES πŸ“

None.


πŸ’œ ENVIRONMENT VARIABLES πŸ’œ

The following environment variables shall affect the execution of pathchk:

  • LANG β€” Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1‐2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)
  • LC_ALL β€” If set to a non-empty string value, override the values of all the other internationalization variables. (She said all. No exceptions. πŸ‘‘)
  • LC_CTYPE β€” Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments).
  • LC_MESSAGES β€” Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
  • NLSPATH β€” Determine the location of message catalogs for the processing of LC_MESSAGES.

🧚 ASYNCHRONOUS EVENTS 🧚

Default.


⭐ STDOUT ⭐

Not used. (She's not here to yap at your terminal output, bestie.)


🌸 STDERR 🌸

The standard error shall be used only for diagnostic messages.


πŸ¦„ OUTPUT FILES πŸ¦„

None.


πŸ’« EXTENDED DESCRIPTION πŸ’«

None.


πŸ”₯ EXIT STATUS πŸ”₯

The following exit values shall be returned:

  • 0 β€” All pathname operands passed all of the checks. (Slay. πŸ’…)
  • >0 β€” An error occurred.

😭 CONSEQUENCES OF ERRORS 😭

Default.


The following sections are informative.


πŸ‘‘ APPLICATION USAGE πŸ‘‘

So here's the thing β€” the test utility can be used to determine whether a given pathname names an existing file. But it does not give any indication of whether or not any component of the pathname was truncated in a directory where the _POSIX_NO_TRUNC feature is not in effect. πŸ™ˆ

The pathchk utility does not check for file existence. What she does do is perform checks to determine whether a pathname does exist or could be created with no pathname component truncation. Different assignment, bestie. πŸ’…

The noclobber option in the shell (see the set special built-in) can be used to atomically create a file. And, as with all file creation semantics in the System Interfaces volume of POSIX.1‐2017, it guarantees atomic creation β€” but it still depends on applications agreeing on conventions and cooperating on the use of files after they've been created. Teamwork makes the dream work, no cap. 🫢

To verify that a pathname meets the requirements of filename portability, applications should use both the -p and -P options together. Not one. Not the other. Both. Periodt. ✨

πŸ¦‹ EXAMPLES πŸ¦‹

Okay so first up: to verify that all pathnames in an imported data interchange archive are legitimate and unambiguous on the current system:

# This example assumes that no pathnames in the archive
# contain <newline> characters.
pax -f archive | sed -e 's/[^[:alnum:]]/\\&/g' | xargs pathchk --
if [ $? -eq 0 ]
then
    pax -r -f archive
else
    echo Investigate problems before importing files.
    exit 1
fi

Next: to verify that all files in the current directory hierarchy could be moved to any system conforming to the System Interfaces volume of POSIX.1‐2017 that also supports the pax utility:

find . -exec pathchk -p -P {} +
if [ $? -eq 0 ]
then
    pax -w -f ../archive .
else
    echo Portable archive cannot be created.
    exit 1
fi

And then the big one πŸ’€ β€” to verify that a user-supplied pathname names a readable file, and that the application can create a file extending the given path without truncation and without overwriting any existing file:

case $- in
    *C*)    reset="";;
    *)      reset="set +C"
            set -C;;
esac
test -r "$path" && pathchk "$path.out" &&
    rm "$path.out" > "$path.out"
if [ $? -ne 0 ]; then
    printf "%s: %s not found or %s.out fails \
creation checks.\n" $0 "$path$path"
    $reset    # Reset the noclobber option in case a trap
              # on EXIT depends on it.
    exit 1
fi
$reset
PROCESSING < "$path" > "$path.out"

The following assumptions are made in this example:

  1. PROCESSING represents the code that is used by the application to use $path once it is verified that $path.out works as intended.
  2. The state of the noclobber option is unknown when this code is invoked and should be set on exit to the state it was in when this code was invoked. (The reset variable is used in this example to restore the initial state.)
  3. Note the usage of:
rm "$path.out" > "$path.out"

a. The pathchk command has already verified, at this point, that $path.out is not truncated. b. With the noclobber option set, the shell verifies that $path.out does not already exist before invoking rm. c. If the shell succeeded in creating $path.out, rm removes it so that the application can create the file again in the PROCESSING step. d. If the PROCESSING step wants the file to exist already when it is invoked, the:

rm "$path.out" > "$path.out"

should be replaced with:

> "$path.out"

which verifies that the file did not already exist, but leaves $path.out in place for use by PROCESSING.


🧠 RATIONALE 🧠

The pathchk utility was new for the ISO POSIX‐2:1993 standard. She, along with the set -C (noclobber) option added to the shell, replaces the mktemp, validfnam, and create utilities that appeared in early proposals. All of these utilities were attempts to solve several common problems:

  • Verify the validity (for several different definitions of ``valid'') of a pathname supplied by a user, generated by an application, or imported from an external source.
  • Atomically create a file.
  • Perform various string handling functions to generate a temporary filename.

The create utility, included in an early proposal, provided checking and atomic creation in a single invocation of the utility β€” but these are orthogonal issues and need not be grouped into a single utility. Note that the noclobber option also provides a way of creating a lock for process synchronization; since it provides an atomic create, there is no race between a test for existence and the following creation if it did not exist. πŸ”₯

Having a function like tmpnam() in the ISO C standard is important in many high-level languages. The shell programming language, however, has built-in string manipulation facilities, making it very easy to construct temporary filenames. The names needed obviously depend on the application, but are frequently of a form similar to:

$TMPDIR/application_abbreviation$$.suffix

In cases where there is likely to be contention for a given suffix, a simple shell for or while loop can be used with the shell noclobber option to create a file without risk of collisions β€” as long as applications trying to use the same filename name space are cooperating on the use of files after they have been created. 🀝

For historical purposes, -p does not check for the use of the <hyphen-minus> character as the first character in a component of the pathname, or for an empty pathname operand. (She said historical, she said what she said. πŸ’…)


⭐ FUTURE DIRECTIONS ⭐

None.


🌈 SEE ALSO 🌈

Section 2.7, Redirection, set, test

The Base Definitions volume of POSIX.1‐2017, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines, <limits.h>

Okay bestie, legal tea time β˜•βœ¨. Big chunks of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, and the copyright on that is (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. Iconic, protected, periodt.

Real talk: if this version and the original IEEE and The Open Group Standard ever disagree, the original IEEE and The Open Group Standard is the referee document. It’s giving final boss energy πŸ‘‘.

The original Standard can be obtained online at http://www.opengroup.org/unix/online.html β€” yes, that exact URL, no cap.

And any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. Not the vibe we wanted, but here we are 😭. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html.


That’s the whole page, bestie! Footer check: source/version is IEEE/The Open Group, the date is 2017, and the page is PATHCHK(1POSIX). Stay slaying those pathnames πŸ’–πŸ¦‹

πŸ“¦ where this page comes from