3 * $Id: cnid_last.c,v 1.1.4.1.2.2 2005-02-06 10:16:03 didg Exp $
5 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
6 * All Rights Reserved. See COPYRIGHT.
12 #endif /* HAVE_CONFIG_H */
14 #ifdef CNID_BACKEND_LAST
16 #include "cnid_last.h"
17 #include <atalk/util.h>
18 #include <atalk/logger.h>
20 #include <sys/types.h>
24 /* ------------------------ */
25 cnid_t cnid_last_add(struct _cnid_db *cdb, const struct stat *st,
26 const cnid_t did, char *name, const int len, cnid_t hint)
29 /* FIXME: it relies on fact, that this is never called twice for the same file/dir. */
30 /* Propably we should look through DID tree. */
33 * First thing: DID and FNUMs are
34 * in the same space for purposes of enumerate (and several
35 * other wierd places). While we consider this Apple's bug,
36 * this is the work-around: In order to maintain constant and
37 * unique DIDs and FNUMs, we monotonically generate the DIDs
38 * during the session, and derive the FNUMs from the filesystem.
39 * Since the DIDs are small, we insure that the FNUMs are fairly
40 * large by setting thier high bits to the device number.
42 * AFS already does something very similar to this for the
43 * inode number, so we don't repeat the procedure.
46 * due to complaints over did's being non-persistent,
47 * here's the current hack to provide semi-persistent
49 * 1) we reserve the first bit for file ids.
50 * 2) the next 7 bits are for the device.
51 * 3) the remaining 24 bits are for the inode.
53 * both the inode and device information are actually hashes
54 * that are then truncated to the requisite bit length.
56 * it should be okay to use lstat to deal with symlinks.
59 struct _cnid_last_private *priv;
61 if (!cdb || !(cdb->_private))
64 priv = (struct _cnid_last_private *) (cdb->_private);
66 if (S_ISDIR(st->st_mode))
67 return htonl(priv->last_did++);
69 return htonl((st->st_dev << 16) | (st->st_ino & 0x0000ffff));
74 void cnid_last_close(struct _cnid_db *cdb)
83 int cnid_last_delete(struct _cnid_db *cdb, const cnid_t id)
90 /* Return CNID for a given did/name. */
91 cnid_t cnid_last_get(struct _cnid_db *cdb, const cnid_t did, char *name, const int len)
93 /* FIXME: it relies on fact, that this is never called twice for the same file/dir. */
94 /* Propably we should look through DID tree. */
101 cnid_t cnid_last_lookup(struct _cnid_db *cdb, const struct stat *st, const cnid_t did, char *name, const int len)
103 /* FIXME: this function doesn't work in [last] scheme ! */
104 /* Should be never called or CNID should be somewhat refactored again. */
109 static struct _cnid_db *cnid_last_new(const char *volpath)
111 struct _cnid_db *cdb;
112 struct _cnid_last_private *priv;
114 if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
117 if ((cdb->volpath = strdup(volpath)) == NULL) {
122 if ((cdb->_private = calloc(1, sizeof(struct _cnid_last_private))) == NULL) {
128 /* Set up private state */
129 priv = (struct _cnid_last_private *) (cdb->_private);
132 /* Set up standard fields */
134 cdb->cnid_add = cnid_last_add;
135 cdb->cnid_delete = cnid_last_delete;
136 cdb->cnid_get = cnid_last_get;
137 cdb->cnid_lookup = cnid_last_lookup;
138 cdb->cnid_nextid = NULL; /* cnid_last_nextid; */
139 cdb->cnid_resolve = cnid_last_resolve;
140 cdb->cnid_update = cnid_last_update;
141 cdb->cnid_close = cnid_last_close;
146 struct _cnid_db *cnid_last_open(const char *dir, mode_t mask)
148 struct _cnid_db *cdb;
154 if ((cdb = cnid_last_new(dir)) == NULL) {
155 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
162 struct _cnid_module cnid_last_module = {
169 /* Return the did/name pair corresponding to a CNID. */
170 char *cnid_last_resolve(struct _cnid_db *cdb, cnid_t * id, void *buffer, u_int32_t len)
172 /* FIXME: frankly, it does not work. As get, add and other functions. */
177 int cnid_last_update(struct _cnid_db *cdb, const cnid_t id, const struct stat *st,
178 const cnid_t did, char *name, const int len
179 /*, const char *info, const int infolen */ )
185 #endif /* CNID_BACKEND_LAST */