create.c revision 99800
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 99800 2002-07-11 18:31:16Z alfred $"; 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; 8066584Sphkstatic u_long flags = 0xffffffff; 811553Srgrimes 8299800Salfredstatic int dsort(const FTSENT **, const FTSENT **); 8399800Salfredstatic void output(int, int *, const char *, ...) __printflike(3, 4); 8499800Salfredstatic int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *); 8599800Salfredstatic void statf(int, FTSENT *); 861553Srgrimes 871553Srgrimesvoid 881553Srgrimescwalk() 891553Srgrimes{ 901553Srgrimes register FTS *t; 911553Srgrimes register FTSENT *p; 921553Srgrimes time_t clock; 931553Srgrimes char *argv[2], host[MAXHOSTNAMELEN]; 942860Srgrimes int indent = 0; 951553Srgrimes 961553Srgrimes (void)time(&clock); 971553Srgrimes (void)gethostname(host, sizeof(host)); 981553Srgrimes (void)printf( 991553Srgrimes "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 1001553Srgrimes getlogin(), host, fullpath, ctime(&clock)); 1011553Srgrimes 1021553Srgrimes argv[0] = "."; 1031553Srgrimes argv[1] = NULL; 1041553Srgrimes if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 10530027Scharnier err(1, "line %d: fts_open", lineno); 1062860Srgrimes while ((p = fts_read(t))) { 1072860Srgrimes if (iflag) 1082860Srgrimes indent = p->fts_level * 4; 10960418Swollman if (check_excludes(p->fts_name, p->fts_path)) { 11060418Swollman fts_set(t, p, FTS_SKIP); 11160418Swollman continue; 11260418Swollman } 1131553Srgrimes switch(p->fts_info) { 1141553Srgrimes case FTS_D: 1152860Srgrimes if (!dflag) 1162860Srgrimes (void)printf("\n"); 1172860Srgrimes if (!nflag) 1182860Srgrimes (void)printf("# %s\n", p->fts_path); 11954375Sjoe statd(t, p, &uid, &gid, &mode, &flags); 1202860Srgrimes statf(indent, p); 1211553Srgrimes break; 1221553Srgrimes case FTS_DP: 1232860Srgrimes if (!nflag && (p->fts_level > 0)) 1242860Srgrimes (void)printf("%*s# %s\n", indent, "", p->fts_path); 1252860Srgrimes (void)printf("%*s..\n", indent, ""); 1262860Srgrimes if (!dflag) 1272860Srgrimes (void)printf("\n"); 1281553Srgrimes break; 1291553Srgrimes case FTS_DNR: 1301553Srgrimes case FTS_ERR: 1311553Srgrimes case FTS_NS: 13230027Scharnier warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1331553Srgrimes break; 1341553Srgrimes default: 1351553Srgrimes if (!dflag) 1362860Srgrimes statf(indent, p); 1371553Srgrimes break; 1388857Srgrimes 1391553Srgrimes } 1402860Srgrimes } 1411553Srgrimes (void)fts_close(t); 1421553Srgrimes if (sflag && keys & F_CKSUM) 14330027Scharnier warnx("%s checksum: %lu", fullpath, crc_total); 1441553Srgrimes} 1451553Srgrimes 1461553Srgrimesstatic void 1472860Srgrimesstatf(indent, p) 1482860Srgrimes int indent; 1491553Srgrimes FTSENT *p; 1501553Srgrimes{ 1511553Srgrimes struct group *gr; 1521553Srgrimes struct passwd *pw; 1531553Srgrimes u_long len, val; 1542860Srgrimes int fd, offset; 15561749Sjoe char *fflags; 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)); 25961749Sjoe if (keys & F_FLAGS && p->fts_statp->st_flags != flags) { 26061749Sjoe fflags = flags_to_string(p->fts_statp->st_flags); 26161749Sjoe output(indent, &offset, "flags=%s", fflags); 26261749Sjoe free(fflags); 26361749Sjoe } 2641553Srgrimes (void)putchar('\n'); 2651553Srgrimes} 2661553Srgrimes 2671553Srgrimes#define MAXGID 5000 2681553Srgrimes#define MAXUID 5000 2691553Srgrimes#define MAXMODE MBITS + 1 27054375Sjoe#define MAXFLAGS 256 27154375Sjoe#define MAXS 16 2721553Srgrimes 2731553Srgrimesstatic int 27454375Sjoestatd(t, parent, puid, pgid, pmode, pflags) 2751553Srgrimes FTS *t; 2761553Srgrimes FTSENT *parent; 2771553Srgrimes uid_t *puid; 2781553Srgrimes gid_t *pgid; 2791553Srgrimes mode_t *pmode; 28054375Sjoe u_long *pflags; 2811553Srgrimes{ 2821553Srgrimes register FTSENT *p; 2831553Srgrimes register gid_t sgid; 2841553Srgrimes register uid_t suid; 2851553Srgrimes register mode_t smode; 28654375Sjoe register u_long sflags; 2871553Srgrimes struct group *gr; 2881553Srgrimes struct passwd *pw; 2892860Srgrimes gid_t savegid = *pgid; 2902860Srgrimes uid_t saveuid = *puid; 2912860Srgrimes mode_t savemode = *pmode; 29266584Sphk u_long saveflags = *pflags; 29354375Sjoe u_short maxgid, maxuid, maxmode, maxflags; 29454375Sjoe u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS]; 29561749Sjoe char *fflags; 2962877Srgrimes static int first = 1; 2971553Srgrimes 2981553Srgrimes if ((p = fts_children(t, 0)) == NULL) { 2991553Srgrimes if (errno) 30030027Scharnier err(1, "line %d: %s", lineno, RP(parent)); 3011553Srgrimes return (1); 3021553Srgrimes } 3031553Srgrimes 3041553Srgrimes bzero(g, sizeof(g)); 3051553Srgrimes bzero(u, sizeof(u)); 3061553Srgrimes bzero(m, sizeof(m)); 30754375Sjoe bzero(f, sizeof(f)); 3081553Srgrimes 30954375Sjoe maxuid = maxgid = maxmode = maxflags = 0; 3101553Srgrimes for (; p; p = p->fts_link) { 3112860Srgrimes if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { 3122860Srgrimes smode = p->fts_statp->st_mode & MBITS; 3132860Srgrimes if (smode < MAXMODE && ++m[smode] > maxmode) { 3142860Srgrimes savemode = smode; 3152860Srgrimes maxmode = m[smode]; 3162860Srgrimes } 3172860Srgrimes sgid = p->fts_statp->st_gid; 3182860Srgrimes if (sgid < MAXGID && ++g[sgid] > maxgid) { 3192860Srgrimes savegid = sgid; 3202860Srgrimes maxgid = g[sgid]; 3212860Srgrimes } 3222860Srgrimes suid = p->fts_statp->st_uid; 3232860Srgrimes if (suid < MAXUID && ++u[suid] > maxuid) { 3242860Srgrimes saveuid = suid; 3252860Srgrimes maxuid = u[suid]; 3262860Srgrimes } 32754375Sjoe 32854375Sjoe /* 32954375Sjoe * XXX 33054375Sjoe * note that the below will break when file flags 33154375Sjoe * are extended beyond the first 4 bytes of each 33254375Sjoe * half word of the flags 33354375Sjoe */ 33454375Sjoe#define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0)) 33554375Sjoe sflags = p->fts_statp->st_flags; 33654375Sjoe if (FLAGS2IDX(sflags) < MAXFLAGS && 33754375Sjoe ++f[FLAGS2IDX(sflags)] > maxflags) { 33854375Sjoe saveflags = sflags; 33966584Sphk maxflags = f[FLAGS2IDX(sflags)]; 34054375Sjoe } 3411553Srgrimes } 3421553Srgrimes } 3432860Srgrimes /* 3442860Srgrimes * If the /set record is the same as the last one we do not need to output 3452877Srgrimes * a new one. So first we check to see if anything changed. Note that we 3462877Srgrimes * always output a /set record for the first directory. 3472860Srgrimes */ 3482860Srgrimes if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || 3492860Srgrimes (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || 35066584Sphk ((keys & F_MODE) && (*pmode != savemode)) || 35166584Sphk ((keys & F_FLAGS) && (*pflags != saveflags)) || 35266584Sphk (first)) { 3532877Srgrimes first = 0; 3542860Srgrimes if (dflag) 3552860Srgrimes (void)printf("/set type=dir"); 3561553Srgrimes else 3572860Srgrimes (void)printf("/set type=file"); 35851705Sbillf if (keys & F_UNAME) { 3592860Srgrimes if ((pw = getpwuid(saveuid)) != NULL) 3602860Srgrimes (void)printf(" uname=%s", pw->pw_name); 3612860Srgrimes else 36230027Scharnier errx(1, 36330027Scharnier "line %d: could not get uname for uid=%u", 36430027Scharnier lineno, saveuid); 36551705Sbillf } 3662860Srgrimes if (keys & F_UID) 36738020Sbde (void)printf(" uid=%lu", (u_long)saveuid); 36851705Sbillf if (keys & F_GNAME) { 3692860Srgrimes if ((gr = getgrgid(savegid)) != NULL) 3702860Srgrimes (void)printf(" gname=%s", gr->gr_name); 3712860Srgrimes else 37230027Scharnier errx(1, 37330027Scharnier "line %d: could not get gname for gid=%u", 37430027Scharnier lineno, savegid); 37551705Sbillf } 3762860Srgrimes if (keys & F_GID) 37738020Sbde (void)printf(" gid=%lu", (u_long)savegid); 3782860Srgrimes if (keys & F_MODE) 3792860Srgrimes (void)printf(" mode=%#o", savemode); 3802860Srgrimes if (keys & F_NLINK) 3812860Srgrimes (void)printf(" nlink=1"); 38266584Sphk if (keys & F_FLAGS) { 38361749Sjoe fflags = flags_to_string(saveflags); 38461749Sjoe (void)printf(" flags=%s", fflags); 38561749Sjoe free(fflags); 38661749Sjoe } 3872860Srgrimes (void)printf("\n"); 3882860Srgrimes *puid = saveuid; 3892860Srgrimes *pgid = savegid; 3902860Srgrimes *pmode = savemode; 39154375Sjoe *pflags = saveflags; 3922860Srgrimes } 3931553Srgrimes return (0); 3941553Srgrimes} 3951553Srgrimes 3961553Srgrimesstatic int 3971553Srgrimesdsort(a, b) 3981553Srgrimes const FTSENT **a, **b; 3991553Srgrimes{ 4001553Srgrimes if (S_ISDIR((*a)->fts_statp->st_mode)) { 4011553Srgrimes if (!S_ISDIR((*b)->fts_statp->st_mode)) 4021553Srgrimes return (1); 4031553Srgrimes } else if (S_ISDIR((*b)->fts_statp->st_mode)) 4041553Srgrimes return (-1); 4051553Srgrimes return (strcmp((*a)->fts_name, (*b)->fts_name)); 4061553Srgrimes} 4071553Srgrimes 4081553Srgrimes#include <stdarg.h> 4091553Srgrimes 4101553Srgrimesvoid 4112860Srgrimesoutput(int indent, int *offset, const char *fmt, ...) 4121553Srgrimes{ 4131553Srgrimes va_list ap; 4141553Srgrimes char buf[1024]; 4151553Srgrimes va_start(ap, fmt); 4161553Srgrimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 4171553Srgrimes va_end(ap); 4181553Srgrimes 4191553Srgrimes if (*offset + strlen(buf) > MAXLINELEN - 3) { 4202860Srgrimes (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); 4212860Srgrimes *offset = INDENTNAMELEN + indent; 4221553Srgrimes } 4231553Srgrimes *offset += printf(" %s", buf) + 1; 4241553Srgrimes} 425