5 * Revision 1.2 1995/05/01 12:43:57 adam
6 * lgets function moved from kernel to util.
8 * Revision 1.1 1995/03/27 08:25:01 adam
9 * New module gip: Gateway IPc module.
10 * New module gw-db: Gateway hash-db module (user information table).
23 #define FILE_HEAD_LEN 128
27 struct file_head { /* file header info */
28 char magic[8]; /* start info */
29 int no_of_entries; /* no of entries in table */
30 int sequence_no; /* number of next to come */
32 int *hash_array; /* hash array pointers (DB_HASH in size) */
33 int fd; /* descriptor of file */
34 int dirty; /* any writes so far? */
37 struct db_bucket { /* information about individual entries */
38 int name_length; /* length of name */
39 int info_length; /* length of information */
40 int next; /* next in hash chain - possibly 0 */
41 int prev; /* prev in hach chain - possibly 0 */
45 * write_head: write header of table
47 static int write_head (GW_DB db)
49 char file_head_buf[FILE_HEAD_LEN];
52 if (lseek (db->fd, 0L, SEEK_SET) == -1)
54 memcpy (file_head_buf, &db->head, sizeof(db->head));
55 r = write (db->fd, file_head_buf, FILE_HEAD_LEN);
58 r = write (db->fd, db->hash_array, DB_HASH * sizeof(*db->hash_array));
65 * hash: calculate hash value
67 static unsigned hash (const char *name, int length)
72 l = l*65599 + *name++;
77 * lock_file: lock entire file
79 static void lock_file (int fd, int type)
83 area.l_whence = SEEK_SET;
86 fcntl (fd, F_SETLKW, &area);
90 * gw_db_lookup: table lookup
92 int gw_db_lookup (GW_DB db, const char *name, int name_length,
93 void **buf, size_t *count)
95 struct db_bucket bucket;
96 unsigned l = hash (name, name_length);
102 for (pos = db->hash_array[l]; pos; pos = bucket.next)
106 if (lseek (db->fd, pos, SEEK_SET) == -1)
111 r = read (db->fd, &bucket, sizeof(bucket));
117 if (r != sizeof(bucket))
122 if (bucket.name_length <= 0 || bucket.info_length <= 0 ||
123 bucket.name_length >= 16384 || bucket.info_length >= 262144)
128 if (bucket.name_length != name_length)
130 blen = bucket.name_length + bucket.info_length;
135 if (!(dbuf = malloc (dsize)))
138 r = read (db->fd, dbuf, blen);
149 if (memcmp (name, dbuf, name_length))
152 *count = bucket.info_length;
153 *buf = malloc (*count);
159 memcpy (*buf, dbuf + name_length, *count);
168 * gw_db_insert: table insertion
170 int gw_db_insert (GW_DB db, const char *name, int name_length,
171 const void *buf, size_t count)
173 struct db_bucket n_bucket;
174 struct db_bucket bucket;
176 unsigned l = hash (name, name_length);
177 int pos = db->hash_array[l];
181 (db->head.no_of_entries)++;
182 (db->head.sequence_no)++;
183 n_bucket.name_length = name_length;
184 n_bucket.info_length = count;
185 n_pos = lseek (db->fd, 0, SEEK_END);
189 db->hash_array[l] = n_pos;
192 r = write (db->fd, &n_bucket, sizeof(n_bucket));
195 else if (r < sizeof(n_bucket))
198 r = write (db->fd, name, name_length);
201 else if (r < name_length)
204 r = write (db->fd, buf, count);
210 r_pos = lseek (db->fd, pos, SEEK_SET);
213 r = read (db->fd, &bucket, sizeof(bucket));
216 else if (r < sizeof(bucket))
220 r_pos = lseek (db->fd, pos, SEEK_SET);
223 r = write (db->fd, &bucket, sizeof(bucket));
226 else if (r < sizeof(bucket))
232 * gw_db_free: release memory associated with table
234 static GW_DB gw_db_free (GW_DB db)
236 free (db->hash_array);
242 * gw_db_open: open and lock table for reading (and writing)
244 GW_DB gw_db_open (const char *fname, int write_flag)
246 char file_head_buf[FILE_HEAD_LEN];
250 if (!(db = malloc (sizeof(*db))))
253 if (!(db->hash_array = malloc (DB_HASH * sizeof(*db->hash_array))))
254 return gw_db_free (db);
256 db->fd = open (fname, O_RDWR|O_CREAT, 0666);
258 db->fd = open (fname, O_RDONLY);
260 return gw_db_free (db);
261 lock_file (db->fd, write_flag ? F_WRLCK : F_RDLCK);
262 r = read (db->fd, file_head_buf, FILE_HEAD_LEN);
264 return gw_db_free (db);
265 if (r < FILE_HEAD_LEN)
270 return gw_db_free (db);
271 db->head.no_of_entries = 0;
272 db->head.sequence_no = 1;
273 for (i=0; i<DB_HASH; i++)
274 db->hash_array[i] = 0;
276 return gw_db_free (db);
280 memcpy (&db->head, file_head_buf, sizeof(db->head));
281 r = read (db->fd, db->hash_array, sizeof(*db->hash_array)*DB_HASH);
282 if (r < sizeof(*db->hash_array)*DB_HASH)
283 return gw_db_free (db);
289 * gw_db_close: close table and flush
291 int gw_db_close (GW_DB db)
297 lock_file (db->fd, F_UNLCK);
298 if (close (db->fd) == -1)
308 * gw_db_no_ent: return number of entries in table
310 int gw_db_no_ent (GW_DB db)
312 return db->head.no_of_entries;
316 * gw_db_seq_no: return sequence number
318 int gw_db_seq_no (GW_DB db)
320 return db->head.sequence_no;