-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdsync.sh
executable file
·659 lines (560 loc) · 18.3 KB
/
gdsync.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
#! /bin/bash
GD_URI="google-drive://address@gmail.com/" # URI with Google Drive access
GDS_INDEX_FILE="$HOME/.gds_index" # Index file of gdsync
GDS_MOD_FILES_INDICATOR="$HOME/.gds_mfiles_indicator" # Indicator of modified files
ENC_PASSWORD='' # Password for symetric encryption (AES-256-CBC in use) / SHOULD BE EMPTY
USE_GNOME_KEYRING=true # Switch to use Gnome Keyring for storing ENC_PASSWORD (will prompt for password at first run)
PBKDF_ITER='100000' # PBKDF2 iteration count (default: 100000, higher = stronger)
REMOTE_DIR="gdsync" # Directory on Google Drive holding sync items
REMOTE_UPDATED=false
declare -A REMOTE_MTIME
declare -A REMOTE_ENCRYPTED_NAMES
declare -A LOCAL_MTIME
declare -A PROCESSED_FILES
# print usage on stderr
usage()
{
echo "Usage: $0 <option> [<absolute path to files>]"
echo "--add Add specified file(s) to the synchronization process"
echo "--del Delete specified file(s) from the synchronization process"
echo "--rdel Delete specified remote file(s)"
echo "--sync Perform a synchronization of all syncing files"
echo "--pull Interactively pull a file from server that is not locally present"
echo "--update-gio Update GIO emblem on synced files"
echo "--force-pull Force pulling of specified file(s)"
echo "--force-push Force pushing of specified file(s)"
} >&2
pull_file()
{
local return_code
gio cat "$GD_URI/$1" | openssl enc -d -aes-256-cbc -salt -pbkdf2 -iter "$PBKDF_ITER" -pass pass:"$ENC_PASSWORD"
return_code=$((PIPESTATUS[0]+PIPESTATUS[1]))
return $return_code
}
push_file()
{
local return_code
openssl enc -aes-256-cbc -salt -pbkdf2 -iter "$PBKDF_ITER" -pass pass:"$ENC_PASSWORD" | gio save "$GD_URI/$1"
return_code=$((PIPESTATUS[0]+PIPESTATUS[1]))
return $return_code
}
# check the access of Google Drive directory in GD_URI,
# return 1 if some fails, 0 otherwise
verify_gd_dir()
{
if ! gio info "$GD_URI" > /dev/null 2>&1
then
return 1
fi
return 0
}
load_remote_mtime()
{
local file_names clear_filename enc_filename mtime line mtime_file
mtime_file="$(pull_file "$REMOTE_DIR/mtime.lst")"
if (($? > 0))
then
zenity --error --text="Error reading remote index (receive)" --title="gdsync" --width=200
exit 1
fi
while read line
do
if test -z "$line"
then
continue
fi
if ! echo "$line" | grep -Eq '^[a-zA-Z]+/.+/[0-9]+$'
then
zenity --error --text="Error reading remote index (regex)" --title="gdsync" --width=200
exit 1
fi
file_names="${line%/*}"
clear_filename="/${file_names#*/}"
enc_filename="${file_names%%/*}"
mtime="${line##*/}"
REMOTE_MTIME["$clear_filename"]="$mtime"
REMOTE_ENCRYPTED_NAMES["$clear_filename"]="$enc_filename"
done <<< "$mtime_file"
}
save_remote_mtime()
{
local mtime_file
gio remove "$GD_URI/$REMOTE_DIR/mtime.lst"
mtime_file="$(for file in "${!REMOTE_MTIME[@]}"; do echo "${REMOTE_ENCRYPTED_NAMES["$file"]}$file/${REMOTE_MTIME["$file"]}"; done)"
echo "$mtime_file" | push_file "$REMOTE_DIR/mtime.lst"
if ((PIPESTATUS[1] > 0))
then
echo -n "$mtime_file" > "$HOME/.mtime.lst.gds"
zenity --error --text="Error writing remote index (saving to $HOME/.mtime.lst.gds)" --title="gdsync" --width=200
exit 1
fi
}
load_local_mtime()
{
local file
local -a local_files
if ! test -e "$GDS_INDEX_FILE"
then
echo -n > "$GDS_INDEX_FILE"
fi
readarray -t local_files < "$GDS_INDEX_FILE"
for file in "${local_files[@]}"
do
if test -f "$file"
then
LOCAL_MTIME["$file"]="$(stat --format=%Y "$file")"
elif ! test -e "$file"
then
unset LOCAL_MTIME["$file"]
fi
done
}
save_local_mtime()
{
echo -n > "$GDS_INDEX_FILE"
for file in "${!LOCAL_MTIME[@]}"
do
echo "$file" >> "$GDS_INDEX_FILE"
done
}
# add files in arguments to index file and upload them
gds_add()
{
local REMOTE_NAME file i len failed_files
len="$(find "$@" -type f | wc -l)"
i=0
progress_bar 'gdsync - Adding files to sync' < /tmp/gds_progress_ipc &
exec 3> /tmp/gds_progress_ipc
while read file
do
echo "#Processing $file..." >&3
bc >&3 <<< "scale=2;$i/$len*100"
((i++))
if test -n "${PROCESSED_FILES["$file"]}"
then
continue
fi
if test -n "${LOCAL_MTIME["$file"]}"
then
echo "$file already syncing ! Skipping..." >&2
continue
fi
REMOTE_NAME="$(cat /dev/urandom | tr -dc '[:alpha:]' | head -c 40)"
if test -f "$file"
then
if test -n "${REMOTE_MTIME["$file"]}"
then
LOCAL_MTIME["$file"]="$(stat --format=%Y "$file")"
gio set "$file" -t stringv metadata::emblems emblem-colors-red
echo -n > "$GDS_MOD_FILES_INDICATOR"
PROCESSED_FILES["$file"]="processed"
else
cat "$file" | push_file "$REMOTE_DIR/$REMOTE_NAME"
if ((PIPESTATUS[1] > 0))
then
PROCESSED_FILES["$file"]="error"
continue
fi
REMOTE_MTIME["$file"]="$(stat --format=%Y "$file")"
REMOTE_UPDATED=true
REMOTE_ENCRYPTED_NAMES["$file"]="$REMOTE_NAME"
LOCAL_MTIME["$file"]=${REMOTE_MTIME["$file"]}
gio set "$file" -t stringv metadata::emblems emblem-colors-green
PROCESSED_FILES["$file"]="processed"
fi
else
echo "$file doesn't exist ! Skipping..." >&2
continue
fi
done <<< "$(find "$@" -type f)"
for file in "${!PROCESSED_FILES[@]}"
do
if test ${PROCESSED_FILES["$file"]} = "error"
then
failed_files="$failed_files $file"
fi
done
if test -n "$failed_files"
then
zenity --error --text="Error adding some files: $failed_files" --title="gdsync" --width=200
fi
}
# remove files in arguments from index file
gds_del()
{
local file i len
len="$(find "$@" -type f | wc -l)"
i=0
progress_bar 'gdsync - Removing files from sync' < /tmp/gds_progress_ipc &
exec 3> /tmp/gds_progress_ipc
while read file
do
echo "#Processing $file..." >&3
bc >&3 <<< "scale=2;$i/$len*100"
((i++))
if test -n "${PROCESSED_FILES["$file"]}"
then
continue
fi
if test -z "${LOCAL_MTIME["$file"]}"
then
echo "$file not syncing ! Skipping..." >&2
continue
fi
unset LOCAL_MTIME["$file"]
if test -f "$file"
then
gio set "$file" -t unset metadata::emblems
fi
PROCESSED_FILES["$file"]="processed"
done <<< "$(find "$@" -type f)"
}
# Perform a synchronization
gds_sync()
{
local file i len failed_files
len="${#LOCAL_MTIME[@]}"
i=0
progress_bar 'gdsync - Syncing files with server' < /tmp/gds_progress_ipc &
exec 3> /tmp/gds_progress_ipc
for file in "${!LOCAL_MTIME[@]}"
do
echo "#Processing $file..." >&3
bc >&3 <<< "scale=2;$i/$len*100"
((i++))
if test "${LOCAL_MTIME["$file"]}" -lt "${REMOTE_MTIME["$file"]}"
then
pull_file "$REMOTE_DIR/${REMOTE_ENCRYPTED_NAMES["$file"]}" > "$file.gds"
if (($? > 0))
then
failed_files="$failed_files $file"
rm -f "$file.gds"
continue
fi
rm -f "$file"
mv "$file.gds" "$file"
touch --date="@${REMOTE_MTIME["$file"]}" "$file"
LOCAL_MTIME["$file"]=${REMOTE_MTIME["$file"]}
gio set "$file" -t stringv metadata::emblems emblem-colors-green
elif test "${LOCAL_MTIME["$file"]}" -gt "${REMOTE_MTIME["$file"]}"
then
gio remove "$GD_URI/$REMOTE_DIR/${REMOTE_ENCRYPTED_NAMES["$file"]}"
cat "$file" | push_file "$REMOTE_DIR/${REMOTE_ENCRYPTED_NAMES["$file"]}"
if ((PIPESTATUS[1] > 0))
then
failed_files="$failed_files $file"
continue
fi
REMOTE_MTIME["$file"]=${LOCAL_MTIME["$file"]}
REMOTE_UPDATED=true
gio set "$file" -t stringv metadata::emblems emblem-colors-green
else
gio set "$file" -t stringv metadata::emblems emblem-colors-green
fi
done
if test -n "$failed_files"
then
zenity --error --text="Error syncing some files: $failed_files" --title="gdsync" --width=200
else
rm "$GDS_MOD_FILES_INDICATOR"
fi
}
# Update emblem of synced files
gds_update_gio()
{
local mfiles=false
for file in "${!LOCAL_MTIME[@]}"
do
if test "${LOCAL_MTIME["$file"]}" -eq "${REMOTE_MTIME["$file"]}"
then
gio set "$file" -t stringv metadata::emblems emblem-colors-green
else
gio set "$file" -t stringv metadata::emblems emblem-colors-red
mfiles=true
fi
done
if $mfiles
then
echo -n > "$GDS_MOD_FILES_INDICATOR"
else
rm "$GDS_MOD_FILES_INDICATOR"
fi
}
# Interactively pull a file from server that is not locally present
gds_pull()
{
local file selected_files i len failed_files
local -a remote_files
remote_files=("" "ALL")
for file in "${!REMOTE_MTIME[@]}"
do
if ! test -f "$file"
then
remote_files+=("" "$file")
fi
done
selected_files="$(zenity --width=500 --height=500 --list --column='' --text='Select file(s) to pull from server:' --checklist --separator='\n' --print-column='2' --column='Remote file' "${remote_files[@]}")"
if test -z "$selected_files"
then
return
fi
if test "$(echo "$selected_files" | head -n 1)" = "ALL"
then
selected_files=""
for rf in "${remote_files[@]}"
do
if test "$rf" = "ALL" -o -z "$rf"
then
continue
fi
selected_files="$selected_files
$rf"
done
selected_files="$(echo "$selected_files" | tail -n +2)"
fi
len="$(echo "$selected_files" | wc -l)"
i=0
progress_bar 'gdsync - Pulling files from server' < /tmp/gds_progress_ipc &
exec 3> /tmp/gds_progress_ipc
while read file
do
echo "#Processing $file..." >&3
bc >&3 <<< "scale=2;$i/$len*100"
((i++))
directory="${file%/*}"
if ! test -d "$directory"
then
mkdir -p "$directory"
fi
pull_file "$REMOTE_DIR/${REMOTE_ENCRYPTED_NAMES["$file"]}" > "$file.gds"
if (($? > 0))
then
failed_files="$failed_files $file"
rm -f "$file.gds"
continue
fi
rm -f "$file"
mv "$file.gds" "$file"
touch --date="@${REMOTE_MTIME["$file"]}" "$file"
LOCAL_MTIME["$file"]=${REMOTE_MTIME["$file"]}
gio set "$file" -t stringv metadata::emblems emblem-colors-green
done <<< "$selected_files"
if test -n "$failed_files"
then
zenity --error --text="Error pulling some files: $failed_files" --title="gdsync" --width=200
fi
}
# Force pulling files
gds_force_pull()
{
local file i len failed_files
len="$(find "$@" -type f | wc -l)"
i=0
progress_bar 'gdsync - Force pulling files from server' < /tmp/gds_progress_ipc &
exec 3> /tmp/gds_progress_ipc
while read file
do
echo "#Processing $file..." >&3
bc >&3 <<< "scale=2;$i/$len*100"
((i++))
if test -n "${PROCESSED_FILES["$file"]}"
then
continue
fi
if test -z "${LOCAL_MTIME["$file"]}"
then
echo "$file not syncing ! Skipping..." >&2
continue
fi
if test -f "$file"
then
pull_file "$REMOTE_DIR/${REMOTE_ENCRYPTED_NAMES["$file"]}" > "$file.gds"
if (($? > 0))
then
failed_files="$failed_files $file"
rm -f "$file.gds"
continue
fi
rm -f "$file"
mv "$file.gds" "$file"
touch --date="@${REMOTE_MTIME["$file"]}" "$file"
LOCAL_MTIME["$file"]=${REMOTE_MTIME["$file"]}
gio set "$file" -t stringv metadata::emblems emblem-colors-green
PROCESSED_FILES["$file"]="processed"
else
echo "$file doesn't exist ! Skipping..." >&2
continue
fi
done <<< "$(find "$@" -type f)"
if test -n "$failed_files"
then
zenity --error --text="Error pulling some files: $failed_files" --title="gdsync" --width=200
fi
}
# Force pushing files
gds_force_push()
{
local file i len failed_files
len="$(find "$@" -type f | wc -l)"
i=0
progress_bar 'gdsync - Force pushing files to server' < /tmp/gds_progress_ipc &
exec 3> /tmp/gds_progress_ipc
while read file
do
echo "#Processing $file..." >&3
bc >&3 <<< "scale=2;$i/$len*100"
((i++))
if test -n "${PROCESSED_FILES["$file"]}"
then
continue
fi
if test -z "${LOCAL_MTIME["$file"]}"
then
echo "$file not syncing ! Skipping..." >&2
continue
fi
if test -f "$file"
then
gio remove "$GD_URI/$REMOTE_DIR/${REMOTE_ENCRYPTED_NAMES["$file"]}"
cat "$file" | push_file "$REMOTE_DIR/${REMOTE_ENCRYPTED_NAMES["$file"]}"
if ((PIPESTATUS[1] > 0))
then
failed_files="$failed_files $file"
continue
fi
REMOTE_MTIME["$file"]="$(stat --format=%Y "$file")"
REMOTE_UPDATED=true
LOCAL_MTIME["$file"]=${REMOTE_MTIME["$file"]}
gio set "$file" -t stringv metadata::emblems emblem-colors-green
PROCESSED_FILES["$file"]="processed"
else
echo "$file doesn't exist ! Skipping..." >&2
continue
fi
done <<< "$(find "$@" -type f)"
if test -n "$failed_files"
then
zenity --error --text="Error pulling some files: $failed_files" --title="gdsync" --width=200
fi
}
# Interactively delete a file from server and untrack associated local file
gds_rdel()
{
local file selected_files i len
local -a remote_files
for file in "${!REMOTE_MTIME[@]}"
do
if test -f "$file"
then
remote_files+=("" "$file" "Yes")
else
remote_files+=("" "$file" "No")
fi
done
selected_files="$(zenity --width=500 --height=500 --list --column='' --text='Select file(s) to remove from server:' --checklist --column='Remote file' --separator='\n' --print-column='2' --column='Local' "${remote_files[@]}")"
if test -z "$selected_files"
then
return
fi
len="$(echo "$selected_files" | wc -l)"
i=0
progress_bar 'gdsync - Deleting files from server' < /tmp/gds_progress_ipc &
exec 3> /tmp/gds_progress_ipc
while read file
do
echo "#Processing $file..." >&3
bc >&3 <<< "scale=2;$i/$len*100"
((i++))
echo "$file"
gio remove "$GD_URI/$REMOTE_DIR/${REMOTE_ENCRYPTED_NAMES["$file"]}"
unset REMOTE_MTIME["$file"]
REMOTE_UPDATED=true
unset REMOTE_ENCRYPTED_NAMES["$file"]
if test -f "$file"
then
unset LOCAL_MTIME["$file"]
gio set "$file" -t unset metadata::emblems
fi
done <<< "$selected_files"
}
prompt_password()
{
if test -n "$ENC_PASSWORD"
then
return
fi
if $USE_GNOME_KEYRING
then
ENC_PASSWORD="$(secret-tool search application gds 2>/dev/null | grep 'secret =' | cut -d '=' -f2 | tail -c +2)"
if test -z "$ENC_PASSWORD"
then
if ! ENC_PASSWORD="$(zenity --password --title='Google Drive Sync Password')"
then
echo "No password provided..." >&2
exit 1
fi
echo -n "$ENC_PASSWORD" | secret-tool store --label="Google Drive Sync" "application" "gds"
fi
else
ENC_PASSWORD="$(zenity --password --title='Google Drive Sync Password')"
fi
}
progress_bar()
{
zenity --width=400 --progress --auto-close --no-cancel --title="$1"
}
if (($# == 0))
then
usage
exit 1
fi
if test -p /tmp/gds_progress_ipc
then
echo "Already running..." >&2
exit 1
fi
#(trap "kill -- -$$" EXIT; while true; do zenity --width=200 --info --text="Google Drive sync in progress..." --title="Google Drive Sync" --icon-name='network-transmit-receive'; done) &
if ! verify_gd_dir
then
echo "No drive access at $GD_URI or no internet connection" >&2
exit 1
fi
prompt_password
if test -f "$HOME/.mtime.lst.gds"
then
cat "$HOME/.mtime.lst.gds" | push_file "$REMOTE_DIR/mtime.lst"
if ((PIPESTATUS[1] > 0))
then
zenity --error --text="Error writing saved remote index (read from $HOME/.mtime.lst.gds)" --title="gdsync" --width=200
exit 1
fi
rm "$HOME/.mtime.lst.gds"
fi
load_local_mtime
load_remote_mtime
for file in "${!LOCAL_MTIME[@]}"
do
if test -z "${REMOTE_MTIME["$file"]}"
then
unset LOCAL_MTIME["$file"]
gio set "$file" -t unset metadata::emblems
fi
done
mkfifo --mode=660 /tmp/gds_progress_ipc
case "$1" in
--add) shift; gds_add "$@" ;;
--del) shift; gds_del "$@" ;;
--rdel) gds_rdel ;;
--sync) gds_sync ;;
--pull) gds_pull ;;
--force-pull) shift; if (($# > 0)); then gds_force_pull "$@"; else gds_force_pull "${!LOCAL_MTIME[@]}"; fi ;;
--force-push) shift; if (($# > 0)); then gds_force_push "$@"; else gds_force_push "${!LOCAL_MTIME[@]}"; fi ;;
--update-gio) gds_update_gio ;;
*) usage; exec 3>&-; rm -f /tmp/gds_progress_ipc; exit 1 ;;
esac
exec 3>&-
rm -f /tmp/gds_progress_ipc
save_local_mtime
$REMOTE_UPDATED && save_remote_mtime
xfce4-panel --plugin-event=genmon:refresh:bool:true
exit 0