-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanonicalize-dir.sh
More file actions
333 lines (278 loc) · 8.04 KB
/
Copy pathcanonicalize-dir.sh
File metadata and controls
333 lines (278 loc) · 8.04 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
# candr.sh / canonicalize-dir.sh
# shellcheck disable=SC2319,SC2292
#
# Script is finished - 12 Sept 2026 WY
#
# Manual canonicalization of directories.
#
# Written for any POSIX-2008 compliant shell. This script can be
# executed successfully in resticted mode. With a few adjustments,
# a list of directories can be canonicalized in the same execution.
#
# (c) Wiley Young 2026
#
# Tested:
# 2026-06-11; Fedora 44; sh(bash), bash 5.3.9, dash 0.5.13, ksh 1.0.10,
# mksh 59c, oksh 7.8.2, yash 2.61, zsh 5.9, and busybox(ash) 1.37.0
#<> Debug
reset #<>
clear #<>
set -e #<>
set -u #<>
#set -x #<>
# Some input required
if [ "$#" -eq 0 ]
then
: "t:0:${LINENO}" #<>
printf 'Input required; exiting.\n' 1>&2
exit "${LINENO}"
else : "f:$?:${LINENO}" #<>
fi
#
_print_findings_()
{
case "$1" in
-v|--verbose )
printf 'Input:\t\t\t%s\n' "$2"
printf ' Canonicalized path:'
if [ "$2" = "$3" ]
then
: "t:0:${LINENO}" #<>
true
else
: "f:$?:${LINENO}" #<>
printf '*'
fi
printf '\t%s\n' "$3"
# Note, to check just one directory, uncomment this \exit\ command;
# to check all directories, comment it.
exit "${LINENO}"
;;
-b|--brief )
printf '%s\n' "$3"
;;
*)
printf 'Error; exiting: %s.\n' "${2}" 1>&2
exit "${LINENO}"
;;
esac
}
:;: "Major loop"
# Note, a list of some of the common temporary directories in use in Linux.
for dd in "$@" /dev/shm /var/tmp /usr/tmp /usr/local/tmp \
"${XDG_RUNTIME_DIR:=}/tmp" /tmp ~/tmp ~
do
:;: "Get the original string."
cc=${dd}
:;: "The path must exist in the filesystem."
if [ -e "${dd}" ]
then
: "t:0:${LINENO}" #<>
true
else
: "f:$?:${LINENO}" #<>
:;: "The path does not exist on this system. Next pathname."
_print_findings_ -v "${cc}" \
"NA: Input pathname does not exist on this filesystem."
continue 1
fi
:;: "Minor loop A: Use \\file\\ to resolve any symlinks."
while true
do
# Note, these data manipulations must be within minor loop A.
:;: "Remove trailing forward slashes if doing so won\\t empty the var."
if [ "${#dd}" -gt 1 ]
then
: "t:0:${LINENO}" #<>
# Note, if the directory is \/\, FS root, then PE\s an error.
dd=${dd%/}
else
: "f:$?:${LINENO}" #<>
fi
:;: "Remove leading double dots."
dd=${dd#..}
# Remove leading double dots if doing so won\t empty the variable.
#if [ "${#dd}" -gt 3 ]
#then
#: "t:0:${LINENO}" #<>
#dd=${dd#..}
#else
#: "f:$?:${LINENO}" #<>
#fi
:;: "Add in a missing leading forward slash, if output\\s a dir."
if [ -d "${dd}" ]
then
: "t:0:${LINENO}" #<>
: "\\dd\\ is a directory."
else
: "f:$?:${LINENO}" #<>
: "\\dd\\ is not a directory."
if [ -d "/${dd}" ]
then
: "t:0:${LINENO}" #<>
: "Adding a leading forward slash makes \\dd\\ a directory."
dd=/${dd}
else
: "f:$?:${LINENO}" #<>
: "Adding a leading fwd. slash doesn\\t make \\dd\\ a directory."
fi
fi
:;: "Get output of \\file\\."
file_out_0=$( file "${dd}" )
:;: "See whether \\file\\ says \\symbolic link\\."
if printf '%s' "${file_out_0}" \
| grep -qEe ': symbolic link to [/\.a-z]'
then
: "t:0:${LINENO}" #<>
:;: "If so, then use \\sed\\ to get the resolved path."
sed_out_0=$(
printf '%s' "${file_out_0}" \
| sed 's,.*: symbolic link to ,,'
)
:;: "...and redefine \\dd\\ for the next iteration."
dd=${sed_out_0}
else
: "f:$?:${LINENO}" #<>
:;: "...otherwise, test if \\directory\\ is present in the string."
if printf '%s' "${file_out_0}" \
| sed 's,.*: ,,' \
| grep -q "directory"
then
: "t:0:${LINENO}" #<>
:;: "If it is, then \\break\\ this loop."
break 1
else
: "f:$?:${LINENO}" #<>
:;: "If it\\s neither a symlink nor a directory, that\\s an error."
echo Error 1>&2
exit "${LINENO}"
fi
fi
done
:;: "Record what aught to be the correct, canonicalized absolute path."
canpth=${dd}
# Note, it\s something of an assumption at this time that \file\ will
# remove any symlinks from the interior of the absolute pathname --
# hence minor loop B.
:;: "Define a variable \\ee\\ that can be re-defined within a loop."
ee=${canpth}
:;: "Minor loop B: Make sure every dir. in the abs. path is not a sym."
while true
do
:;: "If \\ee\\ is a directory..."
if [ -d "${ee}" ]
then
: "t:0:${LINENO}" #<>
:;: "Then get the \\dirname\\ of \\ee\\."
ee=$( dirname "${ee}" )
:;: "If the \\dirname\\ of \\ee\\ is also a directory..."
if [ -d "${ee}" ]
then
: "t:0:${LINENO}" #<>
:;: "Then, if the new \\ee\\ is the filesystem root, \\/\\..."
if [ "${ee}" = "/" ]
then
: "t:0:${LINENO}" #<>
:;: "Then double-check it using \\grep\\."
if printf '%s\n' "${ee}" \
| grep -qEe '^/$'
then
: "t:0:${LINENO}" #<>
# Note, to check all directories, uncomment this block and
# comment the other.
#:;: "\\grep\\ succeeded, so \\break\\ this loop."
#_print_findings_ -b "${cc}" "${canpth}"
#break 1
# Note, to check just one directory, uncomment this block and
# comment the other.
:;: " \\grep\\ succeeded, so \\break\\ all loops."
_print_findings_ -b "${cc}" "${canpth}"
break 2
else
: "f:$?:${LINENO}" #<>
:;: "\\test\\ passed but \\grep\\ failed. Error. Exiting."
exit "${LINENO}"
fi
else
: "f:$?:${LINENO}" #<>
:;: "The new \\ee\\ is not the FS root directory; next iteration."
continue 1
fi
else
: "f:$?:${LINENO}" #<>
:;: "The \\dirname\\ of \\ee\\ is not a directory."
exit "${LINENO}"
fi
else
: "f:$?:${LINENO}" #<>
:;: "Error: \\ee\\ is not a directory. Exiting."
exit "${LINENO}"
fi
done
done
:;: "Print warnings if the directory is not readable\/writable\/searchable."
:;: "Verify it\\s a directory by using \\find\\."
find_out=$( find "${canpth}" -prune -type d )
if [ -n "${find_out}" ]
then
: "t:0:${LINENO}" #<>
:;: "See whether \\canpth\\ and \\find_out\\ are equivalent strings."
if [ "${canpth}" = "${find_out}" ]
then
: "t:0:${LINENO}" #<>
:;: "See whether \\canpth\\ is a symlink."
if [ ! -L "${canpth}" ]
then
: "t:0:${LINENO}" #<>
:;: "\\canpth\\ is not a symlink"
# Tests in serial:
:;: "See whether \\canpth\\ is searchable."
if [ -x "${canpth}" ]
then
: "t:0:${LINENO}" #<>
:;: "\\canpth\\ is searchable."
else
: "f:$?:${LINENO}" #<>
:;: "\\canpth\\ is not searchable."
printf 'Warning: directory is not searchable.\n' 1>&2
fi
:;: "See whether \\canpth\\ is readable."
if [ -r "${canpth}" ]
then
: "t:0:${LINENO}" #<>
:;: "\\canpth\\ is readable."
else
: "f:$?:${LINENO}" #<>
:;: "\\canpth\\ is not readable."
printf 'Warning: directory is not readable.\n' 1>&2
fi
:;: "See whether \\canpth\\ is writable."
if [ -w "${canpth}" ]
then
: "t:0:${LINENO}" #<>
:;: "\\canpth\\ is writable."
else
: "f:$?:${LINENO}" #<>
:;: "\\canpth\\ is not writable."
printf 'Warning: directory is not writable.\n' 1>&2
fi
else
: "f:$?:${LINENO}" #<>
:;: "\\canpth\\ is a symlink."
echo Error 1>&2
exit "${LINENO}"
fi
else
: "f:$?:${LINENO}" #<>
:;: "\\canpth\\ and \\find_out\\ are not equivalent strings."
echo Error 1>&2
exit "${LINENO}"
fi
else
: "f:$?:${LINENO}" #<>
:;: "\\find -type d\\ did not locate the directory."
echo Error 1>&2
exit "${LINENO}"
fi
exit 00