diff --git a/fix-mail-filesize-filename/README.md b/fix-mail-filesize-filename/README.md index 02b4f95..b7104d1 100644 --- a/fix-mail-filesize-filename/README.md +++ b/fix-mail-filesize-filename/README.md @@ -28,7 +28,7 @@ When you run the script, it will prompt you to enter the domain name and usernam ## Requirements -The script requires Bash 3.x and GNU coreutils. +The script requires Bash 4.x and GNU coreutils. ## Note diff --git a/fix-mail-filesize-filename/fix-mail-filesize-filename.sh b/fix-mail-filesize-filename/fix-mail-filesize-filename.sh index 7485457..0c157a3 100644 --- a/fix-mail-filesize-filename/fix-mail-filesize-filename.sh +++ b/fix-mail-filesize-filename/fix-mail-filesize-filename.sh @@ -4,17 +4,14 @@ ############################################################################### # This script validates and corrects discrepancies between actual and stated file sizes in mail files. # Detailed instructions and usage guidelines can be found in the README.md. -# Requirements : bash 3.x, GNU coreutils -# Version : 2.1 +# Requirements : bash 4.x, GNU coreutils +# Version : 2.1.6 ######### # Initialize flags for fix and export options fix_flag=0 export_flag=0 -# Initialize counter for fixed mismatches -fixed_count=0 - # Check the arguments for arg in "$@"; do case $arg in @@ -41,17 +38,17 @@ for arg in "$@"; do esac done -# Start the timer -start_time=$(date +%s) - # Get the domain name and username echo -n "Enter the domain name: " read domain echo -n "Enter the username: " read username +# Get the mailnames directory from psa.conf +plesk_mailnames_dir=$(grep 'PLESK_MAILNAMES_D' /etc/psa/psa.conf | awk '{print $2}') + # Set the directory path -dir="/var/qmail/mailnames/${domain}/${username}" +dir="${plesk_mailnames_dir}/${domain}/${username}" echo "The directory to be checked is: ${dir}" # Check if the directory exists and is readable @@ -60,34 +57,34 @@ if [ ! -d "${dir}" ] || [ ! -r "${dir}" ]; then exit 1 fi -# Get the total number of files that match the expected filename format +# Get the total number of files that match the current filename format total_files=0 -count=0 mismatch_count=0 +fixed_count=0 # Initialize an array to store the mismatches -declare -a mismatches +declare -A mismatches # Function to check filenames check_filenames() { while IFS= read -r -d '' file; do - # Extract the expected size from the filename - expected_size=$(echo ${file} | grep -oP ',S=\K[0-9]+') + # Extract the current size from the filename + current_size=$(echo ${file} | grep -oP ',S=\K[0-9]+') # Get the actual size actual_size=$(stat -c%s "${file}") # Increment the total_files counter - total_files=$((total_files+1)) + ((total_files++)) # Check if the sizes match - if [ "${expected_size}" != "${actual_size}" ]; then + if [ "${current_size}" != "${actual_size}" ]; then echo "Mismatch found in file: ${file}" - echo "Expected size: ${expected_size}, Actual size: ${actual_size}" - mismatch_count=$((mismatch_count+1)) + echo "Current size: ${current_size}, Actual size: ${actual_size}" + ((mismatch_count++)) # Store the mismatch information - mismatches+=("${file} ${expected_size} ${actual_size}") + mismatches["${file}"]="${current_size} ${actual_size}" fi # Show the progress @@ -96,7 +93,6 @@ check_filenames() { echo "" # Move to a new line after the loop } - # Function to export mismatches export_mismatches() { for mismatch in "${mismatches[@]}"; do @@ -108,15 +104,20 @@ export_mismatches() { # Function to fix mismatches fix_mismatches() { - for mismatch in "${mismatches[@]}"; do - IFS= read -r -a array <<< "$mismatch" - file="${array[0]}" - expected_size="${array[1]}" - actual_size="${array[2]}" - new_file=$(echo ${file} | sed "s/S=${expected_size}/S=${actual_size}/") + # Iterate over the keys (file names) of the associative array + for file in "${!mismatches[@]}"; do + # Retrieve the current_size and actual_size values, separated by a space + values=(${mismatches["${file}"]}) + current_size="${values[0]}" + actual_size="${values[1]}" + + # Construct the new filename + new_file=$(echo ${file} | sed "s/S=${current_size}/S=${actual_size}/") + + # Attempt to rename the file if mv "${file}" "${new_file}"; then echo "File has been renamed to: ${new_file}" - fixed_count=$((fixed_count+1)) + ((fixed_count++)) else echo "Error: Failed to rename file: ${file}" fi @@ -153,14 +154,9 @@ if [ $fix_flag -eq 0 ] && [ $export_flag -eq 0 ] && [ $mismatch_count -gt 0 ]; t esac fi -# Calculate the elapsed time -end_time=$(date +%s) -elapsed_time=$((end_time-start_time)) - # Print the statistics echo "Total files checked: ${total_files}" echo "Total mismatches found: ${mismatch_count}" if [ $fix_flag -eq 1 ] || [ "${action:-}" == "fix" ] || [ "${action:-}" == "both" ]; then echo "Total mismatches fixed: ${fixed_count}" fi -echo "Elapsed time: ${elapsed_time} seconds"