summaryrefslogtreecommitdiff
path: root/support/dktools/dk4dbi.h
blob: 2c1dd43d6a96ec3193a0605b380a80ddd19e0d5b (plain)
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4dbi.ctr
*/

/*
Copyright (C) 2015-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef DK4DBI_H_INCLUDED
/** Avoid multiple inclusions. */
#define DK4DBI_H_INCLUDED 1


#line 10 "dk4dbi.ctr"

/**	@file	dk4dbi.h	Interface to databases.
*/

#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif

#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif

#if DK4_HAVE_UNISTD_H
#ifndef UNISTD_H_INCLUDED
#include <unistd.h>
#define	UNISTD_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_SYS_TYPES_H
#ifndef SYS_TYPES_H_INCLUDED
#include <sys/types.h>
#define	SYS_TYPES_H_INCLUDED 1
#endif
#endif

#if	DK4_HAVE_DB_H		&& (DK4_CHAR_SIZE == 1)
#include <db.h>
#endif

#if	DK4_HAVE_NDBM_H		&& (DK4_CHAR_SIZE == 1)
#if DK4_HAVE_FCNTL_H
#ifndef FCNTL_H_INCLUDED
#include <fcntl.h>
#define	FCNTL_H_INCLUDED 1
#endif
#endif
#ifndef	NDBM_H_INCLUDED
#include <ndbm.h>
#define	NDBM_H_INCLUDED	1
#endif
#endif

#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif

#ifndef DK4ERROR_H_INCLUDED
#include "dk4error.h"
#endif

#ifndef	DK4STO_H_INCLUDED
#include "dk4sto.h"
#endif



/**	Backend types.
*/
enum {
			/**	No backend specified yet.
			*/
  DK4_DB_BE_UNKNOWN	= -1 ,

  			/**	In-memory database.
			*/
  DK4_DB_BE_MEMORY	=  0 ,

  			/**	Berkeley DB.
			*/
  DK4_DB_BE_BDB		=  1 ,

  			/**	NDBM.
			*/
  DK4_DB_BE_NDBM	=  2
};



/**	Options.
*/
enum {
				/**	Reset key data before releasing
					memory.
				*/
  DK4_DB_OPT_RESET_KEY		= 1 ,

				/**	Reset value data before releasing
					memory.
				*/
  DK4_DB_OPT_RESET_VALUE	= 2 ,
};



/**	Details for an in-memory database.
*/
typedef struct {
  dk4_sto_t	*s;	/**< Storage container. */
  dk4_sto_it_t	*i;	/**< Storage iterator. */
} dk4_dbi_mem_t;


#if DK4_HAVE_DB_H		&& (DK4_CHAR_SIZE == 1)
/**	Details for a Berkeley DB database.
*/
typedef struct {
  DB		*db;	/**< DB structure. */
} dk4_dbi_bdb_t;
#endif

#if DK4_HAVE_NDBM_H		&& (DK4_CHAR_SIZE == 1)
/**	Details for an NDBM database.
*/
typedef struct {
  DBM		*dbm;	/**< NDBM structure. */
} dk4_dbi_ndbm_t;
#endif

/**	Details for one backend.
*/
typedef union {
  dk4_dbi_mem_t		mem;	/**< Details for in-memory database. */
#if DK4_HAVE_DB_H		&& (DK4_CHAR_SIZE == 1)
  dk4_dbi_bdb_t		bdb;	/**< Details for Berkeley DB. */
#endif
#if DK4_HAVE_NDBM_H		&& (DK4_CHAR_SIZE == 1)
  dk4_dbi_ndbm_t	ndbm;	/**< Details for NDBM. */
#endif
  int			dummy;	/**< Just to have a second component. */
} dk4_dbi_det_t;

/**	Database structure.
*/
typedef struct {
  dk4_dbi_det_t	 dt;	/**< Backend-specific details. */
  dkChar	*fn;	/**< File name. */
  size_t	 km;	/**< Maximum key size. */
  size_t	 vm;	/**< Maximum value size. */
  int		 tp;	/**< Backend type. */
  int		 op;	/**< Options. */
  int		 wa;	/**< Flag: Write access. */
  int		 uc;	/**< Flag: Have unsynchronized changes. */
} dk4_dbi_t;

/**	Function to traverse a simple database.
	The function must check the kb, ks, vb and vs arguments
	for validity, they are passed as found by the backend
	functions (this means pointers may be NULL, sizes may be 0).
	@param	Object modified during traversal, may be NULL.
	@param	kb	Key data.
	@param	ks	Key size.
	@param	vb	Value data.
	@param	vs	Value size.
	@return	1 on success, 0 on error (can continue), -1 on error (abort).
*/
typedef
int
dk4dbi_traverse_fct_t(
  void		*obj,
  const void	*kb,
  size_t	 ks,
  const void	*vb,
  size_t	 vs
);


#ifdef __cplusplus
extern "C" {
#endif

/**	Open database.
	@param	fn	File name.
	@param	tp	Backend type.
	@param	wa	Flag: Write access required.
	@param	tr	Flag: Truncate existing database
			(only in conjunction with wa).
	@param	km	Maximum key size (0 for unlimited).
	@param	vm	Maximum value size (0 for unlimited).
			The limits are used on all backends for writing,
			on in-memory databases also for reading.
	@param	erp	Error report, may be NULL.
	@return	Pointer to new database structure on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL or tp contains an invalid type number,
	- DK4_E_NOT_SUPPORTED<br>
	  if the database type is not supported,
	- DK4_E_MATH_OVERFLOW<br>
	  on numeric overflow when calculating size,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available,
	- DK4_E_SYNTAX<br>
	  if
	  - the database file name does not refer to a regular file
	  - or the file contents is not an in-memory database
	  - or key/value sizes in the file are in conflict with restrictions,
	- DK4_E_SEC_CHECK<br>
	  with failed check id in iDetails1 if access is denied by additional
	  security checks,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available,
	- DK4_E_OPEN_READ_FAILED<br>
	  if fopen() or fread() failed,
	- DK4_E_OPEN_WRITE_FAILED<br>
	  if fopen() or fwrite() failed,
	- DK4_E_CLOSE_FAILED<br>
	  with errno value in iDetails1 if fclose() failed.

*/
dk4_dbi_t *
dk4dbi_open_with_type(
  const dkChar	*fn,
  int		 tp,
  int		 wa,
  int		 tr,
  size_t	 km,
  size_t	 vm,
  dk4_er_t	*erp
);

/**	Open database.
	@param	fn	Database type and file name.
			The database type is one from "mem", "bdb" or "ndbm".
			Database type and file name are separated by two
			colons "::".
	@param	wa	Flag: Write access required.
	@param	tr	Flag: Truncate existing database
			(only in conjunction with wa).
	@param	km	Maximum key size (0 for unlimited).
	@param	vm	Maximum value size (0 for unlimited).
	@param	erp	Error report, may be NULL.
	@return	Pointer to new database structure on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL or tp contains an invalid type number,
	- DK4_E_NOT_SUPPORTED<br>
	  if the database type is not supported,
	- DK4_E_MATH_OVERFLOW<br>
	  on numeric overflow when calculating size,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available,
	- DK4_E_SYNTAX<br>
	  if
	  - the specification contains an invalid backend type
	  - the database file name does not refer to a regular file
	  - or the file contents is not an in-memory database
	  - or key/value sizes in the file are in conflict with restrictions
	  - or an invalid database type was specified,
	- DK4_E_SEC_CHECK<br>
	  with failed check id in iDetails1 if access is denied by additional
	  security checks,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available,
	- DK4_E_OPEN_READ_FAILED<br>
	  if fopen() or fread() failed,
	- DK4_E_OPEN_WRITE_FAILED<br>
	  if fopen() or fwrite() failed,
	- DK4_E_CLOSE_FAILED<br>
	  with errno value in iDetails1 if fclose() failed.
*/
dk4_dbi_t *
dk4dbi_open(
  const dkChar	*fn,
  int		 wa,
  int		 tr,
  size_t	 km,
  size_t	 vm,
  dk4_er_t	*erp
);

/**	Find file name part from combined type/name.
	@param	filename	Combined database type and file name.
	@return	Valid pointer to file name part on success, NULL on error.
*/
const dkChar *
dk4dbi_filename_part(const dkChar *filename);

/**	Close database, release resources.
	@param	db	Database to close.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if db is NULL,
	- DK4_E_NOT_SUPPORTED<br>
	  if db indicates an unsupported backend type,
	- DK4_E_SYNTAX<br>
	  if the database file name does not refer to a regular file,
	- DK4_E_SEC_CHECK<br>
	  with failed check id in iDetails1 if access is denied by additional
	  security checks,
	- DK4_E_OPEN_WRITE_FAILED<br>
	  with errno value in iDetails1 if fopen() or fwrite() failed,
	- DK4_E_CLOSE_FAILED<br>
	  with errno value in iDetails1 if fclose() failed.
*/
int
dk4dbi_close(
  dk4_dbi_t	*db,
  dk4_er_t	*erp
);

/**	Set or reset option.
	@param	db	Database.
	@param	opt	Option.
	@param	val	New value (1 or 0).
	@return	1 on success, 0 on error (unknown option).
*/
int
dk4dbi_set_option(
  dk4_dbi_t	*db,
  int		 opt,
  int		 val
);

/**	Retrieve maximum key size.
	@param	db	Database to query.
	@param	erp	Error report, may be NULL.
	@return	Value on success (0 means unlimited),
	0 with erp set on error.

	Error code:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if db is NULL.
*/
size_t
dk4dbi_get_key_max(
  const dk4_dbi_t	*db,
  dk4_er_t		*erp
);

/**	Retrieve maximum value size.
	@param	db	Database to query.
	@param	erp	Error report, may be NULL.
	@return	Value on success (0 means unlimited),
	0 with erp set on error.


	Error code:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if db is NULL.
*/
size_t
dk4dbi_get_val_max(
  const dk4_dbi_t	*db,
  dk4_er_t		*erp
);

/**	Set or overwrite record.
	@param	db	Database.
	@param 	kp	Pointer to key data.
	@param	ks	Key data size (number of bytes).
	@param	vp	Pointer to value data. Use NULL to delete a record.
	@param	vs	Value data size (number of bytes).
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if db or kp is NULL or ks is 0,
	- DK4_E_SYNTAX<br>
	  if the database was opened read-only or ks or vs
	  exceeds a size limit,
	- DK4_E_NOT_SUPPORTED<br>
	  if db indicates an unsupported backend,
	- DK4_E_NOT_FOUND<br>
	  if no record to delete was found,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available,
	- DK4_E_WRITE_FAILED<br>
	  if modification of the database failed.
*/
int
dk4dbi_set(
  dk4_dbi_t	*db,
  const void	*kp,
  size_t	 ks,
  const void	*vp,
  size_t	 vs,
  dk4_er_t	*erp
);

/**	Synchronize changes to file if supported by backend.
	@param	db	Database.
	@param	erp	error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if db or a backend component is NULL,
	- DK4_E_NOT_SUPPORTED<br>
	  if db indicates an unsupported backend,
	- DK4_E_SYNTAX<br>
	  if the database file name does not refer to a regular file,
	- DK4_E_SEC_CHECK<br>
	  with failed check id in iDetails1 if access is denied by additional
	  security checks,
	- DK4_E_OPEN_WRITE_FAILED<br>
	  with errno value in iDetails1 if fopen() or fwrite() failed,
	- DK4_E_WRITE_FAILED<br>
	  if the backend failed to write to a database,
	- DK4_E_CLOSE_FAILED<br>
	  with errno value in iDetails1 if fclose() failed.
*/
int
dk4dbi_sync(
  dk4_dbi_t	*db,
  dk4_er_t	*erp
);

/**	Retrieve record.
	@param	db	Database.
	@param	kp	Pointer to key data.
	@param	ks	Key data size (number of bytes).
	@param	vp	Address of result buffer.
	@param	vs	Pointer to buffer size variable (in: size, out: used).
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if db, kp, vp or vs is NULL or ks is 0 or *vs is 0
	  or ks exceeds the key size limit,
	- DK4_E_NOT_SUPPORTED<br>
	  if db indicates an unsupported backend,
	- DK4_E_NOT_FOUND<br>
	  if no record was found for the key,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if the destination buffer is too small.
*/
int
dk4dbi_get(
  dk4_dbi_t	*db,
  const void	*kp,
  size_t	 ks,
  void		*vp,
  size_t	*vs,
  dk4_er_t	*erp
);

/**	Delete one record.
	@param	db	Database.
	@param	kp	Pointer to key data.
	@param	ks	Key data size (number of bytes).
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if db or kp is NULL or ks is 0,
	- DK4_E_SYNTAX<br>
	  if the database is opened read-only or ks exceeds the
	  key size limit,
	- DK4_E_NOT_SUPPORTED<br>
	  if db indicates an unsupported backend,
	- DK4_E_WRITE_FAILED<br>
	  if modification of the database failed,
	- DK4_E_NOT_FOUND<br>
	  if no record for the specified key is in the database.
*/
int
dk4dbi_delete(
  dk4_dbi_t	*db,
  const void	*kp,
  size_t	 ks,
  dk4_er_t	*erp
);

/**	Traverse database.
	@param	db	Database to traverse.
	@param	obj	Object to modify during traversal, may be NULL.
	@param	fct	Function to execute for all records.
	@return	1 on success, 0 on error (database traversed),
	-1 on error (traversal aborted).
*/
int
dk4dbi_traverse(
  dk4_dbi_t		*db,
  void			*obj,
  dk4dbi_traverse_fct_t	*fct
);

/**	Retrieve backend type.
	@param	db	Database to check.
	@return	Backend type.
*/
int
dk4dbi_get_type(const dk4_dbi_t *db);

#if !DK4_ON_WINDOWS
#if DK4_CHAR_SIZE == 1
#if DK4_HAVE_CHOWN && DK4_HAVE_CHMOD

/**	Change owner and mode for a database.
	@param	db	Database to modify.
	@param	user	New owner.
	@param	group	New owner group.
	@param	mode	New file permissions.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if db is NULL,
	- DK4_E_NOT_SUPPORTED<br>
	  if db indicates an unsupported backend,
	- DK4_E_SYSTEM<br>
	  with errno value in iDetails1 if a chown or chmod
	  system call failed,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size calculation,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if the file name is too large for an internal buffer.
*/
int
dk4dbi_change_owner_and_mode(
  dk4_dbi_t	*db,
  uid_t		 user,
  gid_t		 group,
  mode_t	 mode,
  dk4_er_t	*erp
);

#endif
/* if DK4_HAVE_CHOWN && DK4_HAVE_CHMOD */
#endif
/* if DK4_CHAR_SIZE == 1 */
#endif
/* if !DK4_ON_WINDOWS */

#ifdef __cplusplus
}
#endif




#endif