-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlndir
executable file
·341 lines (324 loc) · 12.4 KB
/
lndir
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env bash
###########################################################################
# Hardlinks all files from one directory to another (like copying but #
# both files end up pointing to the same data), optionally recursing, #
# and optionally resolving conflicts interactively. #
###########################################################################
# Usage: #
# lndir [options] <source_dir> <destination_dir> #
# #
# Options: #
# -r, --recursive Process directories recursively #
# -d, --dryrun Dry run mode (no changes are made) #
# -i, --interactive Resolve conflicts interactively #
# -s, --sync Two-way synchronization mode (non-recursive) #
# -f, --force Force overwrite of conflicting files #
# -n, --nobak Do not create backup (.bak) when overwriting #
# -h, --help Display this help message #
###########################################################################
_lndir() {
# Helper functions for messages and help
_show_help() {
local s; [ -t 1 ] && s="$(tput smul 2>/dev/null || echo '')"
local r; [ -t 1 ] && r="$(tput rmul 2>/dev/null || echo '')"
echo "NAME"
echo " lndir - Hardlink files from one directory to another, with optional sync mode"
echo "SYNOPSIS"
echo " lndir [${s}options${r}] <${s}source_dir${r}> <${s}destination_dir${r}>"
echo "OPTIONS"
echo " -r, --recursive Process directories recursively"
echo " -d, --dryrun Dry run mode (no changes are made)"
echo " -i, --interactive Prompt on conflict"
echo " -s, --sync Two-way synchronization mode (non-recursive)"
echo " -f, --force Force overwrite of conflicting files"
echo " -n, --nobak, Do not create backup (.bak) when overwriting"
echo " -h, --help Display this help message"
}
_error() { echo "[ERR] lndir: $*" >&2; }
_warn() { echo "[WAR] lndir: $*" >&2; }
_info() { echo "[INF] lndir: $*"; }
# Option defaults
local recursive=false
local dryrun=false
local interactive=false
local sync=false
local force=false
local nobak=false
local from_dir
local to_dir
# Parse arguments
while [ $# -gt 0 ]; do
case "$1" in
-r|--recursive)
recursive=true
shift
;;
-d|--dryrun|--dry-run)
dryrun=true
shift
;;
-i|--interactive)
interactive=true
shift
;;
-s|--sync)
sync=true
shift
;;
-f|--force)
force=true
shift
;;
-n|--nobak|--no-backup)
nobak=true
shift
;;
-h|--help)
_show_help
return 0
;;
# End of options: Parse remainder as positional
--)
shift
while [ $# -gt 0 ]; do
if [ -z "$from_dir" ]; then
from_dir="$1"
else
to_dir="$1"
fi
shift
done
break
;;
-*)
_error "Unknown option '$1'."
return 1
;;
*)
if [ -z "$from_dir" ]; then
from_dir="$1"
else
to_dir="$1"
fi
shift
;;
esac
done
# $from_dir is defined first, so if it isn't defined we know neither is $to_dir
if [ -z "$from_dir" ]; then
_error "Source and destination directories not provided."
_show_help
return 1
elif [ -z "$to_dir" ]; then
_error "Destination directory not provided."
_show_help
return 1
fi
# Backup a file by moving it to file.bak (adding extra .bak if needed)
_backup_file() {
local file="$1"
local backup="$file.bak"
while [ -e "$backup" ]; do
backup="$backup.bak"
done
if mv "$file" "$backup"; then
_info "Backup created: $file -> $backup"
else
_error "Failed to create backup for $file"
fi
}
# Interactive conflict resolution
# Prompts the user using the format:
# "Choose an action for <basename>: 1 (keep source), 2 (keep destination), s (skip), c (cancel), d (diff), v (VSCode diff): "
_handle_conflict() {
local src="$1"
local dst="$2"
local basename
local choice
while true; do
basename="$(basename "$src")"
_info "Choose an action for $basename: 1 (keep source), 2 (keep destination), s (skip), c (cancel), d (diff), v (VSCode diff): "
read -r choice
case "$choice" in
1)
_update_link "$src" "$dst"
break
;;
2)
_update_link "$dst" "$src"
break
;;
s)
_info "Skipping conflict for $basename"
break
;;
c)
_warn "Operation cancelled by user."
return 1
;;
d)
if ! diff "$src" "$dst"; then
_warn "\`diff\` command failed or differences found."
fi
;;
v)
if ! code --diff "$src" "$dst"; then
_warn "\`code --diff\` command failed or differences found."
fi
;;
*)
_warn "Invalid option. Please select again."
;;
esac
done
}
# Common helper to unconditionally update (overwrite) a link
# Backs up the target unless nobak is set
_update_link() {
local src="$1"
local dst="$2"
if $dryrun; then
_info "Would update link: $src -> $dst"
else
if ! $nobak; then
_backup_file "$dst"
fi
if ln -f "$src" "$dst"; then
_info "Updated link: $dst"
else
_error "Failed to update link: $dst"
fi
fi
}
# Common helper to resolve a conflict using force or interactive options
_try_update_link() {
local src="$1"
local dst="$2"
if $force; then
if $dryrun; then
_info "Would force update: $src -> $dst"
else
if ! $nobak; then
_backup_file "$dst"
fi
if ln -f "$src" "$dst"; then
_info "Force updated: $dst"
else
_error "Failed to force update: $dst"
fi
fi
elif $interactive; then
if $dryrun; then
_info "Would prompt for conflict resolution for: $src -> $dst"
else
_handle_conflict "$src" "$dst"
fi
else
_warn "File exists, cannot hardlink: $dst"
fi
}
# One-way linking mode: hardlink files from source to destination
_link_files() {
# Unquoted "#" asserts start of string
local parent_dir="${1/#$from_dir/$to_dir}"
if [ ! -d "$parent_dir" ]; then
if $dryrun; then
_info "Would create directory: $parent_dir"
else
mkdir -p "$parent_dir"
fi
fi
find "$1" -mindepth 1 -maxdepth 1 ! -name .DS_Store -print0 | while IFS= read -r -d $'\0' file; do
local filename; filename="$(basename "$file")"
local to_file="$parent_dir/$filename"
if [ -d "$file" ]; then
if $recursive; then
_link_files "$file"
fi
else
if [ -f "$to_file" ]; then
local existing_link; existing_link="$(find "$to_file" -samefile "$file" 2>/dev/null)"
if [ "$existing_link" ]; then
_info "Hardlink already exists: $to_file"
else
_try_update_link "$file" "$to_file"
fi
else
if $dryrun; then
_info "Would link: $file -> $to_file"
else
ln -v "$file" "$to_file"
fi
fi
fi
done
}
# Sync mode: two-way synchronization of files in the top level of both directories
# For files that exist in both directories, if they are not hardlinked:
# - If the files are identical, update the destination to be a hardlink to the source.
# - If they differ, then:
# - With -f/--force, automatically overwrite (using source as authoritative),
# backing up first unless -n/--nobak is given.
# - With -i/--interactive, prompt for conflict resolution using the format above.
# - Otherwise, print a conflict warning to stderr and skip.
# Files that exist in only one directory are skipped.
_sync_mode() {
local -a from_files
local -a to_files
local -a all_files
mapfile -t from_files < <(find -L "$from_dir" -maxdepth 1 -type f ! -name '.DS_Store' ! -name '*.bak' -exec basename {} \; | sort)
mapfile -t to_files < <(find -L "$to_dir" -maxdepth 1 -type f ! -name '.DS_Store' ! -name '*.bak' -exec basename {} \; | sort)
mapfile -t all_files < <(printf '%s\n' "${from_files[@]}" "${to_files[@]}" | sort -u)
local file
local src_file
local dst_file
for file in "${all_files[@]}"; do
src_file="$from_dir/$file"
dst_file="$to_dir/$file"
if [ -e "$src_file" ] && [ -e "$dst_file" ]; then
# Check whether they point to the same file; in other words, whether their inodes are the same
local existing_link; existing_link="$(find "$to_file" -samefile "$file" 2>/dev/null)"
if [ "$existing_link" ]; then
_info "Already synced: $file"
else
if cmp -s "$src_file" "$dst_file"; then
_update_link "$src_file" "$dst_file"
else
_try_update_link "$src_file" "$dst_file"
fi
fi
else
_warn "Skipping, exists only in one directory: $file"
fi
done
}
# Clean up functions to avoid polluting global namespace
local __old_trap; __old_trap="$(trap -p RETURN)"
trap 'unset -f _lndir _show_help _error _warn _info _backup_file _handle_conflict _update_link _try_update_link _link_files _sync_mode; [ "$__old_trap" ] && eval "$__old_trap" || trap - RETURN' RETURN
# Main execution: if sync mode is enabled, run sync mode; otherwise, one-way linking
if $sync; then
_sync_mode
else
_link_files "$from_dir"
if ! $dryrun; then
# If true, found files in $to_dir which aren't in $from_dir
local found_extra=false
find "$to_dir" -type f -print0 | while IFS= read -r -d $'\0' dest_file; do
src_file="${dest_file/#$to_dir/$from_dir}"
if [ ! -e "$src_file" ]; then
if ! $found_extra; then
_info "Extra files in destination not in source:"
found_extra=true # Ensures header prints only once
fi
_info "$dest_file"
fi
done
find "$to_dir" -type d -empty -print0 | while IFS= read -r -d $'\0' dir; do
_warn "Empty directory: $dir"
done
else
_info "Dry run enabled, no changes were made."
fi
fi
}
_lndir "$@"