1 // Copyright (C) 2000 Bruce Guenter <bruceg@em.ca>
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 #include <config.h>
18 #include "vpwtable.h"
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include "cdb++/cdb++.h"
22
23 class cdb_vpwtable_writer : public vpwtable_writer
24 {
25 private:
26 const mystring tmpname;
27 const mystring cdbname;
28 cdb_writer out;
29 bool opened;
30 public:
31 cdb_vpwtable_writer(const mystring& filename);
32 ~cdb_vpwtable_writer();
33 bool operator!() const;
34 bool put(const vpwentry& vpw);
35 bool end();
36 bool abort();
37 };
38
start_write() const39 vpwtable_writer* vpwtable::start_write() const
40 {
41 return new cdb_vpwtable_writer(filename);
42 }
43
cdb_vpwtable_writer(const mystring & filename)44 cdb_vpwtable_writer::cdb_vpwtable_writer(const mystring& filename)
45 : tmpname(filename + ".tmp"), cdbname(filename),
46 out(filename, 0600), opened(true)
47 {
48 }
49
~cdb_vpwtable_writer()50 cdb_vpwtable_writer::~cdb_vpwtable_writer()
51 {
52 end();
53 }
54
operator !() const55 bool cdb_vpwtable_writer::operator!() const
56 {
57 return opened;
58 }
59
put(const vpwentry & vpw)60 bool cdb_vpwtable_writer::put(const vpwentry& vpw)
61 {
62 return out.put(vpw.name.lower(), vpw.to_record());
63 }
64
end()65 bool cdb_vpwtable_writer::end()
66 {
67 if(!opened)
68 return false;
69 opened = false;
70 return out.end(cdbname);
71 }
72
abort()73 bool cdb_vpwtable_writer::abort()
74 {
75 if(!opened)
76 return false;
77 opened = false;
78 return unlink(tmpname.c_str()) == 0;
79 }
80