2 * $Id: parse_mtab.c,v 1.3 2001-06-19 18:04:39 rufustfirefly Exp $
4 * afpd_mtab_parse & support. -- rgr, 9-Apr-01.
9 #endif /* HAVE_CONFIG_H */
17 #include "directory.h"
18 #include "parse_mtab.h"
20 #define MAX_AFPD_MTAB_ENTRIES 256 /* unreasonably large number */
21 #define MTAB_DELIM_CHARS " \t\n" /* delimiters for parsing */
24 #define COPY_STRING(lval, str) \
25 (lval) = malloc(1+strlen(str)), strcpy((lval), (str))
26 #endif /* COPY_STRING */
29 ceil_log_2 __P((int n))
30 /* Return the number of bits required to represent n. Only works for
43 afpd_st_cnid __P((struct stat *st))
44 /* Given a stat structure, look up the device in afpd_mount_table, and
45 compute and return a CNID. */
48 struct afpd_mtab_entry *entry;
50 if (afpd_mount_table != NULL) {
51 for (id = 0; id < afpd_mount_table->size; id++) {
52 entry = afpd_mount_table->table[id];
54 && entry->dev_major == major(st->st_dev)
55 && entry->dev_minor == minor(st->st_dev)) {
56 return(entry->bit_value | st->st_ino);
65 struct afpd_mount_table *
66 afpd_mtab_parse __P((char *file_name))
67 /* Parse the given mtab file, returning a new afpd_mount_table structure.
68 Also saves it in the afpd_mount_table static variable for use by the
69 afpd_st_cnid function. Returns NULL if it encounters an error.
71 [It would be great if this could generate a warning for any device that
72 allows more inodes than we can allocate bits for. -- rgr, 9-Apr-01.]
75 struct afpd_mtab_entry *entries[MAX_AFPD_MTAB_ENTRIES]; /* temp */
76 int id, max_id = 0, n_errors = 0;
78 char line[1000], *p, *index_tok, *dev_tok, *mount_tok;
79 int line_number = 0; /* one-based */
80 FILE *f = fopen(file_name, "r");
83 fprintf(stderr, "Error: Can't open %s: code %d\n",
87 for (id = 0; id < MAX_AFPD_MTAB_ENTRIES; id++)
89 while (fgets(line, sizeof(line), f) != NULL) {
92 if ((p = strchr(line, '#')) != NULL)
95 index_tok = strtok(line, MTAB_DELIM_CHARS);
96 dev_tok = strtok(NULL, MTAB_DELIM_CHARS);
97 mount_tok = strtok(NULL, MTAB_DELIM_CHARS);
98 if (mount_tok == NULL) {
99 /* [warning here if index_tok nonempty? -- rgr, 9-Apr-01.] */
101 else if (id = strtol(index_tok, &p, 10),
103 fprintf(stderr, "afpd:%s:%d: Non-integer device index value '%s'.\n",
104 file_name, line_number, index_tok);
107 else if (id < 1 || id > MAX_AFPD_MTAB_ENTRIES) {
109 "afpd:%s:%d: Expected an integer from 1 to %d, but got '%s'.\n",
110 file_name, line_number, MAX_AFPD_MTAB_ENTRIES-1, index_tok);
113 else if (entries[id] != NULL) {
116 "afpd:%s:%d: Id %d is already taken for %s.\n",
117 file_name, line_number, id, entries[id]->device);
120 else if (stat(dev_tok, &st) != 0) {
121 fprintf(stderr, "afpd:%s:%d: Can't stat '%s': code %d\n",
122 file_name, line_number, dev_tok, errno);
125 else if (! S_ISBLK(st.st_mode)) {
126 fprintf(stderr, "afpd:%s:%d: '%s' is not a block device.\n",
127 file_name, line_number, dev_tok);
131 /* make a new entry */
132 struct afpd_mtab_entry *entry
133 = (struct afpd_mtab_entry *) malloc(sizeof(struct afpd_mtab_entry));
136 entry->dev_major = major(st.st_rdev);
137 entry->dev_minor = minor(st.st_rdev);
138 COPY_STRING(entry->device, dev_tok);
139 COPY_STRING(entry->mount_point, mount_tok);
148 fprintf(stderr, "Got %d errors while reading %s; exiting.\n",
149 n_errors, file_name);
153 /* make the table. */
154 struct afpd_mount_table *mount_table /* return value */
155 = (struct afpd_mount_table *) malloc(sizeof(struct afpd_mount_table));
156 int n_bits = ceil_log_2(max_id);
158 struct afpd_mtab_entry **new_entries;
160 if (n_bits < AFPD_MTAB_MIN_DEV_BITS)
161 n_bits = AFPD_MTAB_MIN_DEV_BITS;
162 table_size = 1 << n_bits;
163 new_entries = (struct afpd_mtab_entry **)
164 malloc(table_size*sizeof(struct afpd_mtab_entry *));
165 mount_table->size = table_size;
166 mount_table->bits = n_bits;
167 mount_table->shift = AFPD_MTAB_DEV_AND_INODE_BITS-n_bits;
168 for (id = 0; id < table_size; id++) {
169 if (entries[id] != NULL)
170 entries[id]->bit_value = entries[id]->id << mount_table->shift;
171 new_entries[id] = entries[id];
173 mount_table->table = new_entries;
175 afpd_mount_table = mount_table;