]>
git.saurik.com Git - apple/file_cmds.git/blob - mtree/spec.c
915858c93a75671b8b913887ecd9a5af8fd446d3
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>
53 int lineno
; /* Current spec line number. */
55 static void set(char *, NODE
*);
56 static void unset(char *, NODE
*);
59 mtree_readspec(FILE *fi
)
67 centry
= last
= root
= NULL
;
68 bzero(&ginfo
, sizeof(ginfo
));
70 for (lineno
= 1; fgets(buf
, sizeof(buf
), fi
);
71 ++lineno
, c_cur
= c_next
, c_next
= 0) {
72 /* Skip empty lines. */
76 /* Find end of line. */
77 if ((p
= index(buf
, '\n')) == NULL
)
78 errx(1, "line %d too long", lineno
);
80 /* See if next line is continuation line. */
86 /* Null-terminate the line. */
89 /* Skip leading whitespace. */
90 for (p
= buf
; *p
&& isspace(*p
); ++p
);
92 /* If nothing but whitespace or comment char, continue. */
97 (void)fprintf(stderr
, "line %d: {%s}\n", lineno
, p
);
104 /* Grab file name, "$", "set", or "unset". */
105 if ((p
= strtok(p
, "\n\t ")) == NULL
)
106 errx(1, "line %d: missing field", lineno
);
111 if (strcmp(p
+ 1, "set"))
116 if (strcmp(p
+ 1, "unset"))
123 errx(1, "line %d: slash character in file name",
126 if (!strcmp(p
, "..")) {
127 /* Don't go up, if haven't gone down. */
130 if (last
->type
!= F_DIR
|| last
->flags
& F_DONE
) {
135 last
->flags
|= F_DONE
;
138 noparent
: errx(1, "line %d: no parent node", lineno
);
141 if ((centry
= calloc(1, sizeof(NODE
) + strlen(p
))) == NULL
)
145 if (strpbrk(p
, MAGIC
))
146 centry
->flags
|= F_MAGIC
;
147 if (strunvis(centry
->name
, p
) == -1)
148 errx(1, "filename %s is ill-encoded", p
);
152 last
= root
= centry
;
154 } else if (last
->type
== F_DIR
&& !(last
->flags
& F_DONE
)) {
155 centry
->parent
= last
;
156 last
= last
->child
= centry
;
158 centry
->parent
= last
->parent
;
160 last
= last
->next
= centry
;
167 set(char *t
, NODE
*ip
)
170 char *kw
, *val
= NULL
;
177 for (; (kw
= strtok(t
, "= \t\n")); t
= NULL
) {
178 ip
->flags
|= type
= parsekey(kw
, &value
);
179 if ((value
== 0) || (val
= strtok(NULL
, " \t\n")) == NULL
)
180 errx(1, "line %d: missing value", lineno
);
183 ip
->cksum
= strtoul(val
, &ep
, 10);
185 errx(1, "line %d: invalid checksum %s",
189 ip
->md5digest
= strdup(val
);
194 ip
->sha1digest
= strdup(val
);
199 ip
->sha256digest
= strdup(val
);
200 if(!ip
->sha256digest
)
204 ip
->rmd160digest
= strdup(val
);
205 if(!ip
->rmd160digest
)
209 if (strcmp("none", val
) == 0)
211 else if (strtofflags(&val
, &ip
->st_flags
, NULL
) != 0)
212 errx(1, "line %d: invalid flag %s",lineno
, val
);
215 ip
->st_gid
= (gid_t
)strtoul(val
, &ep
, 10);
217 errx(1, "line %d: invalid gid %s", lineno
, val
);
220 if ((gr
= getgrnam(val
)) == NULL
)
221 errx(1, "line %d: unknown group %s", lineno
, val
);
222 ip
->st_gid
= gr
->gr_gid
;
225 /* just set flag bit */
228 if ((m
= setmode(val
)) == NULL
)
229 errx(1, "line %d: invalid file mode %s",
231 ip
->st_mode
= getmode(m
, 0);
235 ip
->st_nlink
= strtoul(val
, &ep
, 10);
237 errx(1, "line %d: invalid link count %s",
241 ip
->st_size
= strtoq(val
, &ep
, 10);
243 errx(1, "line %d: invalid size %s",
247 ip
->slink
= malloc(strlen(val
) + 1);
248 if (ip
->slink
== NULL
)
250 if (strunvis(ip
->slink
, val
) == -1)
251 errx(1, "symlink %s is ill-encoded", val
);
254 ip
->st_mtimespec
.tv_sec
= strtoul(val
, &ep
, 10);
256 errx(1, "line %d: invalid time %s",
259 ip
->st_mtimespec
.tv_nsec
= strtoul(val
, &ep
, 10);
261 errx(1, "line %d: invalid time %s",
267 if (!strcmp(val
, "block"))
271 if (!strcmp(val
, "char"))
275 if (!strcmp(val
, "dir"))
279 if (!strcmp(val
, "file"))
281 if (!strcmp(val
, "fifo"))
285 if (!strcmp(val
, "link"))
289 if (!strcmp(val
, "socket"))
293 errx(1, "line %d: unknown file type %s",
298 ip
->st_uid
= (uid_t
)strtoul(val
, &ep
, 10);
300 errx(1, "line %d: invalid uid %s", lineno
, val
);
303 if ((pw
= getpwnam(val
)) == NULL
)
304 errx(1, "line %d: unknown user %s", lineno
, val
);
305 ip
->st_uid
= pw
->pw_uid
;
312 unset(char *t
, NODE
*ip
)
316 while ((p
= strtok(t
, "\n\t ")))
317 ip
->flags
&= ~parsekey(p
, NULL
);