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