]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/fmtree/create.c
Optionally bind ktls threads to NUMA domains
[FreeBSD/FreeBSD.git] / usr.sbin / fmtree / create.c
1 /*-
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)create.c    8.1 (Berkeley) 6/6/93";
33 #endif /* not lint */
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <dirent.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <fts.h>
45 #include <grp.h>
46 #ifdef MD5
47 #include <md5.h>
48 #endif
49 #ifdef SHA1
50 #include <sha.h>
51 #endif
52 #ifdef RMD160
53 #include <ripemd.h>
54 #endif
55 #ifdef SHA256
56 #include <sha256.h>
57 #endif
58 #include <pwd.h>
59 #include <stdint.h>
60 #include <stdio.h>
61 #include <time.h>
62 #include <unistd.h>
63 #include <vis.h>
64 #include "mtree.h"
65 #include "extern.h"
66
67 #define INDENTNAMELEN   15
68 #define MAXLINELEN      80
69
70 static gid_t gid;
71 static uid_t uid;
72 static mode_t mode;
73 static u_long flags = 0xffffffff;
74
75 static int      dsort(const FTSENT * const *, const FTSENT * const *);
76 static void     output(int, int *, const char *, ...) __printflike(3, 4);
77 static int      statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
78 static void     statf(int, FTSENT *);
79
80 void
81 cwalk(void)
82 {
83         FTS *t;
84         FTSENT *p;
85         time_t cl;
86         char *argv[2], host[MAXHOSTNAMELEN];
87         char dot[] = ".";
88         int indent = 0;
89
90         if (!nflag) {
91                 (void)time(&cl);
92                 (void)gethostname(host, sizeof(host));
93                 (void)printf(
94                     "#\t   user: %s\n#\tmachine: %s\n",
95                     getlogin(), host);
96                 (void)printf(
97                     "#\t   tree: %s\n#\t   date: %s",
98                     fullpath, ctime(&cl));
99         }
100
101         argv[0] = dot;
102         argv[1] = NULL;
103         if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
104                 err(1, "fts_open()");
105         while (errno = 0, (p = fts_read(t))) {
106                 if (iflag)
107                         indent = p->fts_level * 4;
108                 if (check_excludes(p->fts_name, p->fts_path)) {
109                         fts_set(t, p, FTS_SKIP);
110                         continue;
111                 }
112                 switch(p->fts_info) {
113                 case FTS_D:
114                         if (!dflag)
115                                 (void)printf("\n");
116                         if (!nflag)
117                                 (void)printf("# %s\n", p->fts_path);
118                         statd(t, p, &uid, &gid, &mode, &flags);
119                         statf(indent, p);
120                         break;
121                 case FTS_DP:
122                         if (!nflag && (p->fts_level > 0))
123                                 (void)printf("%*s# %s\n", indent, "", p->fts_path);
124                         (void)printf("%*s..\n", indent, "");
125                         if (!dflag)
126                                 (void)printf("\n");
127                         break;
128                 case FTS_DNR:
129                 case FTS_ERR:
130                 case FTS_NS:
131                         warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
132                         break;
133                 default:
134                         if (!dflag)
135                                 statf(indent, p);
136                         break;
137
138                 }
139         }
140         if (errno != 0)
141                 err(1, "fts_read()");
142         (void)fts_close(t);
143         if (sflag && keys & F_CKSUM)
144                 warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);
145 }
146
147 static void
148 statf(int indent, FTSENT *p)
149 {
150         struct group *gr;
151         struct passwd *pw;
152         uint32_t val;
153         off_t len;
154         int fd, offset;
155         char *fflags;
156         char *escaped_name;
157
158         escaped_name = calloc(1, p->fts_namelen * 4  +  1);
159         if (escaped_name == NULL)
160                 errx(1, "statf(): calloc() failed");
161         strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL | VIS_GLOB);
162
163         if (iflag || S_ISDIR(p->fts_statp->st_mode))
164                 offset = printf("%*s%s", indent, "", escaped_name);
165         else
166                 offset = printf("%*s    %s", indent, "", escaped_name);
167
168         free(escaped_name);
169
170         if (offset > (INDENTNAMELEN + indent))
171                 offset = MAXLINELEN;
172         else
173                 offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
174
175         if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
176                 output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
177         if (p->fts_statp->st_uid != uid) {
178                 if (keys & F_UNAME) {
179                         pw = getpwuid(p->fts_statp->st_uid);
180                         if (pw != NULL)
181                                 output(indent, &offset, "uname=%s", pw->pw_name);
182                         else if (wflag)
183                                 warnx("Could not get uname for uid=%u",
184                                     p->fts_statp->st_uid);
185                         else
186                                 errx(1,
187                                     "Could not get uname for uid=%u",
188                                     p->fts_statp->st_uid);
189                 }
190                 if (keys & F_UID)
191                         output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
192         }
193         if (p->fts_statp->st_gid != gid) {
194                 if (keys & F_GNAME) {
195                         gr = getgrgid(p->fts_statp->st_gid);
196                         if (gr != NULL)
197                                 output(indent, &offset, "gname=%s", gr->gr_name);
198                         else if (wflag)
199                                 warnx("Could not get gname for gid=%u",
200                                     p->fts_statp->st_gid);
201                         else
202                                 errx(1,
203                                     "Could not get gname for gid=%u",
204                                     p->fts_statp->st_gid);
205                 }
206                 if (keys & F_GID)
207                         output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
208         }
209         if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
210                 output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
211         if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
212                 output(indent, &offset, "nlink=%ju",
213                     (uintmax_t)p->fts_statp->st_nlink);
214         if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
215                 output(indent, &offset, "size=%jd",
216                     (intmax_t)p->fts_statp->st_size);
217         if (keys & F_TIME)
218                 output(indent, &offset, "time=%ld.%09ld",
219                     (long)p->fts_statp->st_mtim.tv_sec,
220                     p->fts_statp->st_mtim.tv_nsec);
221         if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
222                 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
223                     crc(fd, &val, &len))
224                         err(1, "%s", p->fts_accpath);
225                 (void)close(fd);
226                 output(indent, &offset, "cksum=%lu", (unsigned long)val);
227         }
228 #ifdef MD5
229         if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
230                 char *digest, buf[33];
231
232                 digest = MD5File(p->fts_accpath, buf);
233                 if (!digest)
234                         err(1, "%s", p->fts_accpath);
235                 output(indent, &offset, "md5digest=%s", digest);
236         }
237 #endif /* MD5 */
238 #ifdef SHA1
239         if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
240                 char *digest, buf[41];
241
242                 digest = SHA1_File(p->fts_accpath, buf);
243                 if (!digest)
244                         err(1, "%s", p->fts_accpath);
245                 output(indent, &offset, "sha1digest=%s", digest);
246         }
247 #endif /* SHA1 */
248 #ifdef RMD160
249         if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
250                 char *digest, buf[41];
251
252                 digest = RIPEMD160_File(p->fts_accpath, buf);
253                 if (!digest)
254                         err(1, "%s", p->fts_accpath);
255                 output(indent, &offset, "ripemd160digest=%s", digest);
256         }
257 #endif /* RMD160 */
258 #ifdef SHA256
259         if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
260                 char *digest, buf[65];
261
262                 digest = SHA256_File(p->fts_accpath, buf);
263                 if (!digest)
264                         err(1, "%s", p->fts_accpath);
265                 output(indent, &offset, "sha256digest=%s", digest);
266         }
267 #endif /* SHA256 */
268         if (keys & F_SLINK &&
269             (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
270                 output(indent, &offset, "link=%s", rlink(p->fts_accpath));
271         if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
272                 fflags = flags_to_string(p->fts_statp->st_flags);
273                 output(indent, &offset, "flags=%s", fflags);
274                 free(fflags);
275         }
276         (void)putchar('\n');
277 }
278
279 #define MAXGID  5000
280 #define MAXUID  5000
281 #define MAXMODE MBITS + 1
282 #define MAXFLAGS 256
283 #define MAXS 16
284
285 static int
286 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode, u_long *pflags)
287 {
288         FTSENT *p;
289         gid_t sgid;
290         uid_t suid;
291         mode_t smode;
292         u_long sflags;
293         struct group *gr;
294         struct passwd *pw;
295         gid_t savegid = *pgid;
296         uid_t saveuid = *puid;
297         mode_t savemode = *pmode;
298         u_long saveflags = *pflags;
299         u_short maxgid, maxuid, maxmode, maxflags;
300         u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS];
301         char *fflags;
302         static int first = 1;
303
304         if ((p = fts_children(t, 0)) == NULL) {
305                 if (errno)
306                         err(1, "%s", RP(parent));
307                 return (1);
308         }
309
310         bzero(g, sizeof(g));
311         bzero(u, sizeof(u));
312         bzero(m, sizeof(m));
313         bzero(f, sizeof(f));
314
315         maxuid = maxgid = maxmode = maxflags = 0;
316         for (; p; p = p->fts_link) {
317                 if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
318                         smode = p->fts_statp->st_mode & MBITS;
319                         if (smode < MAXMODE && ++m[smode] > maxmode) {
320                                 savemode = smode;
321                                 maxmode = m[smode];
322                         }
323                         sgid = p->fts_statp->st_gid;
324                         if (sgid < MAXGID && ++g[sgid] > maxgid) {
325                                 savegid = sgid;
326                                 maxgid = g[sgid];
327                         }
328                         suid = p->fts_statp->st_uid;
329                         if (suid < MAXUID && ++u[suid] > maxuid) {
330                                 saveuid = suid;
331                                 maxuid = u[suid];
332                         }
333
334                         /*
335                          * XXX
336                          * note that the below will break when file flags
337                          * are extended beyond the first 4 bytes of each
338                          * half word of the flags
339                          */
340 #define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
341                         sflags = p->fts_statp->st_flags;
342                         if (FLAGS2IDX(sflags) < MAXFLAGS &&
343                             ++f[FLAGS2IDX(sflags)] > maxflags) {
344                                 saveflags = sflags;
345                                 maxflags = f[FLAGS2IDX(sflags)];
346                         }
347                 }
348         }
349         /*
350          * If the /set record is the same as the last one we do not need to output
351          * a new one.  So first we check to see if anything changed.  Note that we
352          * always output a /set record for the first directory.
353          */
354         if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
355             (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
356             ((keys & F_MODE) && (*pmode != savemode)) ||
357             ((keys & F_FLAGS) && (*pflags != saveflags)) ||
358             (first)) {
359                 first = 0;
360                 if (dflag)
361                         (void)printf("/set type=dir");
362                 else
363                         (void)printf("/set type=file");
364                 if (keys & F_UNAME) {
365                         pw = getpwuid(saveuid);
366                         if (pw != NULL)
367                                 (void)printf(" uname=%s", pw->pw_name);
368                         else if (wflag)
369                                 warnx( "Could not get uname for uid=%u", saveuid);
370                         else
371                                 errx(1, "Could not get uname for uid=%u", saveuid);
372                 }
373                 if (keys & F_UID)
374                         (void)printf(" uid=%lu", (u_long)saveuid);
375                 if (keys & F_GNAME) {
376                         gr = getgrgid(savegid);
377                         if (gr != NULL)
378                                 (void)printf(" gname=%s", gr->gr_name);
379                         else if (wflag)
380                                 warnx("Could not get gname for gid=%u", savegid);
381                         else
382                                 errx(1, "Could not get gname for gid=%u", savegid);
383                 }
384                 if (keys & F_GID)
385                         (void)printf(" gid=%lu", (u_long)savegid);
386                 if (keys & F_MODE)
387                         (void)printf(" mode=%#o", savemode);
388                 if (keys & F_NLINK)
389                         (void)printf(" nlink=1");
390                 if (keys & F_FLAGS) {
391                         fflags = flags_to_string(saveflags);
392                         (void)printf(" flags=%s", fflags);
393                         free(fflags);
394                 }
395                 (void)printf("\n");
396                 *puid = saveuid;
397                 *pgid = savegid;
398                 *pmode = savemode;
399                 *pflags = saveflags;
400         }
401         return (0);
402 }
403
404 static int
405 dsort(const FTSENT * const *a, const FTSENT * const *b)
406 {
407         if (S_ISDIR((*a)->fts_statp->st_mode)) {
408                 if (!S_ISDIR((*b)->fts_statp->st_mode))
409                         return (1);
410         } else if (S_ISDIR((*b)->fts_statp->st_mode))
411                 return (-1);
412         return (strcmp((*a)->fts_name, (*b)->fts_name));
413 }
414
415 #include <stdarg.h>
416
417 void
418 output(int indent, int *offset, const char *fmt, ...)
419 {
420         va_list ap;
421         char buf[1024];
422         va_start(ap, fmt);
423         (void)vsnprintf(buf, sizeof(buf), fmt, ap);
424         va_end(ap);
425
426         if (*offset + strlen(buf) > MAXLINELEN - 3) {
427                 (void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
428                 *offset = INDENTNAMELEN + indent;
429         }
430         *offset += printf(" %s", buf) + 1;
431 }