From 54ad4f7f7412a7222768a9853851e4c868d9bc1b Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sun, 27 Aug 2023 21:54:18 -0700 Subject: [PATCH] Work on function to uncompress mail folders --- mailboxes/boxes-lib.pl | 34 ++++++++++++++++++++++++++++++++++ mailboxes/folders-lib.pl | 21 +++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/mailboxes/boxes-lib.pl b/mailboxes/boxes-lib.pl index b9cc5152a..4b6ca8d43 100755 --- a/mailboxes/boxes-lib.pl +++ b/mailboxes/boxes-lib.pl @@ -3006,6 +3006,40 @@ if ($switched) { return $rv; } +# is_gzipped_file(file) +# Returns 1 if a file is gzip compressed +sub is_gzipped_file +{ +my ($file) = @_; +my $fh; +my $rv = open($fh, "<", $file); +return 0 if (!$rv); +my $two; +read($fh, $two, 2); +close($fh); +return $two eq "\037\213" ? 1 : 0; +} + +# gunzip_mail_file(file) +# Uncompress a mail file in place +sub gunzip_mail_file +{ +my ($file) = @_; +my $switched = &switch_to_mail_user(); +my $outfile = $file.".$$.uncompressed"; +my $ex = system("gunzip -c ".quotemeta($file)."> ".quotemeta($outfile)." 2>/dev/null"); +if ($ex) { + unlink($outfile); + } +else { + rename($outfile, $file); + } +if ($switched) { + $) = 0; + $> = 0; + } +} + # create_as_mail_user(fh, file) # Creates a new file, but ensures that it does not yet exist first, and then # sets the ownership to the mail user diff --git a/mailboxes/folders-lib.pl b/mailboxes/folders-lib.pl index 30056ac90..f2d3a12e2 100755 --- a/mailboxes/folders-lib.pl +++ b/mailboxes/folders-lib.pl @@ -1543,6 +1543,27 @@ if ($src->{'sortable'}) { } } +# mailbox_uncompress_folder(&folder) +# If a folder or it's files are gzipped, uncompress them in place +sub mailbox_uncompress_folder +{ +my ($folder) = @_; +if ($folder->{'type'} == 1 || $folder->{'type'} == 3) { + my @files = $src->{'type'} == 1 ? &get_maildir_files($folder->{'file'}) + : &get_mhdir_files($folder->{'file'}); + foreach my $f (@files) { + if (&is_gzipped_file($f)) { + &gunzip_mail_file($f); + } + } + } +elsif ($folder->{'type'} == 0) { + if (&is_gzipped_file($folder->{'file'})) { + &gunzip_mail_file($folder->{'file'}); + } + } +} + # mailbox_copy_mail(&source, &dest, mail, ...) # Copy mail from one folder to another sub mailbox_copy_mail