]>
Commit | Line | Data |
---|---|---|
44a7a5ab A |
1 | /*- |
2 | * Copyright (c) 1989, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
44a7a5ab A |
34 | #ifndef lint |
35 | #if 0 | |
440bd198 | 36 | static char sccsid[] = "@(#)spec.c 8.1 (Berkeley) 6/6/93"; |
44a7a5ab | 37 | #endif |
440bd198 A |
38 | static const char rcsid[] = |
39 | "$FreeBSD: src/usr.sbin/mtree/spec.c,v 1.13.2.1 2000/06/28 02:33:17 joe Exp $"; | |
44a7a5ab A |
40 | #endif /* not lint */ |
41 | ||
42 | #include <sys/types.h> | |
43 | #include <sys/stat.h> | |
44 | #include <ctype.h> | |
440bd198 | 45 | #include <err.h> |
44a7a5ab A |
46 | #include <errno.h> |
47 | #include <fts.h> | |
48 | #include <grp.h> | |
49 | #include <pwd.h> | |
50 | #include <stdio.h> | |
44a7a5ab | 51 | #include <unistd.h> |
440bd198 | 52 | #include <vis.h> |
44a7a5ab A |
53 | #include "mtree.h" |
54 | #include "extern.h" | |
55 | ||
56 | int lineno; /* Current spec line number. */ | |
57 | ||
58 | static void set __P((char *, NODE *)); | |
59 | static void unset __P((char *, NODE *)); | |
60 | ||
61 | NODE * | |
62 | spec() | |
63 | { | |
440bd198 A |
64 | register NODE *centry, *last; |
65 | register char *p; | |
44a7a5ab A |
66 | NODE ginfo, *root; |
67 | int c_cur, c_next; | |
68 | char buf[2048]; | |
69 | ||
440bd198 A |
70 | centry = last = root = NULL; |
71 | bzero(&ginfo, sizeof(ginfo)); | |
44a7a5ab A |
72 | c_cur = c_next = 0; |
73 | for (lineno = 1; fgets(buf, sizeof(buf), stdin); | |
74 | ++lineno, c_cur = c_next, c_next = 0) { | |
75 | /* Skip empty lines. */ | |
76 | if (buf[0] == '\n') | |
77 | continue; | |
78 | ||
79 | /* Find end of line. */ | |
440bd198 A |
80 | if ((p = index(buf, '\n')) == NULL) |
81 | errx(1, "line %d too long", lineno); | |
44a7a5ab A |
82 | |
83 | /* See if next line is continuation line. */ | |
84 | if (p[-1] == '\\') { | |
85 | --p; | |
86 | c_next = 1; | |
87 | } | |
88 | ||
89 | /* Null-terminate the line. */ | |
90 | *p = '\0'; | |
91 | ||
92 | /* Skip leading whitespace. */ | |
93 | for (p = buf; *p && isspace(*p); ++p); | |
94 | ||
95 | /* If nothing but whitespace or comment char, continue. */ | |
96 | if (!*p || *p == '#') | |
97 | continue; | |
98 | ||
99 | #ifdef DEBUG | |
100 | (void)fprintf(stderr, "line %d: {%s}\n", lineno, p); | |
101 | #endif | |
102 | if (c_cur) { | |
103 | set(p, centry); | |
104 | continue; | |
105 | } | |
440bd198 | 106 | |
44a7a5ab A |
107 | /* Grab file name, "$", "set", or "unset". */ |
108 | if ((p = strtok(p, "\n\t ")) == NULL) | |
440bd198 | 109 | errx(1, "line %d: missing field", lineno); |
44a7a5ab A |
110 | |
111 | if (p[0] == '/') | |
112 | switch(p[1]) { | |
113 | case 's': | |
114 | if (strcmp(p + 1, "set")) | |
115 | break; | |
116 | set(NULL, &ginfo); | |
117 | continue; | |
118 | case 'u': | |
119 | if (strcmp(p + 1, "unset")) | |
120 | break; | |
121 | unset(NULL, &ginfo); | |
122 | continue; | |
123 | } | |
124 | ||
440bd198 A |
125 | if (index(p, '/')) |
126 | errx(1, "line %d: slash character in file name", | |
127 | lineno); | |
44a7a5ab A |
128 | |
129 | if (!strcmp(p, "..")) { | |
130 | /* Don't go up, if haven't gone down. */ | |
131 | if (!root) | |
132 | goto noparent; | |
133 | if (last->type != F_DIR || last->flags & F_DONE) { | |
134 | if (last == root) | |
135 | goto noparent; | |
136 | last = last->parent; | |
137 | } | |
138 | last->flags |= F_DONE; | |
139 | continue; | |
140 | ||
440bd198 | 141 | noparent: errx(1, "line %d: no parent node", lineno); |
44a7a5ab A |
142 | } |
143 | ||
144 | if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL) | |
440bd198 | 145 | errx(1, "calloc"); |
44a7a5ab | 146 | *centry = ginfo; |
44a7a5ab A |
147 | #define MAGIC "?*[" |
148 | if (strpbrk(p, MAGIC)) | |
149 | centry->flags |= F_MAGIC; | |
440bd198 A |
150 | if (strunvis(centry->name, p) == -1) { |
151 | warnx("filename %s is ill-encoded and literally used", | |
152 | p); | |
153 | strcpy(centry->name, p); | |
154 | } | |
44a7a5ab A |
155 | set(NULL, centry); |
156 | ||
157 | if (!root) { | |
158 | last = root = centry; | |
159 | root->parent = root; | |
160 | } else if (last->type == F_DIR && !(last->flags & F_DONE)) { | |
161 | centry->parent = last; | |
162 | last = last->child = centry; | |
163 | } else { | |
164 | centry->parent = last->parent; | |
165 | centry->prev = last; | |
166 | last = last->next = centry; | |
167 | } | |
168 | } | |
169 | return (root); | |
170 | } | |
171 | ||
172 | static void | |
173 | set(t, ip) | |
174 | char *t; | |
175 | NODE *ip; | |
176 | { | |
440bd198 A |
177 | register int type; |
178 | char *kw, *val = NULL; | |
44a7a5ab A |
179 | struct group *gr; |
180 | struct passwd *pw; | |
181 | mode_t *m; | |
182 | int value; | |
183 | char *ep; | |
184 | ||
440bd198 | 185 | for (; (kw = strtok(t, "= \t\n")); t = NULL) { |
44a7a5ab A |
186 | ip->flags |= type = parsekey(kw, &value); |
187 | if (value && (val = strtok(NULL, " \t\n")) == NULL) | |
440bd198 | 188 | errx(1, "line %d: missing value", lineno); |
44a7a5ab A |
189 | switch(type) { |
190 | case F_CKSUM: | |
191 | ip->cksum = strtoul(val, &ep, 10); | |
192 | if (*ep) | |
440bd198 A |
193 | errx(1, "line %d: invalid checksum %s", |
194 | lineno, val); | |
195 | break; | |
196 | case F_MD5: | |
197 | ip->md5digest = strdup(val); | |
198 | if(!ip->md5digest) { | |
199 | errx(1, "strdup"); | |
200 | } | |
44a7a5ab | 201 | break; |
440bd198 A |
202 | case F_SHA1: |
203 | ip->sha1digest = strdup(val); | |
204 | if(!ip->sha1digest) { | |
205 | errx(1, "strdup"); | |
206 | } | |
207 | break; | |
208 | case F_RMD160: | |
209 | ip->rmd160digest = strdup(val); | |
210 | if(!ip->rmd160digest) { | |
211 | errx(1, "strdup"); | |
212 | } | |
213 | break; | |
214 | case F_FLAGS: | |
215 | if (strcmp("none", val) == 0) | |
216 | ip->st_flags = 0; | |
217 | else if (strtofflags(&val, &ip->st_flags, NULL) != 0) | |
218 | errx(1, "line %d: invalid flag %s",lineno, val); | |
219 | break; | |
44a7a5ab | 220 | case F_GID: |
440bd198 | 221 | ip->st_gid = strtoul(val, &ep, 10); |
44a7a5ab | 222 | if (*ep) |
440bd198 | 223 | errx(1, "line %d: invalid gid %s", lineno, val); |
44a7a5ab A |
224 | break; |
225 | case F_GNAME: | |
226 | if ((gr = getgrnam(val)) == NULL) | |
440bd198 | 227 | errx(1, "line %d: unknown group %s", lineno, val); |
44a7a5ab A |
228 | ip->st_gid = gr->gr_gid; |
229 | break; | |
230 | case F_IGN: | |
231 | /* just set flag bit */ | |
232 | break; | |
233 | case F_MODE: | |
234 | if ((m = setmode(val)) == NULL) | |
440bd198 A |
235 | errx(1, "line %d: invalid file mode %s", |
236 | lineno, val); | |
44a7a5ab A |
237 | ip->st_mode = getmode(m, 0); |
238 | free(m); | |
239 | break; | |
240 | case F_NLINK: | |
440bd198 | 241 | ip->st_nlink = strtoul(val, &ep, 10); |
44a7a5ab | 242 | if (*ep) |
440bd198 A |
243 | errx(1, "line %d: invalid link count %s", |
244 | lineno, val); | |
44a7a5ab A |
245 | break; |
246 | case F_SIZE: | |
440bd198 | 247 | ip->st_size = strtoq(val, &ep, 10); |
44a7a5ab | 248 | if (*ep) |
440bd198 A |
249 | errx(1, "line %d: invalid size %s", |
250 | lineno, val); | |
44a7a5ab A |
251 | break; |
252 | case F_SLINK: | |
253 | if ((ip->slink = strdup(val)) == NULL) | |
440bd198 | 254 | errx(1, "strdup"); |
44a7a5ab A |
255 | break; |
256 | case F_TIME: | |
440bd198 | 257 | ip->st_mtimespec.tv_sec = strtoul(val, &ep, 10); |
44a7a5ab | 258 | if (*ep != '.') |
440bd198 A |
259 | errx(1, "line %d: invalid time %s", |
260 | lineno, val); | |
44a7a5ab | 261 | val = ep + 1; |
440bd198 | 262 | ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10); |
44a7a5ab | 263 | if (*ep) |
440bd198 A |
264 | errx(1, "line %d: invalid time %s", |
265 | lineno, val); | |
44a7a5ab A |
266 | break; |
267 | case F_TYPE: | |
268 | switch(*val) { | |
269 | case 'b': | |
270 | if (!strcmp(val, "block")) | |
271 | ip->type = F_BLOCK; | |
272 | break; | |
273 | case 'c': | |
274 | if (!strcmp(val, "char")) | |
275 | ip->type = F_CHAR; | |
276 | break; | |
277 | case 'd': | |
278 | if (!strcmp(val, "dir")) | |
279 | ip->type = F_DIR; | |
280 | break; | |
281 | case 'f': | |
282 | if (!strcmp(val, "file")) | |
283 | ip->type = F_FILE; | |
284 | if (!strcmp(val, "fifo")) | |
285 | ip->type = F_FIFO; | |
286 | break; | |
287 | case 'l': | |
288 | if (!strcmp(val, "link")) | |
289 | ip->type = F_LINK; | |
290 | break; | |
291 | case 's': | |
292 | if (!strcmp(val, "socket")) | |
293 | ip->type = F_SOCK; | |
294 | break; | |
295 | default: | |
440bd198 A |
296 | errx(1, "line %d: unknown file type %s", |
297 | lineno, val); | |
44a7a5ab A |
298 | } |
299 | break; | |
300 | case F_UID: | |
440bd198 | 301 | ip->st_uid = strtoul(val, &ep, 10); |
44a7a5ab | 302 | if (*ep) |
440bd198 | 303 | errx(1, "line %d: invalid uid %s", lineno, val); |
44a7a5ab A |
304 | break; |
305 | case F_UNAME: | |
306 | if ((pw = getpwnam(val)) == NULL) | |
440bd198 | 307 | errx(1, "line %d: unknown user %s", lineno, val); |
44a7a5ab A |
308 | ip->st_uid = pw->pw_uid; |
309 | break; | |
310 | } | |
311 | } | |
312 | } | |
313 | ||
314 | static void | |
315 | unset(t, ip) | |
316 | char *t; | |
440bd198 | 317 | register NODE *ip; |
44a7a5ab | 318 | { |
440bd198 | 319 | register char *p; |
44a7a5ab | 320 | |
440bd198 | 321 | while ((p = strtok(t, "\n\t "))) |
44a7a5ab A |
322 | ip->flags &= ~parsekey(p, NULL); |
323 | } |