create.c revision 60418
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 60418 2000-05-12 03:03:00Z wollman $"; 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; 11060418Swollman if (check_excludes(p->fts_name, p->fts_path)) { 11160418Swollman fts_set(t, p, FTS_SKIP); 11260418Swollman continue; 11360418Swollman } 1141553Srgrimes switch(p->fts_info) { 1151553Srgrimes case FTS_D: 1162860Srgrimes if (!dflag) 1172860Srgrimes (void)printf("\n"); 1182860Srgrimes if (!nflag) 1192860Srgrimes (void)printf("# %s\n", p->fts_path); 12054375Sjoe statd(t, p, &uid, &gid, &mode, &flags); 1212860Srgrimes statf(indent, p); 1221553Srgrimes break; 1231553Srgrimes case FTS_DP: 1242860Srgrimes if (!nflag && (p->fts_level > 0)) 1252860Srgrimes (void)printf("%*s# %s\n", indent, "", p->fts_path); 1262860Srgrimes (void)printf("%*s..\n", indent, ""); 1272860Srgrimes if (!dflag) 1282860Srgrimes (void)printf("\n"); 1291553Srgrimes break; 1301553Srgrimes case FTS_DNR: 1311553Srgrimes case FTS_ERR: 1321553Srgrimes case FTS_NS: 13330027Scharnier warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1341553Srgrimes break; 1351553Srgrimes default: 1361553Srgrimes if (!dflag) 1372860Srgrimes statf(indent, p); 1381553Srgrimes break; 1398857Srgrimes 1401553Srgrimes } 1412860Srgrimes } 1421553Srgrimes (void)fts_close(t); 1431553Srgrimes if (sflag && keys & F_CKSUM) 14430027Scharnier warnx("%s checksum: %lu", fullpath, crc_total); 1451553Srgrimes} 1461553Srgrimes 1471553Srgrimesstatic void 1482860Srgrimesstatf(indent, p) 1492860Srgrimes int indent; 1501553Srgrimes FTSENT *p; 1511553Srgrimes{ 1521553Srgrimes struct group *gr; 1531553Srgrimes struct passwd *pw; 1541553Srgrimes u_long len, val; 1552860Srgrimes int fd, offset; 15642561Sjkoshy char *escaped_name; 1571553Srgrimes 15842561Sjkoshy escaped_name = calloc(1, p->fts_namelen * 4 + 1); 15942561Sjkoshy if (escaped_name == NULL) 16042561Sjkoshy errx(1, "statf(): calloc() failed"); 16142787Sjkoshy strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL); 16242561Sjkoshy 1632860Srgrimes if (iflag || S_ISDIR(p->fts_statp->st_mode)) 16442561Sjkoshy offset = printf("%*s%s", indent, "", escaped_name); 1651553Srgrimes else 16642561Sjkoshy offset = printf("%*s %s", indent, "", escaped_name); 16742561Sjkoshy 16842561Sjkoshy free(escaped_name); 1691553Srgrimes 1702860Srgrimes if (offset > (INDENTNAMELEN + indent)) 1712860Srgrimes offset = MAXLINELEN; 1721553Srgrimes else 1732860Srgrimes offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); 1741553Srgrimes 1752860Srgrimes if (!S_ISREG(p->fts_statp->st_mode) && !dflag) 1762860Srgrimes output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); 1772860Srgrimes if (p->fts_statp->st_uid != uid) { 1782860Srgrimes if (keys & F_UNAME) { 1792860Srgrimes if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) { 1802860Srgrimes output(indent, &offset, "uname=%s", pw->pw_name); 1812860Srgrimes } else { 18230027Scharnier errx(1, 18330027Scharnier "line %d: could not get uname for uid=%u", 18430027Scharnier lineno, p->fts_statp->st_uid); 1852860Srgrimes } 1862860Srgrimes } 1872860Srgrimes if (keys & F_UID) 1882860Srgrimes output(indent, &offset, "uid=%u", p->fts_statp->st_uid); 1892860Srgrimes } 1902860Srgrimes if (p->fts_statp->st_gid != gid) { 1912860Srgrimes if (keys & F_GNAME) { 1922860Srgrimes if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) { 1932860Srgrimes output(indent, &offset, "gname=%s", gr->gr_name); 1942860Srgrimes } else { 19530027Scharnier errx(1, 19630027Scharnier "line %d: could not get gname for gid=%u", 19730027Scharnier lineno, p->fts_statp->st_gid); 1982860Srgrimes } 1992860Srgrimes } 2002860Srgrimes if (keys & F_GID) 2012860Srgrimes output(indent, &offset, "gid=%u", p->fts_statp->st_gid); 2022860Srgrimes } 2031553Srgrimes if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 2042860Srgrimes output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); 2051553Srgrimes if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 2062860Srgrimes output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); 2071553Srgrimes if (keys & F_SIZE) 2082860Srgrimes output(indent, &offset, "size=%qd", p->fts_statp->st_size); 2091553Srgrimes if (keys & F_TIME) 2102860Srgrimes output(indent, &offset, "time=%ld.%ld", 21118404Snate p->fts_statp->st_mtimespec.tv_sec, 21218404Snate p->fts_statp->st_mtimespec.tv_nsec); 2131553Srgrimes if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 2141553Srgrimes if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 2151553Srgrimes crc(fd, &val, &len)) 21630027Scharnier err(1, "line %d: %s", lineno, p->fts_accpath); 2171553Srgrimes (void)close(fd); 2182860Srgrimes output(indent, &offset, "cksum=%lu", val); 2191553Srgrimes } 22044303Swollman#ifdef MD5 2216286Swollman if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) { 22244303Swollman char *digest, buf[33]; 2236286Swollman 22444303Swollman digest = MD5File(p->fts_accpath, buf); 22544303Swollman if (!digest) { 22630027Scharnier err(1, "line %d: %s", lineno, p->fts_accpath); 2276286Swollman } else { 22844303Swollman output(indent, &offset, "md5digest=%s", digest); 2296286Swollman } 2306286Swollman } 23144303Swollman#endif /* MD5 */ 23244303Swollman#ifdef SHA1 23344303Swollman if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) { 23444303Swollman char *digest, buf[41]; 23544303Swollman 23644303Swollman digest = SHA1_File(p->fts_accpath, buf); 23744303Swollman if (!digest) { 23844303Swollman err(1, "line %d: %s", lineno, p->fts_accpath); 23944303Swollman } else { 24044303Swollman output(indent, &offset, "sha1digest=%s", digest); 24144303Swollman } 24244303Swollman } 24344303Swollman#endif /* SHA1 */ 24444303Swollman#ifdef RMD160 24544303Swollman if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) { 24644303Swollman char *digest, buf[41]; 24744303Swollman 24844303Swollman digest = RIPEMD160_File(p->fts_accpath, buf); 24944303Swollman if (!digest) { 25044303Swollman err(1, "line %d: %s", lineno, p->fts_accpath); 25144303Swollman } else { 25244303Swollman output(indent, &offset, "ripemd160digest=%s", digest); 25344303Swollman } 25444303Swollman } 25544303Swollman#endif /* RMD160 */ 2561553Srgrimes if (keys & F_SLINK && 2571553Srgrimes (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 2582860Srgrimes output(indent, &offset, "link=%s", rlink(p->fts_accpath)); 25954375Sjoe if (keys & F_FLAGS && p->fts_statp->st_flags != flags) 26054375Sjoe output(indent, &offset, "flags=%s", 26156692Sjoe getflags(p->fts_statp->st_flags, "none")); 2621553Srgrimes (void)putchar('\n'); 2631553Srgrimes} 2641553Srgrimes 2651553Srgrimes#define MAXGID 5000 2661553Srgrimes#define MAXUID 5000 2671553Srgrimes#define MAXMODE MBITS + 1 26854375Sjoe#define MAXFLAGS 256 26954375Sjoe#define MAXS 16 2701553Srgrimes 2711553Srgrimesstatic int 27254375Sjoestatd(t, parent, puid, pgid, pmode, pflags) 2731553Srgrimes FTS *t; 2741553Srgrimes FTSENT *parent; 2751553Srgrimes uid_t *puid; 2761553Srgrimes gid_t *pgid; 2771553Srgrimes mode_t *pmode; 27854375Sjoe u_long *pflags; 2791553Srgrimes{ 2801553Srgrimes register FTSENT *p; 2811553Srgrimes register gid_t sgid; 2821553Srgrimes register uid_t suid; 2831553Srgrimes register mode_t smode; 28454375Sjoe register u_long sflags; 2851553Srgrimes struct group *gr; 2861553Srgrimes struct passwd *pw; 2872860Srgrimes gid_t savegid = *pgid; 2882860Srgrimes uid_t saveuid = *puid; 2892860Srgrimes mode_t savemode = *pmode; 29054375Sjoe u_long saveflags = 0; 29154375Sjoe u_short maxgid, maxuid, maxmode, maxflags; 29254375Sjoe u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS]; 2932877Srgrimes static int first = 1; 2941553Srgrimes 2951553Srgrimes if ((p = fts_children(t, 0)) == NULL) { 2961553Srgrimes if (errno) 29730027Scharnier err(1, "line %d: %s", lineno, RP(parent)); 2981553Srgrimes return (1); 2991553Srgrimes } 3001553Srgrimes 3011553Srgrimes bzero(g, sizeof(g)); 3021553Srgrimes bzero(u, sizeof(u)); 3031553Srgrimes bzero(m, sizeof(m)); 30454375Sjoe bzero(f, sizeof(f)); 3051553Srgrimes 30654375Sjoe maxuid = maxgid = maxmode = maxflags = 0; 3071553Srgrimes for (; p; p = p->fts_link) { 3082860Srgrimes if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { 3092860Srgrimes smode = p->fts_statp->st_mode & MBITS; 3102860Srgrimes if (smode < MAXMODE && ++m[smode] > maxmode) { 3112860Srgrimes savemode = smode; 3122860Srgrimes maxmode = m[smode]; 3132860Srgrimes } 3142860Srgrimes sgid = p->fts_statp->st_gid; 3152860Srgrimes if (sgid < MAXGID && ++g[sgid] > maxgid) { 3162860Srgrimes savegid = sgid; 3172860Srgrimes maxgid = g[sgid]; 3182860Srgrimes } 3192860Srgrimes suid = p->fts_statp->st_uid; 3202860Srgrimes if (suid < MAXUID && ++u[suid] > maxuid) { 3212860Srgrimes saveuid = suid; 3222860Srgrimes maxuid = u[suid]; 3232860Srgrimes } 32454375Sjoe 32554375Sjoe /* 32654375Sjoe * XXX 32754375Sjoe * note that the below will break when file flags 32854375Sjoe * are extended beyond the first 4 bytes of each 32954375Sjoe * half word of the flags 33054375Sjoe */ 33154375Sjoe#define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0)) 33254375Sjoe sflags = p->fts_statp->st_flags; 33354375Sjoe if (FLAGS2IDX(sflags) < MAXFLAGS && 33454375Sjoe ++f[FLAGS2IDX(sflags)] > maxflags) { 33554375Sjoe saveflags = sflags; 33654375Sjoe maxflags = u[FLAGS2IDX(sflags)]; 33754375Sjoe } 3381553Srgrimes } 3391553Srgrimes } 3402860Srgrimes /* 3412860Srgrimes * If the /set record is the same as the last one we do not need to output 3422877Srgrimes * a new one. So first we check to see if anything changed. Note that we 3432877Srgrimes * always output a /set record for the first directory. 3442860Srgrimes */ 3452860Srgrimes if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || 3462860Srgrimes (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || 3472877Srgrimes ((keys & F_MODE) && (*pmode != savemode)) || (first)) { 3482877Srgrimes first = 0; 3492860Srgrimes if (dflag) 3502860Srgrimes (void)printf("/set type=dir"); 3511553Srgrimes else 3522860Srgrimes (void)printf("/set type=file"); 35351705Sbillf if (keys & F_UNAME) { 3542860Srgrimes if ((pw = getpwuid(saveuid)) != NULL) 3552860Srgrimes (void)printf(" uname=%s", pw->pw_name); 3562860Srgrimes else 35730027Scharnier errx(1, 35830027Scharnier "line %d: could not get uname for uid=%u", 35930027Scharnier lineno, saveuid); 36051705Sbillf } 3612860Srgrimes if (keys & F_UID) 36238020Sbde (void)printf(" uid=%lu", (u_long)saveuid); 36351705Sbillf if (keys & F_GNAME) { 3642860Srgrimes if ((gr = getgrgid(savegid)) != NULL) 3652860Srgrimes (void)printf(" gname=%s", gr->gr_name); 3662860Srgrimes else 36730027Scharnier errx(1, 36830027Scharnier "line %d: could not get gname for gid=%u", 36930027Scharnier lineno, savegid); 37051705Sbillf } 3712860Srgrimes if (keys & F_GID) 37238020Sbde (void)printf(" gid=%lu", (u_long)savegid); 3732860Srgrimes if (keys & F_MODE) 3742860Srgrimes (void)printf(" mode=%#o", savemode); 3752860Srgrimes if (keys & F_NLINK) 3762860Srgrimes (void)printf(" nlink=1"); 37754375Sjoe if (keys & F_FLAGS && saveflags) 37854375Sjoe (void)printf(" flags=%s", 37956692Sjoe getflags(saveflags, "none")); 3802860Srgrimes (void)printf("\n"); 3812860Srgrimes *puid = saveuid; 3822860Srgrimes *pgid = savegid; 3832860Srgrimes *pmode = savemode; 38454375Sjoe *pflags = saveflags; 3852860Srgrimes } 3861553Srgrimes return (0); 3871553Srgrimes} 3881553Srgrimes 3891553Srgrimesstatic int 3901553Srgrimesdsort(a, b) 3911553Srgrimes const FTSENT **a, **b; 3921553Srgrimes{ 3931553Srgrimes if (S_ISDIR((*a)->fts_statp->st_mode)) { 3941553Srgrimes if (!S_ISDIR((*b)->fts_statp->st_mode)) 3951553Srgrimes return (1); 3961553Srgrimes } else if (S_ISDIR((*b)->fts_statp->st_mode)) 3971553Srgrimes return (-1); 3981553Srgrimes return (strcmp((*a)->fts_name, (*b)->fts_name)); 3991553Srgrimes} 4001553Srgrimes 4011553Srgrimes#if __STDC__ 4021553Srgrimes#include <stdarg.h> 4031553Srgrimes#else 4041553Srgrimes#include <varargs.h> 4051553Srgrimes#endif 4061553Srgrimes 4071553Srgrimesvoid 4081553Srgrimes#if __STDC__ 4092860Srgrimesoutput(int indent, int *offset, const char *fmt, ...) 4101553Srgrimes#else 4112860Srgrimesoutput(indent, offset, fmt, va_alist) 4122860Srgrimes int indent; 4131553Srgrimes int *offset; 4141553Srgrimes char *fmt; 4151553Srgrimes va_dcl 4161553Srgrimes#endif 4171553Srgrimes{ 4181553Srgrimes va_list ap; 4191553Srgrimes char buf[1024]; 4201553Srgrimes#if __STDC__ 4211553Srgrimes va_start(ap, fmt); 4221553Srgrimes#else 4231553Srgrimes va_start(ap); 4241553Srgrimes#endif 4251553Srgrimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 4261553Srgrimes va_end(ap); 4271553Srgrimes 4281553Srgrimes if (*offset + strlen(buf) > MAXLINELEN - 3) { 4292860Srgrimes (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); 4302860Srgrimes *offset = INDENTNAMELEN + indent; 4311553Srgrimes } 4321553Srgrimes *offset += printf(" %s", buf) + 1; 4331553Srgrimes} 434