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 );
30 use File::Basename qw(&dirname);
39 my $FileHandle = new FileHandle;
40 my $LogfileHandle = new FileHandle;
42 sub ParseVersion1Record
45 my @fields = ( "originator", "date", "subject", "msgid", "package",
46 "keywords", "done", "forwarded", "mergedwith", "severity" );
50 print "D2: (DBase) Record Fields:\n" if $Globals{ 'debug' } > 1;
51 foreach my $line ( @data )
55 $Record{ $tag } = $line;
56 print "\t $tag = $line\n" if $Globals{ 'debug' } > 1;
58 $GTags{ "BUG_$tag" } = $line;
62 sub ParseVersion2Record
64 # I envision the next round of records being totally different in
65 # meaning. In order to maintain compatability, version tagging will be
66 # implemented in thenext go around and different versions will be sent
67 # off to different functions to be parsed and interpreted into a format
68 # that the rest of the system will understand. All data will be saved
69 # in whatever 'new" format ixists. The difference will be a "Version: x"
70 # at the top of the file.
72 print "No version 2 records are understood at this time\n";
79 print "V: Reading status $record\n" if $Globals{ 'verbose' };
80 if ( $record ne $LoadedRecord )
84 seek( $FileHandle, 0, 0 );
85 @data = <$FileHandle>;
86 if ( scalar( @data ) =~ /Version: (\d*)/ )
89 { &ParseVersion2Record( @data ); }
91 { &fail( "Unknown record version: $1\n"); }
93 else { &ParseVersion1Record( @data ); }
94 $LoadedRecord = $record;
96 else { print "D1: (DBase) $record is already loaded\n" if $Globals{ 'debug' }; }
102 my @fields = ( "originator", "date", "subject", "msgid", "package",
103 "keywords", "done", "forwarded", "mergedwith", "severity" );
104 print "V: Writing status $LoadedRecord\n" if $Globals{ 'verbose' };
105 seek( $FileHandle, 0, 0 );
106 for( my $i = 0; $i < $#fields; $i++ )
108 if ( defined( $fields[$i] ) )
109 { print $FileHandle $Record{ $fields[$i] } . "\n"; }
110 else { print $FileHandle "\n"; }
116 my ($prePaths, $stub, $postPath, $desc, $new) = (shift, shift, shift, shift, shift);
117 foreach my $prePath (@$prePaths) {
118 my $path = "/" . $prePath . "/" . $stub . $postPath, my $handle = new FileHandle;
119 print "V: Opening $desc $stub\n" if $Globals{ 'verbose' };
120 print "D2: (DBase) trying $path\n" if $Globals{ 'debug' } > 1;
121 if( ! -r $Globals{ "work-dir" } . $path ) {
122 $path = "/" . $prePath . "/" . &NameToPathHash($stub) . $postPath;
123 print "D2: (DBase) trying $path\n" if $Globals{ 'debug' } > 1;
124 if( ! -r $Globals{ "work-dir" } . $path ) {
125 next if( !$new =~ "new" );
128 open( $handle, $Globals{ "work-dir" } . $path ) && return $handle;
130 my $dir = dirname( $path );
131 if ( ! -d $Globals{ "work-dir" } . $dir ) {
132 mkpath($Globals{ "work-dir" } . $dir);
134 open( $handle, ">" . $Globals{ "work-dir" } . $path ) && return $handle;
142 if ( $record ne $OpenedRecord )
144 $FileHandle = OpenFile ["db", "archive"], $record, ".status", "status", $_[1];
145 flock( $FileHandle, LOCK_EX ) || &fail( "Unable to lock record $record\n" );
146 $OpenedRecord = $record;
152 print "V: Closing status $LoadedRecord\n" if $Globals{ 'verbose' };
160 if ( $record ne $OpenedLog )
162 $LogfileHandle = OpenFile("db", $record, ".log", "log");
163 seek( $FileHandle, 0, 2 );
164 $OpenedLog = $record;
170 print "V: Closing log $OpenedLog\n" if $Globals{ 'verbose' };
171 close $LogfileHandle;
176 # TODO: This is ugly, but the easiest for me to implement.
177 # If you have a better way, then please send a patch.
179 my $dir = new FileHandle;
184 if ( !defined($paths) ) {
191 foreach $path (@paths) {
192 $prefix = $Globals{ "work-dir" } . "/" . $path . "/";
193 opendir $dir, $prefix;
194 my @files = readdir($dir);
196 foreach (grep { /\d*\d\d.status/ } @files) {
197 next if ( ! -s $prefix . "/" . $_ );
200 # print "$_ -> $_\n";
202 foreach (grep { /^[s0-9]$/ } @files) {
204 opendir $dir, $prefix . $_1;
205 my @files = grep { /^\d$/ } readdir($dir);
209 opendir $dir, "$prefix$_1/$_2";
210 my @files = grep { /^\d$/ } readdir($dir);
214 opendir $dir, "$prefix$_1/$_2/$_3";
215 my @files = grep { /\d*\d\d.status/ } readdir($dir);
218 next if ( ! -s "$prefix$_1/$_2/$_3/$_" );
221 # print "$_ -> $_1/$_2/$_3/$_\n";
232 END { } # module clean-up code here (global destructor)