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>
55 #define ENOATTR ENOMSG
58 #define IS_DOT(nm) ((nm)[0] == '.' && (nm)[1] == '\0')
59 #define IS_DOTDOT(nm) ((nm)[0] == '.' && (nm)[1] == '.' && (nm)[2] == '\0')
61 struct mtree_fileinfo {
62 SLIST_ENTRY(mtree_fileinfo) next;
68 /* Global state used while parsing. */
69 static SLIST_HEAD(, mtree_fileinfo) mtree_fileinfo =
70 SLIST_HEAD_INITIALIZER(mtree_fileinfo);
71 static fsnode *mtree_root;
72 static fsnode *mtree_current;
73 static fsnode mtree_global;
74 static fsinode mtree_global_inode;
75 static u_int errors, warnings;
77 static void mtree_error(const char *, ...) __printflike(1, 2);
78 static void mtree_warning(const char *, ...) __printflike(1, 2);
81 mtree_file_push(const char *name, FILE *fp)
83 struct mtree_fileinfo *fi;
85 fi = emalloc(sizeof(*fi));
86 if (strcmp(name, "-") == 0)
87 fi->name = estrdup("(stdin)");
89 fi->name = estrdup(name);
90 if (fi->name == NULL) {
98 SLIST_INSERT_HEAD(&mtree_fileinfo, fi, next);
103 mtree_print(const char *msgtype, const char *fmt, va_list ap)
105 struct mtree_fileinfo *fi;
107 if (msgtype != NULL) {
108 fi = SLIST_FIRST(&mtree_fileinfo);
110 fprintf(stderr, "%s:%u: ", fi->name, fi->line);
111 fprintf(stderr, "%s: ", msgtype);
113 vfprintf(stderr, fmt, ap);
117 mtree_error(const char *fmt, ...)
122 mtree_print("error", fmt, ap);
130 mtree_warning(const char *fmt, ...)
135 mtree_print("warning", fmt, ap);
142 #ifndef MAKEFS_MAX_TREE_DEPTH
143 # define MAKEFS_MAX_TREE_DEPTH (MAXPATHLEN/2)
146 /* construct path to node->name */
148 mtree_file_path(fsnode *node)
152 char *res, *rp[MAKEFS_MAX_TREE_DEPTH];
156 rp[depth] = node->name;
157 for (pnode = node->parent; pnode && depth < MAKEFS_MAX_TREE_DEPTH - 1;
158 pnode = pnode->parent) {
159 if (strcmp(pnode->name, ".") == 0)
161 rp[++depth] = pnode->name;
164 sb = sbuf_new_auto();
170 sbuf_cat(sb, rp[depth--]);
173 sbuf_cat(sb, rp[depth]);
175 res = estrdup(sbuf_data(sb));
183 /* mtree_resolve() sets errno to indicate why NULL was returned. */
185 mtree_resolve(const char *spec, int *istemp)
188 char *res, *var = NULL;
189 const char *base, *p, *v;
191 int c, error, quoted, subst;
199 c = (len > 1) ? (spec[0] == spec[len - 1]) ? spec[0] : 0 : 0;
200 *istemp = (c == '`') ? 1 : 0;
201 subst = (c == '`' || c == '"') ? 1 : 0;
202 quoted = (subst || c == '\'') ? 1 : 0;
205 res = estrdup(spec + quoted);
211 sb = sbuf_new_auto();
221 p = strchr(base, '$');
223 sbuf_bcat(sb, base, len);
228 /* The following is safe. spec always starts with a quote. */
232 sbuf_bcat(sb, base, p - base);
245 /* Handle ${X} vs $X. */
253 len -= (p + 1) - base;
262 var = ecalloc(p - v, 1);
263 memcpy(var, v + 1, p - v - 1);
264 if (strcmp(var, ".CURDIR") == 0) {
265 res = getcwd(NULL, 0);
268 } else if (strcmp(var, ".PROG") == 0) {
269 res = estrdup(getprogname());
289 res = (error == 0) ? strdup(sbuf_data(sb)) : NULL;
297 skip_over(FILE *fp, const char *cs)
302 while (c != EOF && strchr(cs, c) != NULL)
308 return (ferror(fp) ? errno : -1);
312 skip_to(FILE *fp, const char *cs)
317 while (c != EOF && strchr(cs, c) == NULL)
323 return (ferror(fp) ? errno : -1);
327 read_word(FILE *fp, char *buf, size_t bufsz)
329 struct mtree_fileinfo *fi;
331 int c, done, error, esc, qlvl;
346 error = ferror(fp) ? errno : -1;
348 mtree_error("unexpected end of file");
350 case '#': /* comment -- skip to end of line. */
352 error = skip_to(fp, "\n");
368 } else if (c == buf[qidx]) {
373 } while (buf[qidx] != '`' &&
386 if (!esc && qlvl == 0) {
394 * We going to eat the newline ourselves.
397 mtree_warning("quoted word straddles "
399 fi = SLIST_FIRST(&mtree_fileinfo);
410 } while (idx < bufsz && !done);
413 mtree_error("word too long to fit buffer (max %zu characters)",
415 skip_to(fp, " \t\n");
421 create_node(const char *name, u_int type, fsnode *parent, fsnode *global)
425 n = ecalloc(1, sizeof(*n));
426 n->name = estrdup(name);
427 n->type = (type == 0) ? global->type : type;
430 n->inode = ecalloc(1, sizeof(*n->inode));
432 /* Assign global options/defaults. */
433 memcpy(n->inode, global->inode, sizeof(*n->inode));
434 n->inode->st.st_mode = (n->inode->st.st_mode & ~S_IFMT) | n->type;
436 if (n->type == S_IFLNK)
437 n->symlink = global->symlink;
438 else if (n->type == S_IFREG)
439 n->contents = global->contents;
445 destroy_node(fsnode *n)
449 assert(n->name != NULL);
450 assert(n->inode != NULL);
458 read_number(const char *tok, u_int base, intmax_t *res, intmax_t min,
464 val = strtoimax(tok, &end, base);
465 if (end == tok || end[0] != '\0')
467 if (val < min || val > max)
474 read_mtree_keywords(FILE *fp, fsnode *node)
476 char keyword[PATH_MAX];
477 char *name, *p, *value;
486 st = &node->inode->st;
488 error = skip_over(fp, " \t");
492 error = read_word(fp, keyword, sizeof(keyword));
496 if (keyword[0] == '\0')
499 value = strchr(keyword, '=');
504 * We use EINVAL, ENOATTR, ENOSYS and ENXIO to signal
505 * certain conditions:
506 * EINVAL - Value provided for a keyword that does
507 * not take a value. The value is ignored.
508 * ENOATTR - Value missing for a keyword that needs
509 * a value. The keyword is ignored.
510 * ENOSYS - Unsupported keyword encountered. The
511 * keyword is ignored.
512 * ENXIO - Value provided for a keyword that does
513 * not take a value. The value is ignored.
515 switch (keyword[0]) {
517 if (strcmp(keyword, "contents") == 0) {
522 node->contents = estrdup(value);
527 if (strcmp(keyword, "flags") == 0) {
533 if (!strtofflags(&value, &flset, &flclr)) {
534 st->st_flags &= ~flclr;
535 st->st_flags |= flset;
542 if (strcmp(keyword, "gid") == 0) {
547 error = read_number(value, 10, &num,
551 } else if (strcmp(keyword, "gname") == 0) {
556 if (gid_from_group(value, &gid) == 0)
564 if (strcmp(keyword, "link") == 0) {
569 node->symlink = emalloc(strlen(value) + 1);
570 if (node->symlink == NULL) {
574 if (strunvis(node->symlink, value) < 0) {
582 if (strcmp(keyword, "mode") == 0) {
587 if (value[0] >= '0' && value[0] <= '9') {
588 error = read_number(value, 8, &num,
591 st->st_mode &= S_IFMT;
595 /* Symbolic mode not supported. */
603 if (strcmp(keyword, "optional") == 0) {
606 node->flags |= FSNODE_F_OPTIONAL;
611 if (strcmp(keyword, "size") == 0) {
616 error = read_number(value, 10, &num,
624 if (strcmp(keyword, "time") == 0) {
629 p = strchr(value, '.');
632 error = read_number(value, 10, &num, 0,
641 error = read_number(p, 10, &num, 0,
647 } else if (strcmp(keyword, "type") == 0) {
652 if (strcmp(value, "dir") == 0)
653 node->type = S_IFDIR;
654 else if (strcmp(value, "file") == 0)
655 node->type = S_IFREG;
656 else if (strcmp(value, "link") == 0)
657 node->type = S_IFLNK;
664 if (strcmp(keyword, "uid") == 0) {
669 error = read_number(value, 10, &num,
673 } else if (strcmp(keyword, "uname") == 0) {
678 if (uid_from_user(value, &uid) == 0)
692 mtree_error("%s: invalid value '%s'", keyword, value);
695 mtree_error("%s: keyword needs a value", keyword);
698 mtree_warning("%s: unsupported keyword", keyword);
701 mtree_error("%s: keyword does not take a value",
710 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
712 /* Nothing more to do for the global defaults. */
713 if (node->name == NULL)
717 * Be intelligent about the file type.
719 if (node->contents != NULL) {
720 if (node->symlink != NULL) {
721 mtree_error("%s: both link and contents keywords "
722 "defined", node->name);
726 } else if (node->type != 0) {
728 if (type == S_IFREG) {
729 /* the named path is the default contents */
730 node->contents = mtree_file_path(node);
733 type = (node->symlink != NULL) ? S_IFLNK : S_IFDIR;
738 if (node->type != type) {
739 mtree_error("%s: file type and defined keywords to not match",
744 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
746 if (node->contents == NULL)
749 name = mtree_resolve(node->contents, &istemp);
753 if (stat(name, &sb) != 0) {
754 mtree_error("%s: contents file '%s' not found", node->name,
761 * Check for hardlinks. If the contents key is used, then the check
762 * will only trigger if the contents file is a link even if it is used
763 * by more than one file
765 if (sb.st_nlink > 1) {
768 st->st_ino = sb.st_ino;
769 st->st_dev = sb.st_dev;
770 curino = link_check(node->inode);
771 if (curino != NULL) {
773 node->inode = curino;
774 node->inode->nlink++;
778 free(node->contents);
779 node->contents = name;
780 st->st_size = sb.st_size;
785 read_mtree_command(FILE *fp)
790 error = read_word(fp, cmd, sizeof(cmd));
794 error = read_mtree_keywords(fp, &mtree_global);
803 read_mtree_spec1(FILE *fp, bool def, const char *name)
805 fsnode *last, *node, *parent;
809 assert(name[0] != '\0');
812 * Treat '..' specially, because it only changes our current
813 * directory. We don't create a node for it. We simply ignore
814 * any keywords that may appear on the line as well.
815 * Going up a directory is a little non-obvious. A directory
816 * node has a corresponding '.' child. The parent of '.' is
817 * not the '.' node of the parent directory, but the directory
818 * node within the parent to which the child relates. However,
819 * going up a directory means we need to find the '.' node to
820 * which the directoy node is linked. This we can do via the
821 * first * pointer, because '.' is always the first entry in a
824 if (IS_DOTDOT(name)) {
825 /* This deals with NULL pointers as well. */
826 if (mtree_current == mtree_root) {
827 mtree_warning("ignoring .. in root directory");
831 node = mtree_current;
833 assert(node != NULL);
834 assert(IS_DOT(node->name));
835 assert(node->first == node);
837 /* Get the corresponding directory node in the parent. */
838 node = mtree_current->parent;
840 assert(node != NULL);
841 assert(!IS_DOT(node->name));
845 assert(node != NULL);
846 assert(IS_DOT(node->name));
847 assert(node->first == node);
849 mtree_current = node;
854 * If we don't have a current directory and the first specification
855 * (either implicit or defined) is not '.', then we need to create
856 * a '.' node first (using a recursive call).
858 if (!IS_DOT(name) && mtree_current == NULL) {
859 error = read_mtree_spec1(fp, false, ".");
865 * Lookup the name in the current directory (if we have a current
866 * directory) to make sure we do not create multiple nodes for the
867 * same component. For non-definitions, if we find a node with the
868 * same name, simply change the current directory. For definitions
872 node = mtree_current;
873 while (node != NULL) {
874 assert(node->first == mtree_current);
876 if (strcmp(name, node->name) == 0) {
880 "duplicate definition of %s",
884 "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], pathtmp[4*PATH_MAX + 1];
961 error = read_word(fp, pathtmp, sizeof(pathtmp));
964 if (strnunvis(pathspec, PATH_MAX, pathtmp) == -1) {
970 cp = strchr(pathspec, '/');
972 /* Absolute pathname */
973 mtree_current = mtree_root;
978 /* Disallow '..' as a component. */
979 if (IS_DOTDOT(pathspec)) {
980 mtree_error("absolute path cannot contain "
985 /* Ignore multiple adjacent slashes and '.'. */
986 if (pathspec[0] != '\0' && !IS_DOT(pathspec))
987 error = read_mtree_spec1(fp, false, pathspec);
988 memmove(pathspec, cp, strlen(cp) + 1);
989 cp = strchr(pathspec, '/');
990 } while (!error && cp != NULL);
992 /* Disallow '.' and '..' as the last component. */
993 if (!error && (IS_DOT(pathspec) || IS_DOTDOT(pathspec))) {
994 mtree_error("absolute path cannot contain . or .. "
1000 /* Ignore absolute specfications that end with a slash. */
1001 if (!error && pathspec[0] != '\0')
1002 error = read_mtree_spec1(fp, true, pathspec);
1011 read_mtree(const char *fname, fsnode *node)
1013 struct mtree_fileinfo *fi;
1017 /* We do not yet support nesting... */
1018 assert(node == NULL);
1020 if (strcmp(fname, "-") == 0)
1023 fp = fopen(fname, "r");
1025 err(1, "Can't open `%s'", fname);
1028 error = mtree_file_push(fname, fp);
1032 memset(&mtree_global, 0, sizeof(mtree_global));
1033 memset(&mtree_global_inode, 0, sizeof(mtree_global_inode));
1034 mtree_global.inode = &mtree_global_inode;
1035 mtree_global_inode.nlink = 1;
1036 mtree_global_inode.st.st_nlink = 1;
1037 mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime =
1038 mtree_global_inode.st.st_mtime = time(NULL);
1039 errors = warnings = 0;
1045 mtree_current = node;
1047 /* Start of a new line... */
1048 fi = SLIST_FIRST(&mtree_fileinfo);
1051 error = skip_over(fp, " \t");
1057 error = ferror(fp) ? errno : -1;
1062 case '\n': /* empty line */
1065 case '#': /* comment -- skip to end of line. */
1066 error = skip_to(fp, "\n");
1070 case '/': /* special commands */
1071 error = read_mtree_command(fp);
1073 default: /* specification */
1075 error = read_mtree_spec(fp);
1083 if (error <= 0 && (errors || warnings)) {
1084 warnx("%u error(s) and %u warning(s) in mtree manifest",
1092 errc(1, error, "Error reading mtree file");
1097 if (mtree_root != NULL)
1098 return (mtree_root);
1100 /* Handle empty specifications. */
1101 node = create_node(".", S_IFDIR, NULL, &mtree_global);