]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/parse_mtab.c
AC_HEADER_STDC autoconf change
[netatalk.git] / etc / afpd / parse_mtab.c
1 /*
2  * $Id: parse_mtab.c,v 1.4 2001-09-06 20:00:59 rufustfirefly Exp $
3  *
4  * afpd_mtab_parse & support.  -- rgr, 9-Apr-01.
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif /* HAVE_CONFIG_H */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <errno.h>
14
15 /* STDC check */
16 #if STDC_HEADERS
17 #include <string.h>
18 #else /* STDC_HEADERS */
19 #ifndef HAVE_STRCHR
20 #define strchr index
21 #define strrchr index
22 #endif /* HAVE_STRCHR */
23 char *strchr (), *strrchr ();
24 #ifndef HAVE_MEMCPY
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 */
29
30 #include <sys/stat.h>
31
32 #include "directory.h"
33 #include "parse_mtab.h"
34
35 #define MAX_AFPD_MTAB_ENTRIES 256       /* unreasonably large number */
36 #define MTAB_DELIM_CHARS " \t\n"        /* delimiters for parsing */
37
38 #ifndef COPY_STRING
39 #define COPY_STRING(lval, str) \
40           (lval) = malloc(1+strlen(str)), strcpy((lval), (str))
41 #endif /* COPY_STRING */
42
43 static int
44 ceil_log_2 __P((int n))
45      /* Return the number of bits required to represent n.  Only works for
46         positive n.  */
47 {
48   int n_bits = 0;
49
50   while (n) {
51     n >>= 1;
52     n_bits += 1;
53   }
54   return(n_bits);
55 }
56
57 unsigned int
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.  */
61 {
62   int id;
63   struct afpd_mtab_entry *entry;
64
65   if (afpd_mount_table != NULL) {
66     for (id = 0; id < afpd_mount_table->size; id++) {
67       entry = afpd_mount_table->table[id];
68       if (entry != NULL
69           && entry->dev_major == major(st->st_dev)
70           && entry->dev_minor == minor(st->st_dev)) {
71         return(entry->bit_value | st->st_ino);
72       }
73     }
74   }
75
76   /* Fallback. */
77   return(st->st_ino);
78 }
79
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.
85
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.]
88         */
89 {
90   struct afpd_mtab_entry *entries[MAX_AFPD_MTAB_ENTRIES];       /* temp */
91   int id, max_id = 0, n_errors = 0;
92   struct stat st;
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");
96
97   if (f == NULL) {
98     fprintf(stderr, "Error:  Can't open %s:  code %d\n",
99             file_name, errno);
100     return(NULL);
101   }
102   for (id = 0; id < MAX_AFPD_MTAB_ENTRIES; id++)
103     entries[id] = NULL;
104   while (fgets(line, sizeof(line), f) != NULL) {
105     line_number++;
106     /* flush comment */
107     if ((p = strchr(line, '#')) != NULL)
108       *p = '\0';
109     /* get fields */
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.]  */
115     }
116     else if (id = strtol(index_tok, &p, 10),
117              *p != '\0') {
118       fprintf(stderr, "afpd:%s:%d:  Non-integer device index value '%s'.\n",
119               file_name, line_number, index_tok);
120       n_errors++;
121     }
122     else if (id < 1 || id > MAX_AFPD_MTAB_ENTRIES) {
123       fprintf(stderr,
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);
126       n_errors++;
127     }
128     else if (entries[id] != NULL) {
129       /* not unique. */
130       fprintf(stderr,
131               "afpd:%s:%d:  Id %d is already taken for %s.\n",
132               file_name, line_number, id, entries[id]->device);
133       n_errors++;
134     }
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);
138       n_errors++;
139     }
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);
143       n_errors++;
144     }
145     else {
146       /* make a new entry */
147       struct afpd_mtab_entry *entry
148         = (struct afpd_mtab_entry *) malloc(sizeof(struct afpd_mtab_entry));
149
150       entry->id = id;
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);
155       entries[id] = entry;
156       if (id > max_id)
157         max_id = id;
158     }
159   } /* next line */
160
161   fclose(f);
162   if (n_errors) {
163     fprintf(stderr, "Got %d errors while reading %s; exiting.\n",
164             n_errors, file_name);
165     return(NULL);
166   }
167   else {
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);
172     int table_size;
173     struct afpd_mtab_entry **new_entries;
174
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];
187     }
188     mount_table->table = new_entries;
189
190     afpd_mount_table = mount_table;
191     return(mount_table);
192   }
193 }