blob: a4a37a519f2da1eac590176783db06753f3b40dc (
plain) (
blame)
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
|
# Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "Configuration file sharing" project.
# For details, see https://github.com/egor-tensin/config-links.
# Distributed under the MIT License.
# Shared directory settings
shared_root_dir="$( pwd )/"
update_shared_dir() {
if [ "$#" -ne 1 ]; then
echo "usage: ${FUNCNAME[0]} DIR" >&2
return 1
fi
local new_shared_dir
new_shared_dir="$( traverse_path --exist --directory -- "$1" )"
[ "$new_shared_dir" = / ] || new_shared_dir="$new_shared_dir/"
[ "$db_path" = "$shared_root_dir$default_db_name" ] \
&& db_path="$new_shared_dir$default_db_name"
shared_root_dir="$new_shared_dir"
}
# Database maintenance
readonly default_db_name='links.bin'
db_path="$shared_root_dir$default_db_name"
declare -A database=()
declare -A shared_paths=()
declare -A symlink_paths=()
update_database_path() {
if [ "$#" -ne 1 ]; then
echo "usage: ${FUNCNAME[0]} PATH" >&2
return 1
fi
db_path="$( traverse_path --file -- "$1" )"
local db_dir
db_dir="$( dirname -- "$db_path" )"
mkdir -p -- "$db_dir"
}
add_entry() {
local entry
for entry; do
dump "entry: $entry"
local var_name
var_name="$( extract_variable_name "$entry" )"
cache_variable "$var_name"
local shared_var_dir="$shared_root_dir%$var_name%"
local symlink_var_dir
symlink_var_dir="$( resolve_variable "$var_name" )"
local subpath="${entry#%$var_name%/}"
local shared_path="$shared_var_dir"
[ "$shared_var_dir" != / ] && shared_path="$shared_path/"
shared_path="$shared_path$subpath"
local symlink_path="$symlink_var_dir"
[ "$symlink_var_dir" != / ] && symlink_path="$symlink_path/"
symlink_path="$symlink_path$subpath"
dump " shared file path: $shared_path"
dump " symlink path: $symlink_path"
database[$entry]="$var_name"
shared_paths[$entry]="$shared_path"
symlink_paths[$entry]="$symlink_path"
done
}
remove_entry() {
local entry
for entry; do
unset -v 'database[$entry]'
unset -v 'shared_paths[$entry]'
unset -v 'symlink_paths[$entry]'
done
}
read_database() {
[ ! -r "$db_path" ] && return 0
local entry
while IFS= read -d '' -r entry; do
add_entry "$entry"
done < "$db_path"
}
write_database() {
is_dry_run && return 0
[ "${#database[@]}" -eq 0 ] && rm -f -- "$db_path" && return 0
> "$db_path"
local entry
for entry in "${!database[@]}"; do
printf -- '%s\0' "$entry" >> "$db_path"
done
}
link_entry() {
local entry
for entry; do
local shared_path="${shared_paths[$entry]}"
local symlink_path="${symlink_paths[$entry]}"
local symlink_dir
symlink_dir="$( dirname -- "$symlink_path" )"
mkdir -p -- "$symlink_dir"
ln -f -s --no-target-directory -- "$shared_path" "$symlink_path"
done
}
delete_obsolete_dirs() {
if [ "$#" -ne 2 ]; then
echo "usage: ${FUNCNAME[0]} BASE_DIR DIR" >&2
return 1
fi
local base_dir="$1"
local dir="$2"
base_dir="$( traverse_path --exist --directory -- "$base_dir" )"
dir="$( traverse_path --exist --directory -- "$dir" )"
[ "$base_dir" = "$dir" ] && return 0
local subpath="${dir##$base_dir/}"
if [ "$subpath" = "$dir" ]; then
dump "base directory: $base_dir" >&2
dump "... is not a parent of: $dir" >&2
return 1
fi
( cd -- "$base_dir/" && rmdir -p --ignore-fail-on-non-empty -- "$subpath" )
}
unlink_entry() {
local entry
for entry; do
local shared_path="${shared_paths[$entry]}"
local symlink_path="${symlink_paths[$entry]}"
rm -f -- "$symlink_path"
local symlink_dir
symlink_dir="$( dirname -- "$symlink_path" )"
local var_name="${database[$entry]}"
local symlink_var_dir
symlink_var_dir="$( resolve_variable "$var_name" )"
delete_obsolete_dirs "$symlink_var_dir" "$symlink_dir" || true
done
}
symlink_present() {
local entry
for entry; do
local symlink_path="${symlink_paths[$entry]}"
test -L "$symlink_path" -o -e "$symlink_path"
done
}
symlink_points_to_shared_file() {
symlink_present "$@"
local entry
for entry; do
local shared_path="${shared_paths[$entry]}"
local symlink_path="${symlink_paths[$entry]}"
local target_path
target_path="$( traverse_path -- "$symlink_path" )"
test "$target_path" = "$shared_path"
done
}
shared_file_present() {
local entry
for entry; do
local shared_path="${shared_paths[$entry]}"
test -L "$shared_path" -o -e "$shared_path"
done
}
link_all_entries() {
local shared_var_dir
while IFS= read -d '' -r shared_var_dir; do
dump "shared directory: $shared_root_dir$shared_var_dir"
local shared_path
while IFS= read -d '' -r shared_path; do
dump " shared file path: $shared_path"
local entry="${shared_path:${#shared_root_dir}}"
add_entry "$entry" > /dev/null
dump " symlink path: ${symlink_paths[$entry]}"
if symlink_present "$entry"; then
if symlink_points_to_shared_file "$entry"; then
dump ' ... up-to-date'
else
dump " ... not a symlink or doesn't point to the shared file, adding a symlink"
is_dry_run || link_entry "$entry"
fi
else
dump ' ... adding a symlink'
is_dry_run || link_entry "$entry"
fi
done < <( find "$shared_root_dir$shared_var_dir" -type f -print0 )
done < <( find "$shared_root_dir" -regextype posix-basic -mindepth 1 -maxdepth 1 -type d -regex ".*/$var_name_regex\$" -printf '%P\0' )
}
unlink_all_entries() {
local entry
for entry in "${!database[@]}"; do
dump "entry: $entry"
local shared_path="${shared_paths[$entry]}"
local symlink_path="${symlink_paths[$entry]}"
dump " shared file path: $shared_path"
dump " symlink path: $symlink_path"
if symlink_points_to_shared_file "$entry"; then
dump ' ... removing the symlink'
is_dry_run || unlink_entry "$entry"
remove_entry "$entry"
else
dump " ... not a symlink or doesn't point to the shared file"
remove_entry "$entry"
fi
done
}
unlink_obsolete_entries() {
local entry
for entry in "${!database[@]}"; do
dump "entry: $entry"
local shared_path="${shared_paths[$entry]}"
local symlink_path="${symlink_paths[$entry]}"
dump " shared file path: $shared_path"
dump " symlink path: $symlink_path"
if symlink_points_to_shared_file "$entry"; then
if shared_file_present "$entry"; then
dump ' ... up-to-date'
else
dump ' ... obsolete'
is_dry_run || unlink_entry "$entry"
remove_entry "$entry"
fi
else
dump " ... not a symlink or doesn't point to the shared file"
remove_entry "$entry"
fi
done
}
|