1 # TODO: Implement 'stale' checks, so that there is no need to explicitly
2 # write out a record, before closing.
4 package Debbugs::DBase; # assumes Some/Module.pm
10 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
12 # set the version for version checking
16 @EXPORT = qw( %Record );
17 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
19 # your exported package globals go here,
20 # as well as any optionally exported functions
21 @EXPORT_OK = qw( %Record );
29 use Debbugs::DBase::Log;
30 use Debbugs::DBase::Log::Html;
31 use Debbugs::DBase::Log::Message;
32 use Debbugs::DBase::Log::Mail;
35 use File::Basename qw(&dirname);
44 my $FileHandle = new FileHandle;
45 my $LogfileHandle = new FileHandle;
47 sub ParseVersion1Record
50 my @fields = ( "originator", "date", "subject", "msgid", "package",
51 "keywords", "done", "forwarded", "mergedwith", "severity" );
55 print "D2: (DBase) Record Fields:\n" if $Globals{ 'debug' } > 1;
56 foreach my $line ( @data )
60 $Record{ $tag } = $line;
61 print "\t $tag = $line\n" if $Globals{ 'debug' } > 1;
63 $GTags{ "BUG_$tag" } = $line;
67 sub ParseVersion2Record
69 # I envision the next round of records being totally different in
70 # meaning. In order to maintain compatability, version tagging will be
71 # implemented in thenext go around and different versions will be sent
72 # off to different functions to be parsed and interpreted into a format
73 # that the rest of the system will understand. All data will be saved
74 # in whatever 'new" format ixists. The difference will be a "Version: x"
75 # at the top of the file.
77 print "No version 2 records are understood at this time\n";
84 print "V: Reading status $record\n" if $Globals{ 'verbose' };
85 if ( $record ne $LoadedRecord )
89 seek( $FileHandle, 0, 0 );
90 @data = <$FileHandle>;
91 if ( scalar( @data ) =~ /Version: (\d*)/ )
94 { &ParseVersion2Record( @data ); }
96 { &fail( "Unknown record version: $1\n"); }
98 else { &ParseVersion1Record( @data ); }
99 $LoadedRecord = $record;
101 else { print "D1: (DBase) $record is already loaded\n" if $Globals{ 'debug' }; }
107 my @fields = ( "originator", "date", "subject", "msgid", "package",
108 "keywords", "done", "forwarded", "mergedwith", "severity" );
109 print "V: Writing status $LoadedRecord\n" if $Globals{ 'verbose' };
110 seek( $FileHandle, 0, 0 );
111 for( my $i = 0; $i < $#fields; $i++ )
113 if ( defined( $fields[$i] ) )
114 { print $FileHandle $Record{ $fields[$i] } . "\n"; }
115 else { print $FileHandle "\n"; }
121 my ($prePaths, $stub, $postPath, $desc, $new) = (shift, shift, shift, shift, shift);
123 foreach my $prePath (@$prePaths) {
124 $path = "/" . $prePath . "/" . $stub . $postPath;
125 print "V: Opening $desc $stub\n" if $Globals{ 'verbose' };
126 print "D2: (DBase) trying $path\n" if $Globals{ 'debug' } > 1;
127 if( ! -r $Globals{ "work-dir" } . $path ) {
128 $path = "/" . $prePath . "/" . &NameToPathHash($stub) . $postPath;
129 print "D2: (DBase) trying $path\n" if $Globals{ 'debug' } > 1;
130 if( ! -r $Globals{ "work-dir" } . $path ) {
131 next if( !$new =~ "new" );
134 if( -r $Globals{ "work-dir" } . $path ) {
137 if( ( ! -r $Globals{ "work-dir" } . $path ) && defined($new) && $new =~ "new") {
138 my $dir = dirname( $path );
139 if ( ! -d $Globals{ "work-dir" } . $dir ) {
140 mkpath($Globals{ "work-dir" } . $dir);
149 my ($prePaths, $stub, $postPath, $desc, $new) = (shift, shift, shift, shift, shift);
150 my $fileName = GetFileName($prePaths, $stub, $postPath, $desc, $new);
151 my $handle = new FileHandle;
152 open( $handle, $Globals{ "work-dir" } . $fileName ) && return $handle;
159 if ( $record ne $OpenedRecord )
161 $FileHandle = OpenFile ["db", "archive"], $record, ".status", "status", $_[1];
162 flock( $FileHandle, LOCK_EX ) || &fail( "Unable to lock record $record\n" );
163 $OpenedRecord = $record;
169 print "V: Closing status $LoadedRecord\n" if $Globals{ 'verbose' };
177 if ( $record ne $OpenedLog )
179 $LogfileHandle = OpenFile(["db", "archive"], $record, ".log", "log");
180 $OpenedLog = $record;
187 if ( $record eq $OpenedLog )
189 seek( $LogfileHandle, 0, 0 );
190 my $log = new Debbugs::DBase::Log;
191 $log->Load($LogfileHandle);
197 print "V: Closing log $OpenedLog\n" if $Globals{ 'verbose' };
198 close $LogfileHandle;
203 # TODO: This is ugly, but the easiest for me to implement.
204 # If you have a better way, then please send a patch.
206 my $dir = new FileHandle;
211 if ( !defined($paths) ) {
218 foreach $path (@paths) {
219 $prefix = $Globals{ "work-dir" } . "/" . $path . "/";
220 opendir $dir, $prefix;
221 my @files = readdir($dir);
223 foreach (grep { /\d*\d\d.status/ } @files) {
224 next if ( ! -s $prefix . "/" . $_ );
227 # print "$_ -> $_\n";
229 foreach (grep { /^[s0-9]$/ } @files) {
231 opendir $dir, $prefix . $_1;
232 my @files = grep { /^\d$/ } readdir($dir);
236 opendir $dir, "$prefix$_1/$_2";
237 my @files = grep { /^\d$/ } readdir($dir);
241 opendir $dir, "$prefix$_1/$_2/$_3";
242 my @files = grep { /\d*\d\d.status/ } readdir($dir);
245 next if ( ! -s "$prefix$_1/$_2/$_3/$_" );
248 # print "$_ -> $_1/$_2/$_3/$_\n";
259 END { } # module clean-up code here (global destructor)