chiark / gitweb /
add note about unexpected password requests
[bcp5-registry.git] / database.pl
1 # G-RIN database handling.
2 # Copyright (C) 1999 Ian Jackson <ijackson@chiark.greenend.org.uk
3 #
4 # This is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2,
7 # or (at your option) any later version.
8 #
9 # This is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public
15 # License along with this file; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 @db_fields= qw(network prefix name contact email hiddenemail created changed);
19
20 sub read_database () {
21 print DEBUG "reading\n";
22     my (@v,$id);
23     open DBF, "list" or die $!;
24     for (;;) {
25         $_= <DBF>;
26         length or die "$_ $!";
27         s/\n$//;
28         last if m/^\002/;
29         @v= split(/\001/,$_);
30         $id= shift @v;
31         length $id or die;
32         undef $v;
33         foreach $f (@db_fields) { $v->{$f}= shift(@v).''; }
34         $db{$id}= $v;
35     }
36     close DBF or die $!;
37     $db_read= 1;
38 }
39
40 $db_lock_env= 'BCP5REGISTRY_LOCKED';
41
42 sub write_database () {
43     my ($k,$v);
44 print DEBUG "writing\n";
45     die unless $ENV{$db_lock_env};
46     open DBF, ">list.new" or die $!;
47     while (($k,$v) = each %db) {
48         $str= "$k";
49         foreach $f (@db_fields) { $str.= "\1".$v->{$f}; }
50         print DBF $str,"\n" or die $!;
51     }
52     print DBF "\2\n" or die $!;
53     close DBF or die $!;
54     rename "list.new","list" or die $!;
55 }
56
57 sub lock_database () {
58 print DEBUG "locking\n";
59     die if $db_read;
60     return if $ENV{$db_lock_env};
61     $ENV{$db_lock_env}= '1';
62     exec 'with-lock-ex','-w','lockfile',"$0",@org_argv;
63     die "$ENV{'PATH'}: $!";
64 }
65
66 1;