From owner-p4-projects Thu Nov 14 7:11: 3 2002 Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 49CC437B404; Thu, 14 Nov 2002 07:10:57 -0800 (PST) Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id DD4C837B401 for ; Thu, 14 Nov 2002 07:10:56 -0800 (PST) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7FD5F43E4A for ; Thu, 14 Nov 2002 07:10:56 -0800 (PST) (envelope-from green@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.12.6/8.12.6) with ESMTP id gAEF8OmV059266 for ; Thu, 14 Nov 2002 07:08:24 -0800 (PST) (envelope-from green@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.12.6/8.12.6/Submit) id gAEF8NwR059262 for perforce@freebsd.org; Thu, 14 Nov 2002 07:08:23 -0800 (PST) Date: Thu, 14 Nov 2002 07:08:23 -0800 (PST) Message-Id: <200211141508.gAEF8NwR059262@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to green@freebsd.org using -f From: Brian Feldman Subject: PERFORCE change 21054 for review To: Perforce Change Reviews Sender: owner-p4-projects@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG http://perforce.freebsd.org/chv.cgi?CH=21054 Change 21054 by green@green_laptop_2 on 2002/11/14 07:07:48 * Fix a botch in setfsmac(8) that caused SEBSD's <> labels to correctly notice them again. * Add a setfmac(8) mode which implements setfmac(8) in terms of setfsmac(8). This involves adding a flag to specify that a given entry always matches and passing around information on the -h flag/basename(argv[0]) of "setfmac". Affected files ... .. //depot/projects/trustedbsd/mac/sbin/setfsmac/setfsmac.c#5 edit Differences ... ==== //depot/projects/trustedbsd/mac/sbin/setfsmac/setfsmac.c#5 (text+ko) ==== @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,7 @@ char *mactext; /* MAC label to apply */ int flags; /* miscellaneous flags */ #define F_DONTLABEL 0x01 +#define F_ALWAYSMATCH 0x02 } *entries, /* entries[0..nentries] */ *match; /* cached decision for MAC label to apply */ size_t nentries; /* size of entries list */ @@ -32,11 +34,13 @@ STAILQ_HEAD(label_specs_head, label_spec) head; }; -void usage(void) __dead2; +void usage(int) __dead2; struct label_specs *new_specs(void); void add_specs(struct label_specs *, const char *, int); +void add_setfmac_specs(struct label_specs *, char *); void add_spec_line(const char *, int, struct label_spec_entry *, char *); -int apply_specs(struct label_specs *, FTSENT *, int); +int apply_specs(struct label_specs *, FTSENT *, int, int); +int specs_empty(struct label_specs *); int main(int argc, char **argv) @@ -44,11 +48,17 @@ FTSENT *ftsent; FTS *fts; struct label_specs *specs; - int eflag = 0, xflag = 0, vflag = 0; - int ch; + int eflag = 0, xflag = 0, vflag = 0, hflag; + int ch, is_setfmac; + char *bn; + bn = basename(argv[0]); + if (bn == NULL) + err(1, "basename"); + is_setfmac = strcmp(bn, "setfmac") == 0; + hflag = is_setfmac ? FTS_LOGICAL : FTS_PHYSICAL; specs = new_specs(); - while ((ch = getopt(argc, argv, "ef:s:vx")) != -1) { + while ((ch = getopt(argc, argv, is_setfmac ? "h" : "ef:s:vx")) != -1) { switch (ch) { case 'e': eflag = 1; @@ -56,6 +66,9 @@ case 'f': add_specs(specs, optarg, 0); break; + case 'h': + hflag = FTS_PHYSICAL; + break; case 's': add_specs(specs, optarg, 1); break; @@ -66,15 +79,23 @@ xflag = FTS_XDEV; break; default: - usage(); + usage(is_setfmac); } } argc -= optind; argv += optind; - if (argc == 0) - usage(); - fts = fts_open(argv, FTS_PHYSICAL | xflag, NULL); + if (is_setfmac) { + if (argc <= 1) + usage(is_setfmac); + add_setfmac_specs(specs, *argv); + argc--; + argv++; + } else { + if (argc == 0 || specs_empty(specs)) + usage(is_setfmac); + } + fts = fts_open(argv, hflag | xflag, NULL); if (fts == NULL) err(1, "cannot traverse filesystem%s", argc ? "s" : ""); while ((ftsent = fts_read(fts)) != NULL) { @@ -83,11 +104,14 @@ break; case FTS_D: /* do pre-order */ case FTS_DC: /* do cyclic? */ + /* don't ever recurse directories as setfmac(8) */ + if (is_setfmac) + fts_set(fts, ftsent, FTS_SKIP); case FTS_DEFAULT: /* do default */ case FTS_F: /* do regular */ case FTS_SL: /* do symlink */ case FTS_W: /* do whiteout */ - if (apply_specs(specs, ftsent, vflag)) { + if (apply_specs(specs, ftsent, hflag, vflag)) { if (eflag) { errx(1, "labeling not supported in " "%.*s", ftsent->fts_pathlen, @@ -114,10 +138,13 @@ } void -usage(void) +usage(int is_setfmac) { - fprintf(stderr, "usage: setfsmac [-evx] [-f specfile [...]] [-s specfile [...]] path ...\n"); + if (is_setfmac) + fprintf(stderr, "usage: setfmac [-h] label path ...\n"); + else + fprintf(stderr, "usage: setfsmac [-evx] [-f specfile [...]] [-s specfile [...]] path ...\n"); exit(1); } @@ -209,6 +236,24 @@ } void +add_setfmac_specs(struct label_specs *specs, char *label) +{ + struct label_spec *spec; + + spec = malloc(sizeof(*spec)); + if (spec == NULL) + err(1, "malloc"); + spec->nentries = 1; + spec->entries = calloc(spec->nentries, sizeof(*spec->entries)); + if (spec->entries == NULL) + err(1, "malloc"); + /* The _only_ thing specified here is the mactext! */ + spec->entries->mactext = label; + spec->entries->flags |= F_ALWAYSMATCH; + STAILQ_INSERT_TAIL(&specs->head, spec, link); +} + +void add_spec_line(const char *file, int is_sebsd, struct label_spec_entry *entry, char *line) { @@ -249,7 +294,7 @@ } else { if (asprintf(&entry->mactext, "sebsd/%s", macstr) == -1) err(1, "asprintf"); - if (strcmp(entry->mactext, "<>") == 0) + if (strcmp(macstr, "<>") == 0) entry->flags |= F_DONTLABEL; } if (modestr != NULL) { @@ -293,7 +338,14 @@ } int -apply_specs(struct label_specs *specs, FTSENT *ftsent, int vflag) +specs_empty(struct label_specs *specs) +{ + + return (STAILQ_EMPTY(&specs->head)); +} + +int +apply_specs(struct label_specs *specs, FTSENT *ftsent, int hflag, int vflag) { regmatch_t pmatch; struct label_spec *ls; @@ -312,6 +364,8 @@ STAILQ_FOREACH(ls, &specs->head, link) { for (ls->match = NULL, ent = ls->entries; ent < &ls->entries[ls->nentries]; ent++) { + if (ent->flags & F_ALWAYSMATCH) + goto matched; if (ent->mode != 0 && (ftsent->fts_statp->st_mode & S_IFMT) != ent->mode) continue; @@ -323,7 +377,6 @@ case REG_NOMATCH: continue; case 0: - ls->match = ent; break; default: size = regerror(error, &ent->regex, NULL, 0); @@ -334,6 +387,8 @@ size); errx(1, "%s: %s", ent->regexstr, regerrorstr); } + matched: + ls->match = ent; if (vflag) { if (matchedby == 0) { printf("%.*s matched by ", @@ -372,7 +427,8 @@ } if (mac_from_text(&mac, macstr)) err(1, "mac_from_text(%s)", macstr); - if (mac_set_link(ftsent->fts_accpath, mac) != 0) { + if ((hflag == FTS_PHYSICAL ? mac_set_link(ftsent->fts_accpath, mac) : + mac_set_file(ftsent->fts_accpath, mac)) != 0) { if (errno == EOPNOTSUPP) { mac_free(mac); free(macstr); To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe p4-projects" in the body of the message