]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/dbd_update.c
remove gcc warning
[netatalk.git] / etc / cnid_dbd / dbd_update.c
1 /*
2  * $Id: dbd_update.c,v 1.3 2005-05-03 14:55:11 didg Exp $
3  *
4  * Copyright (C) Joerg Lenneis 2003
5  * All Rights Reserved.  See COPYING.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <string.h>
13 #include <errno.h>
14 #include <atalk/logger.h>
15 #include <netatalk/endian.h>
16 #include <atalk/cnid_dbd_private.h>
17
18
19 #include "pack.h"
20 #include "dbif.h"
21 #include "dbd.h"
22
23
24 /* cnid_update: takes the given cnid and updates the metadata. */
25
26 /* FIXME: This calls pack_cnid_data(rqst) three times without modifying rqst */
27 /* FIXME: (Only tested with DB 4.1.25):
28
29       dbif_pget on the secondary index followed by dbif_del with the CNID on the
30       main cnid db could be replaced by a single dbif_del on the secondary index. That 
31       deletes the secondary, the corresponding entry from the main cnid db as well as the 
32       other secondary index.
33 */   
34    
35 int dbd_update(struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply)
36 {
37     DBT key,pkey, data;
38     int rc;
39     unsigned char *buf;                            
40     int notfound = 0;
41     char getbuf[CNID_HEADER_LEN + MAXPATHLEN +1];
42 #ifdef DEBUG
43     cnid_t tmpcnid;
44 #endif
45
46     memset(&key, 0, sizeof(key));
47     memset(&pkey, 0, sizeof(pkey));
48     memset(&data, 0, sizeof(data));
49
50     rply->namelen = 0;
51
52     buf = pack_cnid_data(rqst);
53     key.data = buf +CNID_DEVINO_OFS;
54     key.size = CNID_DEVINO_LEN;
55
56     data.data = getbuf;
57     data.size = CNID_HEADER_LEN + MAXPATHLEN + 1;
58     if ((rc = dbif_pget(DBIF_IDX_DEVINO, &key, &pkey, &data, 0)) < 0 ) {
59         goto err_db;
60     }
61     else if  (rc > 0) {
62 #ifdef DEBUG
63         memcpy(&tmpcnid, pkey.data, sizeof(cnid_t));
64         LOG(log_info, logtype_cnid, "dbd_update: Deleting %u corresponding to dev/ino %s from cnid2.db",
65             ntohl(tmpcnid), stringify_devino(rqst->dev, rqst->ino));
66 #endif
67         if ((rc = dbif_del(DBIF_IDX_CNID, &pkey, 0)) < 0 ) {
68             goto err_db;
69         }
70         else if (!rc) {
71                 LOG(log_error, logtype_cnid, "dbd_update: delete DEVINO %u %s", ntohl(rqst->cnid), db_strerror(errno));
72         }
73     }
74     if (!rc) {
75        notfound = 1;
76     }
77
78     buf = pack_cnid_data(rqst);
79     key.data = buf + CNID_DID_OFS;
80     key.size = CNID_DID_LEN + rqst->namelen +1;
81     memset(&pkey, 0, sizeof(pkey));
82
83     if ((rc = dbif_pget(DBIF_IDX_DIDNAME, &key, &pkey, &data, 0)) < 0) {
84         goto err_db;
85     }
86     else if  (rc > 0) {
87 #ifdef DEBUG
88         memcpy(&tmpcnid, pkey.data, sizeof(cnid_t));
89         LOG(log_info, logtype_cnid, "dbd_update: Deleting %u corresponding to did %u name %s from cnid2.db",
90             ntohl(tmpcnid), ntohl(rqst->did), rqst->name);
91 #endif
92         if ((rc = dbif_del(DBIF_IDX_CNID, &pkey, 0)) < 0) {
93             goto err_db;
94         }
95         else if (!rc) {
96                 LOG(log_error, logtype_cnid, "dbd_update: delete DIDNAME %u %s", ntohl(rqst->cnid), db_strerror(errno));
97         }
98     }
99     if (!rc) {
100        notfound |= 2;
101     }
102
103     memset(&key, 0, sizeof(key));
104     key.data = (cnid_t *) &rqst->cnid;
105     key.size = sizeof(rqst->cnid);
106
107     memset(&data, 0, sizeof(data));
108     /* Make a new entry. */
109     data.data = pack_cnid_data(rqst);
110     memcpy(data.data, &rqst->cnid, sizeof(rqst->cnid));
111     data.size = CNID_HEADER_LEN + rqst->namelen + 1;
112
113     if (dbif_put(DBIF_IDX_CNID, &key, &data, 0) < 0)
114         goto err_db;
115 #ifdef DEBUG
116     LOG(log_info, logtype_cnid, "dbd_update: Updated cnid2.db with dev/ino %s did %u name %s cnid %u",
117         stringify_devino(rqst->dev, rqst->ino),
118         ntohl(rqst->did), rqst->name, ntohl(rqst->cnid));
119 #endif
120     rply->result = CNID_DBD_RES_OK;
121     return 1;
122
123 err_db:
124 #ifdef DEBUG
125     LOG(log_error, logtype_cnid, "dbd_update: Unable to update CNID %u dev/ino %s, DID %x:%s",
126         ntohl(rqst->cnid), stringify_devino(rqst->ino, rqst->did), rqst->name);
127 #endif
128     rply->result = CNID_DBD_RES_ERR_DB;
129     return -1;
130 }