]>
git.saurik.com Git - apple/file_cmds.git/blob - mtree/spec.c
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid
[] = "@(#)spec.c 8.1 (Berkeley) 6/6/93";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.sbin/mtree/spec.c,v 1.22 2005/03/29 11:44:17 tobez Exp $");
38 #include <sys/types.h>
54 int lineno
; /* Current spec line number. */
56 static void set(char *, NODE
*);
57 static void unset(char *, NODE
*);
60 mtree_readspec(FILE *fi
)
68 centry
= last
= root
= NULL
;
69 bzero(&ginfo
, sizeof(ginfo
));
71 for (lineno
= 1; fgets(buf
, sizeof(buf
), fi
);
72 ++lineno
, c_cur
= c_next
, c_next
= 0) {
73 /* Skip empty lines. */
77 /* Find end of line. */
78 if ((p
= index(buf
, '\n')) == NULL
) {
79 RECORD_FAILURE(21, ERANGE
);
80 errx(1, "line %d too long", lineno
);
83 /* See if next line is continuation line. */
89 /* Null-terminate the line. */
92 /* Skip leading whitespace. */
93 for (p
= buf
; *p
&& isspace(*p
); ++p
);
95 /* If nothing but whitespace or comment char, continue. */
100 (void)fprintf(stderr
, "line %d: {%s}\n", lineno
, p
);
107 /* Grab file name, "$", "set", or "unset". */
108 if ((p
= strtok(p
, "\n\t ")) == NULL
) {
109 RECORD_FAILURE(22, EINVAL
);
110 errx(1, "line %d: missing field", lineno
);
116 if (strcmp(p
+ 1, "set"))
121 if (strcmp(p
+ 1, "unset"))
128 RECORD_FAILURE(23, EINVAL
);
129 errx(1, "line %d: slash character in file name",
133 if (!strcmp(p
, "..")) {
134 /* Don't go up, if haven't gone down. */
137 if (last
->type
!= F_DIR
|| last
->flags
& F_DONE
) {
142 last
->flags
|= F_DONE
;
145 noparent
: RECORD_FAILURE(24, EINVAL
);
146 errx(1, "line %d: no parent node", lineno
);
149 if ((centry
= calloc(1, sizeof(NODE
) + strlen(p
))) == NULL
) {
150 RECORD_FAILURE(25, ENOMEM
);
155 if (strpbrk(p
, MAGIC
))
156 centry
->flags
|= F_MAGIC
;
157 if (strunvis(centry
->name
, p
) == -1) {
158 RECORD_FAILURE(26, EILSEQ
);
159 errx(1, "filename %s is ill-encoded", p
);
164 last
= root
= centry
;
166 } else if (last
->type
== F_DIR
&& !(last
->flags
& F_DONE
)) {
167 centry
->parent
= last
;
168 last
= last
->child
= centry
;
170 centry
->parent
= last
->parent
;
172 last
= last
->next
= centry
;
179 set(char *t
, NODE
*ip
)
183 char *kw
, *val
= NULL
;
191 for (; (kw
= strtok(t
, "= \t\n")); t
= NULL
) {
192 ip
->flags
|= type
= parsekey(kw
, &value
);
193 if ((value
== 0) || (val
= strtok(NULL
, " \t\n")) == NULL
) {
194 RECORD_FAILURE(27, EINVAL
);
195 errx(1, "line %d: missing value", lineno
);
199 ip
->cksum
= strtoul(val
, &ep
, 10);
201 RECORD_FAILURE(28, EINVAL
);
202 errx(1, "line %d: invalid checksum %s",
207 ip
->md5digest
= strdup(val
);
208 if (!ip
->md5digest
) {
209 RECORD_FAILURE(29, ENOMEM
);
214 ip
->sha1digest
= strdup(val
);
215 if (!ip
->sha1digest
) {
216 RECORD_FAILURE(30, ENOMEM
);
221 ip
->sha256digest
= strdup(val
);
222 if (!ip
->sha256digest
) {
223 RECORD_FAILURE(31, ENOMEM
);
228 ip
->rmd160digest
= strdup(val
);
229 if (!ip
->rmd160digest
) {
230 RECORD_FAILURE(32, ENOMEM
);
235 if (strcmp("none", val
) == 0) {
237 } else if (strtofflags(&val
, &ip
->st_flags
, NULL
) != 0) {
238 RECORD_FAILURE(33, EINVAL
);
239 errx(1, "line %d: invalid flag %s",lineno
, val
);
243 ip
->st_gid
= (gid_t
)strtoul(val
, &ep
, 10);
245 RECORD_FAILURE(34, EINVAL
);
246 errx(1, "line %d: invalid gid %s", lineno
, val
);
250 if ((gr
= getgrnam(val
)) == NULL
) {
251 RECORD_FAILURE(35, EINVAL
);
252 errx(1, "line %d: unknown group %s", lineno
, val
);
254 ip
->st_gid
= gr
->gr_gid
;
257 /* just set flag bit */
260 if ((m
= setmode(val
)) == NULL
) {
261 RECORD_FAILURE(36, EINVAL
);
262 errx(1, "line %d: invalid file mode %s",
265 ip
->st_mode
= getmode(m
, 0);
269 ip
->st_nlink
= strtoul(val
, &ep
, 10);
271 RECORD_FAILURE(37, EINVAL
);
272 errx(1, "line %d: invalid link count %s",
277 ip
->st_size
= strtoq(val
, &ep
, 10);
279 RECORD_FAILURE(38, EINVAL
);
280 errx(1, "line %d: invalid size %s",
285 ip
->slink
= malloc(strlen(val
) + 1);
286 if (ip
->slink
== NULL
) {
287 RECORD_FAILURE(39, ENOMEM
);
290 if (strunvis(ip
->slink
, val
) == -1) {
291 RECORD_FAILURE(40, EILSEQ
);
292 errx(1, "symlink %s is ill-encoded", val
);
296 ip
->st_mtimespec
.tv_sec
= strtoul(val
, &ep
, 10);
298 RECORD_FAILURE(41, EINVAL
);
299 errx(1, "line %d: invalid time %s",
303 ip
->st_mtimespec
.tv_nsec
= strtoul(val
, &ep
, 10);
305 RECORD_FAILURE(42, EINVAL
);
306 errx(1, "line %d: invalid time %s",
313 if (!strcmp(val
, "block"))
317 if (!strcmp(val
, "char"))
321 if (!strcmp(val
, "dir"))
325 if (!strcmp(val
, "file"))
327 if (!strcmp(val
, "fifo"))
331 if (!strcmp(val
, "link"))
335 if (!strcmp(val
, "socket"))
339 RECORD_FAILURE(43, EINVAL
);
340 errx(1, "line %d: unknown file type %s",
345 ip
->st_uid
= (uid_t
)strtoul(val
, &ep
, 10);
347 RECORD_FAILURE(44, EINVAL
);
348 errx(1, "line %d: invalid uid %s", lineno
, val
);
352 if ((pw
= getpwnam(val
)) == NULL
) {
353 RECORD_FAILURE(45, EINVAL
);
354 errx(1, "line %d: unknown user %s", lineno
, val
);
356 ip
->st_uid
= pw
->pw_uid
;
359 ip
->st_birthtimespec
.tv_sec
= strtoul(val
, &ep
, 10);
361 RECORD_FAILURE(46, EINVAL
);
362 errx(1, "line %d: invalid time %s",
366 ip
->st_birthtimespec
.tv_nsec
= strtoul(val
, &ep
, 10);
368 RECORD_FAILURE(47, EINVAL
);
369 errx(1, "line %d: invalid time %s",
374 ip
->st_atimespec
.tv_sec
= strtoul(val
, &ep
, 10);
376 RECORD_FAILURE(48, EINVAL
);
377 errx(1, "line %d: invalid time %s",
381 ip
->st_atimespec
.tv_nsec
= strtoul(val
, &ep
, 10);
383 RECORD_FAILURE(49, EINVAL
);
384 errx(1, "line %d: invalid time %s",
389 ip
->st_ctimespec
.tv_sec
= strtoul(val
, &ep
, 10);
391 RECORD_FAILURE(50, EINVAL
);
392 errx(1, "line %d: invalid time %s",
396 ip
->st_ctimespec
.tv_nsec
= strtoul(val
, &ep
, 10);
398 RECORD_FAILURE(51, EINVAL
);
399 errx(1, "line %d: invalid time %s",
404 ip
->st_ptimespec
.tv_sec
= strtoul(val
, &ep
, 10);
406 RECORD_FAILURE(52, EINVAL
);
407 errx(1, "line %d: invalid time %s",
411 ip
->st_ptimespec
.tv_nsec
= strtoul(val
, &ep
, 10);
413 RECORD_FAILURE(53, EINVAL
);
414 errx(1, "line %d: invalid time %s",
420 * Note this is nested inside an strtok loop,
421 * strtok_r must be used to preserve the strtok context
424 ep
= strtok_r(val
,".", &l
);
425 ip
->xattrsdigest
= strdup(ep
);
426 if (!ip
->xattrsdigest
) {
428 RECORD_FAILURE(54, error
);
429 errc(1, error
, "strdup");
431 val
= strtok_r(NULL
,".", &l
);
433 ip
->xdstream_priv_id
= strtoull(val
, &ep
, 10);
435 RECORD_FAILURE(55, EINVAL
);
436 errx(1, "line %d: invalid private id %s",
440 ip
->xdstream_priv_id
= 0;
444 ip
->st_ino
= (ino_t
)strtoull(val
, &ep
, 10);
446 RECORD_FAILURE(56, EINVAL
);
447 errx(1, "line %d: invalid inode %s",
452 ip
->acldigest
= strdup(val
);
453 if (!ip
->acldigest
) {
455 RECORD_FAILURE(57, error
);
456 errc(1, error
, "strdup");
460 ip
->sibling_id
= (quad_t
)strtoull(val
, &ep
, 10);
462 RECORD_FAILURE(58, EINVAL
);
463 errx(1, "line %d: invalid sibling id %s", lineno
, val
);
470 unset(char *t
, NODE
*ip
)
474 while ((p
= strtok(t
, "\n\t ")))
475 ip
->flags
&= ~parsekey(p
, NULL
);