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