create.c revision 54375
11553Srgrimes/*- 21553Srgrimes * Copyright (c) 1989, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * Redistribution and use in source and binary forms, with or without 61553Srgrimes * modification, are permitted provided that the following conditions 71553Srgrimes * are met: 81553Srgrimes * 1. Redistributions of source code must retain the above copyright 91553Srgrimes * notice, this list of conditions and the following disclaimer. 101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111553Srgrimes * notice, this list of conditions and the following disclaimer in the 121553Srgrimes * documentation and/or other materials provided with the distribution. 131553Srgrimes * 3. All advertising materials mentioning features or use of this software 141553Srgrimes * must display the following acknowledgement: 151553Srgrimes * This product includes software developed by the University of 161553Srgrimes * California, Berkeley and its contributors. 171553Srgrimes * 4. Neither the name of the University nor the names of its contributors 181553Srgrimes * may be used to endorse or promote products derived from this software 191553Srgrimes * without specific prior written permission. 201553Srgrimes * 211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311553Srgrimes * SUCH DAMAGE. 321553Srgrimes */ 331553Srgrimes 341553Srgrimes#ifndef lint 3530027Scharnier#if 0 361553Srgrimesstatic char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; 3730027Scharnier#endif 3830027Scharnierstatic const char rcsid[] = 3950479Speter "$FreeBSD: head/usr.sbin/mtree/create.c 54375 1999-12-09 20:38:36Z joe $"; 401553Srgrimes#endif /* not lint */ 411553Srgrimes 421553Srgrimes#include <sys/param.h> 431553Srgrimes#include <sys/stat.h> 4430027Scharnier#include <dirent.h> 4530027Scharnier#include <err.h> 4630027Scharnier#include <errno.h> 471553Srgrimes#include <fcntl.h> 481553Srgrimes#include <fts.h> 491553Srgrimes#include <grp.h> 5044303Swollman#ifdef MD5 5130027Scharnier#include <md5.h> 5244303Swollman#endif 5344303Swollman#ifdef SHA1 5444303Swollman#include <sha.h> 5544303Swollman#endif 5644303Swollman#ifdef RMD160 5744303Swollman#include <ripemd.h> 5844303Swollman#endif 591553Srgrimes#include <pwd.h> 6030027Scharnier#include <stdio.h> 6130027Scharnier#include <time.h> 621553Srgrimes#include <unistd.h> 6342561Sjkoshy#include <vis.h> 641553Srgrimes#include "mtree.h" 651553Srgrimes#include "extern.h" 661553Srgrimes 671553Srgrimes#define INDENTNAMELEN 15 681553Srgrimes#define MAXLINELEN 80 691553Srgrimes 702860Srgrimesextern long int crc_total; 712860Srgrimesextern int ftsoptions; 722860Srgrimesextern int dflag, iflag, nflag, sflag; 7336670Speterextern u_int keys; 741553Srgrimesextern char fullpath[MAXPATHLEN]; 7530027Scharnierextern int lineno; 761553Srgrimes 771553Srgrimesstatic gid_t gid; 781553Srgrimesstatic uid_t uid; 791553Srgrimesstatic mode_t mode; 8054375Sjoestatic u_long flags; 811553Srgrimes 821553Srgrimesstatic int dsort __P((const FTSENT **, const FTSENT **)); 832860Srgrimesstatic void output __P((int, int *, const char *, ...)); 8454375Sjoestatic int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, 8554375Sjoe u_long *)); 862860Srgrimesstatic void statf __P((int, FTSENT *)); 871553Srgrimes 881553Srgrimesvoid 891553Srgrimescwalk() 901553Srgrimes{ 911553Srgrimes register FTS *t; 921553Srgrimes register FTSENT *p; 931553Srgrimes time_t clock; 941553Srgrimes char *argv[2], host[MAXHOSTNAMELEN]; 952860Srgrimes int indent = 0; 961553Srgrimes 971553Srgrimes (void)time(&clock); 981553Srgrimes (void)gethostname(host, sizeof(host)); 991553Srgrimes (void)printf( 1001553Srgrimes "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 1011553Srgrimes getlogin(), host, fullpath, ctime(&clock)); 1021553Srgrimes 1031553Srgrimes argv[0] = "."; 1041553Srgrimes argv[1] = NULL; 1051553Srgrimes if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 10630027Scharnier err(1, "line %d: fts_open", lineno); 1072860Srgrimes while ((p = fts_read(t))) { 1082860Srgrimes if (iflag) 1092860Srgrimes indent = p->fts_level * 4; 1101553Srgrimes switch(p->fts_info) { 1111553Srgrimes case FTS_D: 1122860Srgrimes if (!dflag) 1132860Srgrimes (void)printf("\n"); 1142860Srgrimes if (!nflag) 1152860Srgrimes (void)printf("# %s\n", p->fts_path); 11654375Sjoe statd(t, p, &uid, &gid, &mode, &flags); 1172860Srgrimes statf(indent, p); 1181553Srgrimes break; 1191553Srgrimes case FTS_DP: 1202860Srgrimes if (!nflag && (p->fts_level > 0)) 1212860Srgrimes (void)printf("%*s# %s\n", indent, "", p->fts_path); 1222860Srgrimes (void)printf("%*s..\n", indent, ""); 1232860Srgrimes if (!dflag) 1242860Srgrimes (void)printf("\n"); 1251553Srgrimes break; 1261553Srgrimes case FTS_DNR: 1271553Srgrimes case FTS_ERR: 1281553Srgrimes case FTS_NS: 12930027Scharnier warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1301553Srgrimes break; 1311553Srgrimes default: 1321553Srgrimes if (!dflag) 1332860Srgrimes statf(indent, p); 1341553Srgrimes break; 1358857Srgrimes 1361553Srgrimes } 1372860Srgrimes } 1381553Srgrimes (void)fts_close(t); 1391553Srgrimes if (sflag && keys & F_CKSUM) 14030027Scharnier warnx("%s checksum: %lu", fullpath, crc_total); 1411553Srgrimes} 1421553Srgrimes 1431553Srgrimesstatic void 1442860Srgrimesstatf(indent, p) 1452860Srgrimes int indent; 1461553Srgrimes FTSENT *p; 1471553Srgrimes{ 1481553Srgrimes struct group *gr; 1491553Srgrimes struct passwd *pw; 1501553Srgrimes u_long len, val; 1512860Srgrimes int fd, offset; 15242561Sjkoshy char *escaped_name; 1531553Srgrimes 15442561Sjkoshy escaped_name = calloc(1, p->fts_namelen * 4 + 1); 15542561Sjkoshy if (escaped_name == NULL) 15642561Sjkoshy errx(1, "statf(): calloc() failed"); 15742787Sjkoshy strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL); 15842561Sjkoshy 1592860Srgrimes if (iflag || S_ISDIR(p->fts_statp->st_mode)) 16042561Sjkoshy offset = printf("%*s%s", indent, "", escaped_name); 1611553Srgrimes else 16242561Sjkoshy offset = printf("%*s %s", indent, "", escaped_name); 16342561Sjkoshy 16442561Sjkoshy free(escaped_name); 1651553Srgrimes 1662860Srgrimes if (offset > (INDENTNAMELEN + indent)) 1672860Srgrimes offset = MAXLINELEN; 1681553Srgrimes else 1692860Srgrimes offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); 1701553Srgrimes 1712860Srgrimes if (!S_ISREG(p->fts_statp->st_mode) && !dflag) 1722860Srgrimes output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); 1732860Srgrimes if (p->fts_statp->st_uid != uid) { 1742860Srgrimes if (keys & F_UNAME) { 1752860Srgrimes if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) { 1762860Srgrimes output(indent, &offset, "uname=%s", pw->pw_name); 1772860Srgrimes } else { 17830027Scharnier errx(1, 17930027Scharnier "line %d: could not get uname for uid=%u", 18030027Scharnier lineno, p->fts_statp->st_uid); 1812860Srgrimes } 1822860Srgrimes } 1832860Srgrimes if (keys & F_UID) 1842860Srgrimes output(indent, &offset, "uid=%u", p->fts_statp->st_uid); 1852860Srgrimes } 1862860Srgrimes if (p->fts_statp->st_gid != gid) { 1872860Srgrimes if (keys & F_GNAME) { 1882860Srgrimes if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) { 1892860Srgrimes output(indent, &offset, "gname=%s", gr->gr_name); 1902860Srgrimes } else { 19130027Scharnier errx(1, 19230027Scharnier "line %d: could not get gname for gid=%u", 19330027Scharnier lineno, p->fts_statp->st_gid); 1942860Srgrimes } 1952860Srgrimes } 1962860Srgrimes if (keys & F_GID) 1972860Srgrimes output(indent, &offset, "gid=%u", p->fts_statp->st_gid); 1982860Srgrimes } 1991553Srgrimes if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 2002860Srgrimes output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); 2011553Srgrimes if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 2022860Srgrimes output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); 2031553Srgrimes if (keys & F_SIZE) 2042860Srgrimes output(indent, &offset, "size=%qd", p->fts_statp->st_size); 2051553Srgrimes if (keys & F_TIME) 2062860Srgrimes output(indent, &offset, "time=%ld.%ld", 20718404Snate p->fts_statp->st_mtimespec.tv_sec, 20818404Snate p->fts_statp->st_mtimespec.tv_nsec); 2091553Srgrimes if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 2101553Srgrimes if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 2111553Srgrimes crc(fd, &val, &len)) 21230027Scharnier err(1, "line %d: %s", lineno, p->fts_accpath); 2131553Srgrimes (void)close(fd); 2142860Srgrimes output(indent, &offset, "cksum=%lu", val); 2151553Srgrimes } 21644303Swollman#ifdef MD5 2176286Swollman if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) { 21844303Swollman char *digest, buf[33]; 2196286Swollman 22044303Swollman digest = MD5File(p->fts_accpath, buf); 22144303Swollman if (!digest) { 22230027Scharnier err(1, "line %d: %s", lineno, p->fts_accpath); 2236286Swollman } else { 22444303Swollman output(indent, &offset, "md5digest=%s", digest); 2256286Swollman } 2266286Swollman } 22744303Swollman#endif /* MD5 */ 22844303Swollman#ifdef SHA1 22944303Swollman if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) { 23044303Swollman char *digest, buf[41]; 23144303Swollman 23244303Swollman digest = SHA1_File(p->fts_accpath, buf); 23344303Swollman if (!digest) { 23444303Swollman err(1, "line %d: %s", lineno, p->fts_accpath); 23544303Swollman } else { 23644303Swollman output(indent, &offset, "sha1digest=%s", digest); 23744303Swollman } 23844303Swollman } 23944303Swollman#endif /* SHA1 */ 24044303Swollman#ifdef RMD160 24144303Swollman if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) { 24244303Swollman char *digest, buf[41]; 24344303Swollman 24444303Swollman digest = RIPEMD160_File(p->fts_accpath, buf); 24544303Swollman if (!digest) { 24644303Swollman err(1, "line %d: %s", lineno, p->fts_accpath); 24744303Swollman } else { 24844303Swollman output(indent, &offset, "ripemd160digest=%s", digest); 24944303Swollman } 25044303Swollman } 25144303Swollman#endif /* RMD160 */ 2521553Srgrimes if (keys & F_SLINK && 2531553Srgrimes (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 2542860Srgrimes output(indent, &offset, "link=%s", rlink(p->fts_accpath)); 25554375Sjoe if (keys & F_FLAGS && p->fts_statp->st_flags != flags) 25654375Sjoe output(indent, &offset, "flags=%s", 25754375Sjoe flags_to_string(p->fts_statp->st_flags, "none")); 2581553Srgrimes (void)putchar('\n'); 2591553Srgrimes} 2601553Srgrimes 2611553Srgrimes#define MAXGID 5000 2621553Srgrimes#define MAXUID 5000 2631553Srgrimes#define MAXMODE MBITS + 1 26454375Sjoe#define MAXFLAGS 256 26554375Sjoe#define MAXS 16 2661553Srgrimes 2671553Srgrimesstatic int 26854375Sjoestatd(t, parent, puid, pgid, pmode, pflags) 2691553Srgrimes FTS *t; 2701553Srgrimes FTSENT *parent; 2711553Srgrimes uid_t *puid; 2721553Srgrimes gid_t *pgid; 2731553Srgrimes mode_t *pmode; 27454375Sjoe u_long *pflags; 2751553Srgrimes{ 2761553Srgrimes register FTSENT *p; 2771553Srgrimes register gid_t sgid; 2781553Srgrimes register uid_t suid; 2791553Srgrimes register mode_t smode; 28054375Sjoe register u_long sflags; 2811553Srgrimes struct group *gr; 2821553Srgrimes struct passwd *pw; 2832860Srgrimes gid_t savegid = *pgid; 2842860Srgrimes uid_t saveuid = *puid; 2852860Srgrimes mode_t savemode = *pmode; 28654375Sjoe u_long saveflags = 0; 28754375Sjoe u_short maxgid, maxuid, maxmode, maxflags; 28854375Sjoe u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS]; 2892877Srgrimes static int first = 1; 2901553Srgrimes 2911553Srgrimes if ((p = fts_children(t, 0)) == NULL) { 2921553Srgrimes if (errno) 29330027Scharnier err(1, "line %d: %s", lineno, RP(parent)); 2941553Srgrimes return (1); 2951553Srgrimes } 2961553Srgrimes 2971553Srgrimes bzero(g, sizeof(g)); 2981553Srgrimes bzero(u, sizeof(u)); 2991553Srgrimes bzero(m, sizeof(m)); 30054375Sjoe bzero(f, sizeof(f)); 3011553Srgrimes 30254375Sjoe maxuid = maxgid = maxmode = maxflags = 0; 3031553Srgrimes for (; p; p = p->fts_link) { 3042860Srgrimes if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { 3052860Srgrimes smode = p->fts_statp->st_mode & MBITS; 3062860Srgrimes if (smode < MAXMODE && ++m[smode] > maxmode) { 3072860Srgrimes savemode = smode; 3082860Srgrimes maxmode = m[smode]; 3092860Srgrimes } 3102860Srgrimes sgid = p->fts_statp->st_gid; 3112860Srgrimes if (sgid < MAXGID && ++g[sgid] > maxgid) { 3122860Srgrimes savegid = sgid; 3132860Srgrimes maxgid = g[sgid]; 3142860Srgrimes } 3152860Srgrimes suid = p->fts_statp->st_uid; 3162860Srgrimes if (suid < MAXUID && ++u[suid] > maxuid) { 3172860Srgrimes saveuid = suid; 3182860Srgrimes maxuid = u[suid]; 3192860Srgrimes } 32054375Sjoe 32154375Sjoe /* 32254375Sjoe * XXX 32354375Sjoe * note that the below will break when file flags 32454375Sjoe * are extended beyond the first 4 bytes of each 32554375Sjoe * half word of the flags 32654375Sjoe */ 32754375Sjoe#define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0)) 32854375Sjoe sflags = p->fts_statp->st_flags; 32954375Sjoe if (FLAGS2IDX(sflags) < MAXFLAGS && 33054375Sjoe ++f[FLAGS2IDX(sflags)] > maxflags) { 33154375Sjoe saveflags = sflags; 33254375Sjoe maxflags = u[FLAGS2IDX(sflags)]; 33354375Sjoe } 3341553Srgrimes } 3351553Srgrimes } 3362860Srgrimes /* 3372860Srgrimes * If the /set record is the same as the last one we do not need to output 3382877Srgrimes * a new one. So first we check to see if anything changed. Note that we 3392877Srgrimes * always output a /set record for the first directory. 3402860Srgrimes */ 3412860Srgrimes if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || 3422860Srgrimes (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || 3432877Srgrimes ((keys & F_MODE) && (*pmode != savemode)) || (first)) { 3442877Srgrimes first = 0; 3452860Srgrimes if (dflag) 3462860Srgrimes (void)printf("/set type=dir"); 3471553Srgrimes else 3482860Srgrimes (void)printf("/set type=file"); 34951705Sbillf if (keys & F_UNAME) { 3502860Srgrimes if ((pw = getpwuid(saveuid)) != NULL) 3512860Srgrimes (void)printf(" uname=%s", pw->pw_name); 3522860Srgrimes else 35330027Scharnier errx(1, 35430027Scharnier "line %d: could not get uname for uid=%u", 35530027Scharnier lineno, saveuid); 35651705Sbillf } 3572860Srgrimes if (keys & F_UID) 35838020Sbde (void)printf(" uid=%lu", (u_long)saveuid); 35951705Sbillf if (keys & F_GNAME) { 3602860Srgrimes if ((gr = getgrgid(savegid)) != NULL) 3612860Srgrimes (void)printf(" gname=%s", gr->gr_name); 3622860Srgrimes else 36330027Scharnier errx(1, 36430027Scharnier "line %d: could not get gname for gid=%u", 36530027Scharnier lineno, savegid); 36651705Sbillf } 3672860Srgrimes if (keys & F_GID) 36838020Sbde (void)printf(" gid=%lu", (u_long)savegid); 3692860Srgrimes if (keys & F_MODE) 3702860Srgrimes (void)printf(" mode=%#o", savemode); 3712860Srgrimes if (keys & F_NLINK) 3722860Srgrimes (void)printf(" nlink=1"); 37354375Sjoe if (keys & F_FLAGS && saveflags) 37454375Sjoe (void)printf(" flags=%s", 37554375Sjoe flags_to_string(saveflags, "none")); 3762860Srgrimes (void)printf("\n"); 3772860Srgrimes *puid = saveuid; 3782860Srgrimes *pgid = savegid; 3792860Srgrimes *pmode = savemode; 38054375Sjoe *pflags = saveflags; 3812860Srgrimes } 3821553Srgrimes return (0); 3831553Srgrimes} 3841553Srgrimes 3851553Srgrimesstatic int 3861553Srgrimesdsort(a, b) 3871553Srgrimes const FTSENT **a, **b; 3881553Srgrimes{ 3891553Srgrimes if (S_ISDIR((*a)->fts_statp->st_mode)) { 3901553Srgrimes if (!S_ISDIR((*b)->fts_statp->st_mode)) 3911553Srgrimes return (1); 3921553Srgrimes } else if (S_ISDIR((*b)->fts_statp->st_mode)) 3931553Srgrimes return (-1); 3941553Srgrimes return (strcmp((*a)->fts_name, (*b)->fts_name)); 3951553Srgrimes} 3961553Srgrimes 3971553Srgrimes#if __STDC__ 3981553Srgrimes#include <stdarg.h> 3991553Srgrimes#else 4001553Srgrimes#include <varargs.h> 4011553Srgrimes#endif 4021553Srgrimes 4031553Srgrimesvoid 4041553Srgrimes#if __STDC__ 4052860Srgrimesoutput(int indent, int *offset, const char *fmt, ...) 4061553Srgrimes#else 4072860Srgrimesoutput(indent, offset, fmt, va_alist) 4082860Srgrimes int indent; 4091553Srgrimes int *offset; 4101553Srgrimes char *fmt; 4111553Srgrimes va_dcl 4121553Srgrimes#endif 4131553Srgrimes{ 4141553Srgrimes va_list ap; 4151553Srgrimes char buf[1024]; 4161553Srgrimes#if __STDC__ 4171553Srgrimes va_start(ap, fmt); 4181553Srgrimes#else 4191553Srgrimes va_start(ap); 4201553Srgrimes#endif 4211553Srgrimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 4221553Srgrimes va_end(ap); 4231553Srgrimes 4241553Srgrimes if (*offset + strlen(buf) > MAXLINELEN - 3) { 4252860Srgrimes (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); 4262860Srgrimes *offset = INDENTNAMELEN + indent; 4271553Srgrimes } 4281553Srgrimes *offset += printf(" %s", buf) + 1; 4291553Srgrimes} 430