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>
54 #define ENOATTR ENOMSG
57 #define IS_DOT(nm) ((nm)[0] == '.' && (nm)[1] == '\0')
58 #define IS_DOTDOT(nm) ((nm)[0] == '.' && (nm)[1] == '.' && (nm)[2] == '\0')
60 struct mtree_fileinfo {
61 SLIST_ENTRY(mtree_fileinfo) next;
67 /* Global state used while parsing. */
68 static SLIST_HEAD(, mtree_fileinfo) mtree_fileinfo =
69 SLIST_HEAD_INITIALIZER(mtree_fileinfo);
70 static fsnode *mtree_root;
71 static fsnode *mtree_current;
72 static fsnode mtree_global;
73 static fsinode mtree_global_inode;
74 static u_int errors, warnings;
76 static void mtree_error(const char *, ...) __printflike(1, 2);
77 static void mtree_warning(const char *, ...) __printflike(1, 2);
80 mtree_file_push(const char *name, FILE *fp)
82 struct mtree_fileinfo *fi;
84 fi = emalloc(sizeof(*fi));
85 if (strcmp(name, "-") == 0)
86 fi->name = estrdup("(stdin)");
88 fi->name = estrdup(name);
89 if (fi->name == NULL) {
97 SLIST_INSERT_HEAD(&mtree_fileinfo, fi, next);
102 mtree_print(const char *msgtype, const char *fmt, va_list ap)
104 struct mtree_fileinfo *fi;
106 if (msgtype != NULL) {
107 fi = SLIST_FIRST(&mtree_fileinfo);
109 fprintf(stderr, "%s:%u: ", fi->name, fi->line);
110 fprintf(stderr, "%s: ", msgtype);
112 vfprintf(stderr, fmt, ap);
116 mtree_error(const char *fmt, ...)
121 mtree_print("error", fmt, ap);
129 mtree_warning(const char *fmt, ...)
134 mtree_print("warning", fmt, ap);
141 #ifndef MAKEFS_MAX_TREE_DEPTH
142 # define MAKEFS_MAX_TREE_DEPTH (MAXPATHLEN/2)
145 /* construct path to node->name */
147 mtree_file_path(fsnode *node)
151 char *res, *rp[MAKEFS_MAX_TREE_DEPTH];
155 rp[depth] = node->name;
156 for (pnode = node->parent; pnode && depth < MAKEFS_MAX_TREE_DEPTH - 1;
157 pnode = pnode->parent) {
158 if (strcmp(pnode->name, ".") == 0)
160 rp[++depth] = pnode->name;
163 sb = sbuf_new_auto();
169 sbuf_cat(sb, rp[depth--]);
172 sbuf_cat(sb, rp[depth]);
174 res = estrdup(sbuf_data(sb));
182 /* mtree_resolve() sets errno to indicate why NULL was returned. */
184 mtree_resolve(const char *spec, int *istemp)
187 char *res, *var = NULL;
188 const char *base, *p, *v;
190 int c, error, quoted, subst;
198 c = (len > 1) ? (spec[0] == spec[len - 1]) ? spec[0] : 0 : 0;
199 *istemp = (c == '`') ? 1 : 0;
200 subst = (c == '`' || c == '"') ? 1 : 0;
201 quoted = (subst || c == '\'') ? 1 : 0;
204 res = estrdup(spec + quoted);
210 sb = sbuf_new_auto();
220 p = strchr(base, '$');
222 sbuf_bcat(sb, base, len);
227 /* The following is safe. spec always starts with a quote. */
231 sbuf_bcat(sb, base, p - base);
244 /* Handle ${X} vs $X. */
252 len -= (p + 1) - base;
261 var = ecalloc(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 = estrdup(getprogname());
288 res = (error == 0) ? strdup(sbuf_data(sb)) : NULL;
296 skip_over(FILE *fp, const char *cs)
301 while (c != EOF && strchr(cs, c) != NULL)
307 return (ferror(fp) ? errno : -1);
311 skip_to(FILE *fp, const char *cs)
316 while (c != EOF && strchr(cs, c) == NULL)
322 return (ferror(fp) ? errno : -1);
326 read_word(FILE *fp, char *buf, size_t bufsz)
328 struct mtree_fileinfo *fi;
330 int c, done, error, esc, qlvl;
345 error = ferror(fp) ? errno : -1;
347 mtree_error("unexpected end of file");
349 case '#': /* comment -- skip to end of line. */
351 error = skip_to(fp, "\n");
369 } else if (c == buf[qidx]) {
374 } while (buf[qidx] != '`' &&
387 if (!esc && qlvl == 0) {
395 * We going to eat the newline ourselves.
398 mtree_warning("quoted word straddles "
400 fi = SLIST_FIRST(&mtree_fileinfo);
435 } while (idx < bufsz && !done);
438 mtree_error("word too long to fit buffer (max %zu characters)",
440 skip_to(fp, " \t\n");
446 create_node(const char *name, u_int type, fsnode *parent, fsnode *global)
450 n = ecalloc(1, sizeof(*n));
451 n->name = estrdup(name);
452 n->type = (type == 0) ? global->type : type;
455 n->inode = ecalloc(1, sizeof(*n->inode));
457 /* Assign global options/defaults. */
458 memcpy(n->inode, global->inode, sizeof(*n->inode));
459 n->inode->st.st_mode = (n->inode->st.st_mode & ~S_IFMT) | n->type;
461 if (n->type == S_IFLNK)
462 n->symlink = global->symlink;
463 else if (n->type == S_IFREG)
464 n->contents = global->contents;
470 destroy_node(fsnode *n)
474 assert(n->name != NULL);
475 assert(n->inode != NULL);
483 read_number(const char *tok, u_int base, intmax_t *res, intmax_t min,
489 val = strtoimax(tok, &end, base);
490 if (end == tok || end[0] != '\0')
492 if (val < min || val > max)
499 read_mtree_keywords(FILE *fp, fsnode *node)
501 char keyword[PATH_MAX];
502 char *name, *p, *value;
511 st = &node->inode->st;
513 error = skip_over(fp, " \t");
517 error = read_word(fp, keyword, sizeof(keyword));
521 if (keyword[0] == '\0')
524 value = strchr(keyword, '=');
529 * We use EINVAL, ENOATTR, ENOSYS and ENXIO to signal
530 * certain conditions:
531 * EINVAL - Value provided for a keyword that does
532 * not take a value. The value is ignored.
533 * ENOATTR - Value missing for a keyword that needs
534 * a value. The keyword is ignored.
535 * ENOSYS - Unsupported keyword encountered. The
536 * keyword is ignored.
537 * ENXIO - Value provided for a keyword that does
538 * not take a value. The value is ignored.
540 switch (keyword[0]) {
542 if (strcmp(keyword, "contents") == 0) {
547 node->contents = estrdup(value);
552 if (strcmp(keyword, "flags") == 0) {
558 if (!strtofflags(&value, &flset, &flclr)) {
559 st->st_flags &= ~flclr;
560 st->st_flags |= flset;
567 if (strcmp(keyword, "gid") == 0) {
572 error = read_number(value, 10, &num,
576 } else if (strcmp(keyword, "gname") == 0) {
581 if (gid_from_group(value, &gid) == 0)
589 if (strcmp(keyword, "link") == 0) {
594 node->symlink = estrdup(value);
599 if (strcmp(keyword, "mode") == 0) {
604 if (value[0] >= '0' && value[0] <= '9') {
605 error = read_number(value, 8, &num,
608 st->st_mode &= S_IFMT;
612 /* Symbolic mode not supported. */
620 if (strcmp(keyword, "optional") == 0) {
623 node->flags |= FSNODE_F_OPTIONAL;
628 if (strcmp(keyword, "size") == 0) {
633 error = read_number(value, 10, &num,
641 if (strcmp(keyword, "time") == 0) {
646 p = strchr(value, '.');
649 error = read_number(value, 10, &num, 0,
658 error = read_number(p, 10, &num, 0,
664 } else if (strcmp(keyword, "type") == 0) {
669 if (strcmp(value, "dir") == 0)
670 node->type = S_IFDIR;
671 else if (strcmp(value, "file") == 0)
672 node->type = S_IFREG;
673 else if (strcmp(value, "link") == 0)
674 node->type = S_IFLNK;
681 if (strcmp(keyword, "uid") == 0) {
686 error = read_number(value, 10, &num,
690 } else if (strcmp(keyword, "uname") == 0) {
695 if (uid_from_user(value, &uid) == 0)
709 mtree_error("%s: invalid value '%s'", keyword, value);
712 mtree_error("%s: keyword needs a value", keyword);
715 mtree_warning("%s: unsupported keyword", keyword);
718 mtree_error("%s: keyword does not take a value",
727 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
729 /* Nothing more to do for the global defaults. */
730 if (node->name == NULL)
734 * Be intelligent about the file type.
736 if (node->contents != NULL) {
737 if (node->symlink != NULL) {
738 mtree_error("%s: both link and contents keywords "
739 "defined", node->name);
743 } else if (node->type != 0) {
745 if (type == S_IFREG) {
746 /* the named path is the default contents */
747 node->contents = mtree_file_path(node);
750 type = (node->symlink != NULL) ? S_IFLNK : S_IFDIR;
755 if (node->type != type) {
756 mtree_error("%s: file type and defined keywords to not match",
761 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
763 if (node->contents == NULL)
766 name = mtree_resolve(node->contents, &istemp);
770 if (stat(name, &sb) != 0) {
771 mtree_error("%s: contents file '%s' not found", node->name,
778 * Check for hardlinks. If the contents key is used, then the check
779 * will only trigger if the contents file is a link even if it is used
780 * by more than one file
782 if (sb.st_nlink > 1) {
785 st->st_ino = sb.st_ino;
786 st->st_dev = sb.st_dev;
787 curino = link_check(node->inode);
788 if (curino != NULL) {
790 node->inode = curino;
791 node->inode->nlink++;
795 free(node->contents);
796 node->contents = name;
797 st->st_size = sb.st_size;
802 read_mtree_command(FILE *fp)
807 error = read_word(fp, cmd, sizeof(cmd));
811 error = read_mtree_keywords(fp, &mtree_global);
820 read_mtree_spec1(FILE *fp, bool def, const char *name)
822 fsnode *last, *node, *parent;
826 assert(name[0] != '\0');
829 * Treat '..' specially, because it only changes our current
830 * directory. We don't create a node for it. We simply ignore
831 * any keywords that may appear on the line as well.
832 * Going up a directory is a little non-obvious. A directory
833 * node has a corresponding '.' child. The parent of '.' is
834 * not the '.' node of the parent directory, but the directory
835 * node within the parent to which the child relates. However,
836 * going up a directory means we need to find the '.' node to
837 * which the directoy node is linked. This we can do via the
838 * first * pointer, because '.' is always the first entry in a
841 if (IS_DOTDOT(name)) {
842 /* This deals with NULL pointers as well. */
843 if (mtree_current == mtree_root) {
844 mtree_warning("ignoring .. in root directory");
848 node = mtree_current;
850 assert(node != NULL);
851 assert(IS_DOT(node->name));
852 assert(node->first == node);
854 /* Get the corresponding directory node in the parent. */
855 node = mtree_current->parent;
857 assert(node != NULL);
858 assert(!IS_DOT(node->name));
862 assert(node != NULL);
863 assert(IS_DOT(node->name));
864 assert(node->first == node);
866 mtree_current = node;
871 * If we don't have a current directory and the first specification
872 * (either implicit or defined) is not '.', then we need to create
873 * a '.' node first (using a recursive call).
875 if (!IS_DOT(name) && mtree_current == NULL) {
876 error = read_mtree_spec1(fp, false, ".");
882 * Lookup the name in the current directory (if we have a current
883 * directory) to make sure we do not create multiple nodes for the
884 * same component. For non-definitions, if we find a node with the
885 * same name, simply change the current directory. For definitions
889 node = mtree_current;
890 while (node != NULL) {
891 assert(node->first == mtree_current);
893 if (strcmp(name, node->name) == 0) {
897 "duplicate definition of %s",
901 "duplicate definition of %s",
906 if (node->type != S_IFDIR) {
907 mtree_error("%s is not a directory", name);
911 assert(!IS_DOT(name));
915 assert(node != NULL);
916 assert(IS_DOT(node->name));
918 mtree_current = node;
926 parent = (mtree_current != NULL) ? mtree_current->parent : NULL;
927 type = (def == false || IS_DOT(name)) ? S_IFDIR : 0;
928 node = create_node(name, type, parent, &mtree_global);
933 error = read_mtree_keywords(fp, node);
940 node->first = (mtree_current != NULL) ? mtree_current : node;
945 if (node->type != S_IFDIR)
948 if (!IS_DOT(node->name)) {
950 node = create_node(".", S_IFDIR, parent, parent);
953 destroy_node(parent);
956 parent->child = node;
960 assert(node != NULL);
961 assert(IS_DOT(node->name));
962 assert(node->first == node);
964 mtree_current = node;
965 if (mtree_root == NULL)
972 read_mtree_spec(FILE *fp)
974 char pathspec[PATH_MAX];
978 error = read_word(fp, pathspec, sizeof(pathspec));
982 cp = strchr(pathspec, '/');
984 /* Absolute pathname */
985 mtree_current = mtree_root;
990 /* Disallow '..' as a component. */
991 if (IS_DOTDOT(pathspec)) {
992 mtree_error("absolute path cannot contain "
997 /* Ignore multiple adjacent slashes and '.'. */
998 if (pathspec[0] != '\0' && !IS_DOT(pathspec))
999 error = read_mtree_spec1(fp, false, pathspec);
1000 memmove(pathspec, cp, strlen(cp) + 1);
1001 cp = strchr(pathspec, '/');
1002 } while (!error && cp != NULL);
1004 /* Disallow '.' and '..' as the last component. */
1005 if (!error && (IS_DOT(pathspec) || IS_DOTDOT(pathspec))) {
1006 mtree_error("absolute path cannot contain . or .. "
1012 /* Ignore absolute specfications that end with a slash. */
1013 if (!error && pathspec[0] != '\0')
1014 error = read_mtree_spec1(fp, true, pathspec);
1023 read_mtree(const char *fname, fsnode *node)
1025 struct mtree_fileinfo *fi;
1029 /* We do not yet support nesting... */
1030 assert(node == NULL);
1032 if (strcmp(fname, "-") == 0)
1035 fp = fopen(fname, "r");
1037 err(1, "Can't open `%s'", fname);
1040 error = mtree_file_push(fname, fp);
1044 memset(&mtree_global, 0, sizeof(mtree_global));
1045 memset(&mtree_global_inode, 0, sizeof(mtree_global_inode));
1046 mtree_global.inode = &mtree_global_inode;
1047 mtree_global_inode.nlink = 1;
1048 mtree_global_inode.st.st_nlink = 1;
1049 mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime =
1050 mtree_global_inode.st.st_mtime = time(NULL);
1051 errors = warnings = 0;
1057 mtree_current = node;
1059 /* Start of a new line... */
1060 fi = SLIST_FIRST(&mtree_fileinfo);
1063 error = skip_over(fp, " \t");
1069 error = ferror(fp) ? errno : -1;
1074 case '\n': /* empty line */
1077 case '#': /* comment -- skip to end of line. */
1078 error = skip_to(fp, "\n");
1082 case '/': /* special commands */
1083 error = read_mtree_command(fp);
1085 default: /* specification */
1087 error = read_mtree_spec(fp);
1095 if (error <= 0 && (errors || warnings)) {
1096 warnx("%u error(s) and %u warning(s) in mtree manifest",
1104 errc(1, error, "Error reading mtree file");
1109 if (mtree_root != NULL)
1110 return (mtree_root);
1112 /* Handle empty specifications. */
1113 node = create_node(".", S_IFDIR, NULL, &mtree_global);