]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/DBase.pm
9ed81600a002e53e301d9c984a6b3528a03f162f
[debbugs.git] / Debbugs / DBase.pm
1 package Debbugs::DBase;  # assumes Some/Module.pm
2
3 use strict;
4
5 BEGIN {
6         use Exporter   ();
7         use vars       qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8
9         # set the version for version checking
10         $VERSION     = 1.00;
11
12         @ISA         = qw(Exporter);
13         @EXPORT      = qw( %Record );
14         %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
15
16         # your exported package globals go here,
17         # as well as any optionally exported functions
18         @EXPORT_OK   = qw( %Record );
19 }
20
21 use vars      @EXPORT_OK;
22 use Fcntl ':flock';
23 use Debbugs::Config qw(%Globals);
24 use Debbugs::Common;
25 use FileHandle;
26
27 %Record = ();
28
29 my $LoadedRecord = 0;
30 my $FileLocked = 0;
31 my $FileHandle = new FileHandle;
32
33 sub ParseVersion1Record
34 {
35     my @data = @_;
36     my @fields = ( "originator", "date", "subject", "msgid", "package",
37                 "keywords", "done", "forwarded", "mergedwith", "severity" );
38     my $i = 0;
39     foreach my $line ( @data )
40     {
41         chop( $line );
42         $Record{ $fields[$i] } = $line;
43         $i++;
44     }
45 }
46
47 sub ParseVersion2Record
48 {
49     # I envision the next round of records being totally different in
50     # meaning.  In order to maintain compatability, version tagging will be
51     # implemented in thenext go around and different versions will be sent
52     # off to different functions to be parsed and interpreted into a format
53     # that the rest of the system will understand.  All data will be saved
54     # in whatever 'new" format ixists.  The difference will be a "Version: x"
55     # at the top of the file.
56
57     print "No version 2 records are understood at this time\n";
58     exit 1;
59 }
60
61 sub ReadRecord
62 {
63     my $record = $_[0];
64     if ( $record ne $LoadedRecord )
65     {
66         my $path = '';
67         my @data;
68         
69         #find proper directory to store in
70         #later, this will be for tree'd data directory the way
71         #expire is now,..
72         $path = "/db/".$record.".status";
73     
74         open( $FileHandle, $Globals{ "work-dir" } . $path ) 
75             || &fail( "Unable to open record: ".$Globals{ "work-dir" }."$path\n");
76         flock( $FileHandle, LOCK_EX ) || &fail( "Unable to lock record $record\n" );
77         @data = <$FileHandle>;
78         if ( scalar( @data ) =~ /Version: (\d*)/ )
79         {
80             if ( $1 == 2 )
81             { &ParseVersion2Record( @data ); }
82             else
83             { &fail( "Unknown record version: $1\n"); }
84         }
85         else { &ParseVersion1Record( @data ); }
86         $LoadedRecord = $record;
87     }
88
89 }
90
91 sub WriteRecord
92 {
93     my @fields = ( "originator", "date", "subject", "msgid", "package",
94                 "keywords", "done", "forwarded", "mergedwith", "severity" );
95     seek( $FileHandle, 0, 0 );
96     for( my $i = 0; $i < $#fields; $i++ )
97     {
98         if ( defined( $fields[$i] ) )
99         { print $FileHandle $Record{ $fields[$i] } . "\n"; }
100         else { print $FileHandle "\n"; }
101     }
102     close $FileHandle;
103     $LoadedRecord = 0;
104 }
105
106 1;
107
108 END { }       # module clean-up code here (global destructor)