create.c revision 2877
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> 491553Srgrimes#include "mtree.h" 501553Srgrimes#include "extern.h" 511553Srgrimes 521553Srgrimes#define INDENTNAMELEN 15 531553Srgrimes#define MAXLINELEN 80 541553Srgrimes 552860Srgrimesextern long int crc_total; 562860Srgrimesextern int ftsoptions; 572860Srgrimesextern int dflag, iflag, nflag, sflag; 581553Srgrimesextern u_short keys; 591553Srgrimesextern char fullpath[MAXPATHLEN]; 601553Srgrimes 611553Srgrimesstatic gid_t gid; 621553Srgrimesstatic uid_t uid; 631553Srgrimesstatic mode_t mode; 641553Srgrimes 651553Srgrimesstatic int dsort __P((const FTSENT **, const FTSENT **)); 662860Srgrimesstatic void output __P((int, int *, const char *, ...)); 671553Srgrimesstatic int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *)); 682860Srgrimesstatic void statf __P((int, FTSENT *)); 691553Srgrimes 701553Srgrimesvoid 711553Srgrimescwalk() 721553Srgrimes{ 731553Srgrimes register FTS *t; 741553Srgrimes register FTSENT *p; 751553Srgrimes time_t clock; 761553Srgrimes char *argv[2], host[MAXHOSTNAMELEN]; 772860Srgrimes int indent = 0; 781553Srgrimes 791553Srgrimes (void)time(&clock); 801553Srgrimes (void)gethostname(host, sizeof(host)); 811553Srgrimes (void)printf( 821553Srgrimes "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 831553Srgrimes getlogin(), host, fullpath, ctime(&clock)); 841553Srgrimes 851553Srgrimes argv[0] = "."; 861553Srgrimes argv[1] = NULL; 871553Srgrimes if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 881553Srgrimes err("fts_open: %s", strerror(errno)); 892860Srgrimes while ((p = fts_read(t))) { 902860Srgrimes if (iflag) 912860Srgrimes indent = p->fts_level * 4; 921553Srgrimes switch(p->fts_info) { 931553Srgrimes case FTS_D: 942860Srgrimes if (!dflag) 952860Srgrimes (void)printf("\n"); 962860Srgrimes if (!nflag) 972860Srgrimes (void)printf("# %s\n", p->fts_path); 981553Srgrimes statd(t, p, &uid, &gid, &mode); 992860Srgrimes statf(indent, p); 1001553Srgrimes break; 1011553Srgrimes case FTS_DP: 1022860Srgrimes if (!nflag && (p->fts_level > 0)) 1032860Srgrimes (void)printf("%*s# %s\n", indent, "", p->fts_path); 1042860Srgrimes (void)printf("%*s..\n", indent, ""); 1052860Srgrimes if (!dflag) 1062860Srgrimes (void)printf("\n"); 1071553Srgrimes break; 1081553Srgrimes case FTS_DNR: 1091553Srgrimes case FTS_ERR: 1101553Srgrimes case FTS_NS: 1111553Srgrimes (void)fprintf(stderr, 1121553Srgrimes "mtree: %s: %s\n", p->fts_path, strerror(errno)); 1131553Srgrimes break; 1141553Srgrimes default: 1151553Srgrimes if (!dflag) 1162860Srgrimes statf(indent, p); 1171553Srgrimes break; 1181553Srgrimes 1191553Srgrimes } 1202860Srgrimes } 1211553Srgrimes (void)fts_close(t); 1221553Srgrimes if (sflag && keys & F_CKSUM) 1231553Srgrimes (void)fprintf(stderr, 1241553Srgrimes "mtree: %s checksum: %lu\n", fullpath, crc_total); 1251553Srgrimes} 1261553Srgrimes 1271553Srgrimesstatic void 1282860Srgrimesstatf(indent, p) 1292860Srgrimes int indent; 1301553Srgrimes FTSENT *p; 1311553Srgrimes{ 1321553Srgrimes struct group *gr; 1331553Srgrimes struct passwd *pw; 1341553Srgrimes u_long len, val; 1352860Srgrimes int fd, offset; 1361553Srgrimes 1372860Srgrimes if (iflag || S_ISDIR(p->fts_statp->st_mode)) 1382860Srgrimes offset = printf("%*s%s", indent, "", p->fts_name); 1391553Srgrimes else 1402860Srgrimes offset = printf("%*s %s", indent, "", p->fts_name); 1411553Srgrimes 1422860Srgrimes if (offset > (INDENTNAMELEN + indent)) 1432860Srgrimes offset = MAXLINELEN; 1441553Srgrimes else 1452860Srgrimes offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); 1461553Srgrimes 1472860Srgrimes if (!S_ISREG(p->fts_statp->st_mode) && !dflag) 1482860Srgrimes output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); 1492860Srgrimes if (p->fts_statp->st_uid != uid) { 1502860Srgrimes if (keys & F_UNAME) { 1512860Srgrimes if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) { 1522860Srgrimes output(indent, &offset, "uname=%s", pw->pw_name); 1532860Srgrimes } else { 1542860Srgrimes err("could not get uname for uid=%u", 1552860Srgrimes p->fts_statp->st_uid); 1562860Srgrimes } 1572860Srgrimes } 1582860Srgrimes if (keys & F_UID) 1592860Srgrimes output(indent, &offset, "uid=%u", p->fts_statp->st_uid); 1602860Srgrimes } 1612860Srgrimes if (p->fts_statp->st_gid != gid) { 1622860Srgrimes if (keys & F_GNAME) { 1632860Srgrimes if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) { 1642860Srgrimes output(indent, &offset, "gname=%s", gr->gr_name); 1652860Srgrimes } else { 1662860Srgrimes err("could not get gname for gid=%u", 1672860Srgrimes p->fts_statp->st_gid); 1682860Srgrimes } 1692860Srgrimes } 1702860Srgrimes if (keys & F_GID) 1712860Srgrimes output(indent, &offset, "gid=%u", p->fts_statp->st_gid); 1722860Srgrimes } 1731553Srgrimes if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 1742860Srgrimes output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); 1751553Srgrimes if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 1762860Srgrimes output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); 1771553Srgrimes if (keys & F_SIZE) 1782860Srgrimes output(indent, &offset, "size=%qd", p->fts_statp->st_size); 1791553Srgrimes if (keys & F_TIME) 1802860Srgrimes output(indent, &offset, "time=%ld.%ld", 1811553Srgrimes p->fts_statp->st_mtimespec.ts_sec, 1821553Srgrimes p->fts_statp->st_mtimespec.ts_nsec); 1831553Srgrimes if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 1841553Srgrimes if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 1851553Srgrimes crc(fd, &val, &len)) 1861553Srgrimes err("%s: %s", p->fts_accpath, strerror(errno)); 1871553Srgrimes (void)close(fd); 1882860Srgrimes output(indent, &offset, "cksum=%lu", val); 1891553Srgrimes } 1901553Srgrimes if (keys & F_SLINK && 1911553Srgrimes (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 1922860Srgrimes output(indent, &offset, "link=%s", rlink(p->fts_accpath)); 1931553Srgrimes (void)putchar('\n'); 1941553Srgrimes} 1951553Srgrimes 1961553Srgrimes#define MAXGID 5000 1971553Srgrimes#define MAXUID 5000 1981553Srgrimes#define MAXMODE MBITS + 1 1991553Srgrimes 2001553Srgrimesstatic int 2011553Srgrimesstatd(t, parent, puid, pgid, pmode) 2021553Srgrimes FTS *t; 2031553Srgrimes FTSENT *parent; 2041553Srgrimes uid_t *puid; 2051553Srgrimes gid_t *pgid; 2061553Srgrimes mode_t *pmode; 2071553Srgrimes{ 2081553Srgrimes register FTSENT *p; 2091553Srgrimes register gid_t sgid; 2101553Srgrimes register uid_t suid; 2111553Srgrimes register mode_t smode; 2121553Srgrimes struct group *gr; 2131553Srgrimes struct passwd *pw; 2142860Srgrimes gid_t savegid = *pgid; 2152860Srgrimes uid_t saveuid = *puid; 2162860Srgrimes mode_t savemode = *pmode; 2171553Srgrimes u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE]; 2182877Srgrimes static int first = 1; 2191553Srgrimes 2201553Srgrimes if ((p = fts_children(t, 0)) == NULL) { 2211553Srgrimes if (errno) 2221553Srgrimes err("%s: %s", RP(parent), strerror(errno)); 2231553Srgrimes return (1); 2241553Srgrimes } 2251553Srgrimes 2261553Srgrimes bzero(g, sizeof(g)); 2271553Srgrimes bzero(u, sizeof(u)); 2281553Srgrimes bzero(m, sizeof(m)); 2291553Srgrimes 2301553Srgrimes maxuid = maxgid = maxmode = 0; 2311553Srgrimes for (; p; p = p->fts_link) { 2322860Srgrimes if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { 2332860Srgrimes smode = p->fts_statp->st_mode & MBITS; 2342860Srgrimes if (smode < MAXMODE && ++m[smode] > maxmode) { 2352860Srgrimes savemode = smode; 2362860Srgrimes maxmode = m[smode]; 2372860Srgrimes } 2382860Srgrimes sgid = p->fts_statp->st_gid; 2392860Srgrimes if (sgid < MAXGID && ++g[sgid] > maxgid) { 2402860Srgrimes savegid = sgid; 2412860Srgrimes maxgid = g[sgid]; 2422860Srgrimes } 2432860Srgrimes suid = p->fts_statp->st_uid; 2442860Srgrimes if (suid < MAXUID && ++u[suid] > maxuid) { 2452860Srgrimes saveuid = suid; 2462860Srgrimes maxuid = u[suid]; 2472860Srgrimes } 2481553Srgrimes } 2491553Srgrimes } 2502860Srgrimes /* 2512860Srgrimes * If the /set record is the same as the last one we do not need to output 2522877Srgrimes * a new one. So first we check to see if anything changed. Note that we 2532877Srgrimes * always output a /set record for the first directory. 2542860Srgrimes */ 2552860Srgrimes if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || 2562860Srgrimes (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || 2572877Srgrimes ((keys & F_MODE) && (*pmode != savemode)) || (first)) { 2582877Srgrimes first = 0; 2592860Srgrimes if (dflag) 2602860Srgrimes (void)printf("/set type=dir"); 2611553Srgrimes else 2622860Srgrimes (void)printf("/set type=file"); 2632860Srgrimes if (keys & F_UNAME) 2642860Srgrimes if ((pw = getpwuid(saveuid)) != NULL) 2652860Srgrimes (void)printf(" uname=%s", pw->pw_name); 2662860Srgrimes else 2672860Srgrimes err("could not get uname for uid=%u", saveuid); 2682860Srgrimes if (keys & F_UID) 2692860Srgrimes (void)printf(" uid=%lu", saveuid); 2702860Srgrimes if (keys & F_GNAME) 2712860Srgrimes if ((gr = getgrgid(savegid)) != NULL) 2722860Srgrimes (void)printf(" gname=%s", gr->gr_name); 2732860Srgrimes else 2742860Srgrimes err("could not get gname for gid=%u", savegid); 2752860Srgrimes if (keys & F_GID) 2762860Srgrimes (void)printf(" gid=%lu", savegid); 2772860Srgrimes if (keys & F_MODE) 2782860Srgrimes (void)printf(" mode=%#o", savemode); 2792860Srgrimes if (keys & F_NLINK) 2802860Srgrimes (void)printf(" nlink=1"); 2812860Srgrimes (void)printf("\n"); 2822860Srgrimes *puid = saveuid; 2832860Srgrimes *pgid = savegid; 2842860Srgrimes *pmode = savemode; 2852860Srgrimes } 2861553Srgrimes return (0); 2871553Srgrimes} 2881553Srgrimes 2891553Srgrimesstatic int 2901553Srgrimesdsort(a, b) 2911553Srgrimes const FTSENT **a, **b; 2921553Srgrimes{ 2931553Srgrimes if (S_ISDIR((*a)->fts_statp->st_mode)) { 2941553Srgrimes if (!S_ISDIR((*b)->fts_statp->st_mode)) 2951553Srgrimes return (1); 2961553Srgrimes } else if (S_ISDIR((*b)->fts_statp->st_mode)) 2971553Srgrimes return (-1); 2981553Srgrimes return (strcmp((*a)->fts_name, (*b)->fts_name)); 2991553Srgrimes} 3001553Srgrimes 3011553Srgrimes#if __STDC__ 3021553Srgrimes#include <stdarg.h> 3031553Srgrimes#else 3041553Srgrimes#include <varargs.h> 3051553Srgrimes#endif 3061553Srgrimes 3071553Srgrimesvoid 3081553Srgrimes#if __STDC__ 3092860Srgrimesoutput(int indent, int *offset, const char *fmt, ...) 3101553Srgrimes#else 3112860Srgrimesoutput(indent, offset, fmt, va_alist) 3122860Srgrimes int indent; 3131553Srgrimes int *offset; 3141553Srgrimes char *fmt; 3151553Srgrimes va_dcl 3161553Srgrimes#endif 3171553Srgrimes{ 3181553Srgrimes va_list ap; 3191553Srgrimes char buf[1024]; 3201553Srgrimes#if __STDC__ 3211553Srgrimes va_start(ap, fmt); 3221553Srgrimes#else 3231553Srgrimes va_start(ap); 3241553Srgrimes#endif 3251553Srgrimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 3261553Srgrimes va_end(ap); 3271553Srgrimes 3281553Srgrimes if (*offset + strlen(buf) > MAXLINELEN - 3) { 3292860Srgrimes (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); 3302860Srgrimes *offset = INDENTNAMELEN + indent; 3311553Srgrimes } 3321553Srgrimes *offset += printf(" %s", buf) + 1; 3331553Srgrimes} 334