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 /* mtree_resolve() sets errno to indicate why NULL was returned. */
140 mtree_resolve(const char *spec, int *istemp)
144 const char *base, *p, *v;
146 int c, error, quoted, subst;
154 c = (len > 1) ? (spec[0] == spec[len - 1]) ? spec[0] : 0 : 0;
155 *istemp = (c == '`') ? 1 : 0;
156 subst = (c == '`' || c == '"') ? 1 : 0;
157 quoted = (subst || c == '\'') ? 1 : 0;
160 res = strdup(spec + quoted);
161 if (res != NULL && quoted)
166 sb = sbuf_new_auto();
176 p = strchr(base, '$');
178 sbuf_bcat(sb, base, len);
183 /* The following is safe. spec always starts with a quote. */
187 sbuf_bcat(sb, base, p - base);
200 /* Handle ${X} vs $X. */
208 len -= (p + 1) - base;
217 var = calloc(p - v, 1);
221 memcpy(var, v + 1, p - v - 1);
222 if (strcmp(var, ".CURDIR") == 0) {
223 res = getcwd(NULL, 0);
226 } else if (strcmp(var, ".PROG") == 0) {
227 res = strdup(getprogname());
249 res = (error == 0) ? strdup(sbuf_data(sb)) : NULL;
257 skip_over(FILE *fp, const char *cs)
262 while (c != EOF && strchr(cs, c) != NULL)
268 return (ferror(fp) ? errno : -1);
272 skip_to(FILE *fp, const char *cs)
277 while (c != EOF && strchr(cs, c) == NULL)
283 return (ferror(fp) ? errno : -1);
287 read_word(FILE *fp, char *buf, size_t bufsz)
289 struct mtree_fileinfo *fi;
291 int c, done, error, esc, qlvl;
306 error = ferror(fp) ? errno : -1;
308 mtree_error("unexpected end of file");
323 } else if (c == buf[qidx]) {
328 } while (buf[qidx] != '`' &&
341 if (!esc && qlvl == 0) {
349 * We going to eat the newline ourselves.
352 mtree_warning("quoted word straddles "
354 fi = SLIST_FIRST(&mtree_fileinfo);
389 } while (idx < bufsz && !done);
392 mtree_error("word too long to fit buffer (max %zu characters)",
394 skip_to(fp, " \t\n");
400 create_node(const char *name, u_int type, fsnode *parent, fsnode *global)
404 n = calloc(1, sizeof(*n));
408 n->name = strdup(name);
409 if (n->name == NULL) {
414 n->type = (type == 0) ? global->type : type;
417 n->inode = calloc(1, sizeof(*n->inode));
418 if (n->inode == NULL) {
424 /* Assign global options/defaults. */
425 bcopy(global->inode, n->inode, sizeof(*n->inode));
426 n->inode->st.st_mode = (n->inode->st.st_mode & ~S_IFMT) | n->type;
428 if (n->type == S_IFLNK)
429 n->symlink = global->symlink;
430 else if (n->type == S_IFREG)
431 n->contents = global->contents;
437 destroy_node(fsnode *n)
441 assert(n->name != NULL);
442 assert(n->inode != NULL);
450 read_number(const char *tok, u_int base, intmax_t *res, intmax_t min,
456 val = strtoimax(tok, &end, base);
457 if (end == tok || end[0] != '\0')
459 if (val < min || val > max)
466 read_mtree_keywords(FILE *fp, fsnode *node)
468 char keyword[PATH_MAX];
469 char *name, *p, *value;
471 struct passwd *pwent;
475 int error, istemp, type;
477 st = &node->inode->st;
479 error = skip_over(fp, " \t");
483 error = read_word(fp, keyword, sizeof(keyword));
487 if (keyword[0] == '\0')
490 value = strchr(keyword, '=');
495 * We use EINVAL, ENOATTR, ENOSYS and ENXIO to signal
496 * certain conditions:
497 * EINVAL - Value provided for a keyword that does
498 * not take a value. The value is ignored.
499 * ENOATTR - Value missing for a keyword that needs
500 * a value. The keyword is ignored.
501 * ENOSYS - Unsupported keyword encountered. The
502 * keyword is ignored.
503 * ENXIO - Value provided for a keyword that does
504 * not take a value. The value is ignored.
506 switch (keyword[0]) {
508 if (strcmp(keyword, "contents") == 0) {
513 node->contents = strdup(value);
518 if (strcmp(keyword, "flags") == 0) {
524 if (!strtofflags(&value, &flset, &flclr)) {
525 st->st_flags &= ~flclr;
526 st->st_flags |= flset;
533 if (strcmp(keyword, "gid") == 0) {
538 error = read_number(value, 10, &num,
542 } else if (strcmp(keyword, "gname") == 0) {
547 grent = getgrnam(value);
549 st->st_gid = grent->gr_gid;
556 if (strcmp(keyword, "link") == 0) {
561 node->symlink = strdup(value);
566 if (strcmp(keyword, "mode") == 0) {
571 if (value[0] >= '0' && value[0] <= '9') {
572 error = read_number(value, 8, &num,
575 st->st_mode &= S_IFMT;
579 /* Symbolic mode not supported. */
587 if (strcmp(keyword, "optional") == 0) {
590 node->flags |= FSNODE_F_OPTIONAL;
595 if (strcmp(keyword, "size") == 0) {
600 error = read_number(value, 10, &num,
608 if (strcmp(keyword, "time") == 0) {
613 p = strchr(value, '.');
616 error = read_number(value, 10, &num, 0,
623 error = read_number(p, 10, &num, 0,
629 } else if (strcmp(keyword, "type") == 0) {
634 if (strcmp(value, "dir") == 0)
635 node->type = S_IFDIR;
636 else if (strcmp(value, "file") == 0)
637 node->type = S_IFREG;
638 else if (strcmp(value, "link") == 0)
639 node->type = S_IFLNK;
646 if (strcmp(keyword, "uid") == 0) {
651 error = read_number(value, 10, &num,
655 } else if (strcmp(keyword, "uname") == 0) {
660 pwent = getpwnam(value);
662 st->st_uid = pwent->pw_uid;
675 mtree_error("%s: invalid value '%s'", keyword, value);
678 mtree_error("%s: keyword needs a value", keyword);
681 mtree_warning("%s: unsupported keyword", keyword);
684 mtree_error("%s: keyword does not take a value",
693 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
695 /* Nothing more to do for the global defaults. */
696 if (node->name == NULL)
700 * Be intelligent about the file type.
702 if (node->contents != NULL) {
703 if (node->symlink != NULL) {
704 mtree_error("%s: both link and contents keywords "
705 "defined", node->name);
710 type = (node->symlink != NULL) ? S_IFLNK : S_IFDIR;
715 if (node->type != type) {
716 mtree_error("%s: file type and defined keywords to not match",
721 st->st_mode = (st->st_mode & ~S_IFMT) | node->type;
723 if (node->contents == NULL)
726 name = mtree_resolve(node->contents, &istemp);
730 if (stat(name, &sb) != 0) {
731 mtree_error("%s: contents file '%s' not found", node->name,
737 free(node->contents);
738 node->contents = name;
739 st->st_size = sb.st_size;
744 read_mtree_command(FILE *fp)
749 error = read_word(fp, cmd, sizeof(cmd));
753 error = read_mtree_keywords(fp, &mtree_global);
762 read_mtree_spec1(FILE *fp, bool def, const char *name)
764 fsnode *last, *node, *parent;
768 assert(name[0] != '\0');
771 * Treat '..' specially, because it only changes our current
772 * directory. We don't create a node for it. We simply ignore
773 * any keywords that may appear on the line as well.
774 * Going up a directory is a little non-obvious. A directory
775 * node has a corresponding '.' child. The parent of '.' is
776 * not the '.' node of the parent directory, but the directory
777 * node within the parent to which the child relates. However,
778 * going up a directory means we need to find the '.' node to
779 * which the directoy node is linked. This we can do via the
780 * first * pointer, because '.' is always the first entry in a
783 if (IS_DOTDOT(name)) {
784 /* This deals with NULL pointers as well. */
785 if (mtree_current == mtree_root) {
786 mtree_warning("ignoring .. in root directory");
790 node = mtree_current;
792 assert(node != NULL);
793 assert(IS_DOT(node->name));
794 assert(node->first == node);
796 /* Get the corresponding directory node in the parent. */
797 node = mtree_current->parent;
799 assert(node != NULL);
800 assert(!IS_DOT(node->name));
804 assert(node != NULL);
805 assert(IS_DOT(node->name));
806 assert(node->first == node);
808 mtree_current = node;
813 * If we don't have a current directory and the first specification
814 * (either implicit or defined) is not '.', then we need to create
815 * a '.' node first (using a recursive call).
817 if (!IS_DOT(name) && mtree_current == NULL) {
818 error = read_mtree_spec1(fp, false, ".");
824 * Lookup the name in the current directory (if we have a current
825 * directory) to make sure we do not create multiple nodes for the
826 * same component. For non-definitions, if we find a node with the
827 * same name, simply change the current directory. For definitions
831 node = mtree_current;
832 while (node != NULL) {
833 assert(node->first == mtree_current);
835 if (strcmp(name, node->name) == 0) {
837 mtree_error("duplicate definition of %s",
842 if (node->type != S_IFDIR) {
843 mtree_error("%s is not a directory", name);
847 assert(!IS_DOT(name));
851 assert(node != NULL);
852 assert(IS_DOT(node->name));
854 mtree_current = node;
862 parent = (mtree_current != NULL) ? mtree_current->parent : NULL;
863 type = (def == false || IS_DOT(name)) ? S_IFDIR : 0;
864 node = create_node(name, type, parent, &mtree_global);
869 error = read_mtree_keywords(fp, node);
876 node->first = (mtree_current != NULL) ? mtree_current : node;
881 if (node->type != S_IFDIR)
884 if (!IS_DOT(node->name)) {
886 node = create_node(".", S_IFDIR, parent, parent);
889 destroy_node(parent);
892 parent->child = node;
896 assert(node != NULL);
897 assert(IS_DOT(node->name));
898 assert(node->first == node);
900 mtree_current = node;
901 if (mtree_root == NULL)
908 read_mtree_spec(FILE *fp)
910 char pathspec[PATH_MAX];
914 error = read_word(fp, pathspec, sizeof(pathspec));
918 cp = strchr(pathspec, '/');
920 /* Absolute pathname */
921 mtree_current = mtree_root;
926 /* Disallow '.' and '..' as components. */
927 if (IS_DOT(pathspec) || IS_DOTDOT(pathspec)) {
928 mtree_error("absolute path cannot contain . "
933 /* Ignore multiple adjacent slashes. */
934 if (pathspec[0] != '\0')
935 error = read_mtree_spec1(fp, false, pathspec);
936 memmove(pathspec, cp, strlen(cp) + 1);
937 cp = strchr(pathspec, '/');
938 } while (!error && cp != NULL);
940 /* Disallow '.' and '..' as the last component. */
941 if (!error && (IS_DOT(pathspec) || IS_DOTDOT(pathspec))) {
942 mtree_error("absolute path cannot contain . or .. "
948 /* Ignore absolute specfications that end with a slash. */
949 if (!error && pathspec[0] != '\0')
950 error = read_mtree_spec1(fp, true, pathspec);
959 read_mtree(const char *fname, fsnode *node)
961 struct mtree_fileinfo *fi;
965 /* We do not yet support nesting... */
966 assert(node == NULL);
968 if (strcmp(fname, "-") == 0)
971 fp = fopen(fname, "r");
973 err(1, "Can't open `%s'", fname);
976 error = mtree_file_push(fname, fp);
980 bzero(&mtree_global, sizeof(mtree_global));
981 bzero(&mtree_global_inode, sizeof(mtree_global_inode));
982 mtree_global.inode = &mtree_global_inode;
983 mtree_global_inode.nlink = 1;
984 mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime =
985 mtree_global_inode.st.st_mtime = time(NULL);
986 errors = warnings = 0;
992 mtree_current = node;
994 /* Start of a new line... */
995 fi = SLIST_FIRST(&mtree_fileinfo);
998 error = skip_over(fp, " \t");
1004 error = ferror(fp) ? errno : -1;
1009 case '\n': /* empty line */
1012 case '#': /* comment -- skip to end of line. */
1013 error = skip_to(fp, "\n");
1017 case '/': /* special commands */
1018 error = read_mtree_command(fp);
1020 default: /* specification */
1022 error = read_mtree_spec(fp);
1030 if (error <= 0 && (errors || warnings)) {
1031 warnx("%u error(s) and %u warning(s) in mtree manifest",
1039 errc(1, error, "Error reading mtree file");
1044 if (mtree_root != NULL)
1045 return (mtree_root);
1047 /* Handle empty specifications. */
1048 node = create_node(".", S_IFDIR, NULL, &mtree_global);