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 - 1;
154 pnode = pnode->parent) {
155 if (strcmp(pnode->name, ".") == 0)
157 rp[++depth] = pnode->name;
160 sb = sbuf_new_auto();
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)
184 char *res, *var = NULL;
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());
292 res = (error == 0) ? strdup(sbuf_data(sb)) : NULL;
300 skip_over(FILE *fp, const char *cs)
305 while (c != EOF && strchr(cs, c) != NULL)
311 return (ferror(fp) ? errno : -1);
315 skip_to(FILE *fp, const char *cs)
320 while (c != EOF && strchr(cs, c) == NULL)
326 return (ferror(fp) ? errno : -1);
330 read_word(FILE *fp, char *buf, size_t bufsz)
332 struct mtree_fileinfo *fi;
334 int c, done, error, esc, qlvl;
349 error = ferror(fp) ? errno : -1;
351 mtree_error("unexpected end of file");
353 case '#': /* comment -- skip to end of line. */
355 error = skip_to(fp, "\n");
373 } else if (c == buf[qidx]) {
378 } while (buf[qidx] != '`' &&
391 if (!esc && qlvl == 0) {
399 * We going to eat the newline ourselves.
402 mtree_warning("quoted word straddles "
404 fi = SLIST_FIRST(&mtree_fileinfo);
439 } while (idx < bufsz && !done);
442 mtree_error("word too long to fit buffer (max %zu characters)",
444 skip_to(fp, " \t\n");
450 create_node(const char *name, u_int type, fsnode *parent, fsnode *global)
454 n = calloc(1, sizeof(*n));
458 n->name = strdup(name);
459 if (n->name == NULL) {
464 n->type = (type == 0) ? global->type : type;
467 n->inode = calloc(1, sizeof(*n->inode));
468 if (n->inode == NULL) {
474 /* Assign global options/defaults. */
475 bcopy(global->inode, n->inode, sizeof(*n->inode));
476 n->inode->st.st_mode = (n->inode->st.st_mode & ~S_IFMT) | n->type;
478 if (n->type == S_IFLNK)
479 n->symlink = global->symlink;
480 else if (n->type == S_IFREG)
481 n->contents = global->contents;
487 destroy_node(fsnode *n)
491 assert(n->name != NULL);
492 assert(n->inode != NULL);
500 read_number(const char *tok, u_int base, intmax_t *res, intmax_t min,
506 val = strtoimax(tok, &end, base);
507 if (end == tok || end[0] != '\0')
509 if (val < min || val > max)
516 read_mtree_keywords(FILE *fp, fsnode *node)
518 char keyword[PATH_MAX];
519 char *name, *p, *value;
525 int error, istemp, type;
527 st = &node->inode->st;
529 error = skip_over(fp, " \t");
533 error = read_word(fp, keyword, sizeof(keyword));
537 if (keyword[0] == '\0')
540 value = strchr(keyword, '=');
545 * We use EINVAL, ENOATTR, ENOSYS and ENXIO to signal
546 * certain conditions:
547 * EINVAL - Value provided for a keyword that does
548 * not take a value. The value is ignored.
549 * ENOATTR - Value missing for a keyword that needs
550 * a value. The keyword is ignored.
551 * ENOSYS - Unsupported keyword encountered. The
552 * keyword is ignored.
553 * ENXIO - Value provided for a keyword that does
554 * not take a value. The value is ignored.
556 switch (keyword[0]) {
558 if (strcmp(keyword, "contents") == 0) {
563 node->contents = strdup(value);
568 if (strcmp(keyword, "flags") == 0) {
574 if (!strtofflags(&value, &flset, &flclr)) {
575 st->st_flags &= ~flclr;
576 st->st_flags |= flset;
583 if (strcmp(keyword, "gid") == 0) {
588 error = read_number(value, 10, &num,
592 } else if (strcmp(keyword, "gname") == 0) {
597 if (gid_from_group(value, &gid) == 0)
605 if (strcmp(keyword, "link") == 0) {
610 node->symlink = strdup(value);
615 if (strcmp(keyword, "mode") == 0) {
620 if (value[0] >= '0' && value[0] <= '9') {
621 error = read_number(value, 8, &num,
624 st->st_mode &= S_IFMT;
628 /* Symbolic mode not supported. */
636 if (strcmp(keyword, "optional") == 0) {
639 node->flags |= FSNODE_F_OPTIONAL;
644 if (strcmp(keyword, "size") == 0) {
649 error = read_number(value, 10, &num,
657 if (strcmp(keyword, "time") == 0) {
662 p = strchr(value, '.');
665 error = read_number(value, 10, &num, 0,
674 error = read_number(p, 10, &num, 0,
680 } else if (strcmp(keyword, "type") == 0) {
685 if (strcmp(value, "dir") == 0)
686 node->type = S_IFDIR;
687 else if (strcmp(value, "file") == 0)
688 node->type = S_IFREG;
689 else if (strcmp(value, "link") == 0)
690 node->type = S_IFLNK;
697 if (strcmp(keyword, "uid") == 0) {
702 error = read_number(value, 10, &num,
706 } else if (strcmp(keyword, "uname") == 0) {
711 if (uid_from_user(value, &uid) == 0)
725 mtree_error("%s: invalid value '%s'", keyword, value);
728 mtree_error("%s: keyword needs a value", keyword);
731 mtree_warning("%s: unsupported keyword", keyword);
734 mtree_error("%s: keyword does not take a value",
743 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
745 /* Nothing more to do for the global defaults. */
746 if (node->name == NULL)
750 * Be intelligent about the file type.
752 if (node->contents != NULL) {
753 if (node->symlink != NULL) {
754 mtree_error("%s: both link and contents keywords "
755 "defined", node->name);
759 } else if (node->type != 0) {
761 if (type == S_IFREG) {
762 /* the named path is the default contents */
763 node->contents = mtree_file_path(node);
766 type = (node->symlink != NULL) ? S_IFLNK : S_IFDIR;
771 if (node->type != type) {
772 mtree_error("%s: file type and defined keywords to not match",
777 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
779 if (node->contents == NULL)
782 name = mtree_resolve(node->contents, &istemp);
786 if (stat(name, &sb) != 0) {
787 mtree_error("%s: contents file '%s' not found", node->name,
794 * Check for hardlinks. If the contents key is used, then the check
795 * will only trigger if the contents file is a link even if it is used
796 * by more than one file
798 if (sb.st_nlink > 1) {
801 st->st_ino = sb.st_ino;
802 st->st_dev = sb.st_dev;
803 curino = link_check(node->inode);
804 if (curino != NULL) {
806 node->inode = curino;
807 node->inode->nlink++;
811 free(node->contents);
812 node->contents = name;
813 st->st_size = sb.st_size;
818 read_mtree_command(FILE *fp)
823 error = read_word(fp, cmd, sizeof(cmd));
827 error = read_mtree_keywords(fp, &mtree_global);
836 read_mtree_spec1(FILE *fp, bool def, const char *name)
838 fsnode *last, *node, *parent;
842 assert(name[0] != '\0');
845 * Treat '..' specially, because it only changes our current
846 * directory. We don't create a node for it. We simply ignore
847 * any keywords that may appear on the line as well.
848 * Going up a directory is a little non-obvious. A directory
849 * node has a corresponding '.' child. The parent of '.' is
850 * not the '.' node of the parent directory, but the directory
851 * node within the parent to which the child relates. However,
852 * going up a directory means we need to find the '.' node to
853 * which the directoy node is linked. This we can do via the
854 * first * pointer, because '.' is always the first entry in a
857 if (IS_DOTDOT(name)) {
858 /* This deals with NULL pointers as well. */
859 if (mtree_current == mtree_root) {
860 mtree_warning("ignoring .. in root directory");
864 node = mtree_current;
866 assert(node != NULL);
867 assert(IS_DOT(node->name));
868 assert(node->first == node);
870 /* Get the corresponding directory node in the parent. */
871 node = mtree_current->parent;
873 assert(node != NULL);
874 assert(!IS_DOT(node->name));
878 assert(node != NULL);
879 assert(IS_DOT(node->name));
880 assert(node->first == node);
882 mtree_current = node;
887 * If we don't have a current directory and the first specification
888 * (either implicit or defined) is not '.', then we need to create
889 * a '.' node first (using a recursive call).
891 if (!IS_DOT(name) && mtree_current == NULL) {
892 error = read_mtree_spec1(fp, false, ".");
898 * Lookup the name in the current directory (if we have a current
899 * directory) to make sure we do not create multiple nodes for the
900 * same component. For non-definitions, if we find a node with the
901 * same name, simply change the current directory. For definitions
905 node = mtree_current;
906 while (node != NULL) {
907 assert(node->first == mtree_current);
909 if (strcmp(name, node->name) == 0) {
913 "duplicate definition of %s",
917 "duplicate definition of %s",
922 if (node->type != S_IFDIR) {
923 mtree_error("%s is not a directory", name);
927 assert(!IS_DOT(name));
931 assert(node != NULL);
932 assert(IS_DOT(node->name));
934 mtree_current = node;
942 parent = (mtree_current != NULL) ? mtree_current->parent : NULL;
943 type = (def == false || IS_DOT(name)) ? S_IFDIR : 0;
944 node = create_node(name, type, parent, &mtree_global);
949 error = read_mtree_keywords(fp, node);
956 node->first = (mtree_current != NULL) ? mtree_current : node;
961 if (node->type != S_IFDIR)
964 if (!IS_DOT(node->name)) {
966 node = create_node(".", S_IFDIR, parent, parent);
969 destroy_node(parent);
972 parent->child = node;
976 assert(node != NULL);
977 assert(IS_DOT(node->name));
978 assert(node->first == node);
980 mtree_current = node;
981 if (mtree_root == NULL)
988 read_mtree_spec(FILE *fp)
990 char pathspec[PATH_MAX];
994 error = read_word(fp, pathspec, sizeof(pathspec));
998 cp = strchr(pathspec, '/');
1000 /* Absolute pathname */
1001 mtree_current = mtree_root;
1006 /* Disallow '..' as a component. */
1007 if (IS_DOTDOT(pathspec)) {
1008 mtree_error("absolute path cannot contain "
1013 /* Ignore multiple adjacent slashes and '.'. */
1014 if (pathspec[0] != '\0' && !IS_DOT(pathspec))
1015 error = read_mtree_spec1(fp, false, pathspec);
1016 memmove(pathspec, cp, strlen(cp) + 1);
1017 cp = strchr(pathspec, '/');
1018 } while (!error && cp != NULL);
1020 /* Disallow '.' and '..' as the last component. */
1021 if (!error && (IS_DOT(pathspec) || IS_DOTDOT(pathspec))) {
1022 mtree_error("absolute path cannot contain . or .. "
1028 /* Ignore absolute specfications that end with a slash. */
1029 if (!error && pathspec[0] != '\0')
1030 error = read_mtree_spec1(fp, true, pathspec);
1039 read_mtree(const char *fname, fsnode *node)
1041 struct mtree_fileinfo *fi;
1045 /* We do not yet support nesting... */
1046 assert(node == NULL);
1048 if (strcmp(fname, "-") == 0)
1051 fp = fopen(fname, "r");
1053 err(1, "Can't open `%s'", fname);
1056 error = mtree_file_push(fname, fp);
1060 bzero(&mtree_global, sizeof(mtree_global));
1061 bzero(&mtree_global_inode, sizeof(mtree_global_inode));
1062 mtree_global.inode = &mtree_global_inode;
1063 mtree_global_inode.nlink = 1;
1064 mtree_global_inode.st.st_nlink = 1;
1065 mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime =
1066 mtree_global_inode.st.st_mtime = time(NULL);
1067 errors = warnings = 0;
1073 mtree_current = node;
1075 /* Start of a new line... */
1076 fi = SLIST_FIRST(&mtree_fileinfo);
1079 error = skip_over(fp, " \t");
1085 error = ferror(fp) ? errno : -1;
1090 case '\n': /* empty line */
1093 case '#': /* comment -- skip to end of line. */
1094 error = skip_to(fp, "\n");
1098 case '/': /* special commands */
1099 error = read_mtree_command(fp);
1101 default: /* specification */
1103 error = read_mtree_spec(fp);
1111 if (error <= 0 && (errors || warnings)) {
1112 warnx("%u error(s) and %u warning(s) in mtree manifest",
1120 errc(1, error, "Error reading mtree file");
1125 if (mtree_root != NULL)
1126 return (mtree_root);
1128 /* Handle empty specifications. */
1129 node = create_node(".", S_IFDIR, NULL, &mtree_global);