Last active 1757270230

Revision 807bbfd0318c29c3d66766a8da1deb2c37fdcc3f

relativize_symlinks.sh Raw
#!/bin/bash
# Script to convert absolute symlinks to relative symlinks in a directory
# Usage: ./relativize_symlinks.sh [directory]
set -e
# Function to calculate relative path from source to target
calculate_relative_path() {
local source="$1"
local target="$2"
# Convert to absolute paths to ensure consistency
source=$(realpath -m "$source")
target=$(realpath -m "$target")
# Split paths into arrays
IFS='/' read -ra source_parts <<< "${source#/}"
IFS='/' read -ra target_parts <<< "${target#/}"
# Find common prefix length
local common_length=0
local min_length=$((${#source_parts[@]} < ${#target_parts[@]} ? ${#source_parts[@]} : ${#target_parts[@]}))
for ((i=0; i<min_length; i++)); do
if [[ "${source_parts[i]}" == "${target_parts[i]}" ]]; then
((common_length++))
else
break
fi
done
# Calculate number of directories to go up from source
local up_dirs=$((${#source_parts[@]} - common_length - 1))
# Build relative path
local relative_path=""
# Add ../ for each directory we need to go up
for ((i=0; i<up_dirs; i++)); do
if [[ -n "$relative_path" ]]; then
relative_path="${relative_path}/.."
else
relative_path=".."
fi
done
# Add the remaining target path components
for ((i=common_length; i<${#target_parts[@]}; i++)); do
if [[ -n "$relative_path" ]]; then
relative_path="${relative_path}/${target_parts[i]}"
else
relative_path="${target_parts[i]}"
fi
done
# Handle case where source and target are the same
if [[ -z "$relative_path" ]]; then
relative_path="."
fi
echo "$relative_path"
}
# Function to process symlinks in a directory
process_directory() {
local dir="$1"
local base_dir="$2"
echo "Processing directory: $dir"
# Find all symbolic links in the directory
while IFS= read -r -d '' symlink; do
# Get the target of the symlink
local target
target=$(readlink "$symlink")
# Check if it's an absolute path
if [[ "$target" == /* ]]; then
echo "Found absolute symlink: $symlink -> $target"
# Calculate the directory containing the symlink
local symlink_dir
symlink_dir=$(dirname "$symlink")
# Convert absolute target to be relative to base directory
local adjusted_target="${base_dir}${target}"
# Calculate relative path from symlink location to target
local relative_target
relative_target=$(calculate_relative_path "$symlink_dir/dummy" "$adjusted_target")
echo " Converting to: $symlink -> $relative_target"
# Remove the old symlink and create the new relative one
rm "$symlink"
ln -s "$relative_target" "$symlink"
echo " ✓ Converted successfully"
fi
done < <(find "$dir" -type l -print0)
}
# Main script
main() {
local target_dir="${1:-.}"
# Validate directory exists
if [[ ! -d "$target_dir" ]]; then
echo "Error: Directory '$target_dir' does not exist" >&2
exit 1
fi
# Get absolute path of target directory to use as base
local abs_target_dir
abs_target_dir=$(realpath "$target_dir")
echo "Relativizing symlinks in: $abs_target_dir"
echo "Using current directory as filesystem root"
echo
# Process the directory
process_directory "$target_dir" "$abs_target_dir"
echo
echo "Symlink relativization complete!"
}
# Show usage if --help is passed
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
echo "Usage: $0 [directory]"
echo
echo "Convert absolute symlinks to relative symlinks in the specified directory."
echo "If no directory is specified, the current directory is used."
echo
echo "Example:"
echo " $0 ./extracted_filesystem"
echo
echo "This will convert symlinks like:"
echo " usr/lib/libz.so -> /lib/libz.so.1.2.11"
echo "To:"
echo " usr/lib/libz.so -> ../../lib/libz.so.1.2.11"
exit 0
fi
# Run main function
main "$@"