-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathseafile-symlink.sh
executable file
·324 lines (264 loc) · 10.8 KB
/
seafile-symlink.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
#!/bin/bash
#================================================================
# HEADER
#================================================================
#% USAGE
#+ ${SCRIPT_NAME} [preset]
#%
#% SYNOPSIS
#% Saves symlinks to a syncable format and restores them therefrom.
#%
#% DESCRIPTION
#% Has the ability to save symlink date via placeholder files, or via
#% `seafile-symlink.txt` in library root. Create custom ini files in
#% the `preset` directory. Tell the script which ini file to use via
#% the `-Preset` param.
#%
#% Meant to address https://github.com/haiwen/seafile/issues/288
#%
#% OPTIONS
#% [preset] Specifies which ini config file to use
#%
#% EXAMPLES
#% ${SCRIPT_NAME} default # loads ./presets/default.ini
#% ${SCRIPT_NAME} default.ini # loads ./presets/default.ini
#% ${SCRIPT_NAME} ~/foo/default.ini # accepts absolute paths
#%
#================================================================
#- IMPLEMENTATION
#- version 1.0
#- author Illya Moskvin <[email protected]>
#- created 2018-07-08
#- license MIT
#================================================================
# END_OF_HEADER
#================================================================
#================================================================
# Initialization
#================================================================
# Functions for trapping process and exiting from functions and sub-shells.
# https://stackoverflow.com/questions/9893667/is-there-a-way-to-write-a-bash-function-which-aborts-the-whole-execution-no-mat
# https://stackoverflow.com/questions/24597818/exit-with-error-message-in-bash-oneline
trap "exit 1" TERM
export TOP_PID=$$
function error_exit {
echo "$1" >&2
kill -s TERM $TOP_PID
}
# Save the path from which this script was called. We'll restore it later.
DIR_INIT="$(pwd)"
# Change path to where the current script is located
# We will need this for resolving relative paths e.g. within ini files
# https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
cd "$( dirname "${BASH_SOURCE[0]}" )"
#================================================================
# Function definitions
#================================================================
# Retrieves a value from an ini file.
# Trims trailing comments, spaces, and unwraps single quotes.
function get_config_value {
# https://stackoverflow.com/questions/6318809/how-do-i-grab-an-ini-value-within-a-shell-script
v="$(awk -F "=" '/'$1'/ {print $2}' "$PRESET" | cut -f1 -d";" | tr -d ' ' | tr -d "'")"
if [ -z "$v" ]; then
error_exit "Missing key in config: $1"
fi
echo "$v"
}
# Convert potentially Windows path to Unix
# https://stackoverflow.com/questions/13701218/windows-path-to-posix-path-conversion-in-bash
function get_localized_path {
path="$(echo "$1" | sed -E 's/^([A-Za-z]):/\\\1/' | sed 's/\\/\//g')"
if [ "${path:0:1}" != '/' ] && [ "${path:0:1}" != '.' ] ; then
path="./$path"
fi
echo "$path"
}
#================================================================
# Config loading and validation
#================================================================
# Ensure a preset name was passed
if [ $# -ne 1 ]; then
echo "Usage: $0 [prefixname]"
exit 1
fi
# Load the preset name from the argument
PRESET=$1
# If preset isn't an absolute path, look for an ini in `presets` subdir
if [[ "$PRESET" != /* ]]; then
PRESET="${PRESET%.ini}.ini"
PRESET="presets/$PRESET"
fi
# Ensure the specified config exists
if [ ! -f "$PRESET" ]; then
error_exit "Config not found: $(realpath "$PRESET")"
fi
# Unfortunately, associative arrays are only supported in bash 4+
# Installing it on macOS is a hassle, so we'll stick with bash 3
# https://apple.stackexchange.com/questions/193411/update-bash-to-version-4-0-on-osx
LibraryPath="$(get_config_value 'LibraryPath')"
StorageMethod="$(get_config_value 'StorageMethod')"
PlaceholderExt="$(get_config_value 'PlaceholderExt')"
# LibraryPath might be in Windows syntax.
LibraryPath="$(get_localized_path "$LibraryPath")"
# Ensure that LibraryPath exists
if [ ! -d "$LibraryPath" ]; then
error_exit "Cannot resolve LibraryPath to a directory: $LibraryPath"
fi
# Ensure the storage method is valid
if [ "$StorageMethod" != 'placeholder' ] && [ "$StorageMethod" != 'database' ] ; then
error_exit "Invalid StorageMethod: $StorageMethod"
fi
# Ensure PlaceholderExt starts with a period
PlaceholderExt=".$(echo "$PlaceholderExt" | sed -e 's/^\.//')"
# Output some config feedback to user
echo "Processing LibraryPath: $(realpath "$LibraryPath")"
echo "Using StorageMethod: $StorageMethod"
#================================================================
# Data gathering
#================================================================
# Enter the LibraryPath. We'll be resolving all paths from here onward.
cd "$LibraryPath"
# We'll append data in database format to here, then process it later.
DATA_RAW=()
# Gather data from actual symlinks
# https://stackoverflow.com/questions/22691436/unable-to-add-element-to-array-in-bash
# https://stackoverflow.com/questions/2087001/how-can-i-process-the-results-of-find-in-a-bash-script
while read linkPath; do
destPath="$(readlink "$linkPath")"
destPath="$(get_localized_path "$destPath")"
DATA_RAW+=("$linkPath >>> $destPath")
done < <(find . -type l)
# Gather data from symlink placeholders
while read placeholderPath; do
linkPath="$(echo "$placeholderPath" | sed -e "s/$PlaceholderExt//")"
destPath="$(<"$placeholderPath")"
DATA_RAW+=("$linkPath >>> $destPath")
done < <(find . -name "*$PlaceholderExt")
# Gather data from symlink database
# https://stackoverflow.com/questions/5057083/read-a-file-using-a-bash-script
if [ -f "seafile-symlink.txt" ]; then
while IFS= read -r datum || [ -n "$datum" ]; do
if [[ $datum = *' >>> '* ]]; then
# TODO: Refactor w/ reading in next section
linkPath="$(awk -F ' >>> ' '{print $1}' <<< "$datum")"
destPath="$(awk -F ' >>> ' '{print $2}' <<< "$datum")"
# Normalizes to unix convetion
linkPath="$(get_localized_path "$linkPath")"
destPath="$(get_localized_path "$destPath")"
DATA_RAW+=("$linkPath >>> $destPath")
fi
done < "seafile-symlink.txt"
fi
# Ensure the array is unique. Tried a few solutions...
# https://stackoverflow.com/questions/13648410/how-can-i-get-unique-values-from-an-array-in-bash
# https://www.linuxquestions.org/questions/programming-9/bash-combine-arrays-and-delete-duplicates-882286/
OLDIFS="$IFS"
IFS=$'\n'
DATA_RAW=(`for i in "${DATA_RAW[@]}"; do echo "$i" ; done | sort -du`)
IFS="$OLDIFS"
#================================================================
# Writing files
#================================================================
if [[ ${#DATA_RAW[@]} -eq 0 ]]; then
echo 'No symlink data found. Performing cleanup.'
else
echo 'Symlink data found. Processing...'
printf '%s\n' "${DATA_RAW[@]}"
fi
# Reset the contents of the symlink database, or delete it if using placeholders
# https://superuser.com/questions/90008/how-to-clear-the-contents-of-a-file-from-the-command-line
if [ "$StorageMethod" == 'database' ] && [[ ${#DATA_RAW[@]} -gt 0 ]]; then
> "seafile-symlink.txt"
echo "Created database: $(realpath "seafile-symlink.txt")"
elif [ -f "seafile-symlink.txt" ] ; then
rm "seafile-symlink.txt"
echo "Removed database: $(realpath "seafile-symlink.txt")"
fi
# Remove all placeholder files. We'll recreate them below if needed.
find . -name "*$PlaceholderExt" | xargs rm
# Prepare seafile-ignore.txt for writing
NEEDLE='### SEAFILE-SYMLINK (AUTOGENERATED) ###'
IGNORE=()
IGNORE_HAS_HEADER=false
if [ -f "seafile-ignore.txt" ] ; then
while IFS= read -r line || [ -n "$line" ]; do
if [[ "$line" == "$NEEDLE" ]]; then
IGNORE_HAS_HEADER=true
break
fi
IGNORE+=("$line")
done < "seafile-ignore.txt"
fi
if [[ ${#DATA_RAW[@]} -gt 0 ]]; then
lastLine="$(printf %s\\n "${IGNORE[@]:(-1)}")"
if [[ ${#IGNORE[@]} -gt 0 ]] && [[ ${#lastLine} -gt 0 ]]; then
IGNORE+=('')
fi
IGNORE+=("$NEEDLE")
IGNORE+=('# Do not modify the line above or anything below it')
printf "%s\n" "${IGNORE[@]}" > "seafile-ignore.txt"
elif [ "$IGNORE_HAS_HEADER" = true ]; then
echo 'Removing header from seafile-ignore.txt'
lastLine="$(printf %s\\n "${IGNORE[@]:(-1)}")"
if [[ ${#lastLine} -eq 0 ]]; then
unset 'IGNORE[${#IGNORE[@]}-1]'
fi
printf "%s\n" "${IGNORE[@]}" > "seafile-ignore.txt"
fi
for datum in "${DATA_RAW[@]}"
do
# Ignore any rogue blank lines
if [[ $datum != *' >>> '* ]]; then
continue
fi
# https://stackoverflow.com/questions/42662099/how-would-i-delimit-a-string-by-multiple-delimiters-in-bash
linkPath="$(awk -F ' >>> ' '{print $1}' <<< "$datum")"
destPath="$(awk -F ' >>> ' '{print $2}' <<< "$datum")"
# Business logic for normalizing absolute links
# https://stackoverflow.com/a/28523143
if [[ "$destPath" == /* ]] ; then
# Check if item falls inside library root
if [[ "$destPath" == "$(pwd)"* ]] ; then
tempPath="$destPath"
destPath="$(realpath --relative-to="$LibraryPath" "$destPath")"
echo "Converted absolute target: $tempPath >>> $destPath"
fi
else
# Check if item falls outside library root
tempPath="$(realpath "$(dirname "$linkPath")/$destPath")"
if [[ "$tempPath" != "$(pwd)"* ]] ; then
echo "Converted relative target: $destPath >>> $tempPath"
destPath="$tempPath"
fi
fi
# Left-trims ./ to align w/ seafile-ignore.txt examples
linkPath="$(echo "$linkPath" | sed -e 's/^\.\///')"
destPath="$(echo "$destPath" | sed -e 's/^\.\///')"
# Append link path to seafile-ignore.txt
# Determine if the link will point to a directory
if [ -d "$(dirname "$linkPath")/$destPath" ] ; then
echo "$linkPath/" >> "seafile-ignore.txt"
else
echo "$linkPath" >> "seafile-ignore.txt"
fi
# Make a symlink
ln -snf "$destPath" "$linkPath"
# Write a symlink placeholder file
if [ "$StorageMethod" == 'placeholder' ] ; then
echo "$destPath" > "$linkPath$PlaceholderExt"
fi
# Append to symlink database
if [ "$StorageMethod" == 'database' ] ; then
echo "$linkPath >>> $destPath" >> "seafile-symlink.txt"
fi
done
# Remove seafile-ignore.txt if it would be empty
if [ ${#IGNORE[@]} -eq 0 ]; then
rm "seafile-ignore.txt"
echo 'Removed seafile-ignore.txt because it would be empty'
fi
#================================================================
# Cleanup
#================================================================
# Restore our initial working directory
cd "$DIR_INIT"