From 2f73ed2a5256937edc196029926140619e23249a Mon Sep 17 00:00:00 2001 From: Luca Krebs <41986129+obendev@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:48:41 +0200 Subject: [PATCH] Enhance mail script flexibility, readability, and data management (#64) * Refactor mismatch storage from single array to associative array This commit refactors the way mismatches are stored in the script. Previously, mismatches were stored in a single array with each element being a string containing the filename, expected size, and actual size. This led to complications when trying to extract individual components from each mismatch. The script now uses an associative array to store mismatches, with the filename as the key and a string containing the expected and actual sizes as the value. This makes it easier to manage and access the data related to each mismatch. * Code cleanup: formatting and unused variables removal * Change requirements to 4.x as associative arrays are in use * Update patch to 3 * Refactor: Use post-increment operator for variable increment Replaced the traditional method of incrementing variables (var=$((var+1))) with the post-increment operator (var++) in the script. This change simplifies the code and takes advantage of the features available in Bash 4.x. * Dynamically retrieve mailnames directory from configuration Replaced hardcoded mailnames directory path with a dynamic retrieval from the /etc/psa/psa.conf configuration file. This allows for greater flexibility and configurability in the script. * Clarify size variable naming for accuracy and consistency Renamed variables and related elements from expected_size to current_size to more accurately reflect the actual size represented in file names. --- fix-mail-filesize-filename/README.md | 2 +- .../fix-mail-filesize-filename.sh | 60 +++++++++---------- 2 files changed, 29 insertions(+), 33 deletions(-) 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"