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 /*static char sccsid[] = "from: @(#)create.c 5.16 (Berkeley) 3/12/91";*/ 36 static char rcsid[] = "$Id: create.c,v 1.4 1993/10/01 01:06:39 jtc Exp $"; 37 #endif /* not lint */ 38 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 #include <time.h> 42 #include <fts.h> 43 #include <dirent.h> 44 #include <errno.h> 45 #include <stdio.h> 46 #include "mtree.h" 47 48 #define LABEL \ 49 if (label++) \ 50 (void)putchar(' '); \ 51 52 int ftsoptions = FTS_PHYSICAL; 53 54 cwalk() 55 { 56 extern int dflag; 57 register FTS *t; 58 register FTSENT *p; 59 register int cnt, label, notset; 60 time_t clock; 61 uid_t uid; 62 gid_t gid; 63 mode_t mode; 64 int tabs, dsort(); 65 char *argv[2]; 66 char curp[MAXPATHLEN], *inotype(), *getlogin(), *rlink(); 67 68 if (!getwd(curp)) { 69 (void)fprintf(stderr, "mtree: %s\n", curp); 70 exit(1); 71 } 72 (void)time(&clock); 73 (void)printf("#\t fs: %s\n#\t by: %s\n#\tdate: %s\n", 74 curp, getlogin(), ctime(&clock)); 75 76 argv[0] = "."; 77 argv[1] = (char *)NULL; 78 if (!(t = fts_open(argv, ftsoptions, dsort))) { 79 (void)fprintf(stderr, 80 "mtree: fts_open: %s.\n", strerror(errno)); 81 exit(1); 82 } 83 while (p = fts_read(t)) { 84 switch(p->fts_info) { 85 case FTS_D: 86 if (dflag) 87 notset = 1; 88 else 89 notset = 90 statdir(t, p, &uid, &gid, &mode, &tabs); 91 if (!strcmp(p->fts_name, ".")) 92 continue; 93 break; 94 case FTS_DP: 95 if (p->fts_level <= 0) 96 continue; 97 for (cnt = p->fts_level - 1; cnt-- > 0; ) 98 (void)putchar('\t'); 99 (void)printf("..\n"); 100 continue; 101 case FTS_DNR: 102 case FTS_ERR: 103 case FTS_NS: 104 (void)fprintf(stderr, "mtree: %s: %s.\n", 105 p->fts_path, strerror(p->fts_errno)); 106 continue; 107 default: 108 if (dflag) 109 continue; 110 } 111 112 for (cnt = p->fts_level - 1; cnt-- > 0; ) 113 (void)putchar('\t'); 114 (void)printf("%s", p->fts_name); 115 if (p->fts_info == FTS_D) 116 (void)putchar('\t'); 117 else { 118 if (tabs > 1 && p->fts_namelen < 8) 119 (void)putchar('\t'); 120 (void)putchar('\t'); 121 } 122 123 label = 0; 124 if (!S_ISREG(p->fts_statp->st_mode) || notset) { 125 LABEL; 126 (void)printf("type=%s", inotype(p->fts_statp->st_mode)); 127 } 128 if (p->fts_statp->st_uid != uid || notset) { 129 LABEL; 130 (void)printf("owner=%u", p->fts_statp->st_uid); 131 } 132 if (p->fts_statp->st_gid != gid || notset) { 133 LABEL; 134 (void)printf("group=%u", p->fts_statp->st_gid); 135 } 136 if ((p->fts_statp->st_mode & MBITS) != mode || notset) { 137 LABEL; 138 (void)printf("mode=%#o", p->fts_statp->st_mode & MBITS); 139 } 140 if (p->fts_statp->st_nlink != 1 || notset) { 141 LABEL; 142 (void)printf("nlink=%u", p->fts_statp->st_nlink); 143 } 144 LABEL; 145 (void)printf("size=%ld", p->fts_statp->st_size); 146 LABEL; 147 (void)printf("time=%ld", p->fts_statp->st_mtime); 148 149 if (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE) { 150 LABEL; 151 (void)printf("link=%s", rlink(p->fts_accpath)); 152 } 153 (void)putchar('\n'); 154 } 155 (void)fts_close(t); 156 } 157 158 #define MAXGID 5000 159 #define MAXUID 5000 160 #define MAXMODE MBITS + 1 161 162 statdir(t, parent, puid, pgid, pmode, tabs) 163 FTS *t; 164 FTSENT *parent; 165 uid_t *puid; 166 gid_t *pgid; 167 mode_t *pmode; 168 int *tabs; 169 { 170 register FTSENT *p; 171 register gid_t gid; 172 register uid_t uid; 173 register mode_t mode; 174 gid_t savegid; 175 uid_t saveuid; 176 mode_t savemode; 177 u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE]; 178 179 if (!(p = fts_children(t, ftsoptions))) { 180 if (errno) { 181 (void)fprintf(stderr, "mtree: %s: %s.\n", 182 RP(parent), strerror(errno)); 183 exit(1); 184 } 185 return(1); 186 } 187 188 bzero(g, sizeof(g)); 189 bzero(u, sizeof(u)); 190 bzero(m, sizeof(m)); 191 192 *tabs = 1; 193 maxuid = maxgid = maxmode = 0; 194 for (; p; p = p->fts_link) { 195 mode = p->fts_statp->st_mode & MBITS; 196 if (mode < MAXMODE && ++m[mode] > maxmode) { 197 savemode = mode; 198 maxmode = m[mode]; 199 } 200 gid = p->fts_statp->st_gid; 201 if (gid < MAXGID && ++g[gid] > maxgid) { 202 savegid = gid; 203 maxgid = g[gid]; 204 } 205 uid = p->fts_statp->st_uid; 206 if (uid < MAXUID && ++u[uid] > maxuid) { 207 saveuid = uid; 208 maxuid = u[uid]; 209 } 210 if (p->fts_namelen > 7) 211 *tabs = 2; 212 } 213 (void)printf("\n/set group=%u mode=%#o nlink=1 owner=%u type=file\n", 214 savegid, savemode, saveuid); 215 *puid = saveuid; 216 *pgid = savegid; 217 *pmode = savemode; 218 return(0); 219 } 220 221 dsort(p1, p2) 222 FTSENT **p1, **p2; 223 { 224 register FTSENT *a, *b; 225 226 a = *p1; 227 b = *p2; 228 229 if (S_ISDIR(a->fts_statp->st_mode)) { 230 if (!S_ISDIR(b->fts_statp->st_mode)) 231 return(1); 232 } else if (S_ISDIR(b->fts_statp->st_mode)) 233 return(-1); 234 return(strcmp(a->fts_name, b->fts_name)); 235 } 236