1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
$Id: 099u_mkdirhier_rewrite.diff 480 2005-08-02 20:14:26Z branden $
Reimplement mkdirhier; see Debian #141347 and #232357 for some reasons why.
This shell script and manpage by Branden Robinson.
Not submitted upstream to XFree86 or X.Org.
Index: a/imake/mkdirhier
===================================================================
--- a/imake/mkdirhier (revision 309)
+++ b/imake/mkdirhier (working copy)
@@ -1,67 +1,94 @@
#!/bin/sh
-# $Xorg: mkdirhier.sh,v 1.3 2000/08/17 19:41:53 cpqbld Exp $
-# Courtesy of Paul Eggert
-newline='
-'
-IFS=$newline
+# Copyright 2005 Branden Robinson.
-case ${1--} in
--*) echo >&2 "mkdirhier: usage: mkdirhier directory ..."; exit 1
-esac
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following condition:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+# SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR
+# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+# DEALINGS IN THE SOFTWARE.
-status=
+# I rewrote Paul Eggert's script in POSIX shell because it was a little
+# odd, and did not confine itself to puritantical pre-POSIX conventions.
+# For example, in one place it used:
+# case ${1--} in
+# -*)
+# to test for $1 being null, presumably due to fears that test -n and -z
+# will not be available. Yet later in the script, test -n was used.
+#
+# This seemed quite silly. I decided to rewrite it since I am arrgoant
+# enough to think I know what I'm doing in POSIX shell.
+#
+# If someone needs a pre-POSIX version of mkdirhier, they'll probably need to
+# turn to someone else, as I have no idea where such a thing is specified.
-for directory
-do
- case $directory in
- '')
- echo >&2 "mkdirhier: empty directory name"
- status=1
- continue;;
- *"$newline"*)
- echo >&2 "mkdirhier: directory name contains a newline: \`\`$directory''"
- status=1
- continue;;
- ///*) prefix=/;; # See Posix 2.3 "path".
- //*) prefix=//;;
- /*) prefix=/;;
- -*) prefix=./;;
- *) prefix=
- esac
+set -e
- IFS=/
- set x $directory
- case $2 in
- */*) # IFS parsing is broken
- IFS=' '
- set x `echo $directory | tr / ' '`
- ;;
- esac
- IFS=$newline
- shift
+PROGNAME=${0##*/}
+STATUS=0
- for filename
- do
- path=$prefix$filename
- prefix=$path/
- shift
+usage() {
+ cat <<EOF
+usage: $PROGNAME DIRECTORY ...
+Create each directory DIRECTORY, also creating intermediate directories in the
+specified hierarchy as necessary.
- test -d "$path" || {
- paths=$path
- for filename
- do
- if [ -n "$filename" -a "$filename" != "." ]; then
- path=$path/$filename
- paths=$paths$newline$path
- fi
- done
+Note: Use "mkdir -p" instead of "$PROGNAME" if the system supports it.
+EOF
+}
- mkdir $paths || status=$?
+makedir () {
+ FUNC_STATUS=0
+ # Does the desired directory already exist?
+ if ! [ -d "$1" ]; then
+ # Is a directory hierarchy specified (i.e., are any slashes in the
+ # argument)?
+ PARENT=${1%/*}
+ if [ -n "$PARENT" ] && [ "$PARENT" != "$1" ]; then
+ # Yes; does the desired directory's immediate parent exist?
+ if ! [ -d "$PARENT" ]; then
+ # No; push it onto the stack. If that fails, return
+ # immediately, as we know later calls will also fail. E.g., if
+ # we are asked to create /usr/bin/foo/bar/baz/quux and
+ # /usr/bin/foo fails, we don't have to even try anything deeper
+ # in the hierarchy.
+ if ! makedir "$PARENT"; then
+ return $FUNC_STATUS
+ fi
+ fi
+ fi
+ mkdir "$1" || FUNC_STATUS=$?
+ fi
+ return $FUNC_STATUS
+}
- break
- }
- done
- done
+if [ -z "$1" ]; then
+ usage >&2
+ exit 64
+fi
-exit $status
+while [ -n "$1" ]; do
+ ARG="$1"
+ makedir "$ARG" || \
+ {
+ STATUS=$?
+ echo "$PROGNAME: could not create directory \"$ARG\"" >&2
+ }
+ shift
+done
+
+exit $STATUS
+
+# vim:set ai et sts=4 sw=4 tw=80:
Index: a/imake/mkdirhier.man
===================================================================
--- a/imake/mkdirhier.man (revision 309)
+++ b/imake/mkdirhier.man (working copy)
@@ -1,42 +1,111 @@
-.\" $Xorg: mkdirhier.man,v 1.4 2001/02/09 02:03:17 xorgcvs Exp $
-.\" Copyright (c) 1993, 1994, 1998 The Open Group
-.\"
-.\" Permission to use, copy, modify, distribute, and sell this software and its
-.\" documentation for any purpose is hereby granted without fee, provided that
-.\" the above copyright notice appear in all copies and that both that
-.\" copyright notice and this permission notice appear in supporting
-.\" documentation.
-.\"
-.\" The above copyright notice and this permission notice shall be included in
-.\" all copies or substantial portions of the Software.
-.\"
+.\" Copyright 2005 Branden Robinson.
+.\"
+.\" Permission is hereby granted, free of charge, to any person obtaining a
+.\" copy of this software and associated documentation files (the "Software"),
+.\" to deal in the Software without restriction, including without limitation
+.\" the rights to use, copy, modify, merge, publish, distribute, sublicense,
+.\" and/or sell copies of the Software, and to permit persons to whom the
+.\" Software is furnished to do so, subject to the following condition:
+.\"
+.\" The above copyright notice and this permission notice shall be
+.\" included in all copies or substantial portions of the Software.
+.\"
.\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
.\" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-.\" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
-.\" THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-.\" WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
-.\" OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-.\" SOFTWARE.
-.\"
-.\" Except as contained in this notice, the name of The Open Group shall not
-.\" be used in advertising or otherwise to promote the sale, use or other
-.\" dealing in this Software without prior written authorization from The
-.\" Open Group.
-.\"
-.\" $XFree86: xc/config/util/mkdirhier.man,v 1.2 2001/01/27 18:19:55 dawes Exp $
-.\"
-.TH MKDIRHIER 1 __xorgversion__
+.\" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+.\" SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR
+.\" OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+.\" ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+.\" DEALINGS IN THE SOFTWARE.
+.TH mkdirhier 1 __xorgversion__
.SH NAME
-mkdirhier \- makes a directory hierarchy
+mkdirhier \- create a directory hierarchy
.SH SYNOPSIS
.B mkdirhier
-directory ...
+.I directory
+\&...
.SH DESCRIPTION
-The
-.I mkdirhier
-command creates the specified directories. Unlike
-.I mkdir
-if any of the parent directories of the specified directory
-do not exist, it creates them as well.
+.B mkdirhier
+creates the specified directories.
+Unlike some versions of
+.BR mkdir ,
+if any of the parent directories of the specified directory do not exist,
+.B mkdirhier
+creates them as well.
+.PP
+.B mkdirhier
+is a wrapper for
+.BR mkdir ;
+systems with
+.BR mkdir (__osmansuffix__)
+implementations conformant with the Single Unix Specification may simply
+use
+.B mkdir \-p
+instead \(em this includes all systems using the GNU Core Utilities'
+version of
+.BR mkdir .
+.SH DIAGNOSTICS
+If
+.B mkdirhier
+is not supplied with any arguments, a usage message is printed.
+.PP
+.B mkdirhier
+prefixes its diagnostic messages with the name under which it was invoked,
+followed by a colon (\(oq:\(cq) so that its messages can be distinguished
+from others.
+.TP
+.B could not create directory \(dq\fIdirectory\fP\(dq
+indicates that there was a failure while creating
+.IR directory .
+This message will likely be preceded by a diagnostic message from
+.BR mkdir .
+.SH "EXIT STATUS"
+.TP
+.B 64
+.B mkdirhier
+was not given any directory names to create.
+.PP
+.B mkdirhier
+otherwise exits with the exit status of the last
+.B mkdir
+command that failed.
+.SH BUGS
+.B mkdirhier
+does not create all the requested directories as an atomic operation;
+therefore, it is is susceptible to race conditions.
+For example, if
+.B mkdirhier
+is directed to create a hierarchy
+.I a/b/c/d
+and any of
+.IR a/ ,
+.IR b/ ,
+or
+.I c/
+do not yet exist, any of the newly-created directories can be removed
+and/or replaced by a symbolic link to another location in the window of
+time after
+.B mkdirhier
+creates a directory and the directory immediately below it.
+This means that failures can be provoked (since
+.B mkdir
+will fail to create a directory in a directory that does not already
+exist),
+or directories may be created in unexpected locations.
+The same limitation holds for multiple directory arguments to
+.BR mkdirhier ;
+given two arguments
+.I a/b/c/d
+and
+.IR a/b/c/f ,
+it is possible for the directory hierarchy
+.I a/b/c
+to be disrupted in the time between the processing of the two arguments.
+To avoid these problems, use
+.B mkdir \-p
+instead, or do not use
+.B mkdirhier
+to create directories in parts of the filesystem where untrusted users can
+manipulate them.
.SH "SEE ALSO"
-mkdir(1)
+.BR mkdir (__osmansuffix__)
|