Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Nov 2002 07:08:23 -0800 (PST)
From:      Brian Feldman <green@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 21054 for review
Message-ID:  <200211141508.gAEF8NwR059262@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 <<none>> 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 <err.h>
 #include <errno.h>
 #include <fts.h>
+#include <libgen.h>
 #include <regex.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -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, "<<none>>") == 0)
+		if (strcmp(macstr, "<<none>>") == 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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200211141508.gAEF8NwR059262>