aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/usr/bin/.drive-prepend-object-type-size
blob: 09e2d0155fa8d3ca1016a304fcf31a3a2d2e8ad1 (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
#!/bin/bash
###################################################################################################
# .drive-prepend-object-type-size
###################################################################################################
# Copyright 2017-2022 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.
###################################################################################################

###################################################################################################
#
# Prepend a leading character symbolizing the object type followed by its size in B/KB/MB/TB before
# the local object name.
# The leading character may be:
#       - d: directory
#       - -: file
#       - ?: unknown type
#
###################################################################################################
#
# Parameter:
# ---------
#
# - <object_name>: local file/folder name
#
###################################################################################################
#
# Returned value:
# --------------
#
# - <stated_object_name>: string formatted with "<object_type> <object_size> <object_name>" where
#                         <object_type> is one of the following:
#                               - d: directory
#                               - -: file
#                               - ?: unknown type
#
###################################################################################################
#
# Usage example:
# -------------
#
# stated_object_name=$(.drive-prepend-object-type-size "$object_name")
#
###################################################################################################
if [[ $LOG_DRIVE_PREPEND_OBJECT_TYPE_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

object_name=$1

# Getting the local object type and putting it in front
object_type=$(stat -L -c %F "$object_name")
if [[ ("$object_type" =~ 'regular file') || ("$object_type" =~ 'regular empty file') ]]; then
        type='-'
elif [[ "$object_type" =~ 'directory' ]]; then
        type='d'
else
        type='?'
fi

if [[ ("$type" == '-') || ("$type" == '?') ]]; then
        # Getting the object size
        object_size=$(stat -L -c %s "$object_name")
        size=$(numfmt --to=iec --format="%.2f" --round=nearest --suffix=B $object_size)

        # Returning "<object_type> <object_size> <object_name>"
        echo "${type} ${size} ${object_name}"
else
        # Returning "<object_type> <object_size> <object_name>"
        echo "${type} 0.00B ${object_name}"
fi