2 Unix SMB/CIFS implementation.
3 Database interface using a file per record
4 Copyright (C) Volker Lendecke 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Be aware that this is just sample code that has not seen too much testing
29 /* We only support one locked record at a time -- everything else
30 * would lead to a potential deadlock anyway! */
31 struct db_record *locked_record;
34 struct db_locked_file {
39 struct db_file_ctx *parent;
42 /* Copy from statcache.c... */
44 static uint32 fsh(const uint8 *p, int len)
48 for (i=0; i<len; i++) {
49 n = ((n << 5) + n) ^ (uint32)(p[i]);
54 static int db_locked_file_destr(struct db_locked_file *data)
56 if (data->parent != NULL) {
57 data->parent->locked_record = NULL;
60 if (close(data->fd) != 0) {
61 DEBUG(3, ("close failed: %s\n", strerror(errno)));
68 static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag);
69 static NTSTATUS db_file_delete(struct db_record *rec);
71 static struct db_record *db_file_fetch_locked(struct db_context *db,
75 struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data,
77 struct db_record *result;
78 struct db_locked_file *file;
80 SMB_STRUCT_STAT statbuf;
84 SMB_ASSERT(ctx->locked_record == NULL);
87 if (!(result = TALLOC_P(mem_ctx, struct db_record))) {
88 DEBUG(0, ("talloc failed\n"));
92 if (!(file = TALLOC_P(result, struct db_locked_file))) {
93 DEBUG(0, ("talloc failed\n"));
98 result->private_data = file;
99 result->store = db_file_store;
100 result->delete_rec = db_file_delete;
102 result->key.dsize = key.dsize;
103 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
104 if (result->key.dptr == NULL) {
105 DEBUG(0, ("talloc failed\n"));
111 file->hash = fsh(key.dptr, key.dsize);
112 file->name = hex_encode(file, (unsigned char *)key.dptr, key.dsize);
113 if (file->name == NULL) {
114 DEBUG(0, ("hex_encode failed\n"));
119 file->path = talloc_asprintf(file, "%s/%2.2X/%s", ctx->dirname,
120 file->hash, file->name);
121 if (file->path == NULL) {
122 DEBUG(0, ("talloc_asprintf failed\n"));
128 file->fd = open(file->path, O_RDWR|O_CREAT, 0644);
132 DEBUG(3, ("Could not open/create %s: %s\n",
133 file->path, strerror(errno)));
138 talloc_set_destructor(file, db_locked_file_destr);
141 fl.l_whence = SEEK_SET;
147 ret = fcntl(file->fd, F_SETLKW, &fl);
148 } while ((ret == -1) && (errno == EINTR));
151 DEBUG(3, ("Could not get lock on %s: %s\n",
152 file->path, strerror(errno)));
157 if (sys_fstat(file->fd, &statbuf) != 0) {
158 DEBUG(3, ("Could not fstat %s: %s\n",
159 file->path, strerror(errno)));
164 if (statbuf.st_nlink == 0) {
165 /* Someone has deleted it under the lock, retry */
170 result->value.dsize = 0;
171 result->value.dptr = NULL;
173 if (statbuf.st_size != 0) {
174 result->value.dsize = statbuf.st_size;
175 result->value.dptr = TALLOC_ARRAY(result, uint8,
177 if (result->value.dptr == NULL) {
178 DEBUG(1, ("talloc failed\n"));
183 nread = read_data(file->fd, (char *)result->value.dptr,
184 result->value.dsize);
185 if (nread != result->value.dsize) {
186 DEBUG(3, ("read_data failed: %s\n", strerror(errno)));
192 ctx->locked_record = result;
193 file->parent = (struct db_file_ctx *)talloc_reference(file, ctx);
198 static NTSTATUS db_file_store_root(int fd, TDB_DATA data)
200 if (sys_lseek(fd, 0, SEEK_SET) != 0) {
201 DEBUG(0, ("sys_lseek failed: %s\n", strerror(errno)));
202 return map_nt_error_from_unix(errno);
205 if (write_data(fd, (char *)data.dptr, data.dsize) != data.dsize) {
206 DEBUG(3, ("write_data failed: %s\n", strerror(errno)));
207 return map_nt_error_from_unix(errno);
210 if (sys_ftruncate(fd, data.dsize) != 0) {
211 DEBUG(3, ("sys_ftruncate failed: %s\n", strerror(errno)));
212 return map_nt_error_from_unix(errno);
218 static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag)
220 struct db_locked_file *file =
221 talloc_get_type_abort(rec->private_data,
222 struct db_locked_file);
226 status = db_file_store_root(file->fd, data);
232 static NTSTATUS db_file_delete(struct db_record *rec)
234 struct db_locked_file *file =
235 talloc_get_type_abort(rec->private_data,
236 struct db_locked_file);
240 res = unlink(file->path);
244 DEBUG(3, ("unlink(%s) failed: %s\n", file->path,
246 return map_nt_error_from_unix(errno);
252 static int db_file_traverse(struct db_context *db,
253 int (*fn)(struct db_record *rec,
257 struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data,
259 TALLOC_CTX *mem_ctx = talloc_init("traversal %s\n", ctx->dirname);
264 for (i=0; i<256; i++) {
265 const char *dirname = talloc_asprintf(mem_ctx, "%s/%2.2X",
268 struct dirent *dirent;
270 if (dirname == NULL) {
271 DEBUG(0, ("talloc failed\n"));
272 TALLOC_FREE(mem_ctx);
276 dir = opendir(dirname);
278 DEBUG(3, ("Could not open dir %s: %s\n", dirname,
280 TALLOC_FREE(mem_ctx);
284 while ((dirent = readdir(dir)) != NULL) {
287 struct db_record *rec;
289 if ((dirent->d_name[0] == '.') &&
290 ((dirent->d_name[1] == '\0') ||
291 ((dirent->d_name[1] == '.') &&
292 (dirent->d_name[2] == '\0')))) {
296 keyblob = strhex_to_data_blob(mem_ctx, dirent->d_name);
297 if (keyblob.data == NULL) {
298 DEBUG(5, ("strhex_to_data_blob failed\n"));
302 key.dptr = keyblob.data;
303 key.dsize = keyblob.length;
305 if ((ctx->locked_record != NULL) &&
306 (key.dsize == ctx->locked_record->key.dsize) &&
307 (memcmp(key.dptr, ctx->locked_record->key.dptr,
310 if (fn(ctx->locked_record,
311 private_data) != 0) {
312 TALLOC_FREE(mem_ctx);
318 rec = db_file_fetch_locked(db, mem_ctx, key);
320 /* Someone might have deleted it */
324 if (rec->value.dptr == NULL) {
331 if (fn(rec, private_data) != 0) {
332 TALLOC_FREE(mem_ctx);
342 TALLOC_FREE(mem_ctx);
346 struct db_context *db_open_file(TALLOC_CTX *mem_ctx,
347 struct messaging_context *msg_ctx,
349 int hash_size, int tdb_flags,
350 int open_flags, mode_t mode)
352 struct db_context *result = NULL;
353 struct db_file_ctx *ctx;
355 if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
356 DEBUG(0, ("talloc failed\n"));
360 if (!(ctx = TALLOC_P(result, struct db_file_ctx))) {
361 DEBUG(0, ("talloc failed\n"));
366 result->private_data = ctx;
367 result->fetch_locked = db_file_fetch_locked;
368 result->traverse = db_file_traverse;
369 result->traverse_read = db_file_traverse;
371 ctx->locked_record = NULL;
372 if (!(ctx->dirname = talloc_strdup(ctx, name))) {
373 DEBUG(0, ("talloc failed\n"));
378 if (open_flags & O_CREAT) {
381 mode |= (mode & S_IRUSR) ? S_IXUSR : 0;
382 mode |= (mode & S_IRGRP) ? S_IXGRP : 0;
383 mode |= (mode & S_IROTH) ? S_IXOTH : 0;
385 ret = mkdir(name, mode);
386 if ((ret != 0) && (errno != EEXIST)) {
387 DEBUG(5, ("mkdir(%s,%o) failed: %s\n", name, mode,
393 for (i=0; i<256; i++) {
395 path = talloc_asprintf(result, "%s/%2.2X", name, i);
397 DEBUG(0, ("asprintf failed\n"));
401 ret = mkdir(path, mode);
402 if ((ret != 0) && (errno != EEXIST)) {
403 DEBUG(5, ("mkdir(%s,%o) failed: %s\n", path,
404 mode, strerror(errno)));