xref: /386bsd/usr/src/usr.sbin/mtree/create.c (revision a2142627)
1 /*-
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char sccsid[] = "@(#)create.c	5.16 (Berkeley) 3/12/91";
36 #endif /* not lint */
37 
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <time.h>
41 #include <fts.h>
42 #include <dirent.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include "mtree.h"
46 
47 #define	LABEL \
48 	if (label++) \
49 		(void)putchar(' '); \
50 
51 int ftsoptions = FTS_PHYSICAL;
52 
cwalk()53 cwalk()
54 {
55 	extern int dflag;
56 	register FTS *t;
57 	register FTSENT *p;
58 	register int cnt, label, notset;
59 	time_t clock;
60 	uid_t uid;
61 	gid_t gid;
62 	mode_t mode;
63 	int tabs, dsort();
64 	char *argv[2];
65 	char curp[PATH_MAX], *inotype(), *getlogin(), *rlink();
66 
67 	if (!getwd(curp)) {
68 		(void)fprintf(stderr, "mtree: %s\n", curp);
69 		exit(1);
70 	}
71 	(void)time(&clock);
72 	(void)printf("#\t  fs: %s\n#\t  by: %s\n#\tdate: %s\n",
73 	    curp, getlogin(), ctime(&clock));
74 
75 	argv[0] = ".";
76 	argv[1] = (char *)NULL;
77 	if (!(t = fts_open(argv, ftsoptions, dsort))) {
78 		(void)fprintf(stderr,
79 		    "mtree: fts_open: %s.\n", strerror(errno));
80 		exit(1);
81 	}
82 	while (p = fts_read(t)) {
83 		switch(p->fts_info) {
84 		case FTS_D:
85 			if (dflag)
86 				notset = 1;
87 			else
88 				notset =
89 				    statdir(t, p, &uid, &gid, &mode, &tabs);
90 			if (!strcmp(p->fts_name, "."))
91 				continue;
92 			break;
93 		case FTS_DP:
94 			if (p->fts_level <= 0)
95 				continue;
96 			for (cnt = p->fts_level - 1; cnt-- > 0; )
97 				(void)putchar('\t');
98 			(void)printf("..\n");
99 			continue;
100 		case FTS_DNR:
101 		case FTS_ERR:
102 		case FTS_NS:
103 			(void)fprintf(stderr, "mtree: %s: %s.\n",
104 			    p->fts_path, strerror(errno));
105 			continue;
106 		default:
107 			if (dflag)
108 				continue;
109 		}
110 
111 		for (cnt = p->fts_level - 1; cnt-- > 0; )
112 			(void)putchar('\t');
113 		(void)printf("%s", p->fts_name);
114 		if (p->fts_info == FTS_D)
115 			(void)putchar('\t');
116 		else {
117 			if (tabs > 1 && p->fts_namelen < 8)
118 				(void)putchar('\t');
119 			(void)putchar('\t');
120 		}
121 
122 		label = 0;
123 		if (!S_ISREG(p->fts_statp->st_mode) || notset) {
124 			LABEL;
125 			(void)printf("type=%s", inotype(p->fts_statp->st_mode));
126 		}
127 		if (p->fts_statp->st_uid != uid || notset) {
128 			LABEL;
129 			(void)printf("owner=%u", p->fts_statp->st_uid);
130 		}
131 		if (p->fts_statp->st_gid != gid || notset) {
132 			LABEL;
133 			(void)printf("group=%u", p->fts_statp->st_gid);
134 		}
135 		if ((p->fts_statp->st_mode & MBITS) != mode || notset) {
136 			LABEL;
137 			(void)printf("mode=%#o", p->fts_statp->st_mode & MBITS);
138 		}
139 		if (p->fts_statp->st_nlink != 1 || notset) {
140 			LABEL;
141 			(void)printf("nlink=%u", p->fts_statp->st_nlink);
142 		}
143 		LABEL;
144 		(void)printf("size=%ld", p->fts_statp->st_size);
145 		LABEL;
146 		(void)printf("time=%ld", p->fts_statp->st_mtime);
147 
148 		if (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE) {
149 			LABEL;
150 			(void)printf("link=%s", rlink(p->fts_accpath));
151 		}
152 		(void)putchar('\n');
153 	}
154 	(void)fts_close(t);
155 }
156 
157 #define	MAXGID	5000
158 #define	MAXUID	5000
159 #define	MAXMODE	MBITS + 1
160 
statdir(t,parent,puid,pgid,pmode,tabs)161 statdir(t, parent, puid, pgid, pmode, tabs)
162 	FTS *t;
163 	FTSENT *parent;
164 	uid_t *puid;
165 	gid_t *pgid;
166 	mode_t *pmode;
167 	int *tabs;
168 {
169 	register FTSENT *p;
170 	register gid_t gid;
171 	register uid_t uid;
172 	register mode_t mode;
173 	gid_t savegid;
174 	uid_t saveuid;
175 	mode_t savemode;
176 	u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE];
177 
178 	if (!(p = fts_children(t,0))) {
179 		if (errno) {
180 			(void)fprintf(stderr, "mtree: %s: %s.\n",
181 			    RP(parent), strerror(errno));
182 			exit(1);
183 		}
184 		return(1);
185 	}
186 
187 	bzero(g, sizeof(g));
188 	bzero(u, sizeof(u));
189 	bzero(m, sizeof(m));
190 
191 	*tabs = 1;
192 	maxuid = maxgid = maxmode = 0;
193 	for (; p; p = p->fts_link) {
194 		mode = p->fts_statp->st_mode & MBITS;
195 		if (mode < MAXMODE && ++m[mode] > maxmode) {
196 			savemode = mode;
197 			maxmode = m[mode];
198 		}
199 		gid = p->fts_statp->st_gid;
200 		if (gid < MAXGID && ++g[gid] > maxgid) {
201 			savegid = gid;
202 			maxgid = g[gid];
203 		}
204 		uid = p->fts_statp->st_uid;
205 		if (uid < MAXUID && ++u[uid] > maxuid) {
206 			saveuid = uid;
207 			maxuid = u[uid];
208 		}
209 		if (p->fts_namelen > 7)
210 			*tabs = 2;
211 	}
212 	(void)printf("\n/set group=%u mode=%#o nlink=1 owner=%u type=file\n",
213 	    savegid, savemode, saveuid);
214 	*puid = saveuid;
215 	*pgid = savegid;
216 	*pmode = savemode;
217 	return(0);
218 }
219 
dsort(p1,p2)220 dsort(p1, p2)
221 	FTSENT **p1, **p2;
222 {
223 	register FTSENT *a, *b;
224 
225 	a = *p1;
226 	b = *p2;
227 
228 	if (S_ISDIR(a->fts_statp->st_mode)) {
229 		if (!S_ISDIR(b->fts_statp->st_mode))
230 			return(1);
231 	} else if (S_ISDIR(b->fts_statp->st_mode))
232 		return(-1);
233 	return(strcmp(a->fts_name, b->fts_name));
234 }
235