aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/usr/bin/.drive-push-unsynced
blob: 481d5c0f2f0a2d9cfaeb5fb754c80ce2b6ccd5aa (about) (plain)
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
#!/bin/bash
###################################################################################################
# .drive-push-unsynced
###################################################################################################
# Copyright 2017-2022 Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 
# in compliance with the License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# This script is using a project hosted at https://github.com/odeke-em/drive, originally developed 
# by Burcu Dogan while working on the Google Drive team and now maintained by Emmanuel T Odeke.
#
# Attribution — You must give appropriate credit, provide a link to the license, and indicate if 
#               changes were made. You may do so in any reasonable manner, but not in any way that 
#               suggests the licensor endorses you or your use.
###################################################################################################

###################################################################################################
#
# Pushing (uploading) recursively all the files/folders (objects) listed in .driveselect under the 
# section [push] onto the current directory's remote equivalent on your Google Drive.online.
# This operation:
# - updates only unsynced objects that exist remotely with their local counterpart. 
# - uploads only unsynced objects that do not exist remotely but do locally, 
# - **does not** delete remote objects that exist remotely but are not present locally, 
#   except when there are children folders.
# The scale of the operation depends on the depth setting in your <google_drive_folder>/.driverc.
# The list of objects affected by this operation can be optionnaly filtered by
# <google_drive_folder>/.driveignore.
#
###################################################################################################
#
# Requirements:
# ------------
#
# - We assume that:
#       + drive-google package is already installed.
#       + coreutils, perl-base, sed & zenity packages are already installed.
#
# - **This is an internal script called by drive-sync when necessary.**
#
# - <google_drive_folder>/.driverc:it must be properly setup, either manually or with drive-setup,
#                                  with the following settings:
#       * depth=
#               + -1: an infinite traversal depth, i.e recursive traversal from the current working  
#                     directory until there is no more child folder
#               + non-zero number: a fixed traversal depth
#       * ignore-checksum=
#               + true: objects checksum is not verified 
#               + false: objects checksum is verified 
#       * encryption-password="<your_secret>": drive optionally encrypts all pushed objects at rest
#                                              on your Google Drive
#       * ...: cf. https://github.com/odeke-em/drive for a complete list of available  
#              options. They can only be set manually through .driverc or through drive-setup for
#              the sake of simplicity.
#              Cf. /etc/drive-google/.driverc for some examples.
#
# - <google_drive_folder>/.driveignore: it may be used to exclude/include some objects from the 
#                                       scope of the current drive-<command> by using regular
#                                       expressions.
#                                       Cf. /etc/drive-google/.driveignore for its format & some 
#                                       examples.
#
# - ./.driveselect: it may be used to include/exclude the objects from the scope of the current 
#                   drive-<command>. 
#                   Cf. /etc/drive-google/.driveselect for its format & some examples.
#
###################################################################################################
#
# Parameters:
# ----------
#
# - <message> is used in the interactive GUI windows
#
# - -<anything> will display this help message
#
###################################################################################################
#
# Notes:
# -----
#
# - One must be **cautious** with this command: a list of potentially affected objects will be
#   printed before the actual operation.
#   Previous online versions remain available though in "version 1" of each overridden object.
#
# - This command must be used when **recursively found local objects are newer or more important than
#   their online counterparts.**
#   In other words, run this command when **your local Google Drive is the master**.
#
# - Running this command pushes some local objects online; its speed is proportional to: 
#       + the number of different objects recursively found
#       + the size of those local objects recursively pushed online.
#       + the end-to-end bandwidth, latency & quality of your current Internet connection to your 
#         Google Drive online.
#
###################################################################################################
if [[ $LOG_DRIVE_PUSH_UNSYNCED == ON ]]; then
        # Getting the last existing file descriptor of ~/.drive-google/logs/drive-google.log
        fd=$(ls -al /proc/$$/fd | grep ~/.drive-google/logs/drive-google.log | sed -E 's|^.* ([0-9]+) -> .*$|\1|g' | sort -V | tail -n 1)

        # Case where fd is unset, i.e the file ~/.drive-google/logs/drive-google.log has not yet been opened
        if [[ -z $fd ]]; then
                lowest_unused_fd () 
                {
                        local fd=0
                        while [ -e /proc/$$/fd/${fd} ]; do
                                fd=$((fd+1))
                        done
                        echo $fd
                }
                fd=$(lowest_unused_fd)

                # Opening ~/.drive-google/logs/drive-google.log as file descriptor $fd for appending
                eval "exec $fd>> ~/.drive-google/logs/drive-google.log"
        fi

        # Writing the trace output generated when set -x is enabled to file descriptor $fd 
        eval "BASH_XTRACEFD=$fd"
        # Logging line numbers - We could also use ${0} for ${BASH_SOURCE}
        BASH_SOURCE_BASENAME=$(basename ${BASH_SOURCE})
        export PS4='${BASH_SOURCE_BASENAME}.${LINENO}+ '
        # Expanding all variables and prints the full commands before output of the command
        set -x
fi

# Preventing globbing (pathname expansion)
set -o noglob
# Allowing extended globbing
shopt -s extglob
# Expanding aliases
shopt -s expand_aliases

# Including alias definitions to sed/grep/awk/perl commands for readability sake
. /usr/include/drive-google/aliases

#------------------------------------push-objects--------------------------------------------------
# Pushing all objects listed in ${filtered_objects[@]}
# Usage: push-objects | zenity --progress --percentage=0 --auto-close
# Return strings which feed 'zenity --progress'
# - "# Pushing $object at ${speed_eta[0]}/s ETA: ${speed_eta[1]}"
# - "$current_percentage"
# The function uses the global array variable:
# - "${filtered_objects[@]}"
function push-objects ()
{
        # Initializing:
        # - starting point of the operation in seconds
        # - elapsed time since the operation has started in seconds
        # - current size of pushed objects so far,
        # - current speed & estimated time of arrival so far,
        # - current percentage of pushed objects so far,
        # - total size of all objects to be pushed,
        # - current objects number to be pushed.
        # - boolean used in issue #876 - #848 workaround
        # - array used in issue #876 - #848 workaround
        # - index used in issue #876 - #848 workaround
        start=0
        elapsed_time=0
        current_size=0
        speed_eta=()
        current_percentage=0
        total_size=0
        current_objects_number=0
        push_folders=false
        folders=()
        folders_index=0

        # Computing total size of all objects
        for object in "${filtered_objects[@]}"
        do
                # Getting the object size in bytes
                object_size=$(.drive-compute-object-size "$object")

                let "total_size += $object_size"
        done

        start=$SECONDS

        for object in "${filtered_objects[@]}"
        do
                ((current_objects_number++))

                # Getting the object size in bytes
                object_size=$(.drive-compute-object-size "$object")

                # Removing leading object stats
                object=$(echo "$object" | rm_object_stats)

                # Feeding 'zenity --progress'
                if [[ ($current_objects_number -ne 1) && (${speed_eta[0]} != "0.00b") ]]; then
                        LC_ALL=C printf '%s %s%s %s\n' "# Pushing $object" "at ${speed_eta[0]}" "/s" "ETA: ${speed_eta[1]}"
                else
                        # $current_size is 0
                        LC_ALL=C printf '%s\n' "# Pushing $object"
                fi

                # Removing all occurrences of '&' in $object as a workaround to a markup ampersand issue with zenity
                noamp_object=$(echo "${object}" | rm_ampersand)

                # While there is a connection error and the user wants to continue trying
                while :
                do
                        if [[ $push_folders == 'false' ]]; then
                                # Pushing the $object
                                unset output
                                output=$(drive push -force -no-prompt -depth 0 -verbose "$object" 2>&1)
                        else
                                # Working around issue #876 - #848
                                (( folders_index-- ))
                                if [[ $folders_index -ge 0 ]]; then
                                        # Pushing one of the object parent folders
                                        unset output
                                        output=$(drive push -force -no-prompt -depth 0 -verbose "${folders[$folders_index]}" 2>&1)
                                else
                                        # We have pushed all object parent folders
                                        # Trying again to push the $object
                                        push_folders=false
                                        continue
                                fi                        
                        fi

                        # Checking $output
                        if [[ "${output[@]}" =~ 'set to be ignored yet is being processed' ]]; then
                                output="Ignored:\t\t${object}"
                        elif [[ "${output[@]}" =~ 'Get https://www.googleapis.com/drive/' ]]; then
                                # Connection error
                                # Displaying the error 
                                zenity --error --title "Connection error accessing your Google Drive online in ${noamp_local_folder}" --text="${noamp_object}\n\nCould not connect to https://www.googleapis.com/drive/v2/files or/nor to https://accounts.google.com/o/oauth2/token" --width 1400
                                zenity --question --title "Connection error accessing your Google Drive online in ${noamp_local_folder}" --text="${noamp_object}\n\nDo you want to abort current drive operation?" --width 1400

                                if [[ $? -eq 1 ]]; then
                                        # Continuing trying
                                        continue
                                elif [[ $? -eq 0 ]]; then
                                        # Aborting
                                        exit 9
                                fi
                        elif [[ "${output[@]}" =~ "doesn't exist" ]]; then
                                output="Absent:\t\t\t${object}"
                        elif [[ "${output[@]}" =~ "local is a directory while remote is a file" ]]; then
                                output="Inconsistent types:\t${object}"
                        elif [[ "${output[@]}" =~ "local is a file while remote is a directory" ]]; then
                                output="Inconsistent types:\t${object}"
                        elif [[ "${output[@]}" =~ 'Everything is up-to-date'\. ]]; then
                                # This should never happen here: we have most probably hit drive issue #876 - #848
                                # unless we are already working around it'
                                # https://github.com/odeke-em/drive/issues/876
                                if [[ $push_folders == 'false' ]]; then
                                        # Workaround is to push all parent folders
                                        # Getting the list of all parent folders
                                        push_folders=true
                                        folders[$folders_index]=$(dirname "$object")
                                        while [[ "${folders[$folders_index]}" != '.' ]]
                                        do
                                                (( folders_index++ ))
                                                folders[$folders_index]=$(dirname "${folders[`expr ${folders_index} - 1`]}")
                                        done
                                        unset folders[- 1]
                                fi
                                continue
                        elif [[ "${output[@]}" =~ Resolving\.\.\. ]]; then
                                output="Pushed:\t\t\t${object}"
                        else
                                output="??????:\t\t\t${object}"
                        fi

                        if [[ (("$output" =~ 'Pushed') || ("$output" =~ 'Already synced')) && ($push_folders == 'false') ]]; then
                                # Adding the object size to the current size of pushed objects so far
                                let "current_size += $object_size"

                                elapsed_time=$(( SECONDS - start ))

                                # Computing speed & estimated time of arrival
                                readarray -t speed_eta < <(.drive-compute-speed-eta $current_size $total_size $elapsed_time)

                                # Computing the current percentage of pushed objects
                                current_percentage=$(echo "scale=4; (${current_size}/${total_size})*100" | bc)

                                # Feeding 'zenity --progress'
                                LC_ALL=C printf '%.2f\n' "$current_percentage"
                        fi

                        if [[ $push_folders == 'false' ]]; then
                                # We're done
                                break
                        fi
                done

                # Saving $output in temporary .driveoutput.log file after having escaped all "escape sequences"
                printf '%b\n' "$output" | escape_escape_sequences >> .driveoutput.log
        done
}

#-----------------------------------.drive-push-unsynced-------------------------------------------

message=$1

if [[ ! "$message" =~ ^- ]]; then
        local_folder="$(pwd)"

        # Removing all occurrences of '&' in $object as a workaround to a markup ampersand issue with zenity
        noamp_local_folder="$(echo "${local_folder}" | rm_ampersand)"

        # Finding local root Google Drive folder
        gd_root_folder="$(.drive-find-gd)"
        return_code=$?

        # Removing all occurrences of '&' in $gd_root_folder as a workaround to a markup ampersand issue with zenity
        noamp_gd_root_folder="$(echo "${gd_root_folder}" | rm_ampersand)"

        case $return_code in
                0)
                        # Everything is fine - we continue
                        ;;
                7)
                        zenity --warning --title "Drive context found in $noamp_gd_root_folder" --text="We have moved to $noamp_gd_root_folder\n\nIf you prefer to select another Google Drive folder:\n\t--> 'Change local folder' in drive-menu" --width 1400

                        cd "$gd_root_folder"

                        local_folder="$(pwd)"

                        # Removing all occurrences of '&' in $object as a workaround to a markup ampersand issue with zenity
                        noamp_local_folder="$(echo "${local_folder}" | rm_ampersand)"
                        ;;
                8)
                        zenity --error --title "No drive context found below and above $noamp_local_folder" --text="- either drive has not been initialized properly (at least one hop away from '/') with:\n\t--> '# drive init google_drive_folder' within a terminal\n- or we are not located below your google_drive_folder top level branch:\n\t--> 'Change local folder' in drive-menu" --width 1400
                        exit $return_code
                        ;;
                *)
                        # Something is fatally wrong
                        zenity --error --title "Fatal error in drive-menu" --text="\tUnknown return code from .drive-find-gd: $return_code\n\nPlease report that issue to: https://gitlab.com/jean-christophe-manciot/Drive/issues" --width 1200
                        exit 99
                        ;;
        esac

        # Filtering & sorting the objects list
        filtered_objects=()
        readarray -t filtered_objects < <(.drive-select-objects 'select_file' 'push')

        if [[ ${#filtered_objects[@]} -eq 0 ]]; then
                zenity --warning --title "Pushing 0 $message" --text="None needs to be pushed" --width=1400
                exit 0
        fi

        # Purging the section [push] in the file .driveselect
        .drive-write-driveselect push "${null[@]}"

        printf '%s\n' "${filtered_objects[@]}" | zenity --text-info --title "Do you confirm pushing ${#filtered_objects[@]} $message?" --width 1800 --height 800
        if [[ ${PIPESTATUS[1]} -eq 0 ]]; then
                # OK has been selected
                > .driveoutput.log

                # Pushing all objects listed in ${filtered_objects[@]}
                push-objects | zenity --progress --percentage=0 --auto-close --title="Pushing ${#filtered_objects[@]} $message" --width 1800

                readarray -t pushed_objects < <(cat .driveoutput.log | select_Pushed)

                cat .driveoutput.log | zenity --text-info --title "Pushed ${#pushed_objects[@]}/${#filtered_objects[@]} $message" --width 1800 --height 800
                rm .driveoutput.log
        else
                zenity --warning --title "Do you confirm pushing ${#filtered_objects[@]} $message?" --text="Operation cancelled" --width 1400
        fi

        exit 0
else
        # Any -parameter
        end_of_help_message_line_number=$(awk '/^if \[\[ \$LOG_/{ print NR; exit }' $(which drive-push))
        ((end_of_help_message_line_number--))
        awk -v var=$end_of_help_message_line_number 'NR >= 20 && NR <= var' $(which drive-push) | zenity --text-info --title "drive-push help" --width 1125 --height 1000
        exit 1
fi