2 * $Id: parse_mtab.c,v 1.4 2001-09-06 20:00:59 rufustfirefly Exp $
4 * afpd_mtab_parse & support. -- rgr, 9-Apr-01.
9 #endif /* HAVE_CONFIG_H */
18 #else /* STDC_HEADERS */
22 #endif /* HAVE_STRCHR */
23 char *strchr (), *strrchr ();
25 #define memcpy(d,s,n) bcopy ((s), (d), (n))
26 #define memmove(d,s,n) bcopy ((s), (d), (n))
27 #endif /* ! HAVE_MEMCPY */
28 #endif /* STDC_HEADERS */
32 #include "directory.h"
33 #include "parse_mtab.h"
35 #define MAX_AFPD_MTAB_ENTRIES 256 /* unreasonably large number */
36 #define MTAB_DELIM_CHARS " \t\n" /* delimiters for parsing */
39 #define COPY_STRING(lval, str) \
40 (lval) = malloc(1+strlen(str)), strcpy((lval), (str))
41 #endif /* COPY_STRING */
44 ceil_log_2 __P((int n))
45 /* Return the number of bits required to represent n. Only works for
58 afpd_st_cnid __P((struct stat *st))
59 /* Given a stat structure, look up the device in afpd_mount_table, and
60 compute and return a CNID. */
63 struct afpd_mtab_entry *entry;
65 if (afpd_mount_table != NULL) {
66 for (id = 0; id < afpd_mount_table->size; id++) {
67 entry = afpd_mount_table->table[id];
69 && entry->dev_major == major(st->st_dev)
70 && entry->dev_minor == minor(st->st_dev)) {
71 return(entry->bit_value | st->st_ino);
80 struct afpd_mount_table *
81 afpd_mtab_parse __P((char *file_name))
82 /* Parse the given mtab file, returning a new afpd_mount_table structure.
83 Also saves it in the afpd_mount_table static variable for use by the
84 afpd_st_cnid function. Returns NULL if it encounters an error.
86 [It would be great if this could generate a warning for any device that
87 allows more inodes than we can allocate bits for. -- rgr, 9-Apr-01.]
90 struct afpd_mtab_entry *entries[MAX_AFPD_MTAB_ENTRIES]; /* temp */
91 int id, max_id = 0, n_errors = 0;
93 char line[1000], *p, *index_tok, *dev_tok, *mount_tok;
94 int line_number = 0; /* one-based */
95 FILE *f = fopen(file_name, "r");
98 fprintf(stderr, "Error: Can't open %s: code %d\n",
102 for (id = 0; id < MAX_AFPD_MTAB_ENTRIES; id++)
104 while (fgets(line, sizeof(line), f) != NULL) {
107 if ((p = strchr(line, '#')) != NULL)
110 index_tok = strtok(line, MTAB_DELIM_CHARS);
111 dev_tok = strtok(NULL, MTAB_DELIM_CHARS);
112 mount_tok = strtok(NULL, MTAB_DELIM_CHARS);
113 if (mount_tok == NULL) {
114 /* [warning here if index_tok nonempty? -- rgr, 9-Apr-01.] */
116 else if (id = strtol(index_tok, &p, 10),
118 fprintf(stderr, "afpd:%s:%d: Non-integer device index value '%s'.\n",
119 file_name, line_number, index_tok);
122 else if (id < 1 || id > MAX_AFPD_MTAB_ENTRIES) {
124 "afpd:%s:%d: Expected an integer from 1 to %d, but got '%s'.\n",
125 file_name, line_number, MAX_AFPD_MTAB_ENTRIES-1, index_tok);
128 else if (entries[id] != NULL) {
131 "afpd:%s:%d: Id %d is already taken for %s.\n",
132 file_name, line_number, id, entries[id]->device);
135 else if (stat(dev_tok, &st) != 0) {
136 fprintf(stderr, "afpd:%s:%d: Can't stat '%s': code %d\n",
137 file_name, line_number, dev_tok, errno);
140 else if (! S_ISBLK(st.st_mode)) {
141 fprintf(stderr, "afpd:%s:%d: '%s' is not a block device.\n",
142 file_name, line_number, dev_tok);
146 /* make a new entry */
147 struct afpd_mtab_entry *entry
148 = (struct afpd_mtab_entry *) malloc(sizeof(struct afpd_mtab_entry));
151 entry->dev_major = major(st.st_rdev);
152 entry->dev_minor = minor(st.st_rdev);
153 COPY_STRING(entry->device, dev_tok);
154 COPY_STRING(entry->mount_point, mount_tok);
163 fprintf(stderr, "Got %d errors while reading %s; exiting.\n",
164 n_errors, file_name);
168 /* make the table. */
169 struct afpd_mount_table *mount_table /* return value */
170 = (struct afpd_mount_table *) malloc(sizeof(struct afpd_mount_table));
171 int n_bits = ceil_log_2(max_id);
173 struct afpd_mtab_entry **new_entries;
175 if (n_bits < AFPD_MTAB_MIN_DEV_BITS)
176 n_bits = AFPD_MTAB_MIN_DEV_BITS;
177 table_size = 1 << n_bits;
178 new_entries = (struct afpd_mtab_entry **)
179 malloc(table_size*sizeof(struct afpd_mtab_entry *));
180 mount_table->size = table_size;
181 mount_table->bits = n_bits;
182 mount_table->shift = AFPD_MTAB_DEV_AND_INODE_BITS-n_bits;
183 for (id = 0; id < table_size; id++) {
184 if (entries[id] != NULL)
185 entries[id]->bit_value = entries[id]->id << mount_table->shift;
186 new_entries[id] = entries[id];
188 mount_table->table = new_entries;
190 afpd_mount_table = mount_table;