]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/dbd_lookup.c
Remove #ifdef DEBUG stuff, its not neccessary anymore as we have a fast LOG macro...
[netatalk.git] / etc / cnid_dbd / dbd_lookup.c
1 /*
2  * $Id: dbd_lookup.c,v 1.5 2009-05-04 09:09:43 franklahm 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
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/param.h>
16 #include <errno.h>
17 #include <netatalk/endian.h>
18 #include <atalk/logger.h>
19 #include <atalk/cnid_dbd_private.h>
20
21 #include "pack.h"
22 #include "dbif.h"
23 #include "dbd.h"
24
25 /*
26  *  This returns the CNID corresponding to a particular file.  It will also fix
27  *  up the database if there's a problem.
28  */
29
30 int dbd_lookup(struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply)
31 {
32     unsigned char *buf;
33     DBT key, devdata, diddata;
34     char dev[CNID_DEV_LEN];
35 #if 0
36     char ino[CNID_INO_LEN];
37 #endif
38     int devino = 1, didname = 1; 
39     int rc;
40     cnid_t id_devino, id_didname;
41     u_int32_t type_devino  = (unsigned)-1;
42     u_int32_t type_didname = (unsigned)-1;
43     int update = 0;
44     
45     
46     memset(&key, 0, sizeof(key));
47     memset(&diddata, 0, sizeof(diddata));
48     memset(&devdata, 0, sizeof(devdata));
49
50     rply->namelen = 0;
51     rply->cnid = 0;
52     
53     buf = pack_cnid_data(rqst); 
54     memcpy(dev, buf + CNID_DEV_OFS, CNID_DEV_LEN);
55 #if 0
56     /* FIXME: ino is not needed later on, remove? */
57     memcpy(ino, buf + CNID_INO_OFS, CNID_INO_LEN);
58 #endif
59
60     /* Look for a CNID.  We have two options: dev/ino or did/name.  If we
61        only get a match in one of them, that means a file has moved. */
62     key.data = buf + CNID_DEVINO_OFS;
63     key.size = CNID_DEVINO_LEN;
64
65     if ((rc = dbif_get(DBIF_IDX_DEVINO, &key, &devdata, 0))  < 0) {
66         LOG(log_error, logtype_cnid, "dbd_lookup: Unable to get CNID %u, name %s",
67                 ntohl(rqst->did), rqst->name);
68         rply->result = CNID_DBD_RES_ERR_DB;
69         return -1;
70     }
71     if (rc == 0) {
72         devino = 0;
73     }
74     else {
75         memcpy(&id_devino, devdata.data, sizeof(rply->cnid));
76         memcpy(&type_devino, (char *)devdata.data +CNID_TYPE_OFS, sizeof(type_devino));
77         type_devino = ntohl(type_devino);
78     }
79
80     key.data = buf + CNID_DID_OFS;
81     key.size = CNID_DID_LEN + rqst->namelen + 1;
82
83     if ((rc = dbif_get(DBIF_IDX_DIDNAME, &key, &diddata, 0))  < 0) {
84         LOG(log_error, logtype_cnid, "dbd_lookup: Unable to get CNID %u, name %s",
85                 ntohl(rqst->did), rqst->name);
86         rply->result = CNID_DBD_RES_ERR_DB;
87         return -1;
88     }
89     if (rc == 0) {
90         didname = 0;
91     }
92     else {
93         memcpy(&id_didname, diddata.data, sizeof(rply->cnid));
94         memcpy(&type_didname, (char *)diddata.data +CNID_TYPE_OFS, sizeof(type_didname));
95         type_didname = ntohl(type_didname);
96     }
97     
98     if (!devino && !didname) {  
99         /* not found */
100
101         LOG(log_debug, logtype_cnid, "cnid_lookup: dev/ino 0x%llx/0x%llx did %u name %s neither in devino nor didname", 
102             ntoh64((unsigned long long int)rqst->dev), ntoh64((unsigned long long int)rqst->ino), ntohl(rqst->did), rqst->name);
103
104         rply->result = CNID_DBD_RES_NOTFOUND;
105         return 1;
106     }
107
108     if (devino && didname && id_devino == id_didname && type_devino == rqst->type) {
109         /* the same */
110
111         LOG(log_debug, logtype_cnid, "cnid_lookup: Looked up dev/ino 0x%llx/0x%llx did %u name %s as %u", 
112             ntoh64((unsigned long long int)rqst->dev), ntoh64((unsigned long long int)rqst->ino), ntohl(rqst->did), rqst->name, ntohl(id_didname));
113
114         rply->cnid = id_didname;
115         rply->result = CNID_DBD_RES_OK;
116         return 1;
117     }
118     
119     if (didname) {
120         rqst->cnid = id_didname;
121         /* we have a did:name 
122          * if it's the same dev or not the same type
123          * just delete it
124         */
125         if (!memcmp(dev, (char *)diddata.data + CNID_DEV_OFS, CNID_DEV_LEN) ||
126                    type_didname != rqst->type) {
127             if (dbd_delete(rqst, rply) < 0) {
128                 return -1;
129             }
130         }
131         else {
132             update = 1;
133         }
134     }
135
136     if (devino) {
137         rqst->cnid = id_devino;
138         if (type_devino != rqst->type) {
139             /* same dev:inode but not same type one is a folder the other 
140              * is a file,it's an inode reused, delete the record
141             */
142             if (dbd_delete(rqst, rply) < 0) {
143                 return -1;
144             }
145         }
146         else {
147             update = 1;
148         }
149     }
150     if (!update) {
151         rply->result = CNID_DBD_RES_NOTFOUND;
152         return 1;
153     }
154     /* Fix up the database. assume it was a file move and rename */
155     rc = dbd_update(rqst, rply);
156     if (rc >0) {
157         rply->cnid = rqst->cnid;
158     }
159
160     LOG(log_debug, logtype_cnid, "cnid_lookup: Looked up dev/ino 0x%llx/0x%llx did %u name %s as %u (needed update)", 
161         ntoh64((unsigned long long int)rqst->dev), ntoh64((unsigned long long int)rqst->ino), ntohl(rqst->did), rqst->name, ntohl(rply->cnid));
162
163     return rc;
164 }