2 * $Id: dbd_update.c,v 1.6 2009-05-28 10:22:07 franklahm Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
10 #endif /* HAVE_CONFIG_H */
14 #include <atalk/logger.h>
15 #include <netatalk/endian.h>
16 #include <atalk/cnid_dbd_private.h>
24 /* cnid_update: takes the given cnid and updates the metadata. */
26 /* FIXME: This calls pack_cnid_data(rqst) three times without modifying rqst */
27 /* FIXME: (Only tested with DB 4.1.25):
29 dbif_pget on the secondary index followed by dbif_del with the CNID on the
30 main cnid db could be replaced by a single dbif_del on the secondary index. That
31 deletes the secondary, the corresponding entry from the main cnid db as well as the
32 other secondary index.
35 int dbd_update(DBD *dbd, struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply)
41 char getbuf[CNID_HEADER_LEN + MAXPATHLEN +1];
44 memset(&key, 0, sizeof(key));
45 memset(&pkey, 0, sizeof(pkey));
46 memset(&data, 0, sizeof(data));
50 buf = pack_cnid_data(rqst);
51 key.data = buf +CNID_DEVINO_OFS;
52 key.size = CNID_DEVINO_LEN;
55 data.size = CNID_HEADER_LEN + MAXPATHLEN + 1;
56 if ((rc = dbif_pget(dbd, DBIF_IDX_DEVINO, &key, &pkey, &data, 0)) < 0 ) {
60 memcpy(&tmpcnid, pkey.data, sizeof(cnid_t));
61 LOG(log_debug, logtype_cnid, "dbd_update: Deleting %u corresponding to dev/ino 0x%llx/0x%llx from cnid2.db",
62 ntohl(tmpcnid), (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
64 if ((rc = dbif_del(dbd, DBIF_CNID, &pkey, 0)) < 0 ) {
68 LOG(log_error, logtype_cnid, "dbd_update: delete DEVINO %u %s", ntohl(rqst->cnid), db_strerror(errno));
75 buf = pack_cnid_data(rqst);
76 key.data = buf + CNID_DID_OFS;
77 key.size = CNID_DID_LEN + rqst->namelen +1;
78 memset(&pkey, 0, sizeof(pkey));
80 if ((rc = dbif_pget(dbd, DBIF_IDX_DIDNAME, &key, &pkey, &data, 0)) < 0) {
85 memcpy(&tmpcnid, pkey.data, sizeof(cnid_t));
87 LOG(log_debug, logtype_cnid, "dbd_update: Deleting %u corresponding to did %u name %s from cnid2.db",
88 ntohl(tmpcnid), ntohl(rqst->did), rqst->name);
90 if ((rc = dbif_del(dbd, DBIF_CNID, &pkey, 0)) < 0) {
94 LOG(log_error, logtype_cnid, "dbd_update: delete DIDNAME %u %s", ntohl(rqst->cnid), db_strerror(errno));
101 memset(&key, 0, sizeof(key));
102 key.data = (cnid_t *) &rqst->cnid;
103 key.size = sizeof(rqst->cnid);
105 memset(&data, 0, sizeof(data));
106 /* Make a new entry. */
107 data.data = pack_cnid_data(rqst);
108 memcpy(data.data, &rqst->cnid, sizeof(rqst->cnid));
109 data.size = CNID_HEADER_LEN + rqst->namelen + 1;
111 if (dbif_put(dbd, DBIF_CNID, &key, &data, 0) < 0)
114 LOG(log_info, logtype_cnid, "dbd_update: Updated cnid2.db with dev/ino 0x%llx/0x%llx did %u name %s cnid %u",
115 (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, ntohl(rqst->did), rqst->name, ntohl(rqst->cnid));
117 rply->result = CNID_DBD_RES_OK;
121 LOG(log_error, logtype_cnid, "dbd_update: Unable to update CNID %u dev/ino 0x%llx/0x%llx, DID %ul: %s",
122 ntohl(rqst->cnid), (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, rqst->did, rqst->name);
124 rply->result = CNID_DBD_RES_ERR_DB;