-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhrx
More file actions
executable file
·647 lines (541 loc) · 14.6 KB
/
Copy pathhrx
File metadata and controls
executable file
·647 lines (541 loc) · 14.6 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#!/bin/sh
set -eu
case $1 in -x) set -x; shift; esac
# Warning: problematic for binary files,
# shell scripts can not have null char $'\0' in vairables
o=${0##*["${PATHSEP:-/}"]}
log() {
printf %s\\n >&2 "$o: $1"
}
die() {
log "$1"
exit "${2-2}"
}
case $# in 0) die "use --help for usage"; esac
case $1 in --help)
printf %s\\n \
"Usage: $o c|x|t [-f HRXFILE] [-b BOUNDARY] [OPTION]... [FILE]..." \
"" \
"Create, extract, or list files from a hrx file" \
"" \
"(soon ) c Create" \
"(ok, bugs:eof-nl,mparents ) x Extract" \
"(in progress - mostly done ) t List" \
"(ok ) -f FILE Name of HRXFILE ('-' for stdin/out)" \
"(consider ) -C DIR Change to DIR before operation" \
"(soon ) -v Verbose" \
"(soon ) -O Extract to stdout" \
"(consider extension ) -m Don't restore mtime" \
"(consider extension ) -o Don't restore user:group" \
"(consider/soon exists check ) -k Don't replace existing files" \
"(won't add ) -Z (De)compress using compress" \
"(won't add ) -z (De)compress using gzip" \
"(won't add ) -J (De)compress using xz" \
"(won't add ) -j (De)compress using bzip2" \
"(won't add ) --lzma (De)compress using lzma" \
"(won't add/consider ) -a (De)compress based on extension" \
"(consider extension ) -h Follow symlinks ( consider extension to hrx -> .hrx+unix whene boundary have permissions and allows symlinks )" \
"(consider glob ) -T FILE File with names to include" \
"(consider glob ) -X FILE File with glob patterns to exclude" \
"(consider glob ) --exclude PATTERN Glob pattern to exclude" \
"(consider/soon exists check ) --overwrite Replace existing files" \
"(nothing done ) --strip-components NUM NUM of leading components to strip" \
"(nothing done ) --no-recursion Don't descend in directories" \
"(consider extension ) --numeric-owner Use numeric user:group" \
"(consider extension ) --no-same-permissions Don't restore access permissions" \
"" \
"GNU tar:" \
"(consider/soon exists check ) --skip-old-files don't replace existing files when extracting, silently skip over them" \
;
exit; esac
# (todo) errors to consider:
#tar: This does not look like a tar archive
#tar: adoestna: Not found in archive
#tar: Exiting with failure status due to previous errors
case $1 in [!-]*) set -- -"$@"; esac # first argument is always an option
OPTIND=1;
unset opt_c opt_x opt_t opt_f opt_f_val opt_b opt_b_val
while getopts cxtvOf:b: i; do
case $i in
c) opt_c='';;
x) opt_x='';;
t) opt_t='';;
v) opt_v='';;
O) opt_O='';;
f) opt_f=''; opt_f_val=$OPTARG;;
b) opt_b=''; opt_b_val=$OPTARG;;
?) exit;
esac
done
shift $((OPTIND - 1))
case ${opt_c+c}${opt_x+x}${opt_t+t} in
'') die "specify one of -c, -x or -t option";;
??*) die "specify only one of -c, -x or -t option";;
esac
case ${opt_b+b} in b)
case $opt_b_val in
0|'<>'|'') die "0 boundary '<>' is not allowed";;
"${opt_b_val#*[!0-9]*}") # when it is a num (note: including 0, and not including negative nums in -*)
_i=$opt_b_val
boundary='<'
while case $_i in *[!0-9]*) log "ERROR 5"; false;; 0) false; esac; do
boundary=$boundary'='
: $(( _i = _i - 1 )) || exit
done
boundary=$boundary'>'
;;
'<'*'='*'>') # bad: lazy check
boundary=$opt_b_val
;;
=|=*=) # bad: lazy check
boundary='<'$opt_b_val'>'
;;
esac
esac
# path-character ::= any character other than
# U+0000 through U+001F
# 00 NUL # no need to/can't detect, shell scripts does no support it. native shell eror may be printed (if the shell has implemented it)
# 01 SOH
# 02 STX
# 03 ETX
# 04 EOT
# 05 ENQ
# 06 ACK
# 07 BEL
# 08 BS
# 09 TAB
# 0A LF
# 0B VT
# 0C FF
# 0D CR
# 0E SO
# 0F SI
# 10 DLE
# 11 DC1
# 12 DC2
# 13 DC3
# 14 DC4
# 15 NAK
# 16 SYN
# 17 ETB
# 18 CAN
# 19 EM
# 1A SUB
# 1B ESC
# 1C FS
# 1D GS
# 1E RS
# 1F US
# ,
# DEL U+007F DELETE,
# '/' U+002F SOLIDUS, # used as path separator (wont detect)
# ':' U+003A COLON or
# '\' U+005C REVERSE SOLIDUS
path_chars_blacklist=$(
printf \
\\x01\
\\x02\
\\x03\
\\x04\
\\x05\
\\x06\
\\x07\
\\x08\
\\x09\
\\x0A\
\\x0B\
\\x0C\
\\x0D\
\\x0E\
\\x0F\
\\x10\
\\x11\
\\x12\
\\x13\
\\x14\
\\x15\
\\x16\
\\x17\
\\x18\
\\x19\
\\x1A\
\\x1B\
\\x1C\
\\x1D\
\\x1E\
\\x1F\
\
\\x3A\
\\x5C\
;
)
# fns:
# comment:
case ${opt_v+v} in # consider: using option to get only the comments?
v)
hrx_parse_begin_comment() { _hrx_parse__begin_matched=''; }
hrx_parse_comment() { printf %s\\n >&2 "comment: $line"; }
hrx_parse_end_comment() { :; }
;;
*)
hrx_parse_begin_comment() {
:
###_hrx_parse__begin_matched=''
}
###hrx_parse_comment() { :; }
###hrx_parse_end_comment() { :; }
esac
# invalid:
hrx_parse_begin_x_invalid() {
count_invalid=$(( invalid + 1 ))
###_hrx_parse__begin_matched
}
###hrx_parse_x_invalid() { :; }
###hrx_parse_end_x_invalid() { :; }
# unused ends:
###hrx_parse_end_dir() { :; }
hrx_parse_end_file() { :; } # TODO: WHEN '-c' opt specified CREASING ? DOES THIS LINE HAVE TO BE HERE???
case ${opt_c+c}${opt_x+x}${opt_t+t} in
x|t)
# fns for parsing:
in_line() {
printf %s "${1-, }in line $line_num: '$line'"
}
parsing_err() {
die "parsing error($1)$(in_line)" "${2-4}"
}
error() {
die "ERROR $1$(in_line)" "${2-4}"
}
hrx_is_boundary() {
case $1 in
"$boundary"|"$boundary "*) return 0;;
*) return 1;;
esac
}
hrx_parse_boundary_fn1() { # might be needed when `-c` option to auto parese boundary in all files
case $1 in
\<=*\>*) ;;
*) return 1;;
esac
_i=${line#?}
_i=${_i%%\>*}
case ${_i} in
''|*[!=]*) return 1;;
*) boundary=${_i%%\>*}\>
esac
_i=\<${_i}\>
case $line in
"$_i"*) boundary=$_i;; # found first boundary
*) error 1;;
esac
}
hrx_parse_boundary_fn2() {
case ${boundary+b} in
b) # note: if boundary var present, then we find the first line that matches that boundary (skiping start)
#hrx_parse_boundary_fn1
while :; do
case $line in "$boundary "*) break; esac
#log "ignoring begining line ${line_num}: ${line}"
read -r line || break # seems like empty archive
done
;;
*)
hrx_parse_boundary_fn1 "$line" || { # sets 'boundary'
die "mallformed entry, can not parse boundary$(in_line)"
}
;;
esac
}
esac
case ${opt_c+c}${opt_x+x}${opt_t+t} in
x)
_match_path() {
case $# in
0)
return 1
;;
*)
for _i; do
case $path in "$_i"|"$_i"/*) # TODO:! args might be in '*/' todo: iterate over args and trim all end '/'
return 1
esac
done
esac
return 0
}
# -O option in tar combined with -t or with -xv seems to behave the same way, also filenames got printed to stdout after file content
# (I don't see a point of copying this behaviour, this implementation will print the file to begining of the entry - before the file content)...
# `entry_type` should always be in (dir|file|comment|x_invalid)
# -t & -x dir:
hrx_parse_begin_dir() {
count_explisit_dirs=$(( explisit_dirs + 1 ))
if _match_path "$@"; then
case ${opt_t+t}${opt_v+v} in ?*)
printf %s\\n "$path"
esac
case ${opt_O+O}:${opt_x+x} in :x)
mkdir -p -- "$path" || {
count_err=$(( count_err + 1 ))
log "note: failed to create dir: '$path'"
}
esac
fi
# case ${opt_v+v} in v) # TODO
# log "todo: what sohuld -tv options do?"
# esac
###_hrx_parse__begin_matched=''
}
###hrx_parse_dir() {
### case $line in ?*)
### log "todo: dir body should not have content? or allow empty lines? (path=$path$(in_line))" # TODO
### esac
###}
# -t & -x file:
hrx_parse_begin_file() {
unset _hrx_parse__begin_matched
if ! _match_path "$@"; then
return
fi
case ${opt_t+t}${opt_v+v} in ?*)
printf %s\\n "$path"
esac
case ${opt_O+O}:${opt_x+x} in :x)
path_basedir=${path%/[!/]*}
case $path_basedir in ''|"$path") ;; *)
if [ ! -d "$path_basedir" ]; then
mkdir -p -- "$path_basedir" || {
log "failed to create path_basedir directory (expect create/reset file content to fail): '$path_basedir', for file: $path"
}
fi
esac
##unset path_basedir
:> "$path" || { # (unshure comment) consider stopping hrx_parse_file
count_err=$(( count_err + 1 ))
log "failed to create/reset content of file: $path"
return
}
esac
_hrx_parse__begin_matched=''
count_files=$(( files + 1 ))
_hrx_parse_file__write_err_lines=0
}
hrx_parse_file() {
case ${opt_O+O}:${opt_x+x} in
O:*)
printf %s\\n "$line"
;;
:x)
printf %s\\n "$line" >> "$path" || { # note: $line var can not have null char $'\0'
# do not spam for every line
_hrx_parse_file__write_err_lines=$(( _hrx_parse_file__write_err_lines + 1 ))
}
;;
esac
}
hrx_parse_end_file() {
case $_hrx_parse_file__write_err_lines in 0);; *)
count_err=$(( count_err + 1 ))
log "failed to write $_hrx_parse_file__write_err_lines lines to file: $path"
esac
}
hrx_get_entry_type() {
# first time needs to get boundary, after this redefine fn
hrx_parse_boundary_fn2
hrx_get_entry_type() {
unset path
case $line in
"$boundary") entry_type=comment;;
"$boundary "*/) entry_type=dir;;
"$boundary "?*) entry_type=file;;
"$boundary ")
log "warning: comment with space before new line / seems like empty path? $(in_line '')"
entry_type=comment
;;
*) parsing_err 2 1;;
esac
case $entry_type in dir|file) # parsing+validate var `path`
path=${line#"$boundary "}
case $line in "$boundary $path") ;; *)
error 3
esac
while case $path in /*) ;; *) false; esac; do
log "note: triming absolute path$(in_line)"
path=${path#/}
done
case $path in
'../'*|*'/../'*|*'/..'|'..')
log "relative paths contaning '..' are not allowed, path='$path', SKIPING"
entry_type=x_invalid
;;
'') # expected when trimming '/'|'///'
log "empty path, path='$path'$(in_line)"
entry_type=x_invalid
;;
*["$path_chars_blacklist"]*)
log "note: found disawolled char, path='$path'. (allowing it anyway)"
;;
esac
esac
case $entry_type in
dir|file) _needs_args='';;
comment|x_invalid) unset _needs_args;;
*) unset _needs_args;;
esac
}
hrx_get_entry_type "$@"
}
hrx_extract() {
line_num=0
unset ended
unset _needs_args # slightly improve performance for large amount of arguments
while read -r line; do
line_num=$(( line_num + 1 ))
case ${entry_type-} in '') ;; *)
! hrx_is_boundary "$line" || {
${_hrx_parse__begin_matched+hrx_parse_end_"$entry_type"}
entry_type=''
}
esac
case ${entry_type-} in '')
hrx_get_entry_type "$line"
unset _hrx_parse__begin_matched
hrx_parse_begin_"$entry_type" ${_needs_args+"$@"}
continue
esac
# NOTE: `entry_type` should always be in (dir|file|comment|x_invalid)
hrx_parse_"$entry_type"
done
ended=''
case ${entry_type-} in
'')
if hrx_get_entry_type "$line"; then
unset _hrx_parse__begin_matched
hrx_parse_begin_"$entry_type" ${_needs_args+"$@"}
${_hrx_parse__begin_matched+hrx_parse_end_"$entry_type"}
else
log "empty archive"
fi
return
esac
${_hrx_parse__begin_matched+hrx_parse_end_"$entry_type"}
}
;;
c)
# fns for creating
# consider in thsi is decided: comments wont be allowed
# if no boundary option provided, then will scan all files for conflicting boundary-like lines
# note, same as tar behaiviour -O option is ignore here
# specs say:
# > When a HRX file is extracted, the extraction process should (by default) create a directory named after the HRX file, with the extension ".hrx" removed
# however this implementation of hrx is sticking to what `tar` command does:
# extract files as path in PWD
# (todo) if -C opt specified changes PWD to that dir
case ${opt_b+b} in '')
# for now require to be provided
#die "(todo add auto boundary chooser) for now --boundary option is required when creating archive"
# die "(todo add auto boundary chooser) for now -b (boundary) option is required when creating archive"
# default boundary as specs:
boundary='<===>'
esac
# src: /^/\ https://github.com/denisde4ev/sh/blob/56e23b438756f3fc70b5bda83e9423224df640b7/cat.sh
cat() { # because why not
case $# in 0) ;; *)
case $1 in --) shift; esac
case $1 in -) ;; *)
cat < "$1"
return
esac
esac
while read -r line; do # note: $line var can not have null char $'\0'
printf %s\\n "$line"
done
printf %s "$line"
}
hrx_create() {
for file; do
file_type=''
if
[ -L "$file" ]; then file_type=link; elif
[ -f "$file" ]; then file_type=file; elif
[ -d "$file" ]; then file_type=dir; elif
[ -e "$file" ]; then file_type=exists; elif
true; then :; fi
# NOTE: here unexplicitly calling fn-s `hrx_create_link|hrx_create_file|hrx_create_dir|hrx_create_exists`
hrx_create_"$file_type"1 "$file"
done
}
hrx_create_link1() {
printf %s\\n "$boundary $1 (Symlink)"
readlink -- "$1"
}
## todo: something here # case ${opt_1+1} in
hrx_create_dir1() {
printf %s\\n "$boundary $1/"
# recurse inside of directory here:
set -- "$1"/*
if [ -e "$1" ] || [ -L "$1" ]; then
hrx_create "$@"
#else
# set --
fi
}
hrx_create_exists1() {
die "unrecognized file type for: file='${1}'" # TODO:! test this / ask ai what could happen
}
hrx_create_file1() {
case "$1" in "${opt_f_val:?}") log "warning: skiping archive output itself: file='$1'"; return; esac
printf %s\\n "$boundary $1"
cat "$1"
}
;;
t)
#example:
#F 2
#L bb -> aa
#D dir/
#F dir/123
hrx_test() {
#case ${1+x} in x)
case $# in [1-9]*) die "not expected to get extra arguments: \$1='$1'";
# todo
esac
echo NOT DONE! >&2; exit 123;
while IFS='' read -r line; do
exit 123
done < "${opt_f_val:?}"
}
;;
esac
IFS='' # do not trim `read -r line`
case ${opt_b+b} in
b) boundary=$opt_b_val;;
*) unset boundary;;
esac
count_files=''
count_explisit_dirs=''
count_invalid=''
count_err=0
main() {
unset -f main
case ${opt_c+c}${opt_x+x}${opt_t+t} in
t)
: log "NOTE: 'hrx -t' is in progress"
hrx_test "$@"
;;
x)
log 'hrx -x : NOT TESTED'
hrx_extract "$@"
;;
c)
[ ! -e "${opt_f_val:?}" ] || die "arhive file already exists" # TODO: consider further implement appending to file
hrx_create "$@" >> "${opt_f_val:?}"
;;
esac
}
case ${opt_f_val-} in -) unset opt_f; esac # if its '-' then same as missing -> read from stdin
# and do not add test for if the stdin is the tty, hrx is human readable and *typeable*
case ${opt_f+f} in
f) main "$@" < "$opt_f_val";;
*) main "$@";;
esac