]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/dbd_rebuild_add.c
Merge master
[netatalk.git] / etc / cnid_dbd / dbd_rebuild_add.c
1 /*
2  * $Id: dbd_rebuild_add.c,v 1.4 2009-12-23 10:18:48 franklahm Exp $
3  *
4  * Copyright (C) Joerg Lenneis 2005
5  * All Rights Reserved.  See COPYING.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <arpa/inet.h>
15
16 #include <atalk/logger.h>
17 #include <atalk/cnid_dbd_private.h>
18
19 #include "pack.h"
20 #include "dbif.h"
21 #include "dbd.h"
22
23
24 /* rebuild_add: Enter all fields (including the CNID) into the database and
25    update the current cnid, for emergency repairs. */
26
27 int dbd_rebuild_add(DBD *dbd, struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply)
28 {
29     DBT key, data;
30     cnid_t cur, tmp, id;
31
32     memset(&key, 0, sizeof(key));
33     memset(&data, 0, sizeof(data));
34
35     rply->namelen = 0;
36
37     key.data = &rqst->cnid;
38     key.size = sizeof(cnid_t);
39
40     data.data = pack_cnid_data(rqst);
41     data.size = CNID_HEADER_LEN + rqst->namelen + 1;
42     memcpy(data.data, &rqst->cnid, sizeof(cnid_t));
43
44     /* FIXME: In cnid_cdb.c Bjoern does a lookup here and returns the CNID found if sucessful. Why? */
45
46     if (dbif_put(dbd, DBIF_CNID, &key, &data, 0) < 0) {
47         rply->result = CNID_DBD_RES_ERR_DB;
48         return -1;
49     }
50
51     LOG(log_debug, logtype_cnid,
52         "dbd_rebuild_add(CNID: %u, did: %u, name: \"%s\", dev/ino:0x%llx/0x%llx): success",
53         ntohl(rqst->cnid), ntohl(rqst->did), rqst->name,
54         (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
55
56     key.data = ROOTINFO_KEY;
57     key.size = ROOTINFO_KEYLEN;
58
59     if (dbif_get(dbd, DBIF_CNID, &key, &data, 0) <= 0) {
60         /* FIXME: If we cannot find ROOTINFO_KEY, should this be considered
61            fatal or should we just return 0 and roll back? */
62         rply->result = CNID_DBD_RES_ERR_DB;
63         return -1;
64     }
65
66     memcpy(&tmp, (char *) data.data + CNID_TYPE_OFS, sizeof(cnid_t));
67     cur = ntohl(tmp);
68     id  = ntohl(rqst->cnid);
69
70     if (id > cur) {
71         data.size = ROOTINFO_DATALEN;
72         memcpy((char *) data.data + CNID_TYPE_OFS, &rqst->cnid, sizeof(cnid_t));
73         if (dbif_put(dbd, DBIF_CNID, &key, &data, 0) < 0) {
74             rply->result = CNID_DBD_RES_ERR_DB;
75             return -1;
76         }
77     }
78
79     rply->cnid = rqst->cnid;
80     rply->result = CNID_DBD_RES_OK;
81     return 1;
82 }
83
84