2 * $Id: cnid_lookup.c,v 1.14 2002-08-30 03:12:52 jmarcus Exp $
7 #endif /* HAVE_CONFIG_H */
12 #include <sys/param.h>
14 #include <atalk/logger.h>
18 #include <netatalk/endian.h>
19 #include <atalk/adouble.h>
20 #include <atalk/cnid.h>
22 #include "cnid_private.h"
24 #define LOGFILEMAX 100 /* kbytes */
25 #define CHECKTIMEMAX 30 /* minutes */
27 /* This returns the CNID corresponding to a particular file. It will
28 * also fix up the various databases if there's a problem. */
29 cnid_t cnid_lookup(void *CNID, 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;
39 if (!(db = CNID) || !st || !name) {
44 /* Do a little checkpointing if necessary. I stuck it here as cnid_lookup
45 * gets called when we do directory lookups. Only do this if we're using
46 * a read-write database. */
47 if ((db->flags & CNIDFLAG_DB_RO) == 0) {
49 LOG(log_info, logtype_default, "cnid_lookup: Running database checkpoint");
51 #if DB_VERSION_MAJOR >= 4
52 switch (rc = db->dbenv->txn_checkpoint(db->dbenv, LOGFILEMAX, CHECKTIMEMAX, 0)) {
54 switch (rc = txn_checkpoint(db->dbenv, LOGFILEMAX, CHECKTIMEMAX, 0)) {
60 LOG(log_error, logtype_default, "cnid_lookup: txn_checkpoint: %s",
65 #endif /* CNID_DB_CDB */
67 if ((buf = make_cnid_data(st, did, name, len)) == NULL) {
68 LOG(log_error, logtype_default, "cnid_lookup: Pathname is too long");
72 memset(&key, 0, sizeof(key));
73 memset(&devdata, 0, sizeof(devdata));
74 memset(&diddata, 0, sizeof(diddata));
76 /* Look for a CNID. We have two options: dev/ino or did/name. If we
77 * only get a match in one of them, that means a file has moved. */
79 key.size = CNID_DEVINO_LEN;
80 while ((rc = db->db_devino->get(db->db_devino, NULL, &key, &devdata, 0))) {
82 if (rc == DB_LOCK_DEADLOCK) {
85 #endif /* CNID_DB_CDB */
87 if (rc == DB_NOTFOUND) {
92 LOG(log_error, logtype_default, "cnid_lookup: Unable to get CNID dev %u, ino %u: %s",
93 st->st_dev, st->st_ino, db_strerror(rc));
98 key.data = buf + CNID_DEVINO_LEN;
99 key.size = CNID_DID_LEN + len + 1;
100 while ((rc = db->db_didname->get(db->db_didname, NULL, &key, &diddata, 0))) {
102 if (rc == DB_LOCK_DEADLOCK) {
105 #endif /* CNID_DB_CDB */
107 if (rc == DB_NOTFOUND) {
112 LOG(log_error, logtype_default, "cnid_lookup: Unable to get CNID %u, name %s: %s",
113 ntohl(did), name, db_strerror(rc));
117 /* Set id. Honor did/name over dev/ino as dev/ino isn't necessarily
120 memcpy(&id, diddata.data, sizeof(id));
123 memcpy(&id, devdata.data, sizeof(id));
126 /* Either entries are in both databases or neither of them. */
127 if ((devino && didname) || !(devino || didname)) {
129 LOG(log_info, logtype_default, "cnid_lookup: Looked up did %u, name %s, as %u",
130 ntohl(did), name, ntohl(id));
135 /* Fix up the database. */
136 cnid_update(db, id, st, did, name, len);
138 LOG(log_info, logtype_default, "cnid_lookup: Looked up did %u, name %s, as %u (needed update)", ntohl(did), name, ntohl(id));