]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/dbd_lookup.c
- merge branch-netatalk-afp-3x-dev, HEAD was tagged before
[netatalk.git] / etc / cnid_dbd / dbd_lookup.c
1 /*
2  * $Id: dbd_lookup.c,v 1.2 2005-04-28 20:49:48 bfernhomberg 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     char *buf;
33     DBT key, devdata, diddata;
34     char dev[CNID_DEV_LEN];
35     char ino[CNID_INO_LEN];
36     int devino = 1, didname = 1; 
37     int rc;
38     cnid_t id_devino, id_didname;
39     u_int32_t type_devino  = (unsigned)-1;
40     u_int32_t type_didname = (unsigned)-1;
41     int update = 0;
42     
43     
44     memset(&key, 0, sizeof(key));
45     memset(&diddata, 0, sizeof(diddata));
46     memset(&devdata, 0, sizeof(devdata));
47
48     rply->namelen = 0;
49     rply->cnid = 0;
50     
51     buf = pack_cnid_data(rqst); 
52     memcpy(dev, buf + CNID_DEV_OFS, CNID_DEV_LEN);
53     /* FIXME: ino is not needed later on, remove? */
54     memcpy(ino, buf + CNID_INO_OFS, CNID_INO_LEN);
55
56     /* Look for a CNID.  We have two options: dev/ino or did/name.  If we
57        only get a match in one of them, that means a file has moved. */
58     key.data = buf +CNID_DEVINO_OFS;
59     key.size = CNID_DEVINO_LEN;
60
61     if ((rc = dbif_get(DBIF_IDX_DEVINO, &key, &devdata, 0))  < 0) {
62         LOG(log_error, logtype_cnid, "dbd_lookup: Unable to get CNID %u, name %s",
63                 ntohl(rqst->did), rqst->name);
64         rply->result = CNID_DBD_RES_ERR_DB;
65         return -1;
66     }
67     if (rc == 0) {
68         devino = 0;
69     }
70     else {
71         memcpy(&id_devino, devdata.data, sizeof(rply->cnid));
72         memcpy(&type_devino, (char *)devdata.data +CNID_TYPE_OFS, sizeof(type_devino));
73         type_devino = ntohl(type_devino);
74     }
75     
76     /* FIXME: This second call to pack_cnid_data() is redundant, any reason it is here? */
77     buf = pack_cnid_data(rqst); 
78     key.data = buf +CNID_DID_OFS;
79     key.size = CNID_DID_LEN + rqst->namelen + 1;
80
81     if ((rc = dbif_get(DBIF_IDX_DIDNAME, &key, &diddata, 0))  < 0) {
82         LOG(log_error, logtype_cnid, "dbd_lookup: Unable to get CNID %u, name %s",
83                 ntohl(rqst->did), rqst->name);
84         rply->result = CNID_DBD_RES_ERR_DB;
85         return -1;
86     }
87     if (rc == 0) {
88         didname = 0;
89     }
90     else {
91         memcpy(&id_didname, diddata.data, sizeof(rply->cnid));
92         memcpy(&type_didname, (char *)diddata.data +CNID_TYPE_OFS, sizeof(type_didname));
93         type_didname = ntohl(type_didname);
94     }
95     
96     if (!devino && !didname) {  
97         /* not found */
98 #ifdef DEBUG
99         LOG(log_info, logtype_cnid, "cnid_lookup: dev/ino %s did %u name %s neither in devino nor didname", 
100             stringify_devino(rqst->dev, rqst->ino), ntohl(rqst->did), rqst->name);
101 #endif
102         rply->result = CNID_DBD_RES_NOTFOUND;
103         return 1;
104     }
105
106     if (devino && didname && id_devino == id_didname && type_devino == rqst->type) {
107         /* the same */
108 #ifdef DEBUG
109         LOG(log_info, logtype_cnid, "cnid_lookup: Looked up dev/ino %s did %u name %s as %u", 
110             stringify_devino(rqst->dev, rqst->ino), ntohl(rqst->did), rqst->name, ntohl(id_didname));
111 #endif
112         rply->cnid = id_didname;
113         rply->result = CNID_DBD_RES_OK;
114         return 1;
115     }
116     
117     if (didname) {
118         rqst->cnid = id_didname;
119         /* we have a did:name 
120          * if it's the same dev or not the same type
121          * just delete it
122         */
123         if (!memcmp(dev, (char *)diddata.data + CNID_DEV_OFS, CNID_DEV_LEN) ||
124                    type_didname != rqst->type) {
125             if (dbd_delete(rqst, rply) < 0) {
126                 return -1;
127             }
128         }
129         else {
130             update = 1;
131         }
132     }
133
134     if (devino) {
135         rqst->cnid = id_devino;
136         if (type_devino != rqst->type) {
137             /* same dev:inode but not same type one is a folder the other 
138              * is a file,it's an inode reused, delete the record
139             */
140             if (dbd_delete(rqst, rply) < 0) {
141                 return -1;
142             }
143         }
144         else {
145             update = 1;
146         }
147     }
148     if (!update) {
149         rply->result = CNID_DBD_RES_NOTFOUND;
150         return 1;
151     }
152     /* Fix up the database. assume it was a file move and rename */
153     rc = dbd_update(rqst, rply);
154     if (rc >0) {
155         rply->cnid = rqst->cnid;
156     }
157 #ifdef DEBUG
158     LOG(log_info, logtype_cnid, "cnid_lookup: Looked up dev/ino %s did %u name %s as %u (needed update)", 
159         stringify_devino(rqst->dev, rqst->ino), ntohl(rqst->did), rqst->name, ntohl(rply->cnid));
160 #endif
161     return rc;
162 }