create.c revision 2860
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 219 if ((p = fts_children(t, 0)) == NULL) { 220 if (errno) 221 err("%s: %s", RP(parent), strerror(errno)); 222 return (1); 223 } 224 225 bzero(g, sizeof(g)); 226 bzero(u, sizeof(u)); 227 bzero(m, sizeof(m)); 228 229 maxuid = maxgid = maxmode = 0; 230 for (; p; p = p->fts_link) { 231 if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { 232 smode = p->fts_statp->st_mode & MBITS; 233 if (smode < MAXMODE && ++m[smode] > maxmode) { 234 savemode = smode; 235 maxmode = m[smode]; 236 } 237 sgid = p->fts_statp->st_gid; 238 if (sgid < MAXGID && ++g[sgid] > maxgid) { 239 savegid = sgid; 240 maxgid = g[sgid]; 241 } 242 suid = p->fts_statp->st_uid; 243 if (suid < MAXUID && ++u[suid] > maxuid) { 244 saveuid = suid; 245 maxuid = u[suid]; 246 } 247 } 248 } 249 /* 250 * If the /set record is the same as the last one we do not need to output 251 * a new one. So first we check to see if anything changed. 252 */ 253 if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || 254 (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || 255 ((keys & F_MODE) && (*pmode != savemode))) { 256 if (dflag) 257 (void)printf("/set type=dir"); 258 else 259 (void)printf("/set type=file"); 260 if (keys & F_UNAME) 261 if ((pw = getpwuid(saveuid)) != NULL) 262 (void)printf(" uname=%s", pw->pw_name); 263 else 264 err("could not get uname for uid=%u", saveuid); 265 if (keys & F_UID) 266 (void)printf(" uid=%lu", saveuid); 267 if (keys & F_GNAME) 268 if ((gr = getgrgid(savegid)) != NULL) 269 (void)printf(" gname=%s", gr->gr_name); 270 else 271 err("could not get gname for gid=%u", savegid); 272 if (keys & F_GID) 273 (void)printf(" gid=%lu", savegid); 274 if (keys & F_MODE) 275 (void)printf(" mode=%#o", savemode); 276 if (keys & F_NLINK) 277 (void)printf(" nlink=1"); 278 (void)printf("\n"); 279 *puid = saveuid; 280 *pgid = savegid; 281 *pmode = savemode; 282 } 283 return (0); 284} 285 286static int 287dsort(a, b) 288 const FTSENT **a, **b; 289{ 290 if (S_ISDIR((*a)->fts_statp->st_mode)) { 291 if (!S_ISDIR((*b)->fts_statp->st_mode)) 292 return (1); 293 } else if (S_ISDIR((*b)->fts_statp->st_mode)) 294 return (-1); 295 return (strcmp((*a)->fts_name, (*b)->fts_name)); 296} 297 298#if __STDC__ 299#include <stdarg.h> 300#else 301#include <varargs.h> 302#endif 303 304void 305#if __STDC__ 306output(int indent, int *offset, const char *fmt, ...) 307#else 308output(indent, offset, fmt, va_alist) 309 int indent; 310 int *offset; 311 char *fmt; 312 va_dcl 313#endif 314{ 315 va_list ap; 316 char buf[1024]; 317#if __STDC__ 318 va_start(ap, fmt); 319#else 320 va_start(ap); 321#endif 322 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 323 va_end(ap); 324 325 if (*offset + strlen(buf) > MAXLINELEN - 3) { 326 (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); 327 *offset = INDENTNAMELEN + indent; 328 } 329 *offset += printf(" %s", buf) + 1; 330} 331