-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpee.sh
More file actions
595 lines (487 loc) · 11.9 KB
/
Copy pathcpee.sh
File metadata and controls
595 lines (487 loc) · 11.9 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
#!/bin/bash
if [[ ${BASH_VERSINFO[0]} -lt 4 ]]; then
echo "XXX You need bash version 4 or later."
exit 1
fi
if [[ "root" = $(whoami) ]]; then
if [[ "/root" != $HOME ]]; then
real_user=$(basename $HOME)
fi
fi
cpee_home="$HOME/.cpee"
cpee_help(){
echo "cpee accepts any options of cp. The usage is completely same as cp."
echo "If cpee takes only one argument, the first argument is treated as subcommand."
echo ""
echo "cpee subcommands : "
echo " --log : See the history of copies, attributes of copies and the associated logs with less."
echo " --show : Same as log, but without less."
echo " --head : Show the only latest log."
echo " --checkout [hash] : Put the copy from the history on the current directory specified by hash value of md5 without implicit overwritten."
echo " --search [hash] : Show the files or directories which have the same hash value of md5."
echo ""
}
cpee_search(){
md5=$1
md5_len=${#md5}
echo ""
echo -en "\e[33mFiles : [date md5 name]\e[0m\n"
for dir in $(ls -t $cpee_home) ; do
if [[ ! -d $cpee_home/$dir ]]; then
continue
fi
datetime=$dir
if [[ ${#datetime} -eq 14 ]] ; then
YYYYmmdd=${datetime:0:8}
HH=${datetime:8:2}
MM=${datetime:10:2}
SS=${datetime:12:2}
datestr=$(env LC_TIME=en_US.UTF-8 date --date="${YYYYmmdd} ${HH}:${MM}:${SS}")
else
continue
fi
pushd $cpee_home/$dir > /dev/null
for save in $(ls | grep -v log | grep -v path) ; do
if [[ -f $save ]] ; then
file=$save
md5_file=$(md5sum $file | awk '{print $1}')
if [[ $md5 = ${md5_file:0:$md5_len} ]] ; then
echo -e " $datestr\t${md5_file:0:16}\t$file"
fi
else
root=$save
sum_md5="0" # Sum of the results of md5sum for each file
for node in $(ls -R "$root"); do
if [[ -d $subdir/$node ]] ; then
continue
fi
size=${#node}
size_1=$((size-1))
if [[ ${node:size_1:1} = ":" ]]; then
subdir=${node:0:size_1}
else
md5_file=$(md5sum "$subdir/$node" | awk '{print $1}')
if [[ $md5 = ${md5_file:0:$md5_len} ]] ; then
echo -e " $datestr\t${md5_file:0:16}\t$subdir/$node"
fi
sum_md5=$(echo "obase=16;ibase=16;${sum_md5}+${md5_file^^}" | bc )
fi
done
if [[ -z $checkout_path ]]; then
sum_md5=${sum_md5,,}
if [[ $md5 = ${sum_md5:0:$md5_len} ]] ; then
checkout_path="$cpee_home/$dir/$root"
echo -e " $datestr\t${sum_md5:0:16}\t$root"
fi
fi
fi
done
popd > /dev/null
done
}
cpee_checkout(){
md5=$1
md5_len=${#md5}
checkout_path=""
for dir in $(ls -t $cpee_home) ; do
if [[ ! -d $cpee_home/$dir ]]; then
continue
fi
pushd $cpee_home/$dir > /dev/null
for save in $(ls | grep -v log | grep -v path) ; do
if [[ -f $save ]] ; then
file=$save
md5_file=$(md5sum $file | awk '{print $1}')
if [[ $md5 = ${md5_file:0:$md5_len} ]] ; then
checkout_path="$cpee_home/$dir/$file"
break
fi
else
root=$save
sum_md5="0" # Sum of the results of md5sum for each file
for node in $(ls -R "$root"); do
if [[ -d $subdir/$node ]] ; then
continue
fi
size=${#node}
size_1=$((size-1))
if [[ ${node:size_1:1} = ":" ]]; then
subdir=${node:0:size_1}
else
md5_file=$(md5sum "$subdir/$node" | awk '{print $1}')
if [[ $md5 = ${md5_file:0:$md5_len} ]] ; then
checkout_path="$cpee_home/$dir/$subdir/$node"
break
fi
sum_md5=$(echo "obase=16;ibase=16;${sum_md5}+${md5_file^^}" | bc )
fi
done
if [[ -z $checkout_path ]]; then
sum_md5=${sum_md5,,}
if [[ $md5 = ${sum_md5:0:$md5_len} ]] ; then
checkout_path="$cpee_home/$dir/$root"
echo $checkout_path
break
fi
fi
fi
done
popd > /dev/null
if [[ -n $checkout_path ]] ; then
cp -i -a $checkout_path .
cp_ret=$?
if [[ $cp_ret -ne 0 ]] ; then
echo -e "\e[34mFailed.\e[0m\n"
exit $cp_ret
else
echo -e "\e[32mSuccess.\e[0m\n"
echo "Checked out $(basename $checkout_path) here."
fi
break
fi
done
if [[ -z $checkout_path ]] ; then
echo "XXX No such file that has $md5 found."
fi
}
update_log_cache(){
echo -e "$1" > $cpee_home/logcache
}
read_log_cache(){
if [[ -f $cpee_home/logcache ]] ; then
cat $cpee_home/logcache
else
echo ""
fi
}
cpee_read(){
# $1 may be blank or subcommand
sub=$1
if [[ -f $cpee_home/logcache ]] ; then
logdate=$(head -n1 $cpee_home/logcache)
logdatesec=$(date --date="${logdate:17}" +%s)
else
logdatesec=0
fi
for dir in $(ls -t $cpee_home) ; do
if [[ ! -d $cpee_home/$dir ]]; then
continue
fi
datetime=$dir
if [[ ${#datetime} -eq 14 ]] ; then
YYYYmmdd=${datetime:0:8}
HH=${datetime:8:2}
MM=${datetime:10:2}
SS=${datetime:12:2}
datestr=$(env LC_TIME=en_US.UTF-8 date --date="${YYYYmmdd} ${HH}:${MM}:${SS}")
if [[ $sub != "head" ]] ; then
datesec=$(date --date="${YYYYmmdd} ${HH}:${MM}:${SS}" +%s)
if [[ $datesec -le $logdatesec ]] ; then
break
fi
fi
else
continue
fi
pushd $cpee_home/$dir > /dev/null
if [[ -f log ]]; then
log="$(cat log)"
if [[ $sub = "head_log" ]] ; then
echo "$log"
exit 0
fi
else
log="* * *"
fi
# XXX This is to avoid returning the error log
# when subcommand is not "head_log"
if [[ ! -d $cpee_home/$dir ]]; then
buf="${buf}### Include file under $cpee_home : $dir.\n\n"
continue
fi
if [[ -f path ]]; then
src_path=$(head -n1 path)
dst_path=$(tail -n1 path)
else
src_path="* * *"
dst_path="* * *"
fi
buf="${buf}\e[33mDate :\e[0m $datestr\n"
buf="${buf}\e[33mPath :\e[0m $src_path -> $dst_path\n"
buf="${buf}\e[33mLog :\e[0m $log\n" # may include LF
buf="${buf}\e[33mFiles : [md5 name size]\e[0m\n"
_IFS=$IFS
IFS=$'\n'
for save in "$(ls | grep -v log | grep -v path)" ; do
if [[ -f $save ]] ; then
file=$save
md5_file=$(md5sum $file | awk '{print $1}')
file_size=$(ls -lh $file | awk '{print $5}')
buf="${buf} ${md5_file:0:16}\t$file\t$file_size\n"
elif [[ -d $save ]] ; then
root=$save
sum_md5="0" # Sum of the results of md5sum for each file
lbuf=""
for node in $(ls -R "$root"); do
if [[ -d $subdir/$node ]] ; then
continue
fi
size=${#node}
size_1=$((size-1))
if [[ ${node:size_1:1} = ":" ]]; then
subdir=${node:0:size_1}
else
md5_file=$(md5sum "$subdir/$node" | awk '{print $1}')
file_size=$(ls -lh "$subdir/$node"| awk '{print $5}')
lbuf="${lbuf} ${md5_file:0:16}\t$subdir/$node\t$file_size\n"
sum_md5=$(echo "obase=16;ibase=16;${sum_md5}+${md5_file^^}" | bc )
fi
done
sum_md5=${sum_md5,,}
buf="${buf} ${sum_md5:0:16}\t$root/\n${lbuf}"
fi
done
IFS=$_IFS
buf="${buf}\n"
popd > /dev/null
if [[ $sub = "head" ]] ; then
break
fi
done
if [[ $sub != "head" ]] ; then
buf="${buf}$(read_log_cache)"
fi
if [[ $sub = "read" ]]; then
echo -en "$buf" | less -XR
elif [[ $sub = "show" || $sub = "head" ]]; then
echo -en "$buf"
fi
if [[ $sub != "head" ]]; then
update_log_cache "$buf"
fi
}
get(){
echo $(grep $1 $cpee_home/config | awk -F '=' '{print $2}')
}
get_sum_size() {
local sum=0
# "$@" keeps the list of complete full path of source files
for node in "$@" ; do
if [[ -d $node ]]; then
pushd $node > /dev/null
files=$(ls $file)
sum_dir=$(get_sum_size $files)
popd > /dev/null
sum=$((sum + sum_dir))
elif [[ -f $node ]]; then
ksize=$(ls -1sFk $node | awk '{print $1}')
sum=$((sum + ksize))
fi
done
echo $sum
}
get_abs_dirname(){
if [[ -d "$1" ]] ; then
pushd "$1" > /dev/null
echo $(pwd)
popd > /dev/null
else
dir="${1%/*}"
if [[ -d "$dir" ]] ;then
pushd "$dir" > /dev/null
echo $(pwd)
popd > /dev/null
else
echo $(pwd)
fi
fi
}
cpee_subcommand(){
not_cpee_subcommand=0
case $1 in
read|log|show|head)
if [[ $1 = "log" ]]; then
sub="read"
else
sub=$1
fi
cpee_read $sub
;;
checkout|take)
if [[ $# -ne 2 ]]; then
echo "XXX You need to give hash to checkout the file."
exit 1
fi
cpee_checkout $2
;;
search|find)
if [[ $# -ne 2 ]]; then
echo "XXX You need to give hash to find the file."
exit 1
fi
cpee_search $2
;;
help|--help)
cpee_help
;;
*)
not_cpee_subcommand=1
;;
esac
if [[ $not_cpee_subcommand -eq 0 ]] ; then
exit 0
fi
}
if [[ $# -eq 0 ]]; then
# XXX Envirnoment variable is better than config file?
if [[ -f $cpee_home/config ]]; then
echo -n "Do you want to overwrite your config? [y/n] : "
read overw
if [[ ${overw:0:1} = 'y' ]]; then
curmaxtime=$(get "MAXTIME")
curmaxsize=$(get "MAXSIZE")
else
exit 1
fi
else
mkdir -p ~/.cpee/
touch $cpee_home/config
fi
echo -n "How many copies do you want to have at most? $curmaxtime : "
read maxtime
if [[ -z $maxtime ]]; then
maxtime=$curmaxtime
fi
echo -n "How much files do you want to copy at once at most? [MB] $curmaxsize : "
read maxsize
if [[ -z $maxsize ]]; then
maxsize=$curmaxsize
fi
echo "MAXTIME=$maxtime" > $cpee_home/config
echo "MAXSIZE=$maxsize" >> $cpee_home/config
else
if [[ $1 =~ ^-- ]] ; then
cpee_subcommand ${1:2} $2
fi
if [[ $# -eq 1 ]]; then
echo "!!! One more argument is needed at least."
exit 1
fi
if [[ ! -f $cpee_home/config ]]; then
echo "!!! You need to run this command without any argument first."
exit 1
fi
maxtime=$(grep "MAXTIME" $cpee_home/config | awk -F '=' '{print $2}')
if [[ $(ls -1 $cpee_home | wc -l) -gt $maxtime ]]; then
echo "!!! Reached to the maximum limit of the number of copies. Aborted."
exit 1
fi
argc=$#
num_src=$((argc-1))
idx_dst=$((argc)) # The last one must be destination
_IFS=$IFS
IFS=$'\n'
sum_size=$(get_sum_size "${@:1:$num_src}")
sum_size=$((sum_size / 1000))
IFS=$_IFS
maxsize=$(grep "MAXSIZE" $cpee_home/config | awk -F '=' '{print $2}')
if [[ $sum_size -gt $maxsize ]]; then
echo "!!! Reached to the maximum limit size of files (${sum_size}MB > ${maxsize}MB). Aborted."
exit 1
fi
n=1
for src in "${@:1:$num_src}" ; do
if [[ ${src:0:1} == '-' ]]; then
continue
fi
echo "[$n] $src"
n=$((n+1))
done
while [ 1 ]; do
echo -n "Leave log for this copy : "
read log
if [[ -n $log ]]; then
break
else
echo "### Empty string is not allowed."
fi
done
head_log=$(cpee_read "head_log")
if [[ "$head_log" = "$log" ]]; then
echo -n "### This log is just same as head. Enter to quit. 'OK' to go. : "
read check
if [[ -z $check ]] ; then
exit 10
fi
fi
# Argument may include space in thier path
#src_files="${@:1:$num_src}" # This will not keep the arguments which include space
dst="${@:$idx_dst:1}"
cp "$@" # cp "$1" "$2" ...
cp_ret=$?
if [[ $cp_ret -ne 0 ]] ; then
echo -e "\e[34mFailed.\e[0m\n"
exit $cp_ret
else
echo -e "\e[32mSuccess.\e[0m\n"
fi
cpee_dir=$cpee_home/`date +%Y%m%d%H%M%S`
mkdir $cpee_dir
if [[ "root" = $(whoami) ]]; then
chown $real_user:$real_user $cpee_dir
fi
src_repl=""
for src in "${@:1:$num_src}" ; do
if [[ ${src:0:1} == '-' ]]; then
continue
elif [[ -z $src_repl ]] ; then
src_repl=$src
fi
cp -a "$src" "$cpee_dir"
if [[ -f $src ]] ; then
chmod 400 $cpee_dir/$(basename "$src")
else
chmod 500 "$cpee_dir/$src"
fi
done
if [[ -n "$log" ]] ; then
echo "$log" > $cpee_dir/log
fi
echo $(get_abs_dirname "$src_repl") > $cpee_dir/path
echo $(get_abs_dirname "$dst") >> $cpee_dir/path
sync;sync;sync
chmod 500 $cpee_dir
if [[ -d "$dst" ]] ; then
dst_dir=1
else
dst_dir=0
fi
src_dir=$(dirname "$src_repl")
echo "Source directory : $src_dir"
for src in "${@:1:$num_src}" ; do
if [[ ${src:0:1} == '-' ]]; then
continue
fi
if [[ -f $src ]]; then
ls -l "$src"
else
ls -lR "$src"
fi
done
echo ""
echo "Destination directory : $dst"
if [[ $dst_dir -eq 1 ]] ; then
for src in "${@:1:$num_src}" ; do
if [[ ${src:0:1} == '-' ]]; then
continue
fi
if [[ -f $src ]]; then
ls -l "$dst"/$(basename "$src")
else
ls -lR "$dst"
fi
done
else
ls -l "$dst"
fi
fi