]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/db++.cpp
33b9f772f275d1dc2667880a1f5f7040a09f6ede
[apple/security.git] / cdsa / cdsa_utilities / db++.cpp
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // dbm++ - generic C++ layer interface to [n]dbm
21 //
22 #include "db++.h"
23 #include <Security/debugging.h>
24
25
26 namespace Security {
27 namespace UnixPlusPlus {
28
29 UnixDb::UnixDb() : mDb(NULL)
30 {
31 }
32
33 UnixDb::UnixDb(const char *path, int flags, int mode, DBTYPE type) : mDb(NULL)
34 {
35 open(path, flags, mode);
36 }
37
38 UnixDb::UnixDb(const std::string &path, int flags, int mode, DBTYPE type) : mDb(NULL)
39 {
40 open(path, flags, mode);
41 }
42
43 UnixDb::~UnixDb()
44 {
45 close();
46 }
47
48 void UnixDb::open(const char *path, int flags, int mode, DBTYPE type)
49 {
50 if (DB* newDb = ::dbopen(path, flags, mode, type, NULL)) {
51 close();
52 mDb = newDb;
53 setFd(mDb->fd(mDb));
54 secdebug("unixdb", "open(%s,0x%x,0x%x,type=%d)=%p", path, flags, mode, type, mDb);
55 } else
56 UnixError::throwMe();
57 }
58
59 void UnixDb::open(const std::string &path, int flags, int mode, DBTYPE type)
60 {
61 open(path.c_str(), flags, mode);
62 }
63
64 void UnixDb::close()
65 {
66 if (mDb) {
67 secdebug("unixdb", "close(%p)", mDb);
68 mDb->close(mDb);
69 mDb = NULL;
70 setFd(invalidFd);
71 }
72 }
73
74 bool UnixDb::get(const CssmData &key, CssmData &value, int flags) const
75 {
76 Data dKey(key);
77 Data val;
78 int rc = mDb->get(mDb, &dKey, &val, flags);
79 secdebug("unixdb", "get(%p,[:%ld],flags=0x%x)=%d[:%ld]",
80 mDb, key.length(), flags, rc, value.length());
81 checkError(rc);
82 if (!rc) {
83 value = val;
84 return true;
85 } else
86 return false;
87 }
88
89 bool UnixDb::get(const CssmData &key, CssmOwnedData &value, int flags) const
90 {
91 CssmData val;
92 if (get(key, val, flags)) {
93 value = val;
94 return true;
95 } else
96 return false;
97 }
98
99 bool UnixDb::put(const CssmData &key, const CssmData &value, int flags)
100 {
101 Data dKey(key);
102 Data dValue(value);
103 int rc = mDb->put(mDb, &dKey, &dValue, flags);
104 secdebug("unixdb", "put(%p,[:%ld],[:%ld],flags=0x%x)=%d",
105 mDb, key.length(), value.length(), flags, rc);
106 checkError(rc);
107 return !rc;
108 }
109
110 void UnixDb::erase(const CssmData &key, int flags)
111 {
112 Data dKey(key);
113 secdebug("unixdb", "delete(%p,[:%ld],flags=0x%x)", mDb, key.length(), flags);
114 checkError(mDb->del(mDb, &dKey, flags));
115 }
116
117 bool UnixDb::next(CssmData &key, CssmData &value, int flags /* = R_NEXT */) const
118 {
119 Data dKey, dValue;
120 int rc = mDb->seq(mDb, &dKey, &dValue, flags);
121 checkError(rc);
122 if (!rc) {
123 key = dKey;
124 value = dValue;
125 return true;
126 } else
127 return false;
128 }
129
130
131 void UnixDb::flush(int flags)
132 {
133 checkError(mDb->sync(mDb, flags));
134 }
135
136
137 } // end namespace UnixPlusPlus
138 } // end namespace Security