#!/bin/bash ################################################################################ # Script: link-plus.sh # Description: Creates hard links for files from source to destination, saving # disk space while maintaining instant access. Processes entire # directories of files efficiently with progress indicators. # # Usage: ./link-plus.sh [OPTIONS] # # Arguments: # Source file or directory (if directory, links all .zip files) # Destination directory # # Options: # -r, --recursive Include subdirectories when processing source directory # -f, --force Overwrite existing files/links at destination # -v, --verbose Show each file as it's linked # --dry-run Preview what would be linked without making changes # --exclude Skip files matching pattern (can be used multiple times) # --help Show this help and exit # # Behavior: # - Single file: Creates hard link in destination directory # - Directory: Creates hard links for all .zip files to destination # - Progress: Reports every 100 files during batch operations # - Existing: Skips files that are already hard-linked (same inode) # # Benefits: # - Zero disk space usage (hard links share the same data blocks) # - Instant operation (no copying time) # - Perfect for ROM collections where you need files in multiple locations # # Examples: # # Link entire directory of ROMs # ./link-plus.sh /source/ps2 /roms/ps2 # # # Link with recursive subdirectories # ./link-plus.sh -r /source/all-platforms /roms # # # Preview without linking # ./link-plus.sh --dry-run /source/nes /roms/nes # # # Force overwrite existing files # ./link-plus.sh -f /source/ps3 /roms/ps3 # # Dependencies: # - logger.sh for consistent and color-coded logging ################################################################################ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Load shared libraries source "${PROJECT_ROOT}/core/logger.sh" log "DEBUG" "Starting script: $(basename "$0")" usage() { cat << EOF Usage: $(basename "$0") [OPTIONS] Create hard links for files, saving disk space while maintaining instant access. Arguments: Source file or directory (if directory, links all .zip files) Destination directory (will be created if needed) Options: -r, --recursive Include subdirectories when processing source directory -f, --force Overwrite existing files/links at destination -v, --verbose Show each file as it's linked --dry-run Preview what would be linked without making changes --exclude Skip files matching pattern (can use multiple times) --help Show this help and exit Examples: # Link all ROMs from source to destination $(basename "$0") /source/ps2-roms /roms/ps2 # Preview without making changes $(basename "$0") --dry-run /source/nes /roms/nes # Force overwrite and show verbose output $(basename "$0") -f -v /source/ps3 /roms/ps3 Benefits: - Hard links use zero extra disk space (same data blocks) - Instant operation (no copying time) - Files appear in multiple locations simultaneously - Perfect for ROM collections and metadata scraping EOF exit 1 } RECURSIVE=false DRY_RUN=false FORCE=false VERBOSE=false EXCLUDES=() # Parse options while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in -r|--recursive) RECURSIVE=true log "DEBUG" "RECURSIVE set to true" ;; --dry-run) DRY_RUN=true log "DEBUG" "DRY_RUN set to true" ;; --exclude) shift EXCLUDES+=("$1") log "DEBUG" "Added exclude pattern: $1" ;; -f|--force) FORCE=true log "DEBUG" "FORCE set to true" ;; -v|--verbose) VERBOSE=true log "DEBUG" "VERBOSE set to true" ;; --help) usage ;; *) usage ;; esac shift done if [ "$#" -ne 2 ]; then usage fi src="$1" dst="$2" ################################################################################ # Helper Functions (must be defined before use) ################################################################################ should_exclude() { local path="$1" for pat in "${EXCLUDES[@]}"; do if [[ "$path" == $pat ]]; then return 0 fi # Also support basename matching if [[ "$(basename "$path")" == $pat ]]; then return 0 fi done return 1 } link_file() { local src_file="$1" local dst_file="$2" if should_exclude "$src_file"; then [ "$VERBOSE" = true ] && log "INFO" "Excluded: $src_file" return 0 fi # Create parent directory if needed parent_dir=$(dirname "$dst_file") if [ ! -d "$parent_dir" ]; then if [ "$DRY_RUN" = true ]; then log "DEBUG" "[DRY-RUN] Would create directory: $parent_dir" else if ! mkdir -p "$parent_dir"; then log "ERROR" "Failed to create directory: $parent_dir" return 1 fi fi fi # Handle existing destination if [ -e "$dst_file" ] || [ -L "$dst_file" ]; then if [ "$FORCE" = true ]; then if [ "$DRY_RUN" = true ]; then log "DEBUG" "[DRY-RUN] Would remove existing: $dst_file" else if ! rm -f "$dst_file"; then log "ERROR" "Failed to remove existing: $dst_file" return 1 fi fi else # Check if it's already the same hard link if [ -f "$dst_file" ] && [ "$src_file" -ef "$dst_file" ]; then [ "$VERBOSE" = true ] && log "DEBUG" "Already linked: $dst_file" return 0 fi log "WARN" "Target exists (use --force to overwrite): $dst_file" return 0 fi fi # Create the hard link if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would link: $(basename "$src_file") -> $dst_file" else if ! ln "$src_file" "$dst_file" 2>/dev/null; then log "ERROR" "Failed to create hard link: $src_file -> $dst_file" return 1 fi [ "$VERBOSE" = true ] && echo "✓ Linked: $(basename "$src_file")" fi return 0 } link_symlink() { local src_link="$1" local dst_link="$2" if should_exclude "$src_link"; then log "INFO" "Excluded symlink: $src_link" return fi if ! target=$(readlink "$src_link"); then log "ERROR" "Failed to read symlink target: $src_link" return 1 fi if [ -e "$dst_link" ]; then if [ "$FORCE" = true ]; then if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would remove existing: $dst_link" else log "DEBUG" "Removing existing symlink: $dst_link" if ! rm -rf "$dst_link"; then log "ERROR" "Failed to remove existing symlink: $dst_link" return 1 fi fi else log "WARN" "Target exists (use --force to overwrite): $dst_link" return fi fi if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would replicate symlink $src_link -> $dst_link (target: $target)" else log "DEBUG" "Replicating symlink $src_link -> $dst_link (target: $target)" if ! ln -s "$target" "$dst_link"; then log "ERROR" "Failed to create symlink: $src_link -> $dst_link (target: $target)" return 1 fi # Verify the symlink was created successfully if [ ! -L "$dst_link" ]; then log "ERROR" "Symlink creation appeared to succeed but destination symlink does not exist: $dst_link" return 1 fi fi if [ "$VERBOSE" = true ]; then echo "Linked symlink: $src_link -> $dst_link" fi } link_dir_symlink() { local src_dir="$1" local dst_link="$2" if should_exclude "$src_dir"; then log "INFO" "Excluded directory: $src_dir" return fi if [ -e "$dst_link" ]; then if [ "$FORCE" = true ]; then if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would remove existing: $dst_link" else log "DEBUG" "Removing existing directory/symlink: $dst_link" if ! rm -rf "$dst_link"; then log "ERROR" "Failed to remove existing directory/symlink: $dst_link" return 1 fi fi else log "WARN" "Target exists (use --force to overwrite): $dst_link" return fi fi if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would symlink directory $src_dir -> $dst_link" else log "DEBUG" "Symlinking directory $src_dir -> $dst_link" if ! ln -s "$src_dir" "$dst_link"; then log "ERROR" "Failed to create directory symlink: $src_dir -> $dst_link" return 1 fi # Verify the symlink was created successfully if [ ! -L "$dst_link" ]; then log "ERROR" "Directory symlink creation appeared to succeed but destination symlink does not exist: $dst_link" return 1 fi fi if [ "$VERBOSE" = true ]; then echo "Linked directory symlink: $src_dir -> $dst_link" fi } ################################################################################ # Main Execution Logic ################################################################################ # Convert destination to absolute path to avoid issues with relative paths if [ -d "$dst" ]; then # If destination exists as directory, get its absolute path dst="$(cd "$dst" && pwd)" elif [ "$dst" = "." ]; then # Special case for current directory dst="$(pwd)" else # For non-existent paths, resolve parent and append basename dst="$(cd "$(dirname "$dst")" 2>/dev/null && pwd)/$(basename "$dst")" || dst="$(realpath "$dst" 2>/dev/null || readlink -f "$dst" 2>/dev/null || echo "$dst")" fi # Enhancement: If src is a directory, process all .zip files in it (non-recursive unless -r) if [ -d "$src" ] && [[ "$src" != */ ]]; then src="${src}/" fi if [ -d "$src" ]; then # When source is a directory, link all files to destination directory # Create destination directory if it doesn't exist if [ ! -d "$dst" ]; then if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would create destination directory: $dst" else log "DEBUG" "Creating destination directory: $dst" if ! mkdir -p "$dst"; then log "ERROR" "Failed to create destination directory: $dst" exit 1 fi fi fi # Process all zip files in the source directory batch_error=0 file_count=0 if [ "$RECURSIVE" = true ]; then # Recursive: find all .zip files in subdirectories while IFS= read -r -d '' zipfile; do basename_file="$(basename "$zipfile")" dst_file="$dst/$basename_file" if ! link_file "$zipfile" "$dst_file"; then log "ERROR" "Failed to link: $zipfile" batch_error=1 break fi ((file_count++)) # Progress indicator every 100 files if [ $((file_count % 100)) -eq 0 ]; then log "INFO" "Linked $file_count files..." fi done < <(find "$src" -type f -name '*.zip' -print0) else # Non-recursive: only process files in top-level directory while IFS= read -r -d '' zipfile; do basename_file="$(basename "$zipfile")" dst_file="$dst/$basename_file" if ! link_file "$zipfile" "$dst_file"; then log "ERROR" "Failed to link: $zipfile" batch_error=1 break fi ((file_count++)) # Progress indicator every 100 files if [ $((file_count % 100)) -eq 0 ]; then log "INFO" "Linked $file_count files..." fi done < <(find "$src" -maxdepth 1 -type f -name '*.zip' -print0) fi if [ $batch_error -eq 1 ]; then log "ERROR" "Batch processing failed after $file_count files" exit 1 fi log "INFO" "Successfully linked $file_count files from $src to $dst" exit 0 fi if [ ! -e "$src" ]; then log "ERROR" "Source does not exist: $src" exit 1 fi # Single file or symlink handling if [ -f "$src" ]; then if ! link_file "$src" "$dst"; then log "ERROR" "Failed to process source file: $src" exit 1 fi elif [ -L "$src" ]; then if ! link_symlink "$src" "$dst"; then log "ERROR" "Failed to process source symlink: $src" exit 1 fi else log "ERROR" "Unsupported source type: $src" exit 1 fi log "INFO" "Linking operation completed successfully." exit 0