1PATHCHK(1P) POSIX Programmer's Manual PATHCHK(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 pathchk - check pathnames
13
15 pathchk [-p] pathname...
16
18 The pathchk utility shall check that one or more pathnames are valid
19 (that is, they could be used to access or create a file without causing
20 syntax errors) and portable (that is, no filename truncation results).
21 More extensive portability checks are provided by the -p option.
22
23 By default, the pathchk utility shall check each component of each
24 pathname operand based on the underlying file system. A diagnostic
25 shall be written for each pathname operand that:
26
27 * Is longer than {PATH_MAX} bytes (see Pathname Variable Values in the
28 Base Definitions volume of IEEE Std 1003.1-2001, Chapter 13, Head‐
29 ers, <limits.h>)
30
31 * Contains any component longer than {NAME_MAX} bytes in its contain‐
32 ing directory
33
34 * Contains any component in a directory that is not searchable
35
36 * Contains any character in any component that is not valid in its
37 containing directory
38
39 The format of the diagnostic message is not specified, but shall indi‐
40 cate the error detected and the corresponding pathname operand.
41
42 It shall not be considered an error if one or more components of a
43 pathname operand do not exist as long as a file matching the pathname
44 specified by the missing components could be created that does not vio‐
45 late any of the checks specified above.
46
48 The pathchk utility shall conform to the Base Definitions volume of
49 IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
50
51 The following option shall be supported:
52
53 -p Instead of performing checks based on the underlying file sys‐
54 tem, write a diagnostic for each pathname operand that:
55
56 * Is longer than {_POSIX_PATH_MAX} bytes (see Minimum Values in
57 the Base Definitions volume of IEEE Std 1003.1-2001, Chapter
58 13, Headers, <limits.h>)
59
60 * Contains any component longer than {_POSIX_NAME_MAX} bytes
61
62 * Contains any character in any component that is not in the
63 portable filename character set
64
66 The following operand shall be supported:
67
68 pathname
69 A pathname to be checked.
70
71
73 Not used.
74
76 None.
77
79 The following environment variables shall affect the execution of
80 pathchk:
81
82 LANG Provide a default value for the internationalization variables
83 that are unset or null. (See the Base Definitions volume of
84 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
85 ables for the precedence of internationalization variables used
86 to determine the values of locale categories.)
87
88 LC_ALL If set to a non-empty string value, override the values of all
89 the other internationalization variables.
90
91 LC_CTYPE
92 Determine the locale for the interpretation of sequences of
93 bytes of text data as characters (for example, single-byte as
94 opposed to multi-byte characters in arguments).
95
96 LC_MESSAGES
97 Determine the locale that should be used to affect the format
98 and contents of diagnostic messages written to standard error.
99
100 NLSPATH
101 Determine the location of message catalogs for the processing of
102 LC_MESSAGES .
103
104
106 Default.
107
109 Not used.
110
112 The standard error shall be used only for diagnostic messages.
113
115 None.
116
118 None.
119
121 The following exit values shall be returned:
122
123 0 All pathname operands passed all of the checks.
124
125 >0 An error occurred.
126
127
129 Default.
130
131 The following sections are informative.
132
134 The test utility can be used to determine whether a given pathname
135 names an existing file; it does not, however, give any indication of
136 whether or not any component of the pathname was truncated in a direc‐
137 tory where the _POSIX_NO_TRUNC feature is not in effect. The pathchk
138 utility does not check for file existence; it performs checks to deter‐
139 mine whether a pathname does exist or could be created with no pathname
140 component truncation.
141
142 The noclobber option in the shell (see the set special built-in) can be
143 used to atomically create a file. As with all file creation semantics
144 in the System Interfaces volume of IEEE Std 1003.1-2001, it guarantees
145 atomic creation, but still depends on applications to agree on conven‐
146 tions and cooperate on the use of files after they have been created.
147
149 To verify that all pathnames in an imported data interchange archive
150 are legitimate and unambiguous on the current system:
151
152
153 pax -f archive | sed -e '/ == .*/s///' | xargs pathchk
154 if [ $? -eq 0 ]
155 then
156 pax -r -f archive
157 else
158 echo Investigate problems before importing files.
159 exit 1
160 fi
161
162 To verify that all files in the current directory hierarchy could be
163 moved to any system conforming to the System Interfaces volume of
164 IEEE Std 1003.1-2001 that also supports the pax utility:
165
166
167 find . -print | xargs pathchk -p
168 if [ $? -eq 0 ]
169 then
170 pax -w -f archive .
171 else
172 echo Portable archive cannot be created.
173 exit 1
174 fi
175
176 To verify that a user-supplied pathname names a readable file and that
177 the application can create a file extending the given path without
178 truncation and without overwriting any existing file:
179
180
181 case $- in
182 *C*) reset="";;
183 *) reset="set +C"
184 set -C;;
185 esac
186 test -r "$path" && pathchk "$path.out" &&
187 rm "$path.out" > "$path.out"
188 if [ $? -ne 0 ]; then
189 printf "%s: %s not found or %s.out fails \
190 creation checks.\n" $0 "$path" "$path"
191 $reset # Reset the noclobber option in case a trap
192 # on EXIT depends on it.
193 exit 1
194 fi
195 $reset
196 PROCESSING < "$path" > "$path.out"
197
198 The following assumptions are made in this example:
199
200 1. PROCESSING represents the code that is used by the application to
201 use $path once it is verified that $path.out works as intended.
202
203 2. The state of the noclobber option is unknown when this code is
204 invoked and should be set on exit to the state it was in when this
205 code was invoked. (The reset variable is used in this example to
206 restore the initial state.)
207
208 3. Note the usage of:
209
210
211 rm "$path.out" > "$path.out"
212
213 a. The pathchk command has already verified, at this point, that
214 $path.out is not truncated.
215
216 b. With the noclobber option set, the shell verifies that
217 $path.out does not already exist before invoking rm.
218
219 c. If the shell succeeded in creating $path.out, rm removes it so
220 that the application can create the file again in the PROCESS‐
221 ING step.
222
223 d. If the PROCESSING step wants the file to exist already when it
224 is invoked, the:
225
226
227 rm "$path.out" > "$path.out"
228
229 should be replaced with:
230
231
232 > "$path.out"
233
234 which verifies that the file did not already exist, but leaves
235 $path.out in place for use by PROCESSING.
236
238 The pathchk utility was new for the ISO POSIX-2:1993 standard. It,
239 along with the set -C( noclobber) option added to the shell, replaces
240 the mktemp, validfnam, and create utilities that appeared in early pro‐
241 posals. All of these utilities were attempts to solve several common
242 problems:
243
244 * Verify the validity (for several different definitions of "valid")
245 of a pathname supplied by a user, generated by an application, or
246 imported from an external source.
247
248 * Atomically create a file.
249
250 * Perform various string handling functions to generate a temporary
251 filename.
252
253 The create utility, included in an early proposal, provided checking
254 and atomic creation in a single invocation of the utility; these are
255 orthogonal issues and need not be grouped into a single utility. Note
256 that the noclobber option also provides a way of creating a lock for
257 process synchronization; since it provides an atomic create, there is
258 no race between a test for existence and the following creation if it
259 did not exist.
260
261 Having a function like tmpnam() in the ISO C standard is important in
262 many high-level languages. The shell programming language, however, has
263 built-in string manipulation facilities, making it very easy to con‐
264 struct temporary filenames. The names needed obviously depend on the
265 application, but are frequently of a form similar to:
266
267
268 $TMPDIR/application_abbreviation$$.suffix
269
270 In cases where there is likely to be contention for a given suffix, a
271 simple shell for or while loop can be used with the shell noclobber
272 option to create a file without risk of collisions, as long as applica‐
273 tions trying to use the same filename name space are cooperating on the
274 use of files after they have been created.
275
277 None.
278
280 Redirection, set, test
281
283 Portions of this text are reprinted and reproduced in electronic form
284 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
285 -- Portable Operating System Interface (POSIX), The Open Group Base
286 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
287 Electrical and Electronics Engineers, Inc and The Open Group. In the
288 event of any discrepancy between this version and the original IEEE and
289 The Open Group Standard, the original IEEE and The Open Group Standard
290 is the referee document. The original Standard can be obtained online
291 at http://www.opengroup.org/unix/online.html .
292
293
294
295IEEE/The Open Group 2003 PATHCHK(1P)