]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/dbd_lookup.c
fix most solaris compile issues, flock is #ifdef as solaris has no flock implementati...
[netatalk.git] / etc / cnid_dbd / dbd_lookup.c
1 /*
2  * $Id: dbd_lookup.c,v 1.1.4.3 2003-10-30 10:03:19 bfernhomberg Exp $
3  *
4  * Copyright (C) Joerg Lenneis 2003
5  * All Rights Reserved.  See COPYRIGHT.
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     dev_t  dev;
35     ino_t  ino;
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, sizeof(dev));
53     memcpy(&ino, buf + CNID_INO_OFS, sizeof(ino));
54
55     /* Look for a CNID.  We have two options: dev/ino or did/name.  If we
56        only get a match in one of them, that means a file has moved. */
57     key.data = buf +CNID_DEVINO_OFS;
58     key.size = CNID_DEVINO_LEN;
59
60     if ((rc = dbif_get(DBIF_IDX_DEVINO, &key, &devdata, 0))  < 0) {
61         LOG(log_error, logtype_cnid, "dbd_lookup: Unable to get CNID %u, name %s",
62                 ntohl(rqst->did), rqst->name);
63         rply->result = CNID_DBD_RES_ERR_DB;
64         return -1;
65     }
66     if (rc == 0) {
67         devino = 0;
68     }
69     else {
70         memcpy(&id_devino, devdata.data, sizeof(rply->cnid));
71         memcpy(&type_devino, devdata.data +CNID_TYPE_OFS, sizeof(type_devino));
72         type_devino = ntohl(type_devino);
73     }
74     
75     buf = pack_cnid_data(rqst); 
76     key.data = buf +CNID_DID_OFS;
77     key.size = CNID_DID_LEN + rqst->namelen + 1;
78
79     if ((rc = dbif_get(DBIF_IDX_DIDNAME, &key, &diddata, 0))  < 0) {
80         LOG(log_error, logtype_cnid, "dbd_lookup: Unable to get CNID %u, name %s",
81                 ntohl(rqst->did), rqst->name);
82         rply->result = CNID_DBD_RES_ERR_DB;
83         return -1;
84     }
85     if (rc == 0) {
86         didname = 0;
87     }
88     else {
89         memcpy(&id_didname, diddata.data, sizeof(rply->cnid));
90         memcpy(&type_didname, diddata.data +CNID_TYPE_OFS, sizeof(type_didname));
91         type_didname = ntohl(type_didname);
92     }
93     
94     if (!devino && !didname) {  
95         /* not found */
96         rply->result = CNID_DBD_RES_NOTFOUND;
97         return 1;
98     }
99
100     if (devino && didname && id_devino == id_didname && type_devino == rqst->type) {
101         /* the same */
102         rply->cnid = id_didname;
103         rply->result = CNID_DBD_RES_OK;
104         return 1;
105     }
106     
107     if (didname) {
108         rqst->cnid = id_didname;
109         /* we have a did:name 
110          * if it's the same dev or not the same type
111          * just delete it
112         */
113         if (!memcmp(&dev, (char *)diddata.data + CNID_DEV_OFS, sizeof(dev)) ||
114                    type_didname != rqst->type) {
115             if (dbd_delete(rqst, rply) < 0) {
116                 return -1;
117             }
118         }
119         else {
120             update = 1;
121         }
122     }
123
124     if (devino) {
125         rqst->cnid = id_devino;
126         if (type_devino != rqst->type) {
127             /* same dev:inode but not same type one is a folder the other 
128              * is a file,it's an inode reused, delete the record
129             */
130             if (dbd_delete(rqst, rply) < 0) {
131                 return -1;
132             }
133         }
134         else {
135             update = 1;
136         }
137     }
138     if (!update) {
139         rply->result = CNID_DBD_RES_NOTFOUND;
140         return 1;
141     }
142     /* Fix up the database. assume it was a file move and rename */
143     rc = dbd_update(rqst, rply);
144     if (rc >0) {
145         rply->cnid = rqst->cnid;
146     }
147 #ifdef DEBUG
148     LOG(log_info, logtype_cnid, "cnid_lookup: Looked up did %u, name %s, as %u (needed update)", 
149     ntohl(rqst->did), rqst->name, ntohl(rply->cnid));
150 #endif
151     return rc;
152 }