tdb_compat: make tdb2s temporarily read-only for tdb_traverse_read()
[sfrench/samba-autobuild/.git] / lib / tdb_compat / tdb_compat.c
1 #include <tdb_compat.h>
2
3 /* Note: for the moment, we only need this file for TDB2, so we can
4  * assume waf. */
5 #if BUILD_TDB2
6 TDB_DATA tdb_null = { NULL, 0 };
7
8 /* Proxy which sets waitflag to false so we never block. */
9 static int lock_nonblock(int fd, int rw, off_t off, off_t len, bool waitflag,
10                          void *_orig)
11 {
12         struct tdb_attribute_flock *orig = _orig;
13
14         return orig->lock(fd, rw, off, len, false, orig->data);
15 }
16
17 enum TDB_ERROR tdb_transaction_start_nonblock(struct tdb_context *tdb)
18 {
19         union tdb_attribute locking, orig;
20         enum TDB_ERROR ecode;
21
22         orig.base.attr = TDB_ATTRIBUTE_FLOCK;
23         ecode = tdb_get_attribute(tdb, &orig);
24         if (ecode != TDB_SUCCESS)
25                 return ecode;
26
27         /* Replace locking function with our own. */
28         locking = orig;
29         locking.flock.data = &orig;
30         locking.flock.lock = lock_nonblock;
31
32         ecode = tdb_set_attribute(tdb, &locking);
33         if (ecode != TDB_SUCCESS)
34                 return ecode;
35
36         ecode = tdb_transaction_start(tdb);
37         tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
38         return ecode;
39 }
40
41 /* For TDB1 tdbs, read traverse vs normal matters: write traverse
42    locks the entire thing! */
43 int64_t tdb_traverse_read_(struct tdb_context *tdb,
44                            int (*fn)(struct tdb_context *,
45                                                        TDB_DATA, TDB_DATA,
46                                                        void *),
47                            void *p)
48 {
49         int64_t ret;
50
51         if (tdb_get_flags(tdb) & TDB_RDONLY) {
52                 return tdb_traverse(tdb, fn, p);
53         }
54
55         tdb_add_flag(tdb, TDB_RDONLY);
56         ret = tdb_traverse(tdb, fn, p);
57         tdb_remove_flag(tdb, TDB_RDONLY);
58         return ret;
59 }
60
61 /*
62  * This handles TDB_CLEAR_IF_FIRST.
63  */
64 static enum TDB_ERROR clear_if_first(int fd, void *unused)
65 {
66         /* We hold a lock offset 63 always, so we can tell if anyone else is. */
67         struct flock fl;
68
69         fl.l_type = F_WRLCK;
70         fl.l_whence = SEEK_SET;
71         fl.l_start = 63;
72         fl.l_len = 1;
73
74         if (fcntl(fd, F_SETLK, &fl) == 0) {
75                 /* We must be first ones to open it w/ TDB_CLEAR_IF_FIRST! */
76                 if (ftruncate(fd, 0) != 0) {
77                         return TDB_ERR_IO;
78                 }
79         }
80         fl.l_type = F_RDLCK;
81         if (fcntl(fd, F_SETLKW, &fl) != 0) {
82                 return TDB_ERR_IO;
83         }
84         return TDB_SUCCESS;
85 }
86
87 struct tdb_context *
88 tdb_open_compat_(const char *name, int hash_size_unused,
89                  int tdb_flags, int open_flags, mode_t mode,
90                  void (*log_fn)(struct tdb_context *,
91                                 enum tdb_log_level,
92                                 enum TDB_ERROR,
93                                 const char *message,
94                                 void *data),
95                  void *log_data)
96 {
97         union tdb_attribute cif, log, *attr = NULL;
98
99         if (log_fn) {
100                 log.log.base.attr = TDB_ATTRIBUTE_LOG;
101                 log.log.base.next = NULL;
102                 log.log.fn = log_fn;
103                 log.log.data = log_data;
104                 attr = &log;
105         }
106
107         if (tdb_flags & TDB_CLEAR_IF_FIRST) {
108                 cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK;
109                 cif.openhook.base.next = attr;
110                 cif.openhook.fn = clear_if_first;
111                 attr = &cif;
112                 tdb_flags &= ~TDB_CLEAR_IF_FIRST;
113         }
114
115         /* Testsuite uses this to speed things up. */
116         if (getenv("TDB_NO_FSYNC")) {
117                 tdb_flags |= TDB_NOSYNC;
118         }
119
120         return tdb_open(name, tdb_flags|TDB_ALLOW_NESTING, open_flags, mode,
121                         attr);
122 }
123 #endif