summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/prometheus/procfs/ttar
blob: 8227a4a376cfc805e27f827facd0ff87de2c6ef8 (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
#!/usr/bin/env bash
# Purpose: plain text tar format
# Limitations: - only suitable for text files, directories, and symlinks
#              - stores only filename, content, and mode
#              - not designed for untrusted input

# Note: must work with bash version 3.2 (macOS)

set -o errexit -o nounset

# Sanitize environment (for instance, standard sorting of glob matches)
export LC_ALL=C

path=""
CMD=""

function usage {
    bname=$(basename "$0")
    cat << USAGE
Usage:   $bname [-C <DIR>] -c -f <ARCHIVE> <FILE...> (create archive)
         $bname            -t -f <ARCHIVE>           (list archive contents)
         $bname [-C <DIR>] -x -f <ARCHIVE>           (extract archive)

Options:
         -C <DIR>                                    (change directory)

Example: Change to sysfs directory, create ttar file from fixtures directory
         $bname -C sysfs -c -f sysfs/fixtures.ttar fixtures/
USAGE
exit "$1"
}

function vecho {
    if [ "${VERBOSE:-}" == "yes" ]; then
        echo >&7 "$@"
    fi
}

function set_cmd {
    if [ -n "$CMD" ]; then
        echo "ERROR: more than one command given"
        echo
        usage 2
    fi
    CMD=$1
}

while getopts :cf:htxvC: opt; do
    case $opt in
        c)
            set_cmd "create"
            ;;
        f)
            ARCHIVE=$OPTARG
            ;;
        h)
            usage 0
            ;;
        t)
            set_cmd "list"
            ;;
        x)
            set_cmd "extract"
            ;;
        v)
            VERBOSE=yes
            exec 7>&1
            ;;
        C)
            CDIR=$OPTARG
            ;;
        *)
            echo >&2 "ERROR: invalid option -$OPTARG"
            echo
            usage 1
            ;;
    esac
done

# Remove processed options from arguments
shift $(( OPTIND - 1 ));

if [ "${CMD:-}" == "" ]; then
    echo >&2 "ERROR: no command given"
    echo
    usage 1
elif [ "${ARCHIVE:-}" == "" ]; then
    echo >&2 "ERROR: no archive name given"
    echo
    usage 1
fi

function list {
    local path=""
    local size=0
    local line_no=0
    local ttar_file=$1
    if [ -n "${2:-}" ]; then
        echo >&2 "ERROR: too many arguments."
        echo
        usage 1
    fi
    if [ ! -e "$ttar_file" ]; then
        echo >&2 "ERROR: file not found ($ttar_file)"
        echo
        usage 1
    fi
    while read -r line; do
        line_no=$(( line_no + 1 ))
        if [ $size -gt 0 ]; then
            size=$(( size - 1 ))
            continue
        fi
        if [[ $line =~ ^Path:\ (.*)$ ]]; then
            path=${BASH_REMATCH[1]}
        elif [[ $line =~ ^Lines:\ (.*)$ ]]; then
            size=${BASH_REMATCH[1]}
            echo "$path"
        elif [[ $line =~ ^Directory:\ (.*)$ ]]; then
            path=${BASH_REMATCH[1]}
            echo "$path/"
        elif [[ $line =~ ^SymlinkTo:\ (.*)$ ]]; then
            echo  "$path -> ${BASH_REMATCH[1]}"
        fi
    done < "$ttar_file"
}

function extract {
    local path=""
    local size=0
    local line_no=0
    local ttar_file=$1
    if [ -n "${2:-}" ]; then
        echo >&2 "ERROR: too many arguments."
        echo
        usage 1
    fi
    if [ ! -e "$ttar_file" ]; then
        echo >&2 "ERROR: file not found ($ttar_file)"
        echo
        usage 1
    fi
    while IFS= read -r line; do
        line_no=$(( line_no + 1 ))
        if [ "$size" -gt 0 ]; then
            echo "$line" >> "$path"
            size=$(( size - 1 ))
            continue
        fi
        if [[ $line =~ ^Path:\ (.*)$ ]]; then
            path=${BASH_REMATCH[1]}
            if [ -e "$path" ] || [ -L "$path" ]; then
                rm "$path"
            fi
        elif [[ $line =~ ^Lines:\ (.*)$ ]]; then
            size=${BASH_REMATCH[1]}
            # Create file even if it is zero-length.
            touch "$path"
            vecho "    $path"
        elif [[ $line =~ ^Mode:\ (.*)$ ]]; then
            mode=${BASH_REMATCH[1]}
            chmod "$mode" "$path"
            vecho "$mode"
        elif [[ $line =~ ^Directory:\ (.*)$ ]]; then
            path=${BASH_REMATCH[1]}
            mkdir -p "$path"
            vecho "    $path/"
        elif [[ $line =~ ^SymlinkTo:\ (.*)$ ]]; then
            ln -s "${BASH_REMATCH[1]}" "$path"
            vecho "    $path -> ${BASH_REMATCH[1]}"
        elif [[ $line =~ ^# ]]; then
            # Ignore comments between files
            continue
        else
            echo >&2 "ERROR: Unknown keyword on line $line_no: $line"
            exit 1
        fi
    done < "$ttar_file"
}

function div {
    echo "# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" \
         "- - - - - -"
}

function get_mode {
    local mfile=$1
    if [ -z "${STAT_OPTION:-}" ]; then
        if stat -c '%a' "$mfile" >/dev/null 2>&1; then
            STAT_OPTION='-c'
            STAT_FORMAT='%a'
        else
            STAT_OPTION='-f'
            STAT_FORMAT='%A'
        fi
    fi
    stat "${STAT_OPTION}" "${STAT_FORMAT}" "$mfile"
}

function _create {
    shopt -s nullglob
    local mode
    while (( "$#" )); do
        file=$1
        if [ -L "$file" ]; then
            echo "Path: $file"
            symlinkTo=$(readlink "$file")
            echo "SymlinkTo: $symlinkTo"
            vecho "    $file -> $symlinkTo"
            div
        elif [ -d "$file" ]; then
            # Strip trailing slash (if there is one)
            file=${file%/}
            echo "Directory: $file"
            mode=$(get_mode "$file")
            echo "Mode: $mode"
            vecho "$mode $file/"
            div
            # Find all files and dirs, including hidden/dot files
            for x in "$file/"{*,.[^.]*}; do
                _create "$x"
            done
        elif [ -f "$file" ]; then
            echo "Path: $file"
            lines=$(wc -l "$file"|awk '{print $1}')
            echo "Lines: $lines"
            cat "$file"
            mode=$(get_mode "$file")
            echo "Mode: $mode"
            vecho "$mode $file"
            div
        else
            echo >&2 "ERROR: file not found ($file in $(pwd))"
            exit 2
        fi
        shift
    done
}

function create {
    ttar_file=$1
    shift
    if [ -z "${1:-}" ]; then
        echo >&2 "ERROR: missing arguments."
        echo
        usage 1
    fi
    if [ -e "$ttar_file" ]; then
        rm "$ttar_file"
    fi
    exec > "$ttar_file"
    _create "$@"
}

if [ -n "${CDIR:-}" ]; then
    if [[ "$ARCHIVE" != /* ]]; then
        # Relative path: preserve the archive's location before changing
        # directory
        ARCHIVE="$(pwd)/$ARCHIVE"
    fi
    cd "$CDIR"
fi

"$CMD" "$ARCHIVE" "$@"