From 3b3ba529acdf9125ac9d5873502fc43fef409d84 Mon Sep 17 00:00:00 2001 From: Luca Krebs <41986129+obendev@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:05:53 +0200 Subject: [PATCH 1/4] Enhance file detection for broader filename formats and handle spaces in directories MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed a bug where only colon separators where detected (:) but also comma (,) separators are possible – for accurate file identification (just checking for ,S= is sufficient here). Implemented -print0 and -z flags in find and grep commands for robust handling of directories containing spaces. --- .../fix-mail-filesize-filename.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fix-mail-filesize-filename/fix-mail-filesize-filename.sh b/fix-mail-filesize-filename/fix-mail-filesize-filename.sh index eea745c..292e9ed 100644 --- a/fix-mail-filesize-filename/fix-mail-filesize-filename.sh +++ b/fix-mail-filesize-filename/fix-mail-filesize-filename.sh @@ -61,7 +61,7 @@ if [ ! -d "${dir}" ] || [ ! -r "${dir}" ]; then fi # Get the total number of files that match the expected filename format -total_files=$(find ${dir} -type f | grep -E 'S=[0-9]+:' | wc -l) +total_files=$(find ${dir} -type f -print0 | grep -zE ',S=[0-9]+' | wc -l) count=0 mismatch_count=0 @@ -70,9 +70,9 @@ declare -a mismatches # Function to check filenames check_filenames() { - for file in $(find ${dir} -type f | grep -E 'S=[0-9]+:'); do + while IFS= read -r -d '' file; do # Extract the expected size from the filename - expected_size=$(echo ${file} | grep -oP 'S=\K[0-9]+') + expected_size=$(echo ${file} | grep -oP ',S=\K[0-9]+') # Get the actual size actual_size=$(stat -c%s "${file}") @@ -90,14 +90,15 @@ check_filenames() { # Show the progress count=$((count+1)) echo -ne "Progress: ${count}/${total_files} files checked\r" - done + done < <(find ${dir} -type f -print0 | grep -zE ',S=[0-9]+') echo "" # Move to a new line after the loop } + # Function to export mismatches export_mismatches() { for mismatch in "${mismatches[@]}"; do - IFS read -r -a array <<< "$mismatch" + IFS= read -r -a array <<< "$mismatch" file="${array[0]}" echo "${file}" >> "${domain}_${username}_mismatches.txt" done @@ -106,7 +107,7 @@ export_mismatches() { # Function to fix mismatches fix_mismatches() { for mismatch in "${mismatches[@]}"; do - IFS read -r -a array <<< "$mismatch" + IFS= read -r -a array <<< "$mismatch" file="${array[0]}" expected_size="${array[1]}" actual_size="${array[2]}" From ccec0030adccd78121e6c4cd3272f0bb33f9911d Mon Sep 17 00:00:00 2001 From: Luca Krebs <41986129+obendev@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:16:33 +0200 Subject: [PATCH 2/4] Optimize script by reducing file reads Refactored the check_filenames function to count the total number of files while checking for size mismatches. This change eliminates the need for an additional file read operation previously performed to count the total files. The script now reads the files only once, improving its efficiency. --- fix-mail-filesize-filename/fix-mail-filesize-filename.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/fix-mail-filesize-filename/fix-mail-filesize-filename.sh b/fix-mail-filesize-filename/fix-mail-filesize-filename.sh index 292e9ed..7485457 100644 --- a/fix-mail-filesize-filename/fix-mail-filesize-filename.sh +++ b/fix-mail-filesize-filename/fix-mail-filesize-filename.sh @@ -61,7 +61,7 @@ if [ ! -d "${dir}" ] || [ ! -r "${dir}" ]; then fi # Get the total number of files that match the expected filename format -total_files=$(find ${dir} -type f -print0 | grep -zE ',S=[0-9]+' | wc -l) +total_files=0 count=0 mismatch_count=0 @@ -77,6 +77,9 @@ check_filenames() { # Get the actual size actual_size=$(stat -c%s "${file}") + # Increment the total_files counter + total_files=$((total_files+1)) + # Check if the sizes match if [ "${expected_size}" != "${actual_size}" ]; then echo "Mismatch found in file: ${file}" @@ -88,8 +91,7 @@ check_filenames() { fi # Show the progress - count=$((count+1)) - echo -ne "Progress: ${count}/${total_files} files checked\r" + echo -ne "Progress: ${total_files} files checked\\r" done < <(find ${dir} -type f -print0 | grep -zE ',S=[0-9]+') echo "" # Move to a new line after the loop } From dda6462802ae10b6ee65685cac30f8e8dd22e723 Mon Sep 17 00:00:00 2001 From: Luca Krebs <41986129+obendev@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:34:03 +0200 Subject: [PATCH 3/4] Convert README from Markdown to AsciiDoc This commit changes the README file from Markdown to AsciiDoc format. AsciiDoc is chosen over Markdown due to its richer set of features. This change will make the README more versatile and maintainable. --- .../{README.md => README.adoc} | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) rename fix-mail-filesize-filename/{README.md => README.adoc} (94%) diff --git a/fix-mail-filesize-filename/README.md b/fix-mail-filesize-filename/README.adoc similarity index 94% rename from fix-mail-filesize-filename/README.md rename to fix-mail-filesize-filename/README.adoc index 02b4f95..e7c4a2b 100644 --- a/fix-mail-filesize-filename/README.md +++ b/fix-mail-filesize-filename/README.adoc @@ -1,18 +1,18 @@ -# Mail Filesize/Filename Fixer Script += Mail Filesize/Filename Fixer Script -## Overview +== Overview This script is designed to validate and correct discrepancies between the actual and stated file sizes in mail files. It's intended for use by technically experienced IT engineers. The script checks mail files in a specified directory, and identifies any files where the size stated in the filename does not match the actual file size. It can optionally fix these discrepancies by renaming the files, and/or export the filenames of the mismatched files to a text file. -## Usage +== Usage The script can be run with the following command: -``` +---- ./scriptname [--fix] [--export] -``` +---- Replace `scriptname` with the name of the script file. @@ -26,10 +26,10 @@ If no options are provided, the script will just check for mismatches and displa When you run the script, it will prompt you to enter the domain name and username. The script will then check the mail files in the directory `/var/qmail/mailnames/{domain}/{username}`. -## Requirements +== Requirements The script requires Bash 3.x and GNU coreutils. -## Note +== Note Please ensure that you have the necessary permissions to read and write to the directory and files before running the script. From d62743e9d4c5018ad11841357e11ff60865fdbc5 Mon Sep 17 00:00:00 2001 From: obendev <41986129+obendev@users.noreply.github.com> Date: Wed, 10 Apr 2024 17:32:53 +0200 Subject: [PATCH 4/4] Revert "Convert README from Markdown to AsciiDoc" This reverts commit dda6462802ae10b6ee65685cac30f8e8dd22e723. --- .../{README.adoc => README.md} | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) rename fix-mail-filesize-filename/{README.adoc => README.md} (94%) diff --git a/fix-mail-filesize-filename/README.adoc b/fix-mail-filesize-filename/README.md similarity index 94% rename from fix-mail-filesize-filename/README.adoc rename to fix-mail-filesize-filename/README.md index e7c4a2b..02b4f95 100644 --- a/fix-mail-filesize-filename/README.adoc +++ b/fix-mail-filesize-filename/README.md @@ -1,18 +1,18 @@ -= Mail Filesize/Filename Fixer Script +# Mail Filesize/Filename Fixer Script -== Overview +## Overview This script is designed to validate and correct discrepancies between the actual and stated file sizes in mail files. It's intended for use by technically experienced IT engineers. The script checks mail files in a specified directory, and identifies any files where the size stated in the filename does not match the actual file size. It can optionally fix these discrepancies by renaming the files, and/or export the filenames of the mismatched files to a text file. -== Usage +## Usage The script can be run with the following command: ----- +``` ./scriptname [--fix] [--export] ----- +``` Replace `scriptname` with the name of the script file. @@ -26,10 +26,10 @@ If no options are provided, the script will just check for mismatches and displa When you run the script, it will prompt you to enter the domain name and username. The script will then check the mail files in the directory `/var/qmail/mailnames/{domain}/{username}`. -== Requirements +## Requirements The script requires Bash 3.x and GNU coreutils. -== Note +## Note Please ensure that you have the necessary permissions to read and write to the directory and files before running the script.