1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #ifndef CFENGINE_DBM_API_H
26 #define CFENGINE_DBM_API_H
27 
28 #define EC_CORRUPTION_REPAIRED 120
29 #define EC_CORRUPTION_REPAIR_FAILED 121
30 
31 #include <map.h>
32 #include <dbm_api_types.h>
33 
34 // Only append to the end, keep in sync with DB_PATHS_STATEDIR array
35 typedef enum
36 {
37     dbid_classes,   // Deprecated
38     dbid_variables, // Deprecated
39     dbid_performance,
40     dbid_checksums, // Deprecated
41     dbid_filestats, // Deprecated
42     dbid_changes,
43     dbid_observations,
44     dbid_state,
45     dbid_lastseen,
46     dbid_audit,
47     dbid_locks,
48     dbid_history,
49     dbid_measure,
50     dbid_static,
51     dbid_scalars,
52     dbid_windows_registry,
53     dbid_cache,
54     dbid_license,
55     dbid_value,
56     dbid_agent_execution,
57     dbid_bundles,   // Deprecated
58     dbid_packages_installed, //new package promise installed packages list
59     dbid_packages_updates,   //new package promise list of available updates
60     dbid_cookies, // Enterprise reporting cookies for duplicate host detection
61 
62     dbid_max
63 } dbid;
64 
65 typedef struct DBHandle_ DBHandle;
66 typedef struct DBCursor_ DBCursor;
67 
68 typedef DBHandle CF_DB;
69 typedef DBCursor CF_DBC;
70 
71 void DBSetMaximumConcurrentTransactions(int max_txn);
72 
73 bool OpenDB(CF_DB **dbp, dbid db);
74 bool OpenSubDB(DBHandle **dbp, dbid id, const char *sub_name);
75 bool CleanDB(DBHandle *handle);
76 void CloseDB(CF_DB *dbp);
77 
78 DBHandle *GetDBHandleFromFilename(const char *db_file_name);
79 time_t GetDBOpenTimestamp(const DBHandle *handle);
80 
81 bool HasKeyDB(CF_DB *dbp, const char *key, int key_size);
82 int ValueSizeDB(CF_DB *dbp, const char *key, int key_size);
83 bool ReadComplexKeyDB(CF_DB *dbp, const char *key, int key_size, void *dest, int destSz);
84 bool WriteComplexKeyDB(CF_DB *dbp, const char *key, int keySz, const void *src, int srcSz);
85 bool DeleteComplexKeyDB(CF_DB *dbp, const char *key, int size);
86 bool ReadDB(CF_DB *dbp, const char *key, void *dest, int destSz);
87 bool WriteDB(CF_DB *dbp, const char *key, const void *src, int srcSz);
88 bool OverwriteDB(DBHandle *handle, const char *key, const void *value, size_t value_size,
89                  OverwriteCondition Condition, void *data);
90 bool DeleteDB(CF_DB *dbp, const char *key);
91 void FreezeDB(DBHandle *handle);
92 
93 /*
94  * Creating cursor locks the whole database, so keep the amount of work here to
95  * minimum.
96  *
97  * Don't use WriteDB/DeleteDB while iterating database, it will result in
98  * deadlock. Use cursor-specific operations instead. They work on the current
99  * key.
100  */
101 bool NewDBCursor(CF_DB *dbp, CF_DBC **dbcp);
102 bool NextDB(CF_DBC *dbcp, char **key, int *ksize, void **value, int *vsize);
103 bool DBCursorDeleteEntry(CF_DBC *cursor);
104 bool DBCursorWriteEntry(CF_DBC *cursor, const void *value, int value_size);
105 bool DeleteDBCursor(CF_DBC *dbcp);
106 
107 char *DBIdToPath(dbid id);
108 char *DBIdToSubPath(dbid id, const char *subdb_name);
109 
110 StringMap *LoadDatabaseToStringMap(dbid database_id);
111 
112 bool CheckDBRepairFlagFile();
113 #endif  /* NOT CFENGINE_DBM_API_H */
114