create.c revision 6286
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 351553Srgrimesstatic char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; 361553Srgrimes#endif /* not lint */ 371553Srgrimes 381553Srgrimes#include <sys/param.h> 391553Srgrimes#include <sys/stat.h> 401553Srgrimes#include <time.h> 411553Srgrimes#include <fcntl.h> 421553Srgrimes#include <fts.h> 431553Srgrimes#include <dirent.h> 441553Srgrimes#include <grp.h> 451553Srgrimes#include <pwd.h> 461553Srgrimes#include <errno.h> 471553Srgrimes#include <unistd.h> 481553Srgrimes#include <stdio.h> 496286Swollman#include <md5.h> 501553Srgrimes#include "mtree.h" 511553Srgrimes#include "extern.h" 521553Srgrimes 531553Srgrimes#define INDENTNAMELEN 15 541553Srgrimes#define MAXLINELEN 80 551553Srgrimes 562860Srgrimesextern long int crc_total; 572860Srgrimesextern int ftsoptions; 582860Srgrimesextern int dflag, iflag, nflag, sflag; 591553Srgrimesextern u_short keys; 601553Srgrimesextern char fullpath[MAXPATHLEN]; 611553Srgrimes 621553Srgrimesstatic gid_t gid; 631553Srgrimesstatic uid_t uid; 641553Srgrimesstatic mode_t mode; 651553Srgrimes 661553Srgrimesstatic int dsort __P((const FTSENT **, const FTSENT **)); 672860Srgrimesstatic void output __P((int, int *, const char *, ...)); 681553Srgrimesstatic int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *)); 692860Srgrimesstatic void statf __P((int, FTSENT *)); 701553Srgrimes 711553Srgrimesvoid 721553Srgrimescwalk() 731553Srgrimes{ 741553Srgrimes register FTS *t; 751553Srgrimes register FTSENT *p; 761553Srgrimes time_t clock; 771553Srgrimes char *argv[2], host[MAXHOSTNAMELEN]; 782860Srgrimes int indent = 0; 791553Srgrimes 801553Srgrimes (void)time(&clock); 811553Srgrimes (void)gethostname(host, sizeof(host)); 821553Srgrimes (void)printf( 831553Srgrimes "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 841553Srgrimes getlogin(), host, fullpath, ctime(&clock)); 851553Srgrimes 861553Srgrimes argv[0] = "."; 871553Srgrimes argv[1] = NULL; 881553Srgrimes if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 891553Srgrimes err("fts_open: %s", strerror(errno)); 902860Srgrimes while ((p = fts_read(t))) { 912860Srgrimes if (iflag) 922860Srgrimes indent = p->fts_level * 4; 931553Srgrimes switch(p->fts_info) { 941553Srgrimes case FTS_D: 952860Srgrimes if (!dflag) 962860Srgrimes (void)printf("\n"); 972860Srgrimes if (!nflag) 982860Srgrimes (void)printf("# %s\n", p->fts_path); 991553Srgrimes statd(t, p, &uid, &gid, &mode); 1002860Srgrimes statf(indent, p); 1011553Srgrimes break; 1021553Srgrimes case FTS_DP: 1032860Srgrimes if (!nflag && (p->fts_level > 0)) 1042860Srgrimes (void)printf("%*s# %s\n", indent, "", p->fts_path); 1052860Srgrimes (void)printf("%*s..\n", indent, ""); 1062860Srgrimes if (!dflag) 1072860Srgrimes (void)printf("\n"); 1081553Srgrimes break; 1091553Srgrimes case FTS_DNR: 1101553Srgrimes case FTS_ERR: 1111553Srgrimes case FTS_NS: 1121553Srgrimes (void)fprintf(stderr, 1131553Srgrimes "mtree: %s: %s\n", p->fts_path, strerror(errno)); 1141553Srgrimes break; 1151553Srgrimes default: 1161553Srgrimes if (!dflag) 1172860Srgrimes statf(indent, p); 1181553Srgrimes break; 1191553Srgrimes 1201553Srgrimes } 1212860Srgrimes } 1221553Srgrimes (void)fts_close(t); 1231553Srgrimes if (sflag && keys & F_CKSUM) 1241553Srgrimes (void)fprintf(stderr, 1251553Srgrimes "mtree: %s checksum: %lu\n", fullpath, crc_total); 1261553Srgrimes} 1271553Srgrimes 1281553Srgrimesstatic void 1292860Srgrimesstatf(indent, p) 1302860Srgrimes int indent; 1311553Srgrimes FTSENT *p; 1321553Srgrimes{ 1331553Srgrimes struct group *gr; 1341553Srgrimes struct passwd *pw; 1351553Srgrimes u_long len, val; 1362860Srgrimes int fd, offset; 1371553Srgrimes 1382860Srgrimes if (iflag || S_ISDIR(p->fts_statp->st_mode)) 1392860Srgrimes offset = printf("%*s%s", indent, "", p->fts_name); 1401553Srgrimes else 1412860Srgrimes offset = printf("%*s %s", indent, "", p->fts_name); 1421553Srgrimes 1432860Srgrimes if (offset > (INDENTNAMELEN + indent)) 1442860Srgrimes offset = MAXLINELEN; 1451553Srgrimes else 1462860Srgrimes offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); 1471553Srgrimes 1482860Srgrimes if (!S_ISREG(p->fts_statp->st_mode) && !dflag) 1492860Srgrimes output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); 1502860Srgrimes if (p->fts_statp->st_uid != uid) { 1512860Srgrimes if (keys & F_UNAME) { 1522860Srgrimes if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) { 1532860Srgrimes output(indent, &offset, "uname=%s", pw->pw_name); 1542860Srgrimes } else { 1552860Srgrimes err("could not get uname for uid=%u", 1562860Srgrimes p->fts_statp->st_uid); 1572860Srgrimes } 1582860Srgrimes } 1592860Srgrimes if (keys & F_UID) 1602860Srgrimes output(indent, &offset, "uid=%u", p->fts_statp->st_uid); 1612860Srgrimes } 1622860Srgrimes if (p->fts_statp->st_gid != gid) { 1632860Srgrimes if (keys & F_GNAME) { 1642860Srgrimes if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) { 1652860Srgrimes output(indent, &offset, "gname=%s", gr->gr_name); 1662860Srgrimes } else { 1672860Srgrimes err("could not get gname for gid=%u", 1682860Srgrimes p->fts_statp->st_gid); 1692860Srgrimes } 1702860Srgrimes } 1712860Srgrimes if (keys & F_GID) 1722860Srgrimes output(indent, &offset, "gid=%u", p->fts_statp->st_gid); 1732860Srgrimes } 1741553Srgrimes if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 1752860Srgrimes output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); 1761553Srgrimes if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 1772860Srgrimes output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); 1781553Srgrimes if (keys & F_SIZE) 1792860Srgrimes output(indent, &offset, "size=%qd", p->fts_statp->st_size); 1801553Srgrimes if (keys & F_TIME) 1812860Srgrimes output(indent, &offset, "time=%ld.%ld", 1821553Srgrimes p->fts_statp->st_mtimespec.ts_sec, 1831553Srgrimes p->fts_statp->st_mtimespec.ts_nsec); 1841553Srgrimes if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 1851553Srgrimes if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 1861553Srgrimes crc(fd, &val, &len)) 1871553Srgrimes err("%s: %s", p->fts_accpath, strerror(errno)); 1881553Srgrimes (void)close(fd); 1892860Srgrimes output(indent, &offset, "cksum=%lu", val); 1901553Srgrimes } 1916286Swollman if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) { 1926286Swollman char *md5digest = MD5File(p->fts_accpath); 1936286Swollman 1946286Swollman if (!md5digest) { 1956286Swollman err("%s: %s", p->fts_accpath, strerror(errno)); 1966286Swollman } else { 1976286Swollman output(indent, &offset, "md5digest=%s", md5digest); 1986286Swollman free(md5digest); 1996286Swollman } 2006286Swollman } 2011553Srgrimes if (keys & F_SLINK && 2021553Srgrimes (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 2032860Srgrimes output(indent, &offset, "link=%s", rlink(p->fts_accpath)); 2041553Srgrimes (void)putchar('\n'); 2051553Srgrimes} 2061553Srgrimes 2071553Srgrimes#define MAXGID 5000 2081553Srgrimes#define MAXUID 5000 2091553Srgrimes#define MAXMODE MBITS + 1 2101553Srgrimes 2111553Srgrimesstatic int 2121553Srgrimesstatd(t, parent, puid, pgid, pmode) 2131553Srgrimes FTS *t; 2141553Srgrimes FTSENT *parent; 2151553Srgrimes uid_t *puid; 2161553Srgrimes gid_t *pgid; 2171553Srgrimes mode_t *pmode; 2181553Srgrimes{ 2191553Srgrimes register FTSENT *p; 2201553Srgrimes register gid_t sgid; 2211553Srgrimes register uid_t suid; 2221553Srgrimes register mode_t smode; 2231553Srgrimes struct group *gr; 2241553Srgrimes struct passwd *pw; 2252860Srgrimes gid_t savegid = *pgid; 2262860Srgrimes uid_t saveuid = *puid; 2272860Srgrimes mode_t savemode = *pmode; 2281553Srgrimes u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE]; 2292877Srgrimes static int first = 1; 2301553Srgrimes 2311553Srgrimes if ((p = fts_children(t, 0)) == NULL) { 2321553Srgrimes if (errno) 2331553Srgrimes err("%s: %s", RP(parent), strerror(errno)); 2341553Srgrimes return (1); 2351553Srgrimes } 2361553Srgrimes 2371553Srgrimes bzero(g, sizeof(g)); 2381553Srgrimes bzero(u, sizeof(u)); 2391553Srgrimes bzero(m, sizeof(m)); 2401553Srgrimes 2411553Srgrimes maxuid = maxgid = maxmode = 0; 2421553Srgrimes for (; p; p = p->fts_link) { 2432860Srgrimes if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { 2442860Srgrimes smode = p->fts_statp->st_mode & MBITS; 2452860Srgrimes if (smode < MAXMODE && ++m[smode] > maxmode) { 2462860Srgrimes savemode = smode; 2472860Srgrimes maxmode = m[smode]; 2482860Srgrimes } 2492860Srgrimes sgid = p->fts_statp->st_gid; 2502860Srgrimes if (sgid < MAXGID && ++g[sgid] > maxgid) { 2512860Srgrimes savegid = sgid; 2522860Srgrimes maxgid = g[sgid]; 2532860Srgrimes } 2542860Srgrimes suid = p->fts_statp->st_uid; 2552860Srgrimes if (suid < MAXUID && ++u[suid] > maxuid) { 2562860Srgrimes saveuid = suid; 2572860Srgrimes maxuid = u[suid]; 2582860Srgrimes } 2591553Srgrimes } 2601553Srgrimes } 2612860Srgrimes /* 2622860Srgrimes * If the /set record is the same as the last one we do not need to output 2632877Srgrimes * a new one. So first we check to see if anything changed. Note that we 2642877Srgrimes * always output a /set record for the first directory. 2652860Srgrimes */ 2662860Srgrimes if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || 2672860Srgrimes (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || 2682877Srgrimes ((keys & F_MODE) && (*pmode != savemode)) || (first)) { 2692877Srgrimes first = 0; 2702860Srgrimes if (dflag) 2712860Srgrimes (void)printf("/set type=dir"); 2721553Srgrimes else 2732860Srgrimes (void)printf("/set type=file"); 2742860Srgrimes if (keys & F_UNAME) 2752860Srgrimes if ((pw = getpwuid(saveuid)) != NULL) 2762860Srgrimes (void)printf(" uname=%s", pw->pw_name); 2772860Srgrimes else 2782860Srgrimes err("could not get uname for uid=%u", saveuid); 2792860Srgrimes if (keys & F_UID) 2802860Srgrimes (void)printf(" uid=%lu", saveuid); 2812860Srgrimes if (keys & F_GNAME) 2822860Srgrimes if ((gr = getgrgid(savegid)) != NULL) 2832860Srgrimes (void)printf(" gname=%s", gr->gr_name); 2842860Srgrimes else 2852860Srgrimes err("could not get gname for gid=%u", savegid); 2862860Srgrimes if (keys & F_GID) 2872860Srgrimes (void)printf(" gid=%lu", savegid); 2882860Srgrimes if (keys & F_MODE) 2892860Srgrimes (void)printf(" mode=%#o", savemode); 2902860Srgrimes if (keys & F_NLINK) 2912860Srgrimes (void)printf(" nlink=1"); 2922860Srgrimes (void)printf("\n"); 2932860Srgrimes *puid = saveuid; 2942860Srgrimes *pgid = savegid; 2952860Srgrimes *pmode = savemode; 2962860Srgrimes } 2971553Srgrimes return (0); 2981553Srgrimes} 2991553Srgrimes 3001553Srgrimesstatic int 3011553Srgrimesdsort(a, b) 3021553Srgrimes const FTSENT **a, **b; 3031553Srgrimes{ 3041553Srgrimes if (S_ISDIR((*a)->fts_statp->st_mode)) { 3051553Srgrimes if (!S_ISDIR((*b)->fts_statp->st_mode)) 3061553Srgrimes return (1); 3071553Srgrimes } else if (S_ISDIR((*b)->fts_statp->st_mode)) 3081553Srgrimes return (-1); 3091553Srgrimes return (strcmp((*a)->fts_name, (*b)->fts_name)); 3101553Srgrimes} 3111553Srgrimes 3121553Srgrimes#if __STDC__ 3131553Srgrimes#include <stdarg.h> 3141553Srgrimes#else 3151553Srgrimes#include <varargs.h> 3161553Srgrimes#endif 3171553Srgrimes 3181553Srgrimesvoid 3191553Srgrimes#if __STDC__ 3202860Srgrimesoutput(int indent, int *offset, const char *fmt, ...) 3211553Srgrimes#else 3222860Srgrimesoutput(indent, offset, fmt, va_alist) 3232860Srgrimes int indent; 3241553Srgrimes int *offset; 3251553Srgrimes char *fmt; 3261553Srgrimes va_dcl 3271553Srgrimes#endif 3281553Srgrimes{ 3291553Srgrimes va_list ap; 3301553Srgrimes char buf[1024]; 3311553Srgrimes#if __STDC__ 3321553Srgrimes va_start(ap, fmt); 3331553Srgrimes#else 3341553Srgrimes va_start(ap); 3351553Srgrimes#endif 3361553Srgrimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 3371553Srgrimes va_end(ap); 3381553Srgrimes 3391553Srgrimes if (*offset + strlen(buf) > MAXLINELEN - 3) { 3402860Srgrimes (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); 3412860Srgrimes *offset = INDENTNAMELEN + indent; 3421553Srgrimes } 3431553Srgrimes *offset += printf(" %s", buf) + 1; 3441553Srgrimes} 345