upgrade rpm version to 4.14.1
[platform/upstream/rpm.git] / lib / backend / dbi.h
1 #ifndef _DBI_H
2 #define _DBI_H
3
4 #include "dbiset.h"
5
6 /* XXX: make this backend-specific, eliminate or something... */
7 #define _USE_COPY_LOAD
8
9 enum rpmdbFlags {
10     RPMDB_FLAG_JUSTCHECK        = (1 << 0),
11     RPMDB_FLAG_REBUILD          = (1 << 1),
12     RPMDB_FLAG_VERIFYONLY       = (1 << 2),
13 };
14
15 typedef enum dbCtrlOp_e {
16     DB_CTRL_LOCK_RO             = 1,
17     DB_CTRL_UNLOCK_RO           = 2,
18     DB_CTRL_LOCK_RW             = 3,
19     DB_CTRL_UNLOCK_RW           = 4,
20     DB_CTRL_INDEXSYNC           = 5
21 } dbCtrlOp;
22
23 typedef struct dbiIndex_s * dbiIndex;
24 typedef struct dbiCursor_s * dbiCursor;
25
26 struct dbConfig_s {
27     int db_mmapsize;    /*!< (10Mb) */
28     int db_cachesize;   /*!< (128Kb) */
29     int db_verbose;
30     int db_no_fsync;    /*!< no-op fsync for db */
31     int db_eflags;      /*!< obsolete */
32 };
33
34 struct dbiConfig_s {
35     int dbi_oflags;             /*!< open flags */
36     int dbi_no_dbsync;          /*!< don't call dbiSync */
37     int dbi_lockdbfd;           /*!< do fcntl lock on db fd */
38 };
39
40 struct rpmdbOps_s;
41
42 /** \ingroup rpmdb
43  * Describes the collection of index databases used by rpm.
44  */
45 struct rpmdb_s {
46     char        * db_root;/*!< path prefix */
47     char        * db_home;/*!< directory path */
48     char        * db_fullpath;  /*!< full db path including prefix */
49     int         db_flags;
50     int         db_mode;        /*!< open mode */
51     int         db_perms;       /*!< open permissions */
52     char        * db_descr;     /*!< db backend description (for error msgs) */
53     struct dbChk_s * db_checked;/*!< headerCheck()'ed package instances */
54     rpmdb       db_next;
55     int         db_opens;
56     dbiIndex    db_pkgs;        /*!< Package db */
57     const rpmDbiTag * db_tags;
58     int         db_ndbi;        /*!< No. of tag indices. */
59     dbiIndex    * db_indexes;   /*!< Tag indices. */
60     int         db_buildindex;  /*!< Index rebuild indicator */
61
62     struct rpmdbOps_s * db_ops; /*!< backend ops */
63
64     /* dbenv and related parameters */
65     void * db_dbenv;            /*!< Backend private handle */
66     struct dbConfig_s cfg;
67     int db_remove_env;
68
69     struct rpmop_s db_getops;
70     struct rpmop_s db_putops;
71     struct rpmop_s db_delops;
72
73     int nrefs;                  /*!< Reference count. */
74 };
75
76 /* Type of the dbi, also serves as the join key size */
77 typedef enum dbiIndexType_e {
78     DBI_PRIMARY         = (1 * sizeof(int32_t)),
79     DBI_SECONDARY       = (2 * sizeof(int32_t)),
80 } dbiIndexType;
81
82 enum dbiFlags_e {
83     DBI_NONE            = 0,
84     DBI_CREATED         = (1 << 0),
85     DBI_RDONLY          = (1 << 1),
86 };
87
88 enum dbcFlags_e {
89     DBC_READ    = 0,
90     DBC_WRITE   = (1 << 0),
91 };
92
93 enum dbcSearchType_e {
94     DBC_NORMAL_SEARCH   = 0,
95     DBC_PREFIX_SEARCH   = (1 << 0),
96 };
97
98 /** \ingroup dbi
99  * Describes an index database (implemented on Berkeley db functionality).
100  */
101 struct dbiIndex_s {
102     rpmdb dbi_rpmdb;            /*!< the parent rpm database */
103     dbiIndexType dbi_type;      /*! Type of dbi (primary / index) */
104     const char * dbi_file;      /*!< file component of path */
105     int dbi_flags;
106     int dbi_byteswapped;
107
108     struct dbiConfig_s cfg;
109
110     void * dbi_db;              /*!< Backend private handle */
111 };
112
113 union _dbswap {
114     unsigned int ui;
115     unsigned char uc[4];
116 };
117
118 #define _DBSWAP(_a) \
119 \
120   { unsigned char _b, *_c = (_a).uc; \
121     _b = _c[3]; _c[3] = _c[0]; _c[0] = _b; \
122     _b = _c[2]; _c[2] = _c[1]; _c[1] = _b; \
123 \
124   }
125
126 #ifdef __cplusplus
127 extern "C" {
128 #endif
129
130 /** \ingroup dbi
131  * Suspend the exclusive lock on the dbi
132  * @param dbi           index database handle
133  * @param flags         (unused)
134  * @return              0 on success
135  */
136 RPM_GNUC_INTERNAL
137 int dbiSuspendDBLock(dbiIndex dbi, unsigned int flags);
138
139 /** \ingroup dbi
140  * Reacquire an exclusive lock on the dbi
141  * @param dbi           index database handle
142  * @param flags         (unused)
143  * @return              0 on success
144  */
145 RPM_GNUC_INTERNAL
146 int dbiResumeDBLock(dbiIndex dbi, unsigned int flags);
147
148
149 RPM_GNUC_INTERNAL
150 /* Globally enable/disable fsync in the backend */
151 void dbSetFSync(rpmdb rdb, int enable);
152
153 RPM_GNUC_INTERNAL
154 int dbCtrl(rpmdb rdb, dbCtrlOp ctrl);
155
156 /** \ingroup dbi
157  * Return new configured index database handle instance.
158  * @param rdb           rpm database
159  * @param rpmtag        database index tag
160  * @return              index database handle
161  */
162 RPM_GNUC_INTERNAL
163 dbiIndex dbiNew(rpmdb rdb, rpmDbiTagVal rpmtag);
164
165 /** \ingroup dbi
166  * Destroy index database handle instance.
167  * @param dbi           index database handle
168  * @return              NULL always
169  */
170 RPM_GNUC_INTERNAL
171 dbiIndex dbiFree( dbiIndex dbi);
172
173 /** \ingroup dbi
174  * Actually open the database of the index.
175  * @param db            rpm database
176  * @param rpmtag        database index tag
177  * @param dbiIndex      address of index database handle
178  * @param flags
179  * @return              0 on success
180  */
181 RPM_GNUC_INTERNAL
182 int dbiOpen(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags);
183
184 /** \ingroup dbi
185  * Close index database.
186  * @param dbi           index database handle
187  * @param flags         (unused)
188  * @return              0 on success
189  */
190 RPM_GNUC_INTERNAL
191 int dbiClose(dbiIndex dbi, unsigned int flags);
192
193 /** \ingroup dbi
194  * Verify (and close) index database.
195  * @param dbi           index database handle
196  * @param flags         (unused)
197  * @return              0 on success
198  */
199 RPM_GNUC_INTERNAL
200 int dbiVerify(dbiIndex dbi, unsigned int flags);
201
202 /** \ingroup dbi
203  * Retrieve index control flags (new/existing, read-only etc)
204  * @param dbi           index database handle
205  * @return              dbi control flags
206  */
207 RPM_GNUC_INTERNAL
208 int dbiFlags(dbiIndex dbi);
209
210 /** \ingroup dbi
211  * Retrieve index name (same as the backing file name)
212  * @param dbi           index database handle
213  * @return              dbi name
214  */
215 RPM_GNUC_INTERNAL
216 const char * dbiName(dbiIndex dbi);
217
218 /** \ingroup dbi
219  * Open a database cursor.
220  * @param dbi           index database handle
221  * @param flags         DBC_WRITE if writing, or 0 (DBC_READ) for reading
222  * @return              database cursor handle
223  */
224 RPM_GNUC_INTERNAL
225 dbiCursor dbiCursorInit(dbiIndex dbi, unsigned int flags);
226
227 /** \ingroup dbi
228  * Destroy a database cursor handle
229  * @param dbc           database cursor handle
230  * @return              NULL always
231  */
232 RPM_GNUC_INTERNAL
233 dbiCursor dbiCursorFree(dbiIndex dbi, dbiCursor dbc);
234
235
236 RPM_GNUC_INTERNAL
237 rpmRC pkgdbPut(dbiIndex dbi, dbiCursor dbc,  unsigned int hdrNum,
238                unsigned char *hdrBlob, unsigned int hdrLen);
239 RPM_GNUC_INTERNAL
240 rpmRC pkgdbDel(dbiIndex dbi, dbiCursor dbc,  unsigned int hdrNum);
241 RPM_GNUC_INTERNAL
242 rpmRC pkgdbGet(dbiIndex dbi, dbiCursor dbc, unsigned int hdrNum,
243                unsigned char **hdrBlob, unsigned int *hdrLen);
244 RPM_GNUC_INTERNAL
245 rpmRC pkgdbNew(dbiIndex dbi, dbiCursor dbc,  unsigned int *hdrNum);
246 RPM_GNUC_INTERNAL
247 unsigned int pkgdbKey(dbiIndex dbi, dbiCursor dbc);
248
249 RPM_GNUC_INTERNAL
250 rpmRC idxdbGet(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen,
251                dbiIndexSet *set, int curFlags);
252 RPM_GNUC_INTERNAL
253 rpmRC idxdbPut(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen,
254                dbiIndexItem rec);
255 RPM_GNUC_INTERNAL
256 rpmRC idxdbDel(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen,
257                dbiIndexItem rec);
258 RPM_GNUC_INTERNAL
259 const void * idxdbKey(dbiIndex dbi, dbiCursor dbc, unsigned int *keylen);
260
261 struct rpmdbOps_s {
262     int (*open)(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags);
263     int (*close)(dbiIndex dbi, unsigned int flags);
264     int (*verify)(dbiIndex dbi, unsigned int flags);
265     void (*setFSync)(rpmdb rdb, int enable);
266     int (*ctrl)(rpmdb rdb, dbCtrlOp ctrl);
267
268     dbiCursor (*cursorInit)(dbiIndex dbi, unsigned int flags);
269     dbiCursor (*cursorFree)(dbiIndex dbi, dbiCursor dbc);
270
271     rpmRC (*pkgdbGet)(dbiIndex dbi, dbiCursor dbc, unsigned int hdrNum, unsigned char **hdrBlob, unsigned int *hdrLen);
272     rpmRC (*pkgdbPut)(dbiIndex dbi, dbiCursor dbc, unsigned int hdrNum, unsigned char *hdrBlob, unsigned int hdrLen);
273     rpmRC (*pkgdbDel)(dbiIndex dbi, dbiCursor dbc,  unsigned int hdrNum);
274     rpmRC (*pkgdbNew)(dbiIndex dbi, dbiCursor dbc,  unsigned int *hdrNum);
275     unsigned int (*pkgdbKey)(dbiIndex dbi, dbiCursor dbc);
276
277     rpmRC (*idxdbGet)(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen, dbiIndexSet *set, int curFlags);
278     rpmRC (*idxdbPut)(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen, dbiIndexItem rec);
279     rpmRC (*idxdbDel)(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen, dbiIndexItem rec);
280     const void * (*idxdbKey)(dbiIndex dbi, dbiCursor dbc, unsigned int *keylen);
281 };
282
283 RPM_GNUC_INTERNAL
284 extern struct rpmdbOps_s db3_dbops;
285
286 #ifdef ENABLE_NDB
287 RPM_GNUC_INTERNAL
288 extern struct rpmdbOps_s ndb_dbops;
289 #endif
290
291 #if defined(WITH_LMDB)
292 RPM_GNUC_INTERNAL
293 extern struct rpmdbOps_s lmdb_dbops;
294 #endif
295
296 #ifdef __cplusplus
297 }
298 #endif
299
300 #endif /* _DBI_H */