aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/usr/bin/.drive-compute-object-size
blob: d199324dc866cd9568f4964a0c081d8d6eed166a (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
#!/bin/bash
###################################################################################################
# .drive-compute-object-size
###################################################################################################
# Copyright 2017-2023 Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under a GPLv3 License; you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at
#
#    https://www.gnu.org/licenses/gpl-3.0.md
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# 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.
###################################################################################################

###################################################################################################
#
# Compute the size in bytes of a file/folder name containing its size expressed as a floating point
# number followed by its unit:
#
#       - B:  bytes
#       - KB: kilobytes
#       - MB: megabytes
#       - GB: gigabytes
#       - TB: terabytes
#       - PB: petabytes
#       - EB: exabytes
#       - ZB: zettabytes
#       - YB: yottabytes
#
###################################################################################################
#
# Requirements:
# ------------
#
# - We assume that:
#       + bc, coreutils, perl-base & zenity packages are already installed.
#
###################################################################################################
#
# Parameter:
# ---------
#
# - <object>: file/folder name with the following format:
#             <object_type> <object_size> <objezct_name>
#
###################################################################################################
#
# Returned value:
# --------------
#
# - <object_size>: size of the object converted in bytes
#
###################################################################################################
#
# Usage example:
# -------------
#
# object_size=$(.drive-compute-object-size "$object")
#
###################################################################################################
if [[ $LOG_DRIVE_COMPUTE_OBJECT_SIZE == 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

object="$1"

# Getting object size with perl: sed does not recognize the non-greedy quantifier '?'
object_size_with_multiplier=$(echo "$object" | extract_object_size)

# Getting object size multiplier with perl: sed does not recognize the non-greedy quantifier '?'
multiplier=$(echo "$object_size_with_multiplier" | extract_object_size_multiplier)

if [[ ! ("$multiplier" =~ [0-9]B) ]]; then
        # Removing $multiplier from $object_size_with_multiplier
        object_size_base=${object_size_with_multiplier%$multiplier}
else
        # Removing $multiplier from $object_size_with_multiplier
        object_size_base=${object_size_with_multiplier%B}
fi

# Going far just for fun :-)
case $multiplier in
        [0-9]B)
                object_size=$(LC_ALL=C printf '%.0f' "$object_size_base")
                ;;
        KB)
                floating_object_size=$(echo "${object_size_base}*1024" | bc)
                object_size=$(LC_ALL=C printf '%.0f' "$floating_object_size")
                ;;
        MB)
                floating_object_size=$(echo "${object_size_base}*(1024^2)" | bc)
                object_size=$(LC_ALL=C printf '%.0f' "$floating_object_size")
                ;;
        GB)
                floating_object_size=$(echo "${object_size_base}*(1024^3)" | bc)
                object_size=$(LC_ALL=C printf '%.0f' "$floating_object_size")
                ;;
        TB)
                floating_object_size=$(echo "${object_size_base}*(1024^4)" | bc)
                object_size=$(LC_ALL=C printf '%.0f' "$floating_object_size")
                ;;
        PB)
                floating_object_size=$(echo "${object_size_base}*(1024^5)" | bc)
                object_size=$(LC_ALL=C printf '%.0f' "$floating_object_size")
                ;;
        EB)
                floating_object_size=$(echo "${object_size_base}*(1024^6)" | bc)
                object_size=$(LC_ALL=C printf '%.0f' "$floating_object_size")
                ;;
        ZB)
                floating_object_size=$(echo "${object_size_base}*(1024^7)" | bc)
                object_size=$(LC_ALL=C printf '%.0f' "$floating_object_size")
                ;;
        YB)
                floating_object_size=$(echo "${object_size_base}*(1024^8)" | bc)
                object_size=$(LC_ALL=C printf '%.0f' "$floating_object_size")
                ;;
        *)
                zenity --error --title "Fatal error in .drive-compute-object-size" --text="\t- Object: $object\n\t- Unknown multiplier: $multiplier\n\nPlease report that issue to: https://gitlab.com/jean-christophe-manciot/Drive/issues" --width 1200
                exit 1
                ;;
esac

# Returning the object size in bytes
echo ${object_size}