2 * $Id: cnid_add.c,v 1.2 2001-06-29 14:14:46 rufustfirefly Exp $
4 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
7 * cnid_add (db, dev, ino, did, name, hint):
8 * add a name to the CNID database. we use both dev/ino and did/name
9 * to keep track of things.
14 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
22 #endif /* HAVE_UNISTD_H */
25 #endif /* HAVE_FCNTL_H */
30 #include <netatalk/endian.h>
32 #include <atalk/adouble.h>
33 #include <atalk/cnid.h>
34 #include <atalk/util.h>
36 #include "cnid_private.h"
38 /* add an entry to the CNID databases. we do this as a transaction
39 * to prevent messiness. */
40 static int add_cnid(CNID_private *db, DBT *key, DBT *data)
46 txnp = db->dbenv.tx_info;
47 memset(&altkey, 0, sizeof(altkey));
48 memset(&altdata, 0, sizeof(altdata));
51 if (errno = txn_begin(txnp, NULL, &tid)) {
56 if (errno = db->db_cnid->put(db->db_cnid, tid,
57 key, data, DB_NOOVERWRITE)) {
65 /* dev/ino database */
66 altkey.data = data->data;
67 altkey.size = CNID_DEVINO_LEN;
68 altdata.data = key->data;
69 altdata.size = key->size;
70 if ((errno = db->db_devino->put(db->db_devino, tid,
71 &altkey, &altdata, 0))) {
79 /* did/name database */
80 altkey.data = data->data + CNID_DEVINO_LEN;
81 altkey.size = data->size - CNID_DEVINO_LEN;
82 if (errno = db->db_didname->put(db->db_didname, tid,
83 &altkey, &altdata, 0)) {
91 return txn_commit(tid);
94 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
95 return that cnid if it exists. */
96 cnid_t cnid_add(void *CNID, const struct stat *st,
97 const cnid_t did, const char *name, const int len,
106 if (!(db = CNID) || !st || !name)
109 /* just do a lookup if RootInfo is read-only. */
110 if (db->flags & (CNIDFLAG_ROOTINFO_RO | CNIDFLAG_DB_RO))
111 return cnid_lookup(db, st, did, name, len);
113 /* initialize everything */
114 memset(&key, 0, sizeof(key));
115 memset(&data, 0, sizeof(data));
117 /* acquire a lock on RootInfo. as the cnid database is the only user
118 * of RootInfo, we just use our own locks.
120 * NOTE: we lock it here to serialize access to the database. */
121 lock.l_type = F_WRLCK;
122 lock.l_whence = SEEK_SET;
123 lock.l_start = ad_getentryoff(&db->rootinfo, ADEID_DID);
124 lock.l_len = ad_getentrylen(&db->rootinfo, ADEID_DID);
125 if (fcntl(ad_hfileno(&db->rootinfo), F_SETLKW, &lock) < 0) {
126 syslog(LOG_ERR, "cnid_add: can't establish lock: %m");
130 /* if it's already stored, just return it */
131 if ((id = cnid_lookup(db, st, did, name, len))) {
136 /* just set hint, and the key will change. */
138 key.size = sizeof(hint);
141 make_cnid_data(st, did, name, len)) == NULL) {
142 syslog(LOG_ERR, "cnid_add: path name too long.");
145 data.size = CNID_HEADER_LEN + len + 1;
147 /* start off with the hint. it should be in network byte order.
148 * we need to make sure that somebody doesn't add in restricted
149 * cnid's to the database. */
150 if (ntohl(hint) >= CNID_START) {
151 /* if the key doesn't exist, add it in. don't fiddle with nextID. */
152 errno = add_cnid(db, &key, &data);
154 case DB_KEYEXIST: /* need to use RootInfo after all. */
157 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
165 /* no need to refresh the header file */
166 memcpy(&hint, ad_entry(&db->rootinfo, ADEID_DID), sizeof(hint));
168 /* search for a new id. we keep the first id around to check for
169 * wrap-around. NOTE: i do it this way so that we can go back and
171 save = id = ntohl(hint);
172 while (errno = add_cnid(db, &key, &data)) {
173 /* don't use any of the special CNIDs */
174 if (++id < CNID_START)
177 if ((errno != DB_KEYEXIST) || (save == id)) {
178 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
185 /* update RootInfo with the next id. */
187 memcpy(ad_entry(&db->rootinfo, ADEID_DID), &id, sizeof(id));
188 ad_flush(&db->rootinfo, ADFLAGS_HF);
191 lock.l_type = F_UNLCK;
192 fcntl(ad_hfileno(&db->rootinfo), F_SETLK, &lock);