2 * $Id: cnid_lookup.c,v 1.3 2001-08-14 14:00:10 rufustfirefly Exp $
7 #endif /* HAVE_CONFIG_H */
11 #include <sys/param.h>
17 #include <netatalk/endian.h>
18 #include <atalk/adouble.h>
19 #include <atalk/cnid.h>
21 #include "cnid_private.h"
23 #define LOGFILEMAX 100 /* kbytes */
24 #define CHECKTIMEMAX 30 /* minutes */
26 /* this returns the cnid corresponding to a particular file. it will
27 also fix up the various databases if there's a problem. */
28 cnid_t cnid_lookup(void *CNID,
29 const struct stat *st, const cnid_t did,
30 const char *name, const int len)
34 DBT key, devdata, diddata;
35 int devino = 1, didname = 1;
40 if (!(db = CNID) || !st || !name)
43 /* do a little checkpointing if necessary. i stuck it here as
44 * cnid_lookup gets called when we do directory lookups. only do
45 * this if we're using a read-write database. */
46 if ((db->flags & CNIDFLAG_DB_RO) == 0) {
47 errno = txn_checkpoint(db->dbenv, LOGFILEMAX, CHECKTIMEMAX, 0);
48 while (errno == DB_INCOMPLETE)
49 errno = txn_checkpoint(db->dbenv, 0, 0, 0);
52 if ((buf = make_cnid_data(st, did, name, len)) == NULL) {
53 syslog(LOG_ERR, "cnid_lookup: path name too long");
57 memset(&key, 0, sizeof(key));
58 memset(&devdata, 0, sizeof(devdata));
60 /* look for a CNID. we have two options: dev/ino or did/name. if we
61 only get a match on one of them, that means a file has moved. */
62 key.data = buf; /* dev/ino is the first part of the buffer */
63 key.size = CNID_DEVINO_LEN;
64 while (errno = db->db_devino->get(db->db_devino, NULL,
69 if (errno == DB_NOTFOUND) {
74 syslog(LOG_ERR, "cnid_lookup: can't get CNID(%u/%u)",
75 st->st_dev, st->st_ino);
79 /* did/name is right afterwards. */
80 key.data = buf + CNID_DEVINO_LEN;
81 key.size = CNID_DID_LEN + len + 1;
82 memset(&diddata, 0, sizeof(diddata));
83 while (errno = db->db_didname->get(db->db_didname, NULL,
88 if (errno == DB_NOTFOUND) {
93 syslog(LOG_ERR, "cnid_lookup: can't get CNID(%u:%s)",
98 /* set id. honor did/name over dev/ino as dev/ino isn't necessarily
101 memcpy(&id, diddata.data, sizeof(id));
103 memcpy(&id, devdata.data, sizeof(id));
106 /* either entries in both databases exist or neither of them do. */
107 if ((devino && didname) || !(devino || didname)) {
109 syslog(LOG_ERR, "cnid_lookup: looked up did %d, name %s as %d", did, name, id);
112 /* fix up the databases */
113 cnid_update(db, id, st, did, name, len);
115 syslog(LOG_ERR, "cnid_lookup: looked up did %d, name %s as %d (needed update)", did, name, id);