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