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;
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 if (gid_from_group(value, &gid) == 0)
596 if (strcmp(keyword, "link") == 0) {
601 node->symlink = strdup(value);
606 if (strcmp(keyword, "mode") == 0) {
611 if (value[0] >= '0' && value[0] <= '9') {
612 error = read_number(value, 8, &num,
615 st->st_mode &= S_IFMT;
619 /* Symbolic mode not supported. */
627 if (strcmp(keyword, "optional") == 0) {
630 node->flags |= FSNODE_F_OPTIONAL;
635 if (strcmp(keyword, "size") == 0) {
640 error = read_number(value, 10, &num,
648 if (strcmp(keyword, "time") == 0) {
653 p = strchr(value, '.');
656 error = read_number(value, 10, &num, 0,
663 error = read_number(p, 10, &num, 0,
669 } else if (strcmp(keyword, "type") == 0) {
674 if (strcmp(value, "dir") == 0)
675 node->type = S_IFDIR;
676 else if (strcmp(value, "file") == 0)
677 node->type = S_IFREG;
678 else if (strcmp(value, "link") == 0)
679 node->type = S_IFLNK;
686 if (strcmp(keyword, "uid") == 0) {
691 error = read_number(value, 10, &num,
695 } else if (strcmp(keyword, "uname") == 0) {
700 if (uid_from_user(value, &uid) == 0)
714 mtree_error("%s: invalid value '%s'", keyword, value);
717 mtree_error("%s: keyword needs a value", keyword);
720 mtree_warning("%s: unsupported keyword", keyword);
723 mtree_error("%s: keyword does not take a value",
732 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
734 /* Nothing more to do for the global defaults. */
735 if (node->name == NULL)
739 * Be intelligent about the file type.
741 if (node->contents != NULL) {
742 if (node->symlink != NULL) {
743 mtree_error("%s: both link and contents keywords "
744 "defined", node->name);
748 } else if (node->type != 0) {
750 if (type == S_IFREG) {
751 /* the named path is the default contents */
752 node->contents = mtree_file_path(node);
755 type = (node->symlink != NULL) ? S_IFLNK : S_IFDIR;
760 if (node->type != type) {
761 mtree_error("%s: file type and defined keywords to not match",
766 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
768 if (node->contents == NULL)
771 name = mtree_resolve(node->contents, &istemp);
775 if (stat(name, &sb) != 0) {
776 mtree_error("%s: contents file '%s' not found", node->name,
783 * Check for hardlinks. If the contents key is used, then the check
784 * will only trigger if the contents file is a link even if it is used
785 * by more than one file
787 if (sb.st_nlink > 1) {
790 st->st_ino = sb.st_ino;
791 st->st_dev = sb.st_dev;
792 curino = link_check(node->inode);
793 if (curino != NULL) {
795 node->inode = curino;
796 node->inode->nlink++;
800 free(node->contents);
801 node->contents = name;
802 st->st_size = sb.st_size;
807 read_mtree_command(FILE *fp)
812 error = read_word(fp, cmd, sizeof(cmd));
816 error = read_mtree_keywords(fp, &mtree_global);
825 read_mtree_spec1(FILE *fp, bool def, const char *name)
827 fsnode *last, *node, *parent;
831 assert(name[0] != '\0');
834 * Treat '..' specially, because it only changes our current
835 * directory. We don't create a node for it. We simply ignore
836 * any keywords that may appear on the line as well.
837 * Going up a directory is a little non-obvious. A directory
838 * node has a corresponding '.' child. The parent of '.' is
839 * not the '.' node of the parent directory, but the directory
840 * node within the parent to which the child relates. However,
841 * going up a directory means we need to find the '.' node to
842 * which the directoy node is linked. This we can do via the
843 * first * pointer, because '.' is always the first entry in a
846 if (IS_DOTDOT(name)) {
847 /* This deals with NULL pointers as well. */
848 if (mtree_current == mtree_root) {
849 mtree_warning("ignoring .. in root directory");
853 node = mtree_current;
855 assert(node != NULL);
856 assert(IS_DOT(node->name));
857 assert(node->first == node);
859 /* Get the corresponding directory node in the parent. */
860 node = mtree_current->parent;
862 assert(node != NULL);
863 assert(!IS_DOT(node->name));
867 assert(node != NULL);
868 assert(IS_DOT(node->name));
869 assert(node->first == node);
871 mtree_current = node;
876 * If we don't have a current directory and the first specification
877 * (either implicit or defined) is not '.', then we need to create
878 * a '.' node first (using a recursive call).
880 if (!IS_DOT(name) && mtree_current == NULL) {
881 error = read_mtree_spec1(fp, false, ".");
887 * Lookup the name in the current directory (if we have a current
888 * directory) to make sure we do not create multiple nodes for the
889 * same component. For non-definitions, if we find a node with the
890 * same name, simply change the current directory. For definitions
894 node = mtree_current;
895 while (node != NULL) {
896 assert(node->first == mtree_current);
898 if (strcmp(name, node->name) == 0) {
902 "duplicate definition of %s",
906 "duplicate definition of %s",
911 if (node->type != S_IFDIR) {
912 mtree_error("%s is not a directory", name);
916 assert(!IS_DOT(name));
920 assert(node != NULL);
921 assert(IS_DOT(node->name));
923 mtree_current = node;
931 parent = (mtree_current != NULL) ? mtree_current->parent : NULL;
932 type = (def == false || IS_DOT(name)) ? S_IFDIR : 0;
933 node = create_node(name, type, parent, &mtree_global);
938 error = read_mtree_keywords(fp, node);
945 node->first = (mtree_current != NULL) ? mtree_current : node;
950 if (node->type != S_IFDIR)
953 if (!IS_DOT(node->name)) {
955 node = create_node(".", S_IFDIR, parent, parent);
958 destroy_node(parent);
961 parent->child = node;
965 assert(node != NULL);
966 assert(IS_DOT(node->name));
967 assert(node->first == node);
969 mtree_current = node;
970 if (mtree_root == NULL)
977 read_mtree_spec(FILE *fp)
979 char pathspec[PATH_MAX];
983 error = read_word(fp, pathspec, sizeof(pathspec));
987 cp = strchr(pathspec, '/');
989 /* Absolute pathname */
990 mtree_current = mtree_root;
995 /* Disallow '..' as a component. */
996 if (IS_DOTDOT(pathspec)) {
997 mtree_error("absolute path cannot contain "
1002 /* Ignore multiple adjacent slashes and '.'. */
1003 if (pathspec[0] != '\0' && !IS_DOT(pathspec))
1004 error = read_mtree_spec1(fp, false, pathspec);
1005 memmove(pathspec, cp, strlen(cp) + 1);
1006 cp = strchr(pathspec, '/');
1007 } while (!error && cp != NULL);
1009 /* Disallow '.' and '..' as the last component. */
1010 if (!error && (IS_DOT(pathspec) || IS_DOTDOT(pathspec))) {
1011 mtree_error("absolute path cannot contain . or .. "
1017 /* Ignore absolute specfications that end with a slash. */
1018 if (!error && pathspec[0] != '\0')
1019 error = read_mtree_spec1(fp, true, pathspec);
1028 read_mtree(const char *fname, fsnode *node)
1030 struct mtree_fileinfo *fi;
1034 /* We do not yet support nesting... */
1035 assert(node == NULL);
1037 if (strcmp(fname, "-") == 0)
1040 fp = fopen(fname, "r");
1042 err(1, "Can't open `%s'", fname);
1045 error = mtree_file_push(fname, fp);
1049 bzero(&mtree_global, sizeof(mtree_global));
1050 bzero(&mtree_global_inode, sizeof(mtree_global_inode));
1051 mtree_global.inode = &mtree_global_inode;
1052 mtree_global_inode.nlink = 1;
1053 mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime =
1054 mtree_global_inode.st.st_mtime = time(NULL);
1055 errors = warnings = 0;
1061 mtree_current = node;
1063 /* Start of a new line... */
1064 fi = SLIST_FIRST(&mtree_fileinfo);
1067 error = skip_over(fp, " \t");
1073 error = ferror(fp) ? errno : -1;
1078 case '\n': /* empty line */
1081 case '#': /* comment -- skip to end of line. */
1082 error = skip_to(fp, "\n");
1086 case '/': /* special commands */
1087 error = read_mtree_command(fp);
1089 default: /* specification */
1091 error = read_mtree_spec(fp);
1099 if (error <= 0 && (errors || warnings)) {
1100 warnx("%u error(s) and %u warning(s) in mtree manifest",
1108 errc(1, error, "Error reading mtree file");
1113 if (mtree_root != NULL)
1114 return (mtree_root);
1116 /* Handle empty specifications. */
1117 node = create_node(".", S_IFDIR, NULL, &mtree_global);