1 /*- 2 * Copyright (c) 1989 The Regents of the University of California. 3 * 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 /* from: static char sccsid[] = "@(#)create.c 5.19 (Berkeley) 3/2/92"; */ 36 static char *rcsid = "$Id: create.c,v 1.7 1994/03/27 09:09:49 cgd Exp $"; 37 #endif /* not lint */ 38 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 #include <time.h> 42 #include <fcntl.h> 43 #include <fts.h> 44 #include <dirent.h> 45 #include <grp.h> 46 #include <pwd.h> 47 #include <errno.h> 48 #include <unistd.h> 49 #include <stdio.h> 50 #include "mtree.h" 51 #include "extern.h" 52 53 #define INDENTNAMELEN 15 54 #define MAXLINELEN 80 55 56 extern int crc_total, ftsoptions; 57 extern int dflag, sflag; 58 extern u_short keys; 59 extern char fullpath[MAXPATHLEN]; 60 61 static gid_t gid; 62 static uid_t uid; 63 static mode_t mode; 64 65 static int dsort __P((const FTSENT **, const FTSENT **)); 66 static void output __P((int *, const char *, ...)); 67 static int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *)); 68 static void statf __P((FTSENT *)); 69 70 void 71 cwalk() 72 { 73 register FTS *t; 74 register FTSENT *p; 75 time_t clock; 76 char *argv[2], host[MAXHOSTNAMELEN]; 77 78 (void)time(&clock); 79 (void)gethostname(host, sizeof(host)); 80 (void)printf( 81 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", 82 getlogin(), host, fullpath, ctime(&clock)); 83 84 argv[0] = "."; 85 argv[1] = NULL; 86 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) 87 err("fts_open: %s", strerror(errno)); 88 while (p = fts_read(t)) 89 switch(p->fts_info) { 90 case FTS_D: 91 (void)printf("\n# %s\n", p->fts_path); 92 statd(t, p, &uid, &gid, &mode); 93 statf(p); 94 break; 95 case FTS_DP: 96 if (p->fts_level > 0) 97 (void)printf("# %s\n..\n\n", p->fts_path); 98 break; 99 case FTS_DNR: 100 case FTS_ERR: 101 case FTS_NS: 102 (void)fprintf(stderr, 103 "mtree: %s: %s\n", p->fts_path, strerror(errno)); 104 break; 105 default: 106 if (!dflag) 107 statf(p); 108 break; 109 110 } 111 (void)fts_close(t); 112 if (sflag && keys & F_CKSUM) 113 (void)fprintf(stderr, 114 "mtree: %s checksum: %lu\n", fullpath, crc_total); 115 } 116 117 static void 118 statf(p) 119 FTSENT *p; 120 { 121 struct group *gr; 122 struct passwd *pw; 123 u_long len, val; 124 int fd, indent; 125 126 if (S_ISDIR(p->fts_statp->st_mode)) 127 indent = printf("%s", p->fts_name); 128 else 129 indent = printf(" %s", p->fts_name); 130 131 if (indent > INDENTNAMELEN) 132 indent = MAXLINELEN; 133 else 134 indent += printf("%*s", INDENTNAMELEN - indent, ""); 135 136 if (!S_ISREG(p->fts_statp->st_mode)) 137 output(&indent, "type=%s", inotype(p->fts_statp->st_mode)); 138 if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) 139 if (keys & F_UNAME && (pw = getpwuid(p->fts_statp->st_uid))) 140 output(&indent, "uname=%s", pw->pw_name); 141 else /* if (keys & F_UID) */ 142 output(&indent, "uid=%u", p->fts_statp->st_uid); 143 if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) 144 if (keys & F_GNAME && (gr = getgrgid(p->fts_statp->st_gid))) 145 output(&indent, "gname=%s", gr->gr_name); 146 else /* if (keys & F_GID) */ 147 output(&indent, "gid=%u", p->fts_statp->st_gid); 148 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) 149 output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS); 150 if (keys & F_NLINK && p->fts_statp->st_nlink != 1) 151 output(&indent, "nlink=%u", p->fts_statp->st_nlink); 152 if (keys & F_SIZE) 153 output(&indent, "size=%qd", p->fts_statp->st_size); 154 if (keys & F_TIME) 155 output(&indent, "time=%ld", p->fts_statp->st_mtime); 156 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { 157 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || 158 crc(fd, &val, &len)) 159 err("%s: %s", p->fts_accpath, strerror(errno)); 160 (void)close(fd); 161 output(&indent, "cksum=%lu", val); 162 } 163 if (keys & F_SLINK && 164 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) 165 output(&indent, "link=%s", rlink(p->fts_accpath)); 166 (void)putchar('\n'); 167 } 168 169 #define MAXGID 5000 170 #define MAXUID 5000 171 #define MAXMODE MBITS + 1 172 173 static int 174 statd(t, parent, puid, pgid, pmode) 175 FTS *t; 176 FTSENT *parent; 177 uid_t *puid; 178 gid_t *pgid; 179 mode_t *pmode; 180 { 181 register FTSENT *p; 182 register gid_t sgid; 183 register uid_t suid; 184 register mode_t smode; 185 struct group *gr; 186 struct passwd *pw; 187 gid_t savegid; 188 uid_t saveuid; 189 mode_t savemode; 190 u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE]; 191 192 if ((p = fts_children(t, 0)) == NULL) { 193 if (errno) 194 err("%s: %s", RP(parent), strerror(errno)); 195 return (1); 196 } 197 198 bzero(g, sizeof(g)); 199 bzero(u, sizeof(u)); 200 bzero(m, sizeof(m)); 201 202 maxuid = maxgid = maxmode = 0; 203 for (; p; p = p->fts_link) { 204 smode = p->fts_statp->st_mode & MBITS; 205 if (smode < MAXMODE && ++m[smode] > maxmode) { 206 savemode = smode; 207 maxmode = m[smode]; 208 } 209 sgid = p->fts_statp->st_gid; 210 if (sgid < MAXGID && ++g[sgid] > maxgid) { 211 savegid = sgid; 212 maxgid = g[sgid]; 213 } 214 suid = p->fts_statp->st_uid; 215 if (suid < MAXUID && ++u[suid] > maxuid) { 216 saveuid = suid; 217 maxuid = u[suid]; 218 } 219 } 220 (void)printf("/set type=file"); 221 if (keys & F_GID) 222 (void)printf(" gid=%u", savegid); 223 if (keys & F_GNAME) 224 if ((gr = getgrgid(savegid)) != NULL) 225 (void)printf(" gname=%s", gr->gr_name); 226 else 227 (void)printf(" gid=%u", savegid); 228 if (keys & F_UNAME) 229 if ((pw = getpwuid(saveuid)) != NULL) 230 (void)printf(" uname=%s", pw->pw_name); 231 else 232 (void)printf(" uid=%u", saveuid); 233 if (keys & F_UID) 234 (void)printf(" uid=%u", saveuid); 235 if (keys & F_MODE) 236 (void)printf(" mode=%#o", savemode); 237 if (keys & F_NLINK) 238 (void)printf(" nlink=1"); 239 (void)printf("\n"); 240 *puid = saveuid; 241 *pgid = savegid; 242 *pmode = savemode; 243 return (0); 244 } 245 246 static int 247 dsort(a, b) 248 const FTSENT **a, **b; 249 { 250 if (S_ISDIR((*a)->fts_statp->st_mode)) { 251 if (!S_ISDIR((*b)->fts_statp->st_mode)) 252 return (1); 253 } else if (S_ISDIR((*b)->fts_statp->st_mode)) 254 return (-1); 255 return (strcmp((*a)->fts_name, (*b)->fts_name)); 256 } 257 258 #if __STDC__ 259 #include <stdarg.h> 260 #else 261 #include <varargs.h> 262 #endif 263 264 void 265 #if __STDC__ 266 output(int *offset, const char *fmt, ...) 267 #else 268 output(offset, fmt, va_alist) 269 int *offset; 270 char *fmt; 271 va_dcl 272 #endif 273 { 274 va_list ap; 275 int len; 276 char buf[1024]; 277 #if __STDC__ 278 va_start(ap, fmt); 279 #else 280 va_start(ap); 281 #endif 282 len = vsnprintf(buf, sizeof(buf), fmt, ap); 283 va_end(ap); 284 285 if (*offset + len > MAXLINELEN - 3) { 286 (void)printf(" \\\n%*s", INDENTNAMELEN, ""); 287 *offset = INDENTNAMELEN; 288 } 289 *offset += printf(" %s", buf) + 1; 290 } 291