int get_cnid(DBD *dbd, struct cnid_dbd_rply *rply)
{
static cnid_t id;
+ static char buf[ROOTINFO_DATALEN];
DBT rootinfo_key, rootinfo_data;
int rc;
cnid_t hint;
-
- if (id == 0) {
- memset(&rootinfo_key, 0, sizeof(rootinfo_key));
- memset(&rootinfo_data, 0, sizeof(rootinfo_data));
- rootinfo_key.data = ROOTINFO_KEY;
- rootinfo_key.size = ROOTINFO_KEYLEN;
- if ((rc = dbif_get(dbd, DBIF_CNID, &rootinfo_key, &rootinfo_data, 0)) <= 0) {
+ memset(&rootinfo_key, 0, sizeof(rootinfo_key));
+ memset(&rootinfo_data, 0, sizeof(rootinfo_data));
+ rootinfo_key.data = ROOTINFO_KEY;
+ rootinfo_key.size = ROOTINFO_KEYLEN;
+
+ if (id == 0) {
+ if ((rc = dbif_get(dbd, DBIF_CNID, &rootinfo_key, &rootinfo_data, 0)) < 0) {
rply->result = CNID_DBD_RES_ERR_DB;
return -1;
}
- memcpy(&hint, (char *)rootinfo_data.data +CNID_TYPE_OFS, sizeof(hint));
- id = ntohl(hint);
+ if (rc == 0) {
+ /* no rootinfo key yet */
+ memcpy(buf, ROOTINFO_DATA, ROOTINFO_DATALEN);
+ id = CNID_START - 1;
+ } else {
+ memcpy(buf, (char *)rootinfo_data.data, ROOTINFO_DATALEN);
+ memcpy(&hint, buf + CNID_TYPE_OFS, sizeof(hint));
+ id = ntohl(hint);
+ if (id < CNID_START - 1)
+ id = CNID_START - 1;
+ }
}
/* If we've hit the max CNID allowed, we return an error. CNID
return -1;
}
+ rootinfo_data.data = buf;
rootinfo_data.size = ROOTINFO_DATALEN;
hint = htonl(id);
- memcpy((char *)rootinfo_data.data + CNID_TYPE_OFS, &hint, sizeof(hint));
+ memcpy(buf + CNID_TYPE_OFS, &hint, sizeof(hint));
if (dbif_put(dbd, DBIF_CNID, &rootinfo_key, &rootinfo_data, 0) < 0) {
rply->result = CNID_DBD_RES_ERR_DB;
return 0;
}
-/* ---------------
-*/
-int dbd_stamp(DBD *dbd)
-{
- DBT rootinfo_key, rootinfo_data;
- cnid_t hint;
- char buf[ROOTINFO_DATALEN];
- char stamp[CNID_DEV_LEN];
-
- memset(&rootinfo_key, 0, sizeof(rootinfo_key));
- memset(&rootinfo_data, 0, sizeof(rootinfo_data));
- rootinfo_key.data = ROOTINFO_KEY;
- rootinfo_key.size = ROOTINFO_KEYLEN;
-
- switch (dbif_get(dbd, DBIF_CNID, &rootinfo_key, &rootinfo_data, 0)) {
- case 0:
- hint = htonl(CNID_START);
- memcpy(buf, ROOTINFO_DATA, ROOTINFO_DATALEN);
- rootinfo_data.data = buf;
- rootinfo_data.size = ROOTINFO_DATALEN;
- if (dbif_stamp(dbd, stamp, CNID_DEV_LEN) < 0) {
- return -1;
- }
- memcpy((char *)rootinfo_data.data + CNID_TYPE_OFS, &hint, sizeof(hint));
- memcpy((char *)rootinfo_data.data + CNID_DEV_OFS, stamp, sizeof(stamp));
- if (dbif_put(dbd, DBIF_CNID, &rootinfo_key, &rootinfo_data, 0) < 0) {
- return -1;
- }
- return 0;
-
- case 1: /* we already have one */
- return 0;
- default:
- return -1;
- }
- return -1;
-}
-
/* ------------------------ */
/* We need a nolookup version for `dbd` */
int dbd_add(DBD *dbd, struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply, int nolookup)