2 * Copyright (c) 2011 Marcel Moolenaar
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/queue.h>
33 #include <sys/types.h>
51 #define IS_DOT(nm) ((nm)[0] == '.' && (nm)[1] == '\0')
52 #define IS_DOTDOT(nm) ((nm)[0] == '.' && (nm)[1] == '.' && (nm)[2] == '\0')
54 struct mtree_fileinfo {
55 SLIST_ENTRY(mtree_fileinfo) next;
61 /* Global state used while parsing. */
62 static SLIST_HEAD(, mtree_fileinfo) mtree_fileinfo =
63 SLIST_HEAD_INITIALIZER(mtree_fileinfo);
64 static fsnode *mtree_root;
65 static fsnode *mtree_current;
66 static fsnode mtree_global;
67 static fsinode mtree_global_inode;
68 static u_int errors, warnings;
70 static void mtree_error(const char *, ...) __printflike(1, 2);
71 static void mtree_warning(const char *, ...) __printflike(1, 2);
74 mtree_file_push(const char *name, FILE *fp)
76 struct mtree_fileinfo *fi;
78 fi = malloc(sizeof(*fi));
82 if (strcmp(name, "-") == 0)
83 fi->name = strdup("(stdin)");
85 fi->name = strdup(name);
86 if (fi->name == NULL) {
94 SLIST_INSERT_HEAD(&mtree_fileinfo, fi, next);
99 mtree_print(const char *msgtype, const char *fmt, va_list ap)
101 struct mtree_fileinfo *fi;
103 if (msgtype != NULL) {
104 fi = SLIST_FIRST(&mtree_fileinfo);
106 fprintf(stderr, "%s:%u: ", fi->name, fi->line);
107 fprintf(stderr, "%s: ", msgtype);
109 vfprintf(stderr, fmt, ap);
113 mtree_error(const char *fmt, ...)
118 mtree_print("error", fmt, ap);
126 mtree_warning(const char *fmt, ...)
131 mtree_print("warning", fmt, ap);
138 #ifndef MAKEFS_MAX_TREE_DEPTH
139 # define MAKEFS_MAX_TREE_DEPTH (MAXPATHLEN/2)
142 /* construct path to node->name */
144 mtree_file_path(fsnode *node)
148 char *res, *rp[MAKEFS_MAX_TREE_DEPTH];
152 rp[depth] = node->name;
153 for (pnode = node->parent; pnode && depth < MAKEFS_MAX_TREE_DEPTH;
154 pnode = pnode->parent) {
155 if (strcmp(pnode->name, ".") == 0)
157 rp[++depth] = pnode->name;
160 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
166 sbuf_cat(sb, rp[depth--]);
169 sbuf_cat(sb, rp[depth]);
171 res = strdup(sbuf_data(sb));
179 /* mtree_resolve() sets errno to indicate why NULL was returned. */
181 mtree_resolve(const char *spec, int *istemp)
185 const char *base, *p, *v;
187 int c, error, quoted, subst;
195 c = (len > 1) ? (spec[0] == spec[len - 1]) ? spec[0] : 0 : 0;
196 *istemp = (c == '`') ? 1 : 0;
197 subst = (c == '`' || c == '"') ? 1 : 0;
198 quoted = (subst || c == '\'') ? 1 : 0;
201 res = strdup(spec + quoted);
202 if (res != NULL && quoted)
207 sb = sbuf_new_auto();
217 p = strchr(base, '$');
219 sbuf_bcat(sb, base, len);
224 /* The following is safe. spec always starts with a quote. */
228 sbuf_bcat(sb, base, p - base);
241 /* Handle ${X} vs $X. */
249 len -= (p + 1) - base;
258 var = calloc(p - v, 1);
262 memcpy(var, v + 1, p - v - 1);
263 if (strcmp(var, ".CURDIR") == 0) {
264 res = getcwd(NULL, 0);
267 } else if (strcmp(var, ".PROG") == 0) {
268 res = strdup(getprogname());
290 res = (error == 0) ? strdup(sbuf_data(sb)) : NULL;
298 skip_over(FILE *fp, const char *cs)
303 while (c != EOF && strchr(cs, c) != NULL)
309 return (ferror(fp) ? errno : -1);
313 skip_to(FILE *fp, const char *cs)
318 while (c != EOF && strchr(cs, c) == NULL)
324 return (ferror(fp) ? errno : -1);
328 read_word(FILE *fp, char *buf, size_t bufsz)
330 struct mtree_fileinfo *fi;
332 int c, done, error, esc, qlvl;
347 error = ferror(fp) ? errno : -1;
349 mtree_error("unexpected end of file");
364 } else if (c == buf[qidx]) {
369 } while (buf[qidx] != '`' &&
382 if (!esc && qlvl == 0) {
390 * We going to eat the newline ourselves.
393 mtree_warning("quoted word straddles "
395 fi = SLIST_FIRST(&mtree_fileinfo);
430 } while (idx < bufsz && !done);
433 mtree_error("word too long to fit buffer (max %zu characters)",
435 skip_to(fp, " \t\n");
441 create_node(const char *name, u_int type, fsnode *parent, fsnode *global)
445 n = calloc(1, sizeof(*n));
449 n->name = strdup(name);
450 if (n->name == NULL) {
455 n->type = (type == 0) ? global->type : type;
458 n->inode = calloc(1, sizeof(*n->inode));
459 if (n->inode == NULL) {
465 /* Assign global options/defaults. */
466 bcopy(global->inode, n->inode, sizeof(*n->inode));
467 n->inode->st.st_mode = (n->inode->st.st_mode & ~S_IFMT) | n->type;
469 if (n->type == S_IFLNK)
470 n->symlink = global->symlink;
471 else if (n->type == S_IFREG)
472 n->contents = global->contents;
478 destroy_node(fsnode *n)
482 assert(n->name != NULL);
483 assert(n->inode != NULL);
491 read_number(const char *tok, u_int base, intmax_t *res, intmax_t min,
497 val = strtoimax(tok, &end, base);
498 if (end == tok || end[0] != '\0')
500 if (val < min || val > max)
507 read_mtree_keywords(FILE *fp, fsnode *node)
509 char keyword[PATH_MAX];
510 char *name, *p, *value;
512 struct passwd *pwent;
516 int error, istemp, type;
518 st = &node->inode->st;
520 error = skip_over(fp, " \t");
524 error = read_word(fp, keyword, sizeof(keyword));
528 if (keyword[0] == '\0')
531 value = strchr(keyword, '=');
536 * We use EINVAL, ENOATTR, ENOSYS and ENXIO to signal
537 * certain conditions:
538 * EINVAL - Value provided for a keyword that does
539 * not take a value. The value is ignored.
540 * ENOATTR - Value missing for a keyword that needs
541 * a value. The keyword is ignored.
542 * ENOSYS - Unsupported keyword encountered. The
543 * keyword is ignored.
544 * ENXIO - Value provided for a keyword that does
545 * not take a value. The value is ignored.
547 switch (keyword[0]) {
549 if (strcmp(keyword, "contents") == 0) {
554 node->contents = strdup(value);
559 if (strcmp(keyword, "flags") == 0) {
565 if (!strtofflags(&value, &flset, &flclr)) {
566 st->st_flags &= ~flclr;
567 st->st_flags |= flset;
574 if (strcmp(keyword, "gid") == 0) {
579 error = read_number(value, 10, &num,
583 } else if (strcmp(keyword, "gname") == 0) {
588 grent = getgrnam(value);
590 st->st_gid = grent->gr_gid;
597 if (strcmp(keyword, "link") == 0) {
602 node->symlink = strdup(value);
607 if (strcmp(keyword, "mode") == 0) {
612 if (value[0] >= '0' && value[0] <= '9') {
613 error = read_number(value, 8, &num,
616 st->st_mode &= S_IFMT;
620 /* Symbolic mode not supported. */
628 if (strcmp(keyword, "optional") == 0) {
631 node->flags |= FSNODE_F_OPTIONAL;
636 if (strcmp(keyword, "size") == 0) {
641 error = read_number(value, 10, &num,
649 if (strcmp(keyword, "time") == 0) {
654 p = strchr(value, '.');
657 error = read_number(value, 10, &num, 0,
664 error = read_number(p, 10, &num, 0,
670 } else if (strcmp(keyword, "type") == 0) {
675 if (strcmp(value, "dir") == 0)
676 node->type = S_IFDIR;
677 else if (strcmp(value, "file") == 0)
678 node->type = S_IFREG;
679 else if (strcmp(value, "link") == 0)
680 node->type = S_IFLNK;
687 if (strcmp(keyword, "uid") == 0) {
692 error = read_number(value, 10, &num,
696 } else if (strcmp(keyword, "uname") == 0) {
701 pwent = getpwnam(value);
703 st->st_uid = pwent->pw_uid;
716 mtree_error("%s: invalid value '%s'", keyword, value);
719 mtree_error("%s: keyword needs a value", keyword);
722 mtree_warning("%s: unsupported keyword", keyword);
725 mtree_error("%s: keyword does not take a value",
734 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
736 /* Nothing more to do for the global defaults. */
737 if (node->name == NULL)
741 * Be intelligent about the file type.
743 if (node->contents != NULL) {
744 if (node->symlink != NULL) {
745 mtree_error("%s: both link and contents keywords "
746 "defined", node->name);
750 } else if (node->type != 0) {
752 if (type == S_IFREG) {
753 /* the named path is the default contents */
754 node->contents = mtree_file_path(node);
757 type = (node->symlink != NULL) ? S_IFLNK : S_IFDIR;
762 if (node->type != type) {
763 mtree_error("%s: file type and defined keywords to not match",
768 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
770 if (node->contents == NULL)
773 name = mtree_resolve(node->contents, &istemp);
777 if (stat(name, &sb) != 0) {
778 mtree_error("%s: contents file '%s' not found", node->name,
784 free(node->contents);
785 node->contents = name;
786 st->st_size = sb.st_size;
791 read_mtree_command(FILE *fp)
796 error = read_word(fp, cmd, sizeof(cmd));
800 error = read_mtree_keywords(fp, &mtree_global);
809 read_mtree_spec1(FILE *fp, bool def, const char *name)
811 fsnode *last, *node, *parent;
815 assert(name[0] != '\0');
818 * Treat '..' specially, because it only changes our current
819 * directory. We don't create a node for it. We simply ignore
820 * any keywords that may appear on the line as well.
821 * Going up a directory is a little non-obvious. A directory
822 * node has a corresponding '.' child. The parent of '.' is
823 * not the '.' node of the parent directory, but the directory
824 * node within the parent to which the child relates. However,
825 * going up a directory means we need to find the '.' node to
826 * which the directoy node is linked. This we can do via the
827 * first * pointer, because '.' is always the first entry in a
830 if (IS_DOTDOT(name)) {
831 /* This deals with NULL pointers as well. */
832 if (mtree_current == mtree_root) {
833 mtree_warning("ignoring .. in root directory");
837 node = mtree_current;
839 assert(node != NULL);
840 assert(IS_DOT(node->name));
841 assert(node->first == node);
843 /* Get the corresponding directory node in the parent. */
844 node = mtree_current->parent;
846 assert(node != NULL);
847 assert(!IS_DOT(node->name));
851 assert(node != NULL);
852 assert(IS_DOT(node->name));
853 assert(node->first == node);
855 mtree_current = node;
860 * If we don't have a current directory and the first specification
861 * (either implicit or defined) is not '.', then we need to create
862 * a '.' node first (using a recursive call).
864 if (!IS_DOT(name) && mtree_current == NULL) {
865 error = read_mtree_spec1(fp, false, ".");
871 * Lookup the name in the current directory (if we have a current
872 * directory) to make sure we do not create multiple nodes for the
873 * same component. For non-definitions, if we find a node with the
874 * same name, simply change the current directory. For definitions
878 node = mtree_current;
879 while (node != NULL) {
880 assert(node->first == mtree_current);
882 if (strcmp(name, node->name) == 0) {
884 mtree_error("duplicate definition of %s",
889 if (node->type != S_IFDIR) {
890 mtree_error("%s is not a directory", name);
894 assert(!IS_DOT(name));
898 assert(node != NULL);
899 assert(IS_DOT(node->name));
901 mtree_current = node;
909 parent = (mtree_current != NULL) ? mtree_current->parent : NULL;
910 type = (def == false || IS_DOT(name)) ? S_IFDIR : 0;
911 node = create_node(name, type, parent, &mtree_global);
916 error = read_mtree_keywords(fp, node);
923 node->first = (mtree_current != NULL) ? mtree_current : node;
928 if (node->type != S_IFDIR)
931 if (!IS_DOT(node->name)) {
933 node = create_node(".", S_IFDIR, parent, parent);
936 destroy_node(parent);
939 parent->child = node;
943 assert(node != NULL);
944 assert(IS_DOT(node->name));
945 assert(node->first == node);
947 mtree_current = node;
948 if (mtree_root == NULL)
955 read_mtree_spec(FILE *fp)
957 char pathspec[PATH_MAX];
961 error = read_word(fp, pathspec, sizeof(pathspec));
965 cp = strchr(pathspec, '/');
967 /* Absolute pathname */
968 mtree_current = mtree_root;
973 /* Disallow '.' and '..' as components. */
974 if (IS_DOT(pathspec) || IS_DOTDOT(pathspec)) {
975 mtree_error("absolute path cannot contain . "
980 /* Ignore multiple adjacent slashes. */
981 if (pathspec[0] != '\0')
982 error = read_mtree_spec1(fp, false, pathspec);
983 memmove(pathspec, cp, strlen(cp) + 1);
984 cp = strchr(pathspec, '/');
985 } while (!error && cp != NULL);
987 /* Disallow '.' and '..' as the last component. */
988 if (!error && (IS_DOT(pathspec) || IS_DOTDOT(pathspec))) {
989 mtree_error("absolute path cannot contain . or .. "
995 /* Ignore absolute specfications that end with a slash. */
996 if (!error && pathspec[0] != '\0')
997 error = read_mtree_spec1(fp, true, pathspec);
1006 read_mtree(const char *fname, fsnode *node)
1008 struct mtree_fileinfo *fi;
1012 /* We do not yet support nesting... */
1013 assert(node == NULL);
1015 if (strcmp(fname, "-") == 0)
1018 fp = fopen(fname, "r");
1020 err(1, "Can't open `%s'", fname);
1023 error = mtree_file_push(fname, fp);
1027 bzero(&mtree_global, sizeof(mtree_global));
1028 bzero(&mtree_global_inode, sizeof(mtree_global_inode));
1029 mtree_global.inode = &mtree_global_inode;
1030 mtree_global_inode.nlink = 1;
1031 mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime =
1032 mtree_global_inode.st.st_mtime = time(NULL);
1033 errors = warnings = 0;
1039 mtree_current = node;
1041 /* Start of a new line... */
1042 fi = SLIST_FIRST(&mtree_fileinfo);
1045 error = skip_over(fp, " \t");
1051 error = ferror(fp) ? errno : -1;
1056 case '\n': /* empty line */
1059 case '#': /* comment -- skip to end of line. */
1060 error = skip_to(fp, "\n");
1064 case '/': /* special commands */
1065 error = read_mtree_command(fp);
1067 default: /* specification */
1069 error = read_mtree_spec(fp);
1077 if (error <= 0 && (errors || warnings)) {
1078 warnx("%u error(s) and %u warning(s) in mtree manifest",
1086 errc(1, error, "Error reading mtree file");
1091 if (mtree_root != NULL)
1092 return (mtree_root);
1094 /* Handle empty specifications. */
1095 node = create_node(".", S_IFDIR, NULL, &mtree_global);