-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibfiles.sh
736 lines (672 loc) · 18.1 KB
/
libfiles.sh
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#!/bin/bash
function calcshasum {
file="$1"
if [ -f "$file" ]; then
local shasum=$(shasum "$file")
local pattern='^([^ ]+) '
if [[ "$shasum" =~ $pattern ]]; then
shasum=${BASH_REMATCH[1]}
else
errcho "Wrong shasum format"
fi
else
shasum=""
fi
echo $shasum
}
function apply_patch {
local file="$1"
local hash_orig="$2"
local hash_dest="$3"
local patchfile="$4"
if [[ -f "$file" ]] && [[ -f "$patchfile" ]]; then
local shasum=$(calcshasum "$file")
if [[ "$shasum"=="$hash_orig" ]]; then
if patch --dry-run "$file" "$patchfile" >/dev/null; then
if [ -w "$file" ]; then
logexec patch "$file" "$patchfile"
else
logexec sudo patch "$file" "$patchfile"
fi
else
errcho "Error while applying the patch"
fi
elif [[ "$(shasum "$file")"=="$hash_dest" ]]; then
#Do nothing. Work is already done
return 0
else
errcho "Wrong contents of the $file. Expected hash $hash_orig."
return 1
fi
else
errcho "Missing $file"
return 1
fi
}
function edit_bash_augeas {
local file=$1
local var=$2
local value=$3
install_apt_package augeas-tools augtool
local oldvalue=$(sudo augtool -L -A --transform "Shellvars incl $file" get "/files${file}/${var}" | sed -En 's/\/.* = \"?([^\"]*)\"?$/\1/p')
if [ "$value" != "$oldvalue" ]; then
logexec sudo augtool -L -A --transform "Shellvars incl $file" set "/files${file}/${var}" "${value}" >/dev/null
fi
}
function logmkdir {
local dir=$1
local user=$2
if ! [ -d "$dir" ]; then
logexec sudo mkdir -p "$dir"
fi
if [ -n "$user" ]; then
curuser=$(stat -c '%U' "${dir}")
if [[ "$curuser" != "$user" ]]; then
logexec sudo chown -R ${user}:${user} "$dir"
fi
fi
}
function linetextfile {
local plik="$1"
local line="$2"
if ! grep -qF -- "$line" "$plik"; then
if [ -w "$plik" ]; then
# $loglog
echo "$line" >> "$plik"
else
# $loglog
echo "$line" | sudo tee -a "$plik"
fi
return 0
fi
return 1
}
function textfile {
local plik=$1
local contents=$2
local user=$3
if [ -z "$user" ]; then
user="$USER"
fi
local flag
logmkdir $(dirname ${plik}) $user
if [ ! -f "${plik}" ]; then
flag=1
else
tmpfile=$(mktemp)
echo "$contents" > "${tmpfile}"
if ! cmp $tmpfile $plik; then
flag=1
fi
fi
if [ "$flag" == "1" ]; then
if [ -w "${plik}" ] && [ "$user" == "$USER" ]; then
loglog
echo "$contents" | tee "${plik}" >/dev/null
else
loglog
echo "$contents" | sudo -u "$user" -- tee "${plik}" >/dev/null
fi
return 0
fi
return 1
}
function install_file {
local input_file="$1"
local dest="$2"
local user=$3
if [ -z ${user} ]; then
user=auto
fi
if [ ! -f "${input_file}" ]; then
errcho "Cannot find ${input_file}"
# return 1
fi
if [ -z "$dest" ]; then
dest=/usr/local/bin
fi
if [ -d "$dest" ]; then
dest="${dest}/$(basename "$input_file")"
fi
if [ -f "$dest" ]; then
if ! cmp "$dest" "$input_file"; then
if [ -w "$dest" ]; then
logexec cp "$input_file" "$dest"
else
logexec sudo cp "$input_file" "$dest"
fi
fi
fi
if [ ! -f "$dest" ]; then
if [ -w "$dest" ]; then
logexec cp "$input_file" "$dest"
else
logexec sudo cp "$input_file" "$dest"
fi
fi
if [ ! -f "$dest" ]; then
errcho "Error when copying ${input_file} into ${dest}"
# return 1
fi
if [ $user != "auto" ]; then
cur_owner="$(stat --format '%U' "$dest")"
if [ "$user" != "$cur_owner" ]; then
if [ -w "$dest" ]; then
logexec chown $user "$dest"
else
logexec sudo chown $user "$dest"
fi
fi
fi
}
function set_executable {
local input_file="$1"
if [[ -f "$dest" ]]; then
if [[ ! -x "$dest" ]]; then
if [ -w "$dest" ]; then
logexec chmod +x "$dest"
else
logexec sudo chmod +x "$dest"
fi
fi
if [[ ! -x "$dest" ]]; then
errcho "Cannot set executable permission to $dest"
return 1
fi
else
errcho "File $dest is not found"
return 1
fi
}
function set_non_executable {
local input_file="$1"
if [[ -f "$dest" ]]; then
if [[ ! -x "$dest" ]]; then
if [ -w "$dest" ]; then
logexec chmod -x "$dest"
else
logexec sudo chmod -x "$dest"
fi
fi
if [[ -x "$dest" ]]; then
errcho "Cannot unset executable permission to $dest"
return 1
fi
else
errcho "File $dest is not found"
return 1
fi
}
function install_script {
local input_file="$1"
local dest="$2"
local user=$3
install_file $input_file $dest $user
set_executable $dest
}
function install_data_file {
local input_file="$1"
local dest="$2"
local user=$3
install_file $input_file $dest $user
set_non_executable $dest
}
function chmod_file {
local file=$1
local desired_mode=$2
local pattern='^[[:digit:]]+$'
local actual_mode
if [[ ! "${desired_mode}" =~ $pattern ]]; then
errcho "Wrong file permissions. Needs octal format."
return 1
fi
if [ ! -f "${file}" ]; then
errcho "File ${file} doesn't exist"
return 1
fi
actual_mode=$(stat -c "%a" ${file})
if [ "${desired_mode}" != "${actual_mode}" ]; then
logexec sudo chmod ${desired_mode} ${file}
fi
}
function chmod_dir {
local file=$1
local desired_mode_dir=$2
local desired_mode_file=$3
local desired_mode_exec_file=$4
local pattern='^[[:digit:]]+$'
local actual_mode
if [[ ! "${desired_mode_file}" =~ $pattern ]]; then
errcho "Wrong file permissions. Needs octal format."
return 1
fi
if [[ ! "${desired_mode_dir}" =~ $pattern ]]; then
errcho "Wrong dir permissions. Needs octal format."
return 1
fi
if [ ! -d "${file}" ]; then
errcho "File ${file} doesn't exist"
return 1
fi
logexec sudo find "$file" -type d -not -perm "$desired_mode_dir" -exec chmod "$desired_mode_dir" {} \;
if [ -z "$desired_mode_file" ]; then
logexec sudo find "$file" -type f -not -perm "$desired_mode_file" -exec chmod "$desired_mode_file" {} \;
else
logexec sudo find "$file" -type f -perm /111 -not -perm "$desired_mode_exec_file" -exec chmod "$desired_mode_exec_file" {} \;
logexec sudo find "$file" -type f -not -perm /111 -not -perm "$desired_mode_file" -exec chmod "$desired_mode_file" {} \;
fi
}
function chown_dir {
local file=$1
local user=$2
local group=$3
if [ -z "$group" ]; then
group="$user"
fi
if [ -z "$user" ]; then
errcho "No user name exitting"
return 1
fi
if [ -z "$file" ]; then
errcho "No path. Exiting"
return 1
fi
if [ ! -d "${file}" ]; then
errcho "File ${file} doesn't exist"
return 1
fi
logexec sudo find "$file" -not -user "$user" -or -not -group "$group" -exec chown "${user}:${group}" {} \;
}
# returns path
function get_cached_file {
local filename="$1"
local download_link="$2"
if [ ! -d "${repo_path}" ]; then
mkdir -p "/tmp/repo_path/$(dirname "${filename}")"
local repo_path="/tmp/repo_path"
fi
if [ ! -f "${repo_path}/${filename}" ]; then
if [ -z "$download_link" ]; then
errcho "File is missing from cache"
return 1
fi
if [ ! -w "${repo_path}" ]; then
if ! sudo chown ${USER} "${repo_path}"; then
echo "Cannot write to the repo ${repo_path}" >/dev/stderr
return 1
fi
local repo_path="/tmp/repo_path"
mkdir -p /tmp/repo_path
fi
wget -c "${download_link}" -O "${repo_path}/${filename}"
fi
if [ ! -f "${repo_path}/${filename}" ]; then
errcho "Cannot download the file"
return 1
fi
echo "${repo_path}/${filename}"
}
function logexec {
exec $@ | true
}
function extract_archive {
local archive_path="$1"
local destination_parent="$2"
local act_as_user="$3"
local single_item_policy="$4" #'rename' - if archive with single folder - the files in that folder will be extracted to the extracted_name subfolder of the destination_parent. The same, if archive with a single file - that file will be renamed into extracted_name, and put in destination_parent.
#'original' - (default) the archive with single folder will be extracted directly to destination_parent, ignoring extracted_name. Similarily, if archive with a single file - that file will be extracted in the destination_parent under its original name.
#'onedir' - if archive with single folder - acts exactly as 'rename' policy. If archive with a single file - that file will be put in the subfolder extracted_name with the name extracted_name.
local extracted_name="$5" #Defaults to the archive name minus extensions
if [[ "$single_item_policy" == "" ]]; then
single_item_policy=original
fi
if [[ "$extracted_name" == "" ]]; then
local extension="${archive_path##*.}"
extracted_name="${archive_path%.*}"
fi
make_sure_dtrx_exists
local dest_path="${destination_parent}/${extracted_name}"
logmkdir "${dest_path}" "$act_as_user"
if is_folder_writable $(dirname "$destination_parent") "$act_as_user"; then
if [ "$act_as_user" == "$USER" ]; then
mode=1
(cd "${dest_path}" ; dtrx --one here "$archive_path")
else
mode=2
(cd "${dest_path}" ; sudo -u "$act_as_user" $(which dtrx) --one here "$archive_path")
fi
else
mode=2
(cd "${dest_path}" ; sudo -u "$act_as_user" $(which dtrx) --one here "$archive_path")
fi
filecount=$(ls "${dest_path}" | wc -l)
if [[ $filecount == 1 ]]; then
filename1=$(ls "${dest_path}" | head -n 1)
if [ -d "${dest_path}/${filename1}" ]; then
if [[ $single_item_policy == original ]]; then
if [[ $mode == 1 ]]; then
mv "${dest_path}/${filename1}" "${destination_parent}/${filename1}"
rmdir "${dest_path}"
else
sudo -u "$act_as_user" mv "${dest_path}/${filename1}" "${destination_parent}/${filename1}"
sudo -u "$act_as_user" rmdir "${dest_path}"
fi
elif [[ $single_item_policy == rename || $single_item_policy == onedir ]]; then
if [[ $mode == 1 ]]; then
find "${dest_path}/${filename1}/" -mindepth 1 -maxdepth 1 -exec mv -t "${dest_path}" -- {} +
rmdir "${dest_path}/${filename1}"
else
sudo -u "$act_as_user" find "${dest_path}/${filename1}/" -mindepth 1 -maxdepth 1 -exec mv -t "${dest_path}" -- {} +
sudo -u "$act_as_user" rmdir "${dest_path}/${filename1}"
fi
fi
elif [ -f "${dest_path}/${filename1}" ]; then
if [[ $single_item_policy == original ]]; then
if [[ $mode == 1 ]]; then
mv "${dest_path}/${filename1}" "${destination_parent}/${filename1}"
rmdir "${dest_path}"
else
sudo -u "$act_as_user" mv "${dest_path}/${filename1}" "${destination_parent}/${filename1}"
sudo -u "$act_as_user" rmdir "${dest_path}"
fi
elif [[ $single_item_policy == rename ]]; then
if [[ $mode == 1 ]]; then
tmpname=$(mktemp -p "${destination_parent}" --dry-run)
mv "${dest_path}/${filename1}" "${tmpname}"
rmdir "${dest_path}"
mv "${tmpname}" "${dest_path}"
else
tmpname=$(sudo -u "$act_as_user" mktemp -p "${destination_parent}" --dry-run)
sudo -u "$act_as_user" mv "${dest_path}/${filename1}" "${tmpname}"
sudo -u "$act_as_user" rmdir "${dest_path}"
sudo -u "$act_as_user" mv "${tmpname}" "${dest_path}"
fi
elif [[ $single_item_policy == onedir ]]; then
if [[ $mode == 1 ]]; then
mv "${dest_path}/${filename1}" "${dest_path}/${extracted_name}"
else
sudo -u "$act_as_user" mv "${dest_path}/${filename1}" "${dest_path}/${extracted_name}"
fi
fi
fi
fi
}
function make_sure_dtrx_exists {
local mute="$1"
if ! which dtrx >/dev/null; then
if [[ $mute == "" ]]; then
install_apt_package dtrx dtrx
else
install_apt_package dtrx dtrx >/dev/null 2>/dev/null
fi
if [[ "$?" != "0" ]]; then
install_apt_package pipx pipx
if [[ $mute == "" ]]; then
pipx install dtrx
else
pipx install dtrx >/dev/null 2>/dev/null
fi
make_sure_dir_is_in_a_path $(get_home_dir)/.local/bin
fi
fi
which dtrx >/dev/null
}
#function get_from_cache_and_uncompress_file {
# local filename="$1"
# local download_link="$2"
# local destination_parent="$3"
# local folder_name="$4"
# local usergr="$5"
# local user
# if [ -z "$usergr" ]; then
# user=$USER
# usergr=$user
# group=""
# else
# local pattern='^([^:]+):([^:]+)$'
# if [[ "$usergr" =~ $pattern ]]; then
# group=${BASH_REMATCH[2]}
# user=${BASH_REMATCH[1]}
# else
# group=""
# user=$usergr
# fi
# fi
#
# if [ ! -f "$filename" ]; then
# path_filename=$(get_cached_file "$filename" "${download_link}")
# else
# path_filename="$filename"
# fi
# if [ -z "$path_filename" ]; then
# echo "no input file $path_filename"
# return 1
# fi
# if [ -z "$destination_parent" ]; then
# echo "no destination $destination_parent"
# return 2
# fi
# if [ -d "$destination_parent" ]; then
# moddate_remote=$(stat -c %y "$destination_parent")
# if [ -f "$timestamp_path" ]; then
# moddate_hdd=$(cat "$timestamp_path")
# if [ "$moddate_hdd" == "$moddate_remote" ]; then
# return 0
# fi
# fi
# fi
#
# extract_archive "$path_filename" "$destination_parent" "$folder_name" "$user"
# local timestamp_path="${destination_parent}/${folder_name}.timestamp"
# if [[ $mode == 1 ]]; then
# echo "$moddate_remote" | tee "$timestamp_path" >/dev/null
# elif [[ $mode == 2 ]]; then
# echo "$moddate_remote" | sudo -u "$user" -- tee "$timestamp_path" >/dev/null
# fi
#}
function uncompress_cached_file {
local filename="$1"
local destination_parent="$2"
if [ -n "$3" ]; then
local usergr="$3"
fi
if [ -n "$4" ]; then
local extracted_name="$4"
fi
if [ "$5" != "" ]; then
local skip_timestamps="$5"
fi
local user
if [ -z "$usergr" ]; then
local user=$USER
usergr=$user
local group="$(id -gn)"
else
local pattern='^([^:]+):([^:]+)$'
if [[ "$usergr" =~ $pattern ]]; then
local group=${BASH_REMATCH[2]}
local user=${BASH_REMATCH[1]}
else
local group=""
local user=$usergr
fi
fi
if [ -z "$extracted_name" ]; then
basename_extension $filename
extracted_name=$(basename $base)
# errcho "Empty extracted_name argument to uncompress_cached_file"
fi
if [ ! -f "$filename" ]; then
path_filename=$(get_cached_file "$filename")
else
path_filename="$filename"
fi
if [ -z "$path_filename" ]; then
echo "no input file $path_filename"
return 1
fi
if [ -z "$destination_parent" ]; then
echo "no destination $destination_parent"
return 2
fi
if [[ "$skip_timestampls" == "" ]]; then
if [ -d "$destination_parent" ]; then
moddate_remote=$(stat -c %y "$destination_parent")
local timestamp_path="$(dirname ${destination_parent})/${extracted_name}/.timestamp"
if [ -f "$timestamp_path" ]; then
moddate_hdd=$(cat "$timestamp_path")
if [ "$moddate_hdd" == "$moddate_remote" ]; then
return 0
fi
fi
fi
fi
extract_archive "$path_filename" "$destination_parent" "$user" onedir "$extracted_name"
if [[ "$skip_timestampls" == "" ]]; then
if [[ $mode == 1 ]]; then
echo "$moddate_remote" | tee "$timestamp_path" >/dev/null
elif [[ $mode == 2 ]]; then
echo "$moddate_remote" | sudo -u "$user" -- tee "$timestamp_path" >/dev/null
fi
fi
}
function cp_file {
local source="$1"
local dest="$2"
local user="$3"
if [ -z "$user" ]; then
errcho "No username!"
fi
i=$((${#dest}-1))
last="${dest:$i:1}"
if [ "$last" == "/" ]; then
destdir="${dest:0:$i}"
destfile="$(basename $source)"
else
destdir="$(dirname $dest)"
destfile="$(basename $dest)"
fi
if [ ! -d "$destdir" ]; then
logmkdir "$destdir" "$user"
fi
if [ -w "$destdir" ]; then
local prefix=""
else
local prefix="sudo"
fi
if [ ! -f "${destdir}/${destfile}" ]; then
logexec ${prefix} cp "${source}" "${dest}"
fi
owner=$(stat -c '%U' "${destdir}/${destfile}")
if [ "$owner" != "$user" ]; then
logexec chmod -R "${user}" "${destdir}/${destfile}"
fi
}
function is_folder_writable {
local folder="$1"
local user="$2"
#source: https://stackoverflow.com/questions/14103806/bash-test-if-a-directory-is-writable-by-a-given-uid
# Use -L to get information about the target of a symlink,
# not the link itself, as pointed out in the comments
INFO=( $(stat -L -c "0%a %G %U" $folder) )
PERM=${INFO[0]}
GROUP=${INFO[1]}
OWNER=${INFO[2]}
ACCESS=no
if (( ($PERM & 0002) != 0 )); then
# Everyone has write access
ACCESS=yes
elif (( ($PERM & 0020) != 0 )); then
# Some group has write access.
# Is user in that group?
gs=( $(groups $user) )
for g in "${gs[@]}"; do
if [[ $GROUP == $g ]]; then
ACCESS=yes
break
fi
done
elif (( ($PERM & 0200) != 0 )); then
# The owner has write access.
# Does the user own the file?
[[ $user == $OWNER ]] && ACCESS=yes
fi
if [ "$ACCESS" == 'yes' ]; then
return 0
else
return 1
fi
}
function get_usergr_owner_of_file {
local path="$1"
stat -c '%U:%G' "${path}"
}
function get_files_matching_regex {
local path="$1"
local regex="$2"
local recurse="$3"
if [ -z "$recurse" ]; then
grep -HiRE "$regex" "$path" #H for file printing, i for case-insensitive, R for recursive search, E for regex
else
grep -HiE "$regex" "$path" #H for file printing, i for case-insensitive, R for recursive search, E for regex
fi
}
function get_relative_path {
local base_path="$1"
local target_path="$2"
python -c "import os.path; print os.path.relpath('$base_path', '$target_path')"
}
function guess_repo_path {
local guess="$1"
if [ -f "${guess}/repo_path" ]; then
export repo_path="$guess"
fi
}
function basename_extension {
#from https://stackoverflow.com/a/1403489/1261153
local filename="$1"
base="${filename%.[^.]*}" # Strip shortest match of . plus at least one non-dot char from end
ext="${filename:${#base} + 1}" # Substring from len of base thru end
if [[ -z "$base" && -n "$ext" ]]; then # If we have an extension and no base, it's really the base
base=".$ext"
ext=""
return
fi
local base2="${base%.[^.]*}"
local ext2="${base:${#base2} + 1}"
if [[ "$ext2" == "tar" ]]; then
ext="tar.${ext}"
base="$base2"
fi
}
function make_symlink {
local source="$1"
local dest="$2"
if [ ! -f "$source" ] && [ ! -d "$source" ]; then
errcho "$source does not exist"
return 1
fi
if [ -L "$dest" ]; then
if [ "$(readlink -f $dest)"!="$(readlink -f $source)" ]; then
if is_folder_writable $(dirname "$dest") "$user"; then
$logexec rm $dest
$logexec ln -s $source $dest
else
$logexec sudo rm $dest
$logexec sudo ln -s $source $dest
fi
fi
return 0
else
if [ -f "$dest" ] || [ -d "$dest" ]; then
errcho "$dest already exists"
return 1
fi
if is_folder_writable $(dirname "$dest") "$user"; then
logexec ln -s $source $dest
else
logexec sudo ln -s $source $dest
fi
return 0
fi
}