1/*
2 * $Id: cnid_cdb_update.c,v 1.4 2009-11-20 17:22:11 didg Exp $
3 */
4
5#ifdef HAVE_CONFIG_H
6#include "config.h"
7#endif /* HAVE_CONFIG_H */
8
9#ifdef CNID_BACKEND_CDB
10#include "cnid_cdb_private.h"
11
12#define tid    NULL
13
14/* cnid_update: takes the given cnid and updates the metadata.  To
15 * handle the did/name data, there are a bunch of functions to get
16 * and set the various fields. */
17int cnid_cdb_update(struct _cnid_db *cdb, const cnid_t id, const struct stat *st,
18                const cnid_t did, char *name, const size_t len
19                /*, const char *info, const int infolen*/)
20{
21    unsigned char *buf;
22    CNID_private *db;
23    DBT key, pkey, data;
24    int rc;
25    int notfound = 0;
26    char getbuf[CNID_HEADER_LEN + MAXPATHLEN +1];
27
28    if (!cdb || !(db = cdb->_private) || !id || !st || !name || (db->flags & CNIDFLAG_DB_RO)) {
29        return -1;
30    }
31
32    memset(&key, 0, sizeof(key));
33    memset(&pkey, 0, sizeof(pkey));
34    memset(&data, 0, sizeof(data));
35
36    buf = make_cnid_data(cdb->flags, st, did, name, len);
37
38    key.data = buf +CNID_DEVINO_OFS;
39    key.size = CNID_DEVINO_LEN;
40    data.data = getbuf;
41    data.size = CNID_HEADER_LEN + MAXPATHLEN + 1;
42
43    if (0 != (rc = db->db_devino->pget(db->db_devino, tid, &key, &pkey, &data, 0)) ) {
44#if DB_VERSION_MAJOR >= 4
45        if (rc != DB_NOTFOUND && rc != DB_SECONDARY_BAD) {
46#else
47	if (rc != DB_NOTFOUND) {
48#endif
49           LOG(log_error, logtype_default, "cnid_update: Unable to get devino CNID %u, name %s: %s",
50               ntohl(did), name, db_strerror(rc));
51           goto fin;
52        }
53        notfound = 1;
54    } else {
55        if ((rc = db->db_cnid->del(db->db_cnid, tid, &pkey, 0))) {
56            LOG(log_error, logtype_default, "cnid_update: Unable to delete CNID %u: %s",
57                ntohl(id), db_strerror(rc));
58	}
59    }
60
61    memset(&pkey, 0, sizeof(pkey));
62    buf = make_cnid_data(cdb->flags, st, did, name, len);
63    key.data = buf + CNID_DID_OFS;
64    key.size = CNID_DID_LEN + len + 1;
65
66    if (0 != (rc = db->db_didname->pget(db->db_didname, tid, &key, &pkey, &data, 0)) ) {
67#if DB_VERSION_MAJOR >= 4
68        if (rc != DB_NOTFOUND && rc != DB_SECONDARY_BAD) {
69#else
70	if (rc != DB_NOTFOUND) {
71#endif
72           LOG(log_error, logtype_default, "cnid_update: Unable to get didname CNID %u, name %s: %s",
73               ntohl(did), name, db_strerror(rc));
74           goto fin;
75        }
76        notfound |= 2;
77    } else {
78        if ((rc = db->db_cnid->del(db->db_cnid, tid, &pkey, 0))) {
79            LOG(log_error, logtype_default, "cnid_update: Unable to delete CNID %u: %s",
80                ntohl(id), db_strerror(rc));
81	}
82    }
83
84
85    memset(&key, 0, sizeof(key));
86    key.data = (cnid_t *)&id;
87    key.size = sizeof(id);
88
89    memset(&data, 0, sizeof(data));
90    /* Make a new entry. */
91    buf = make_cnid_data(cdb->flags, st, did, name, len);
92    data.data = buf;
93    memcpy(data.data, &id, sizeof(id));
94    data.size = CNID_HEADER_LEN + len + 1;
95
96    /* Update the old CNID with the new info. */
97    if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
98        LOG(log_error, logtype_default, "cnid_update: (%d) Unable to update CNID %u:%s: %s",
99            notfound, ntohl(id), name, db_strerror(rc));
100        goto fin;
101    }
102
103    return 0;
104fin:
105    return -1;
106
107}
108
109#endif
110