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 cnid_t cnid_cdb_rebuild_add(struct _cnid_db *cdb, const struct stat *st,
77 cnid_t did, const char *name, size_t len, cnid_t hint)
83 if (!cdb || !(db = cdb->_private) || !st || !name || hint == CNID_INVALID || hint < CNID_START) {
84 errno = CNID_ERR_PARAM;
89 /* FIXME: Bjoern does a lookup. Should we not overwrite unconditionally? */
91 id = cnid_cdb_lookup(cdb, st, did, name, len);
92 /* ... Return id if it is valid, or if Rootinfo is read-only. */
93 if (id || (db->flags & CNIDFLAG_DB_RO)) {
95 LOG(log_debug9, logtype_default, "cnid_add: Looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
101 /* Initialize our DBT data structures. */
102 memset(&key, 0, sizeof(key));
103 memset(&data, 0, sizeof(data));
105 if ((data.data = make_cnid_data(cdb->flags, st, did, name, len)) == NULL) {
106 LOG(log_error, logtype_default, "cnid_add: Path name is too long");
107 errno = CNID_ERR_PATH;
110 data.size = CNID_HEADER_LEN + len + 1;
112 memcpy(data.data, &hint, sizeof(hint));
115 key.size = sizeof(hint);
117 /* Now we need to add the CNID data to the databases. */
118 if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
119 LOG(log_error, logtype_default
120 , "cnid_add: Failed to add CNID for %s to database using hint %u: %s",
121 name, ntohl(hint), db_strerror(rc));
126 if (set_max_cnid(db, hint) == CNID_INVALID) {
132 LOG(log_debug9, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
141 #endif /* CNID_BACKEND_CDB */