]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_update.c
MFH: Sync CNID DB code with HEAD.
[netatalk.git] / libatalk / cnid / cnid_update.c
1 /*
2  * $Id: cnid_update.c,v 1.12.2.3 2001-12-15 06:35:28 jmarcus Exp $
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif /* HAVE_CONFIG_H */
8
9 #ifdef CNID_DB
10 #include <stdio.h>
11 #include <string.h>
12 #include <sys/param.h>
13 #include <sys/stat.h>
14 #include <errno.h>
15 #include <syslog.h>
16
17 #include <db.h>
18 #include <netatalk/endian.h>
19 #include <atalk/adouble.h>
20 #include <atalk/cnid.h>
21
22 #include "cnid_private.h"
23
24 /* cnid_update: takes the given cnid and updates the metadata.  To
25  * handle the did/name data, there are a bunch of functions to get
26  * and set the various fields. */
27 int cnid_update(void *CNID, const cnid_t id, const struct stat *st,
28                 const cnid_t did, const char *name, const int len
29                 /*, const char *info, const int infolen*/)
30 {
31     CNID_private *db;
32     DBT key, data, altdata;
33     DB_TXN *tid;
34     int rc;
35
36     if (!(db = CNID) || !id || !st || !name || (db->flags & CNIDFLAG_DB_RO)) {
37         return -1;
38     }
39
40     memset(&key, 0, sizeof(key));
41     memset(&altdata, 0, sizeof(altdata));
42
43 retry:
44     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0))) {
45         syslog(LOG_ERR, "cnid_update: Failed to begin transaction: %s",
46                db_strerror(rc));
47         return rc;
48     }
49
50     /* Get the old info. */
51     key.data = (cnid_t *)&id;
52     key.size = sizeof(id);
53     memset(&data, 0, sizeof(data));
54     if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, DB_RMW))) {
55         txn_abort(tid);
56         switch (rc) {
57         case DB_LOCK_DEADLOCK:
58             goto retry;
59         case DB_NOTFOUND:
60             /* Silently fail here.  We're allowed to do this since this CNID
61              * might have been deleted out from under us, or someone has
62              * called cnid_lookup then cnid_update (which is redundant). */
63             return 0;
64         default:
65             goto update_err;
66         }
67     }
68
69     /* Delete the old dev/ino mapping. */
70     key.data = data.data;
71     key.size = CNID_DEVINO_LEN;
72     if ((rc = db->db_devino->del(db->db_devino, tid, &key, 0))) {
73         switch (rc) {
74         case DB_LOCK_DEADLOCK:
75             txn_abort(tid);
76             goto retry;
77         case DB_NOTFOUND:
78             break;
79         default:
80             txn_abort(tid);
81             goto update_err;
82         }
83     }
84
85     /* Delete the old did/name mapping. */
86     key.data = (char *) data.data + CNID_DEVINO_LEN;
87     key.size = data.size - CNID_DEVINO_LEN;
88     if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
89         switch (rc) {
90         case DB_LOCK_DEADLOCK:
91             txn_abort(tid);
92             goto retry;
93         case DB_NOTFOUND:
94             break;
95         default:
96             txn_abort(tid);
97             goto update_err;
98         }
99     }
100
101     /* Make a new entry. */
102     data.data = make_cnid_data(st, did, name, len);
103     data.size = CNID_HEADER_LEN + len + 1;
104
105     /* Update the old CNID with the new info. */
106     key.data = (cnid_t *) &id;
107     key.size = sizeof(id);
108     if ((rc = db->db_cnid->put(db->db_cnid, tid, &key, &data, 0))) {
109         txn_abort(tid);
110         switch (rc) {
111         case DB_LOCK_DEADLOCK:
112             goto retry;
113         default:
114             goto update_err;
115         }
116     }
117
118     /* Put in a new dev/ino mapping. */
119     key.data = data.data;
120     key.size = CNID_DEVINO_LEN;
121     altdata.data = (cnid_t *) &id;
122     altdata.size = sizeof(id);
123     if ((rc = db->db_devino->put(db->db_devino, tid, &key, &altdata, 0))) {
124         txn_abort(tid);
125         switch (rc) {
126         case DB_LOCK_DEADLOCK:
127             goto retry;
128         default:
129             goto update_err;
130         }
131     }
132
133     /* put in a new did/name mapping. */
134     key.data = (char *) data.data + CNID_DEVINO_LEN;
135     key.size = data.size - CNID_DEVINO_LEN;
136     if ((rc = db->db_didname->put(db->db_didname, tid, &key, &altdata, 0))) {
137         txn_abort(tid);
138         switch (rc) {
139         case DB_LOCK_DEADLOCK:
140             goto retry;
141         default:
142             goto update_err;
143         }
144     }
145
146
147     return txn_commit(tid, 0);
148
149 update_err:
150     syslog(LOG_ERR, "cnid_update: Unable to update CNID %u: %s",
151            ntohl(id), db_strerror(rc));
152     return -1;
153 }
154 #endif /* CNID_DB */