create.c revision 36670
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 35#if 0 36static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; 37#endif 38static const char rcsid[] = 39 "$Id: create.c,v 1.9 1997/10/01 06:30:00 charnier Exp $"; 40#endif /* not lint */ 41 42#include <sys/param.h> 43#include <sys/stat.h> 44#include <dirent.h> 45#include <err.h> 46#include <errno.h> 47#include <fcntl.h> 48#include <fts.h> 49#include <grp.h> 50#include <md5.h> 51#include <pwd.h> 52#include <stdio.h> 53#include <time.h> 54#include <unistd.h> 55#include "mtree.h" 56#include "extern.h" 57 58#define INDENTNAMELEN 15 59#define MAXLINELEN 80 60 61extern long int crc_total; 62extern int ftsoptions; 63extern int dflag, iflag, nflag, sflag; 64extern u_int keys; 65extern char fullpath[MAXPATHLEN]; 66extern int lineno; 67 68static gid_t gid; 69static uid_t uid; 70static mode_t mode; 71 72static int dsort __P((const FTSENT **, const FTSENT **)); 73static void output __P((int, int *, const char *, ...)); 74static int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *)); 75static void statf __P((int, FTSENT *)); 76 77void 78cwalk() 79{ 80 register FTS *t; 81 register FTSENT *p; 82 time_t clock; 83 char *argv[2], host[MAXHOSTNAMELEN]; 84 int indent = 0; 85 86 (void)time(&clock); 87 (void)gethostname(host, sizeof(host)); 88 (void)printf( 89 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 90 getlogin(), host, fullpath, ctime(&clock)); 91 92 argv[0] = "."; 93 argv[1] = NULL; 94 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 95 err(1, "line %d: fts_open", lineno); 96 while ((p = fts_read(t))) { 97 if (iflag) 98 indent = p->fts_level * 4; 99 switch(p->fts_info) { 100 case FTS_D: 101 if (!dflag) 102 (void)printf("\n"); 103 if (!nflag) 104 (void)printf("# %s\n", p->fts_path); 105 statd(t, p, &uid, &gid, &mode); 106 statf(indent, p); 107 break; 108 case FTS_DP: 109 if (!nflag && (p->fts_level > 0)) 110 (void)printf("%*s# %s\n", indent, "", p->fts_path); 111 (void)printf("%*s..\n", indent, ""); 112 if (!dflag) 113 (void)printf("\n"); 114 break; 115 case FTS_DNR: 116 case FTS_ERR: 117 case FTS_NS: 118 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 119 break; 120 default: 121 if (!dflag) 122 statf(indent, p); 123 break; 124 125 } 126 } 127 (void)fts_close(t); 128 if (sflag && keys & F_CKSUM) 129 warnx("%s checksum: %lu", fullpath, crc_total); 130} 131 132static void 133statf(indent, p) 134 int indent; 135 FTSENT *p; 136{ 137 struct group *gr; 138 struct passwd *pw; 139 u_long len, val; 140 int fd, offset; 141 142 if (iflag || S_ISDIR(p->fts_statp->st_mode)) 143 offset = printf("%*s%s", indent, "", p->fts_name); 144 else 145 offset = printf("%*s %s", indent, "", p->fts_name); 146 147 if (offset > (INDENTNAMELEN + indent)) 148 offset = MAXLINELEN; 149 else 150 offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); 151 152 if (!S_ISREG(p->fts_statp->st_mode) && !dflag) 153 output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); 154 if (p->fts_statp->st_uid != uid) { 155 if (keys & F_UNAME) { 156 if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) { 157 output(indent, &offset, "uname=%s", pw->pw_name); 158 } else { 159 errx(1, 160 "line %d: could not get uname for uid=%u", 161 lineno, p->fts_statp->st_uid); 162 } 163 } 164 if (keys & F_UID) 165 output(indent, &offset, "uid=%u", p->fts_statp->st_uid); 166 } 167 if (p->fts_statp->st_gid != gid) { 168 if (keys & F_GNAME) { 169 if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) { 170 output(indent, &offset, "gname=%s", gr->gr_name); 171 } else { 172 errx(1, 173 "line %d: could not get gname for gid=%u", 174 lineno, p->fts_statp->st_gid); 175 } 176 } 177 if (keys & F_GID) 178 output(indent, &offset, "gid=%u", p->fts_statp->st_gid); 179 } 180 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 181 output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); 182 if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 183 output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); 184 if (keys & F_SIZE) 185 output(indent, &offset, "size=%qd", p->fts_statp->st_size); 186 if (keys & F_TIME) 187 output(indent, &offset, "time=%ld.%ld", 188 p->fts_statp->st_mtimespec.tv_sec, 189 p->fts_statp->st_mtimespec.tv_nsec); 190 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 191 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 192 crc(fd, &val, &len)) 193 err(1, "line %d: %s", lineno, p->fts_accpath); 194 (void)close(fd); 195 output(indent, &offset, "cksum=%lu", val); 196 } 197 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) { 198 char *md5digest, buf[33]; 199 200 md5digest = MD5File(p->fts_accpath,buf); 201 if (!md5digest) { 202 err(1, "line %d: %s", lineno, p->fts_accpath); 203 } else { 204 output(indent, &offset, "md5digest=%s", md5digest); 205 } 206 } 207 if (keys & F_SLINK && 208 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 209 output(indent, &offset, "link=%s", rlink(p->fts_accpath)); 210 (void)putchar('\n'); 211} 212 213#define MAXGID 5000 214#define MAXUID 5000 215#define MAXMODE MBITS + 1 216 217static int 218statd(t, parent, puid, pgid, pmode) 219 FTS *t; 220 FTSENT *parent; 221 uid_t *puid; 222 gid_t *pgid; 223 mode_t *pmode; 224{ 225 register FTSENT *p; 226 register gid_t sgid; 227 register uid_t suid; 228 register mode_t smode; 229 struct group *gr; 230 struct passwd *pw; 231 gid_t savegid = *pgid; 232 uid_t saveuid = *puid; 233 mode_t savemode = *pmode; 234 u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE]; 235 static int first = 1; 236 237 if ((p = fts_children(t, 0)) == NULL) { 238 if (errno) 239 err(1, "line %d: %s", lineno, RP(parent)); 240 return (1); 241 } 242 243 bzero(g, sizeof(g)); 244 bzero(u, sizeof(u)); 245 bzero(m, sizeof(m)); 246 247 maxuid = maxgid = maxmode = 0; 248 for (; p; p = p->fts_link) { 249 if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { 250 smode = p->fts_statp->st_mode & MBITS; 251 if (smode < MAXMODE && ++m[smode] > maxmode) { 252 savemode = smode; 253 maxmode = m[smode]; 254 } 255 sgid = p->fts_statp->st_gid; 256 if (sgid < MAXGID && ++g[sgid] > maxgid) { 257 savegid = sgid; 258 maxgid = g[sgid]; 259 } 260 suid = p->fts_statp->st_uid; 261 if (suid < MAXUID && ++u[suid] > maxuid) { 262 saveuid = suid; 263 maxuid = u[suid]; 264 } 265 } 266 } 267 /* 268 * If the /set record is the same as the last one we do not need to output 269 * a new one. So first we check to see if anything changed. Note that we 270 * always output a /set record for the first directory. 271 */ 272 if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || 273 (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || 274 ((keys & F_MODE) && (*pmode != savemode)) || (first)) { 275 first = 0; 276 if (dflag) 277 (void)printf("/set type=dir"); 278 else 279 (void)printf("/set type=file"); 280 if (keys & F_UNAME) 281 if ((pw = getpwuid(saveuid)) != NULL) 282 (void)printf(" uname=%s", pw->pw_name); 283 else 284 errx(1, 285 "line %d: could not get uname for uid=%u", 286 lineno, saveuid); 287 if (keys & F_UID) 288 (void)printf(" uid=%lu", saveuid); 289 if (keys & F_GNAME) 290 if ((gr = getgrgid(savegid)) != NULL) 291 (void)printf(" gname=%s", gr->gr_name); 292 else 293 errx(1, 294 "line %d: could not get gname for gid=%u", 295 lineno, savegid); 296 if (keys & F_GID) 297 (void)printf(" gid=%lu", savegid); 298 if (keys & F_MODE) 299 (void)printf(" mode=%#o", savemode); 300 if (keys & F_NLINK) 301 (void)printf(" nlink=1"); 302 (void)printf("\n"); 303 *puid = saveuid; 304 *pgid = savegid; 305 *pmode = savemode; 306 } 307 return (0); 308} 309 310static int 311dsort(a, b) 312 const FTSENT **a, **b; 313{ 314 if (S_ISDIR((*a)->fts_statp->st_mode)) { 315 if (!S_ISDIR((*b)->fts_statp->st_mode)) 316 return (1); 317 } else if (S_ISDIR((*b)->fts_statp->st_mode)) 318 return (-1); 319 return (strcmp((*a)->fts_name, (*b)->fts_name)); 320} 321 322#if __STDC__ 323#include <stdarg.h> 324#else 325#include <varargs.h> 326#endif 327 328void 329#if __STDC__ 330output(int indent, int *offset, const char *fmt, ...) 331#else 332output(indent, offset, fmt, va_alist) 333 int indent; 334 int *offset; 335 char *fmt; 336 va_dcl 337#endif 338{ 339 va_list ap; 340 char buf[1024]; 341#if __STDC__ 342 va_start(ap, fmt); 343#else 344 va_start(ap); 345#endif 346 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 347 va_end(ap); 348 349 if (*offset + strlen(buf) > MAXLINELEN - 3) { 350 (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); 351 *offset = INDENTNAMELEN + indent; 352 } 353 *offset += printf(" %s", buf) + 1; 354} 355