1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
/*
* Implementation using QDBM
*/
#include <cf3.defs.h>
#include <dbm_api.h>
#include <dbm_priv.h>
#include <string_lib.h>
#ifdef QDB
# include <qdbm/depot.h>
struct DBPriv_
{
/*
* This mutex controls the access to depot, which is not thread-aware
*/
pthread_mutex_t lock;
/*
* This mutex prevents two cursors to be active on depot at same time, as
* cursors are internal for QDBM. 'cursor_lock' is always taken before
* 'lock' to avoid deadlocks.
*/
pthread_mutex_t cursor_lock;
DEPOT *depot;
};
struct DBCursorPriv_
{
DBPriv *db;
char *curkey;
int curkey_size;
char *curval;
};
/******************************************************************************/
static bool Lock(DBPriv *db)
{
int ret = pthread_mutex_lock(&db->lock);
if (ret != 0)
{
errno = ret;
Log(LOG_LEVEL_ERR, "Unable to lock QDBM database. (pthread_mutex_lock: %s)", GetErrorStr());
return false;
}
return true;
}
static void Unlock(DBPriv *db)
{
int ret = pthread_mutex_unlock(&db->lock);
if (ret != 0)
{
errno = ret;
Log(LOG_LEVEL_ERR, "Unable to unlock QDBM database. (pthread_mutex_unlock: %s)", GetErrorStr());
}
}
static bool LockCursor(DBPriv *db)
{
int ret = pthread_mutex_lock(&db->cursor_lock);
if (ret != 0)
{
errno = ret;
Log(LOG_LEVEL_ERR, "Unable to obtain cursor lock for QDBM database. (pthread_mutex_lock: %s)", GetErrorStr());
return false;
}
return true;
}
static void UnlockCursor(DBPriv *db)
{
int ret = pthread_mutex_unlock(&db->cursor_lock);
if (ret != 0)
{
errno = ret;
Log(LOG_LEVEL_ERR, "Unable to release cursor lock for QDBM database. (pthread_mutex_unlock: %s)", GetErrorStr());
}
}
const char *DBPrivGetFileExtension(void)
{
return "qdbm";
}
void DBPrivSetMaximumConcurrentTransactions(ARG_UNUSED int max_txn)
{
}
DBPriv *DBPrivOpenDB(const char *filename, ARG_UNUSED dbid id)
{
DBPriv *db = xcalloc(1, sizeof(DBPriv));
pthread_mutex_init(&db->lock, NULL);
pthread_mutex_init(&db->cursor_lock, NULL);
db->depot = dpopen(filename, DP_OWRITER | DP_OCREAT, -1);
if ((db->depot == NULL) && (dpecode == DP_EBROKEN))
{
Log(LOG_LEVEL_ERR, "Database '%s' is broken, trying to repair...", filename);
if (dprepair(filename))
{
Log(LOG_LEVEL_INFO, "Successfully repaired database '%s'", filename);
}
else
{
Log(LOG_LEVEL_ERR, "Failed to repair database '%s', recreating...", filename);
return DB_PRIV_DATABASE_BROKEN;
}
db->depot = dpopen(filename, DP_OWRITER | DP_OCREAT, -1);
}
if (db->depot == NULL)
{
Log(LOG_LEVEL_ERR, "dpopen: Opening database '%s' failed. (dpopen: %s)",
filename, dperrmsg(dpecode));
pthread_mutex_destroy(&db->cursor_lock);
pthread_mutex_destroy(&db->lock);
free(db);
return NULL;
}
return db;
}
void DBPrivCloseDB(DBPriv *db)
{
int ret;
if ((ret = pthread_mutex_destroy(&db->lock)) != 0)
{
errno = ret;
Log(LOG_LEVEL_ERR, "Lock is still active during QDBM database handle close. (pthread_mutex_destroy: %s)", GetErrorStr());
}
if ((ret = pthread_mutex_destroy(&db->cursor_lock)) != 0)
{
errno = ret;
Log(LOG_LEVEL_ERR, "Cursor lock is still active during QDBM database handle close. (pthread_mutex_destroy: %s)", GetErrorStr());
}
if (!dpclose(db->depot))
{
Log(LOG_LEVEL_ERR, "Unable to close QDBM database. (dpclose: %s)", dperrmsg(dpecode));
}
free(db);
}
void DBPrivCommit(ARG_UNUSED DBPriv *db)
{
}
bool DBPrivClean(DBPriv *db)
{
if (!Lock(db))
{
return false;
}
if (!dpiterinit(db->depot))
{
Log(LOG_LEVEL_ERR, "Could not initialize QuickDB iterator. (dpiterinit: %s)", dperrmsg(dpecode));
Unlock(db);
return false;
}
char *key = NULL;
while((key = dpiternext(db->depot, NULL)))
{
dpout(db->depot, key, -1);
}
Unlock(db);
return true;
}
int DBPrivGetDBUsagePercentage(ARG_UNUSED const char *db_path)
{
Log(LOG_LEVEL_WARNING, "Cannot determine usage of a QuickDB database");
return -1;
}
bool DBPrivRead(DBPriv *db, const void *key, int key_size, void *dest, size_t dest_size)
{
if (!Lock(db))
{
return false;
}
if (dpgetwb(db->depot, key, key_size, 0, dest_size, dest) == -1)
{
// FIXME: distinguish between "entry not found" and "failure to read"
Log(LOG_LEVEL_DEBUG, "QDBM DBPrivRead: Could not read '%s', (dpgetwb: %s)",
(const char *)key, dperrmsg(dpecode));
Unlock(db);
return false;
}
Unlock(db);
return true;
}
bool DBPrivWrite(DBPriv *db, const void *key, int key_size, const void *value, int value_size)
{
if (!Lock(db))
{
return false;
}
if (!dpput(db->depot, key, key_size, value, value_size, DP_DOVER))
{
char *db_name = dpname(db->depot);
Log(LOG_LEVEL_ERR, "Could not write key to DB '%s'. (dpput: %s)",
db_name, dperrmsg(dpecode));
free(db_name);
Unlock(db);
return false;
}
Unlock(db);
return true;
}
bool DBPrivOverwrite(DBPriv *db, const char *key, int key_size, const void *value, size_t value_size,
OverwriteCondition Condition, void *data)
{
if (!Lock(db))
{
return false;
}
ssize_t cur_val_size = dpvsiz(db->depot, key, key_size);
bool exists = (cur_val_size != -1);
void *cur_val = NULL;
if (exists)
{
assert(cur_val_size > 0);
cur_val = xmalloc((size_t) cur_val_size);
if (dpgetwb(db->depot, key, key_size, 0, value_size, cur_val) == -1)
{
Log(LOG_LEVEL_DEBUG, "QDBM DBPrivRead: Could not read '%s', (dpgetwb: %s)",
(const char *)key, dperrmsg(dpecode));
Unlock(db);
return false;
}
}
if ((Condition != NULL) && !Condition(cur_val, cur_val_size, data))
{
free(cur_val);
Unlock(db);
return false;
}
free(cur_val);
if (!dpput(db->depot, key, key_size, value, value_size, DP_DOVER))
{
char *db_name = dpname(db->depot);
Log(LOG_LEVEL_ERR, "Could not write key to DB '%s'. (dpput: %s)",
db_name, dperrmsg(dpecode));
free(db_name);
Unlock(db);
return false;
}
Unlock(db);
return true;
}
bool DBPrivHasKey(DBPriv *db, const void *key, int key_size)
{
if (!Lock(db))
{
return false;
}
int ret = dpvsiz(db->depot, key, key_size) != -1;
Unlock(db);
return ret;
}
int DBPrivGetValueSize(DBPriv *db, const void *key, int key_size)
{
if (!Lock(db))
{
return false;
}
int ret = dpvsiz(db->depot, key, key_size);
Unlock(db);
return ret;
}
bool DBPrivDelete(DBPriv *db, const void *key, int key_size)
{
if (!Lock(db))
{
return false;
}
/* dpout returns false both for error and if key is not found */
if (!dpout(db->depot, key, key_size) && dpecode != DP_ENOITEM)
{
Unlock(db);
return false;
}
Unlock(db);
return true;
}
DBCursorPriv *DBPrivOpenCursor(DBPriv *db)
{
if (!LockCursor(db))
{
return NULL;
}
if (!Lock(db))
{
UnlockCursor(db);
return NULL;
}
if (!dpiterinit(db->depot))
{
Log(LOG_LEVEL_ERR, "Could not initialize QuickDB iterator. (dpiterinit: %s)", dperrmsg(dpecode));
Unlock(db);
UnlockCursor(db);
return NULL;
}
DBCursorPriv *cursor = xcalloc(1, sizeof(DBCursorPriv));
cursor->db = db;
Unlock(db);
/* Cursor remains locked */
return cursor;
}
bool DBPrivAdvanceCursor(DBCursorPriv *cursor, void **key, int *ksize, void **value, int *vsize)
{
if (!Lock(cursor->db))
{
return false;
}
free(cursor->curkey);
free(cursor->curval);
cursor->curkey = NULL;
cursor->curval = NULL;
*key = dpiternext(cursor->db->depot, ksize);
if (*key == NULL)
{
/* Reached the end of database */
Unlock(cursor->db);
return false;
}
*value = dpget(cursor->db->depot, *key, *ksize, 0, -1, vsize);
// keep pointers for later free
cursor->curkey = *key;
cursor->curkey_size = *ksize;
cursor->curval = *value;
Unlock(cursor->db);
return true;
}
bool DBPrivDeleteCursorEntry(DBCursorPriv *cursor)
{
return DBPrivDelete(cursor->db, cursor->curkey, cursor->curkey_size);
}
bool DBPrivWriteCursorEntry(DBCursorPriv *cursor, const void *value, int value_size)
{
return DBPrivWrite(cursor->db, cursor->curkey, cursor->curkey_size, value, value_size);
}
void DBPrivCloseCursor(DBCursorPriv *cursor)
{
DBPriv *db = cursor->db;
/* FIXME: communicate the deadlock if happens */
Lock(db);
free(cursor->curkey);
free(cursor->curval);
free(cursor);
Unlock(db);
/* Cursor lock was obtained in DBPrivOpenCursor */
UnlockCursor(db);
}
char *DBPrivDiagnose(const char *dbpath)
{
return StringFormat("Unable to diagnose QuickDB file (not implemented) for '%s'", dbpath);
}
#endif
|