2 * $Id: cnid_cdb_rebuild_add.c,v 1.6 2009-11-20 17:22:11 didg Exp $
4 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
12 #ifdef CNID_BACKEND_CDB
13 #include "cnid_cdb_private.h"
20 /* ----------------------------- */
21 static cnid_t set_max_cnid(CNID_private *db, cnid_t hint)
23 DBT rootinfo_key, rootinfo_data;
25 char buf[ROOTINFO_DATALEN];
29 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
30 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
32 rootinfo_key.data = ROOTINFO_KEY;
33 rootinfo_key.size = ROOTINFO_KEYLEN;
35 switch ((rc = db->db_cnid->get(db->db_cnid, tid, &rootinfo_key, &rootinfo_data, 0))) {
37 memcpy(buf, (char *)rootinfo_data.data, ROOTINFO_DATALEN);
40 /* FIXME: This duplicates stuff from cnid_cdb_add.c.
41 We also implicitely assume that sizeof(time_t) <= CNID_DEV_LEN */
42 memcpy(buf, ROOTINFO_DATA, ROOTINFO_DATALEN);
44 memset(buf + CNID_DEV_OFS, 0, CNID_DEV_LEN);
45 memcpy(buf + CNID_DEV_OFS, &t, sizeof(time_t));
46 id = htonl(CNID_START);
47 memcpy(buf + CNID_TYPE_OFS, &id, sizeof(id));
50 LOG(log_error, logtype_default, "set_max_cnid: Unable to read rootinfo: %s", db_strerror(rc));
55 memcpy(&id, buf + CNID_TYPE_OFS, sizeof(id));
60 memcpy(buf + CNID_TYPE_OFS, &hint, sizeof(hint));
61 rootinfo_data.data = buf;
62 rootinfo_data.size = ROOTINFO_DATALEN;
63 if ((rc = db->db_cnid->put(db->db_cnid, tid, &rootinfo_key, &rootinfo_data, 0))) {
64 LOG(log_error, logtype_default, "set_max_cnid: Unable to write rootinfo: %s", db_strerror(rc));
76 /* ------------------------ */
77 cnid_t cnid_cdb_rebuild_add(struct _cnid_db *cdb, const struct stat *st,
78 const cnid_t did, char *name, const size_t len,
85 if (!cdb || !(db = cdb->_private) || !st || !name || hint == CNID_INVALID || hint < CNID_START) {
86 errno = CNID_ERR_PARAM;
91 /* FIXME: Bjoern does a lookup. Should we not overwrite unconditionally? */
93 id = cnid_cdb_lookup(cdb, st, did, name, len);
94 /* ... Return id if it is valid, or if Rootinfo is read-only. */
95 if (id || (db->flags & CNIDFLAG_DB_RO)) {
97 LOG(log_debug9, logtype_default, "cnid_add: Looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
103 /* Initialize our DBT data structures. */
104 memset(&key, 0, sizeof(key));
105 memset(&data, 0, sizeof(data));
107 if ((data.data = make_cnid_data(cdb->flags, st, did, name, len)) == NULL) {
108 LOG(log_error, logtype_default, "cnid_add: Path name is too long");
109 errno = CNID_ERR_PATH;
112 data.size = CNID_HEADER_LEN + len + 1;
114 memcpy(data.data, &hint, sizeof(hint));
117 key.size = sizeof(hint);
119 /* Now we need to add the CNID data to the databases. */
120 if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
121 LOG(log_error, logtype_default
122 , "cnid_add: Failed to add CNID for %s to database using hint %u: %s",
123 name, ntohl(hint), db_strerror(rc));
128 if (set_max_cnid(db, hint) == CNID_INVALID) {
134 LOG(log_debug9, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
143 #endif /* CNID_BACKEND_CDB */