Fix bugs in bootleg stow, such as not copying symlinks and empty directory removal not working

This commit is contained in:
Salt 2021-01-28 23:45:17 -06:00
parent 2c4341e5e8
commit 03293faf89

View File

@ -75,14 +75,13 @@ checkconflict() {
error "Could not read directory: $1" 2
fi
# Get our list of files
local files="$(find "$1" -type f)"
local files="$(find "$1" -type f -o -type l)"
local directories="$(find "$1" -type d)"
local -a conflict
# Iterate over them
for file in $files; do
# Get the basename of the file
filename="${file#"$1"/}"
log "$filename" 2
if [ -f ../"$filename" ]; then
if [ -h ../"$filename" ]; then
continue
@ -106,7 +105,7 @@ stow() {
# Note that you should checkconflict first
[ -z "$1" ] && return 1
stowdir="$(basename -- "$PWD")"
log "Stow directory is: $stowdir" 2
log "Stowing package: $stowdir" 1
pushd .. > /dev/null 2>&1
for dir in $_directories; do
dirname="${dir#"$1"/}"
@ -126,30 +125,42 @@ stow() {
else
path="$PWD/$stowdir/$1/$filename"
fi
log "Linking file: $filename to $path" 2
if [ -h "$filename" ]; then
rm "$filename"
fi
ln -s "$path" "$filename"
if [ -h "$PWD/$stowdir/$1/$filename" ]; then
log "Copying symlink: $filename" 2
cp -d "$PWD/$stowdir/$1/$filename" "$filename"
else
log "Linking file: $filename to $path" 2
ln -s "$path" "$filename"
fi
done
echo "Done stowing package: $1" 1
popd > /dev/null 2>&1
}
unstow() {
# Unstow all of _files and _directories
# Takes a packagename as $1
[ -z "$1" ] && return 1
pushd .. > /dev/null 2>&1
for file in $_files; do
filename="${file#"$1"/}"
if [ -h "$filename" ]; then
rm "$filename"
elif ! [ -e "$filename" ]; then
warn "File does not exist, skipping: $filename" 1
else
warn "File is not a symlink: $filename"
warn "File is not a symlink, skipping: $filename"
fi
done
_directories="$(echo "$_directories" | tac)"
log "Removing empty directories" 2
for dir in $_directories; do
dirname="${dir#"$1"/}"
# We silently ignore errors here so that rmdiring a directory with stuff in
# it doesn't break our loop.
rmdir -p "$dir" > /dev/null 2>&1 || continue
rmdir -p "$dirname" > /dev/null 2>&1 || continue
done
popd > /dev/null 2>&1
}