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