create.c revision 2877
1/*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; 36#endif /* not lint */ 37 38#include <sys/param.h> 39#include <sys/stat.h> 40#include <time.h> 41#include <fcntl.h> 42#include <fts.h> 43#include <dirent.h> 44#include <grp.h> 45#include <pwd.h> 46#include <errno.h> 47#include <unistd.h> 48#include <stdio.h> 49#include "mtree.h" 50#include "extern.h" 51 52#define INDENTNAMELEN 15 53#define MAXLINELEN 80 54 55extern long int crc_total; 56extern int ftsoptions; 57extern int dflag, iflag, nflag, sflag; 58extern u_short keys; 59extern char fullpath[MAXPATHLEN]; 60 61static gid_t gid; 62static uid_t uid; 63static mode_t mode; 64 65static int dsort __P((const FTSENT **, const FTSENT **)); 66static void output __P((int, int *, const char *, ...)); 67static int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *)); 68static void statf __P((int, FTSENT *)); 69 70void 71cwalk() 72{ 73 register FTS *t; 74 register FTSENT *p; 75 time_t clock; 76 char *argv[2], host[MAXHOSTNAMELEN]; 77 int indent = 0; 78 79 (void)time(&clock); 80 (void)gethostname(host, sizeof(host)); 81 (void)printf( 82 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 83 getlogin(), host, fullpath, ctime(&clock)); 84 85 argv[0] = "."; 86 argv[1] = NULL; 87 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 88 err("fts_open: %s", strerror(errno)); 89 while ((p = fts_read(t))) { 90 if (iflag) 91 indent = p->fts_level * 4; 92 switch(p->fts_info) { 93 case FTS_D: 94 if (!dflag) 95 (void)printf("\n"); 96 if (!nflag) 97 (void)printf("# %s\n", p->fts_path); 98 statd(t, p, &uid, &gid, &mode); 99 statf(indent, p); 100 break; 101 case FTS_DP: 102 if (!nflag && (p->fts_level > 0)) 103 (void)printf("%*s# %s\n", indent, "", p->fts_path); 104 (void)printf("%*s..\n", indent, ""); 105 if (!dflag) 106 (void)printf("\n"); 107 break; 108 case FTS_DNR: 109 case FTS_ERR: 110 case FTS_NS: 111 (void)fprintf(stderr, 112 "mtree: %s: %s\n", p->fts_path, strerror(errno)); 113 break; 114 default: 115 if (!dflag) 116 statf(indent, p); 117 break; 118 119 } 120 } 121 (void)fts_close(t); 122 if (sflag && keys & F_CKSUM) 123 (void)fprintf(stderr, 124 "mtree: %s checksum: %lu\n", fullpath, crc_total); 125} 126 127static void 128statf(indent, p) 129 int indent; 130 FTSENT *p; 131{ 132 struct group *gr; 133 struct passwd *pw; 134 u_long len, val; 135 int fd, offset; 136 137 if (iflag || S_ISDIR(p->fts_statp->st_mode)) 138 offset = printf("%*s%s", indent, "", p->fts_name); 139 else 140 offset = printf("%*s %s", indent, "", p->fts_name); 141 142 if (offset > (INDENTNAMELEN + indent)) 143 offset = MAXLINELEN; 144 else 145 offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); 146 147 if (!S_ISREG(p->fts_statp->st_mode) && !dflag) 148 output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); 149 if (p->fts_statp->st_uid != uid) { 150 if (keys & F_UNAME) { 151 if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) { 152 output(indent, &offset, "uname=%s", pw->pw_name); 153 } else { 154 err("could not get uname for uid=%u", 155 p->fts_statp->st_uid); 156 } 157 } 158 if (keys & F_UID) 159 output(indent, &offset, "uid=%u", p->fts_statp->st_uid); 160 } 161 if (p->fts_statp->st_gid != gid) { 162 if (keys & F_GNAME) { 163 if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) { 164 output(indent, &offset, "gname=%s", gr->gr_name); 165 } else { 166 err("could not get gname for gid=%u", 167 p->fts_statp->st_gid); 168 } 169 } 170 if (keys & F_GID) 171 output(indent, &offset, "gid=%u", p->fts_statp->st_gid); 172 } 173 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 174 output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); 175 if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 176 output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); 177 if (keys & F_SIZE) 178 output(indent, &offset, "size=%qd", p->fts_statp->st_size); 179 if (keys & F_TIME) 180 output(indent, &offset, "time=%ld.%ld", 181 p->fts_statp->st_mtimespec.ts_sec, 182 p->fts_statp->st_mtimespec.ts_nsec); 183 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 184 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 185 crc(fd, &val, &len)) 186 err("%s: %s", p->fts_accpath, strerror(errno)); 187 (void)close(fd); 188 output(indent, &offset, "cksum=%lu", val); 189 } 190 if (keys & F_SLINK && 191 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 192 output(indent, &offset, "link=%s", rlink(p->fts_accpath)); 193 (void)putchar('\n'); 194} 195 196#define MAXGID 5000 197#define MAXUID 5000 198#define MAXMODE MBITS + 1 199 200static int 201statd(t, parent, puid, pgid, pmode) 202 FTS *t; 203 FTSENT *parent; 204 uid_t *puid; 205 gid_t *pgid; 206 mode_t *pmode; 207{ 208 register FTSENT *p; 209 register gid_t sgid; 210 register uid_t suid; 211 register mode_t smode; 212 struct group *gr; 213 struct passwd *pw; 214 gid_t savegid = *pgid; 215 uid_t saveuid = *puid; 216 mode_t savemode = *pmode; 217 u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE]; 218 static int first = 1; 219 220 if ((p = fts_children(t, 0)) == NULL) { 221 if (errno) 222 err("%s: %s", RP(parent), strerror(errno)); 223 return (1); 224 } 225 226 bzero(g, sizeof(g)); 227 bzero(u, sizeof(u)); 228 bzero(m, sizeof(m)); 229 230 maxuid = maxgid = maxmode = 0; 231 for (; p; p = p->fts_link) { 232 if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { 233 smode = p->fts_statp->st_mode & MBITS; 234 if (smode < MAXMODE && ++m[smode] > maxmode) { 235 savemode = smode; 236 maxmode = m[smode]; 237 } 238 sgid = p->fts_statp->st_gid; 239 if (sgid < MAXGID && ++g[sgid] > maxgid) { 240 savegid = sgid; 241 maxgid = g[sgid]; 242 } 243 suid = p->fts_statp->st_uid; 244 if (suid < MAXUID && ++u[suid] > maxuid) { 245 saveuid = suid; 246 maxuid = u[suid]; 247 } 248 } 249 } 250 /* 251 * If the /set record is the same as the last one we do not need to output 252 * a new one. So first we check to see if anything changed. Note that we 253 * always output a /set record for the first directory. 254 */ 255 if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || 256 (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || 257 ((keys & F_MODE) && (*pmode != savemode)) || (first)) { 258 first = 0; 259 if (dflag) 260 (void)printf("/set type=dir"); 261 else 262 (void)printf("/set type=file"); 263 if (keys & F_UNAME) 264 if ((pw = getpwuid(saveuid)) != NULL) 265 (void)printf(" uname=%s", pw->pw_name); 266 else 267 err("could not get uname for uid=%u", saveuid); 268 if (keys & F_UID) 269 (void)printf(" uid=%lu", saveuid); 270 if (keys & F_GNAME) 271 if ((gr = getgrgid(savegid)) != NULL) 272 (void)printf(" gname=%s", gr->gr_name); 273 else 274 err("could not get gname for gid=%u", savegid); 275 if (keys & F_GID) 276 (void)printf(" gid=%lu", savegid); 277 if (keys & F_MODE) 278 (void)printf(" mode=%#o", savemode); 279 if (keys & F_NLINK) 280 (void)printf(" nlink=1"); 281 (void)printf("\n"); 282 *puid = saveuid; 283 *pgid = savegid; 284 *pmode = savemode; 285 } 286 return (0); 287} 288 289static int 290dsort(a, b) 291 const FTSENT **a, **b; 292{ 293 if (S_ISDIR((*a)->fts_statp->st_mode)) { 294 if (!S_ISDIR((*b)->fts_statp->st_mode)) 295 return (1); 296 } else if (S_ISDIR((*b)->fts_statp->st_mode)) 297 return (-1); 298 return (strcmp((*a)->fts_name, (*b)->fts_name)); 299} 300 301#if __STDC__ 302#include <stdarg.h> 303#else 304#include <varargs.h> 305#endif 306 307void 308#if __STDC__ 309output(int indent, int *offset, const char *fmt, ...) 310#else 311output(indent, offset, fmt, va_alist) 312 int indent; 313 int *offset; 314 char *fmt; 315 va_dcl 316#endif 317{ 318 va_list ap; 319 char buf[1024]; 320#if __STDC__ 321 va_start(ap, fmt); 322#else 323 va_start(ap); 324#endif 325 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 326 va_end(ap); 327 328 if (*offset + strlen(buf) > MAXLINELEN - 3) { 329 (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); 330 *offset = INDENTNAMELEN + indent; 331 } 332 *offset += printf(" %s", buf) + 1; 333} 334