]> git.donarmstrong.com Git - debbugs.git/blobdiff - Debbugs/DBase.pm
assume unknown encodings are UTF-8
[debbugs.git] / Debbugs / DBase.pm
index b82d791ade6cfcbc662c0a3a1d3dfc073010b918..5df78b01d2b747830f4355ca99a3d8f6a5120f77 100644 (file)
@@ -13,12 +13,12 @@ BEGIN {
        $VERSION     = 1.00;
 
        @ISA         = qw(Exporter);
-       @EXPORT      = qw( %Record );
+       @EXPORT      = qw();
        %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
 
        # your exported package globals go here,
        # as well as any optionally exported functions
-       @EXPORT_OK   = qw( %Record );
+       @EXPORT_OK   = qw();
 }
 
 use vars      @EXPORT_OK;
@@ -26,16 +26,18 @@ use Fcntl ':flock';
 use Debbugs::Config;
 use Debbugs::Email;
 use Debbugs::Common;
-use FileHandle;
-use File::Basename qw(&basename);
+use Debbugs::DBase::Log;
+use Debbugs::DBase::Log::Html;
+use Debbugs::DBase::Log::Message;
+use Debbugs::DBase::Log::Mail;
 
-%Record = ();
+use FileHandle;
+use File::Basename qw(&dirname);
+use File::Path;
 
-my $LoadedRecord = 0;
 my $OpenedRecord = 0;
 my $OpenedLog = 0;
-my $FileLocked = 0;
-my $FileHandle = new FileHandle;
+my $FileHandle;
 my $LogfileHandle = new FileHandle;
 
 sub ParseVersion1Record
@@ -45,27 +47,29 @@ sub ParseVersion1Record
                "keywords", "done", "forwarded", "mergedwith", "severity" );
     my $i = 0;
     my $tag;
+    my (%record, %btags);
 
     print "D2: (DBase) Record Fields:\n" if $Globals{ 'debug' } > 1;
     foreach my $line ( @data )
     {
        chop( $line );
        $tag = $fields[$i];
-       $Record{ $tag } = $line;
+       $record{ $tag } = $line;
        print "\t $tag = $line\n" if $Globals{ 'debug' } > 1;
        $i++;
-       $GTags{ "BUG_$tag" } = $line;
+       $btags{ "BUG_$tag" } = $line;
     }
+    return ( \%record, \%btags );
 }
 
 sub ParseVersion2Record
 {
     # I envision the next round of records being totally different in
     # meaning.  In order to maintain compatability, version tagging will be
-    # implemented in thenext go around and different versions will be sent
+    # implemented in the next go around and different versions will be sent
     # off to different functions to be parsed and interpreted into a format
     # that the rest of the system will understand.  All data will be saved
-    # in whatever 'new" format ixists.  The difference will be a "Version: x"
+    # in whatever 'new" format exists.  The difference will be a "Version: x"
     # at the top of the file.
 
     print "No version 2 records are understood at this time\n";
@@ -74,77 +78,127 @@ sub ParseVersion2Record
 
 sub ReadRecord
 {
-    my $record = $_[0];
-    print "V: Reading status $record\n" if $Globals{ 'verbose' };
-    if ( $record ne $LoadedRecord )
-    {
-       my @data;
+    my ($recordnum, $with_log, $new) = (shift, shift, shift);
+    my @data;
+    my $record;
+    my $btags;
 
-       seek( $FileHandle, 0, 0 );
-       @data = <$FileHandle>;
-       if ( scalar( @data ) =~ /Version: (\d*)/ )
+    #Open Status File
+    print "V: Reading status $recordnum\n" if $Globals{ 'verbose' };
+    if( $OpenedRecord != $recordnum )
+    {
+       if( defined( $FileHandle ) )
        {
-           if ( $1 == 2 )
-           { &ParseVersion2Record( @data ); }
-           else
-           { &fail( "Unknown record version: $1\n"); }
+           print "D1: Closing status $recordnum\n" if $Globals{ 'debug' };
+           $OpenedRecord = 0;
+           close $FileHandle;
+           $FileHandle = undef;
        }
-       else { &ParseVersion1Record( @data ); }
-       $LoadedRecord = $record;
+       print "D1: Opening status $recordnum\n" if $Globals{ 'debug' };
+       $FileHandle = &OpenFile( ["db", "archive"], $recordnum, ".status", "status", $new );
+       if( !defined( $FileHandle ) ) { return undef; }
     }
-    else { print "D1: (DBase) $record is already loaded\n" if $Globals{ 'debug' }; }
+    else { print "D1: Reusing status $recordnum\n" if $Globals{ 'debug' }; }
+
+    #Lock status file
+    print "D1: Locking status $recordnum\n" if $Globals{ 'debug' };
+    flock( $FileHandle, LOCK_EX ) || &fail( "Unable to lock record $recordnum\n" );
 
+    #Read in status file contents
+    print "D1: Loading status $recordnum\n" if $Globals{ 'debug' };
+    seek( $FileHandle, 0, 0 );
+    @data = <$FileHandle>;
+
+    #Parse Status File Contents
+    if ( scalar( @data ) =~ /Version: (\d*)/ )
+    {
+       if ( $1 == 2 )
+       { &ParseVersion2Record( @data ); }
+       else
+       { &fail( "Unknown record version: $1\n"); }
+    }
+    else { ($record, $btags) = &ParseVersion1Record( @data ); }
+    if( $with_log )
+    {
+       #DO READ IN LOG RECORD DATA STUFF
+    }
+    return ($record, $btags);
 }
 
 sub WriteRecord
 {
+    my ($recordnum, %record) = @_;
     my @fields = ( "originator", "date", "subject", "msgid", "package",
                "keywords", "done", "forwarded", "mergedwith", "severity" );
-    print "V: Writing status $LoadedRecord\n" if $Globals{ 'verbose' };
+
+    #Open Status File
+    print "V: Writing status $recordnum\n" if $Globals{ 'verbose' };
+    if( $OpenedRecord != $recordnum )
+    {
+       if( defined( $FileHandle ) )
+       {
+           print "D1: Closing status $recordnum\n" if $Globals{ 'debug' };
+           $OpenedRecord = 0;
+           close $FileHandle;
+           $FileHandle = undef;
+       }
+       print "D1: Opening status $recordnum\n" if $Globals{ 'debug' };
+       $FileHandle = &OpenFile( ["db", "archive"], $recordnum, ".status", "status", "old" );
+       if( !defined( $FileHandle ) ) { return undef; }
+    }
+    else { print "D1: Reusing status $recordnum\n" if $Globals{ 'debug' }; }
+
+    #Lock status file
+    print "D1: Locking status $recordnum\n" if $Globals{ 'debug' };
+    flock( $FileHandle, LOCK_EX ) || &fail( "Unable to lock record $recordnum\n" );
+
+    #Read in status file contents
+    print "D1: Saving status $recordnum\n" if $Globals{ 'debug' };
     seek( $FileHandle, 0, 0 );
     for( my $i = 0; $i < $#fields; $i++ )
     {
-       if ( defined( $fields[$i] ) )
-       { print $FileHandle $Record{ $fields[$i] } . "\n"; }
+       if ( defined( $record{ $fields[$i] } ) )
+       { print $FileHandle $record{ $fields[$i] } . "\n"; }
        else { print $FileHandle "\n"; }
     }
 }
 
-sub OpenFile
+sub GetFileName
 {
-    my $prePath = $_[0], my $stub = $_[1], my $postPath = $_[2], my $desc = $_[3];
-    my $path = "/db/".$stub.".status", my $handle = new FileHandle;
-    print "V: Opening $desc $stub\n" if $Globals{ 'verbose' };
-    print "D2: (DBase) $path found as data path\n" if $Globals{ 'debug' } > 1;
-    if( ! -r $Globals{ "work-dir" } . $path ) {
-       my $dir;
-       $path = $prePath. &NameToPathHash($stub) .$postPath;
-       $dir = basename($path);
-       if( ! -d $Globals{ "work-dir" } . $dir ) {
-           print "D1 (DBase) making dir $dir\n" if $Globals{ 'debug' };
-           mkdir $Globals{ "work-dir" } . $dir, umask();
+    my ($prePaths, $stub, $postPath, $desc, $new) = (shift, shift, shift, shift, shift);
+    my $path;
+    foreach my $prePath (@$prePaths) {
+       $path = "/" . $prePath . "/" . $stub . $postPath;
+       print "V: Opening $desc $stub\n" if $Globals{ 'verbose' };
+       print "D2: (DBase) trying $path\n" if $Globals{ 'debug' } > 1;
+       if( ! -r $Globals{ "work-dir" } . $path ) {
+           $path = "/" . $prePath . "/" . &NameToPathHash($stub) . $postPath;
+           print "D2: (DBase) trying $path\n" if $Globals{ 'debug' } > 1;
+           if( ! -r $Globals{ "work-dir" } . $path ) {
+               next if( !$new =~ "new" );
+           }
+       }
+       if( -r $Globals{ "work-dir" } . $path ) {
+           return $path;
+       }
+       if( ( ! -r $Globals{ "work-dir" } . $path ) && defined($new) && $new =~ "new") {
+           my $dir = dirname( $path );
+           if ( ! -d $Globals{ "work-dir" } . $dir ) {
+               mkpath($Globals{ "work-dir" } . $dir);
+           }
+           return $path;
        }
     }
-    open( $handle, $Globals{ "work-dir" } . $path ) 
-       || &fail( "Unable to open $desc: ".$Globals{ "work-dir" }."$path\n");
-    return $handle;
-}
-sub OpenRecord
-{
-    my $record = $_[0];
-    if ( $record ne $OpenedRecord )
-    {
-       $FileHandle = OpenFile("/db/", $record, ".status", "status");
-       flock( $FileHandle, LOCK_EX ) || &fail( "Unable to lock record $record\n" );
-       $OpenedRecord = $record;
-    }
+    return undef;
 }
 
-sub CloseRecord
+sub OpenFile
 {
-    print "V: Closing status $LoadedRecord\n" if $Globals{ 'verbose' };
-    close $FileHandle;
-    $OpenedRecord = 0;
+    my ($prePaths, $stub, $postPath, $desc, $new) = (shift, shift, shift, shift, shift);
+    my $fileName = GetFileName($prePaths, $stub, $postPath, $desc, $new);
+    my $handle = new FileHandle;
+    open( $handle, $Globals{ "work-dir" } . $fileName ) && return $handle;
+    return undef;
 }
 
 sub OpenLogfile
@@ -152,18 +206,84 @@ sub OpenLogfile
     my $record = $_[0];
     if ( $record ne $OpenedLog )
     {
-       $LogfileHandle = OpenFile("/db/", $record, ".log", "log");
-       seek( $FileHandle, 0, 2 );
+       $LogfileHandle = OpenFile(["db", "archive"], $record, ".log", "log");
        $OpenedLog = $record;
     }
 }
 
+sub ReadLogfile
+{
+    my $record = $_[0];
+    if ( $record eq $OpenedLog )
+    {
+       seek( $LogfileHandle, 0, 0 );
+       my $log = new Debbugs::DBase::Log;
+       $log->Load($LogfileHandle);
+    }
+}
+
 sub CloseLogfile
 {
     print "V: Closing log $OpenedLog\n" if $Globals{ 'verbose' };
     close $LogfileHandle;
     $OpenedLog = 0;
 }
+sub GetBugList
+{
+# TODO: This is ugly, but the easiest for me to implement.
+#      If you have a better way, then please send a patch.
+#
+    my $dir = new FileHandle;
+
+    my $prefix;
+    my $paths = shift;
+    my @paths;
+    if ( !defined($paths) ) {
+       @paths = ("db");
+    } else {
+       @paths = @$paths;
+    }
+    my @ret;
+    my $path;
+    foreach $path (@paths) {
+       $prefix = $Globals{ "work-dir" } . "/" . $path . "/";
+       opendir $dir, $prefix;
+       my @files = readdir($dir);
+       closedir $dir;
+       foreach (grep { /\d*\d\d.status/ } @files) {
+           next if ( ! -s $prefix . "/" . $_ );
+           s/.status$//;
+           push @ret, $_;
+#          print "$_ -> $_\n";
+       }
+       foreach (grep { /^[s0-9]$/ } @files) {
+           my $_1 = $_;
+           opendir $dir, $prefix . $_1;
+           my @files = grep { /^\d$/ } readdir($dir);
+           closedir $dir;
+           foreach (@files) {
+               my $_2 = $_;
+               opendir $dir, "$prefix$_1/$_2";
+               my @files = grep { /^\d$/ } readdir($dir);
+               close $dir;
+               foreach (@files) {
+                   my $_3 = $_;
+                   opendir $dir, "$prefix$_1/$_2/$_3";
+                   my @files = grep { /\d*\d\d.status/ } readdir($dir);
+                   close $dir;
+                   foreach (@files) {
+                       next if ( ! -s "$prefix$_1/$_2/$_3/$_" );
+                       s/.status$//;
+                       push @ret, $_;
+#                      print "$_ -> $_1/$_2/$_3/$_\n";
+                   }
+               }
+           }
+       }
+    }
+    return @ret;
+}
+
 1;
 
 END { }       # module clean-up code here (global destructor)