]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.sbin/makefs/walk.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.sbin / makefs / walk.c
1 /*      $NetBSD: walk.c,v 1.17 2004/06/20 22:20:18 jmc Exp $    */
2
3 /*
4  * Copyright (c) 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Luke Mewburn for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 /*
39  * The function link_check() was inspired from NetBSD's usr.bin/du/du.c,
40  * which has the following copyright notice:
41  *
42  *
43  * Copyright (c) 1989, 1993, 1994
44  *      The Regents of the University of California.  All rights reserved.
45  *
46  * This code is derived from software contributed to Berkeley by
47  * Chris Newcomb.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  *    notice, this list of conditions and the following disclaimer.
54  * 2. Redistributions in binary form must reproduce the above copyright
55  *    notice, this list of conditions and the following disclaimer in the
56  *    documentation and/or other materials provided with the distribution.
57  * 3. Neither the name of the University nor the names of its contributors
58  *    may be used to endorse or promote products derived from this software
59  *    without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  */
73
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
76
77 #include <sys/param.h>
78
79 #include <assert.h>
80 #include <errno.h>
81 #include <fcntl.h>
82 #include <stdio.h>
83 #include <dirent.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87
88 #include "makefs.h"
89
90 #include "mtree.h"
91 #include "extern.h"             /* NB: mtree */
92
93 static  void     apply_specdir(const char *, NODE *, fsnode *);
94 static  void     apply_specentry(const char *, NODE *, fsnode *);
95 static  fsnode  *create_fsnode(const char *, struct stat *);
96 static  fsinode *link_check(fsinode *);
97
98
99 /*
100  * walk_dir --
101  *      build a tree of fsnodes from `dir', with a parent fsnode of `parent'
102  *      (which may be NULL for the root of the tree).
103  *      each "level" is a directory, with the "." entry guaranteed to be
104  *      at the start of the list, and without ".." entries.
105  */
106 fsnode *
107 walk_dir(const char *dir, fsnode *parent)
108 {
109         fsnode          *first, *cur, *prev;
110         DIR             *dirp;
111         struct dirent   *dent;
112         char            path[MAXPATHLEN + 1];
113         struct stat     stbuf;
114
115         assert(dir != NULL);
116
117         if (debug & DEBUG_WALK_DIR)
118                 printf("walk_dir: %s %p\n", dir, parent);
119         if ((dirp = opendir(dir)) == NULL)
120                 err(1, "Can't opendir `%s'", dir);
121         first = prev = NULL;
122         while ((dent = readdir(dirp)) != NULL) {
123                 if (strcmp(dent->d_name, "..") == 0)
124                         continue;
125                 if (debug & DEBUG_WALK_DIR_NODE)
126                         printf("scanning %s/%s\n", dir, dent->d_name);
127                 if (snprintf(path, sizeof(path), "%s/%s", dir, dent->d_name)
128                     >= sizeof(path))
129                         errx(1, "Pathname too long.");
130                 if (lstat(path, &stbuf) == -1)
131                         err(1, "Can't lstat `%s'", path);
132 #ifdef S_ISSOCK
133                 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
134                         if (debug & DEBUG_WALK_DIR_NODE)
135                                 printf("  skipping socket %s\n", path);
136                         continue;
137                 }
138 #endif
139
140                 cur = create_fsnode(dent->d_name, &stbuf);
141                 cur->parent = parent;
142                 if (strcmp(dent->d_name, ".") == 0) {
143                                 /* ensure "." is at the start of the list */
144                         cur->next = first;
145                         first = cur;
146                         if (! prev)
147                                 prev = cur;
148                 } else {                        /* not "." */
149                         if (prev)
150                                 prev->next = cur;
151                         prev = cur;
152                         if (!first)
153                                 first = cur;
154                         if (S_ISDIR(cur->type)) {
155                                 cur->child = walk_dir(path, cur);
156                                 continue;
157                         }
158                 }
159                 if (stbuf.st_nlink > 1) {
160                         fsinode *curino;
161
162                         curino = link_check(cur->inode);
163                         if (curino != NULL) {
164                                 free(cur->inode);
165                                 cur->inode = curino;
166                                 cur->inode->nlink++;
167                         }
168                 }
169                 if (S_ISLNK(cur->type)) {
170                         char    slink[PATH_MAX+1];
171                         int     llen;
172
173                         llen = readlink(path, slink, sizeof(slink) - 1);
174                         if (llen == -1)
175                                 err(1, "Readlink `%s'", path);
176                         slink[llen] = '\0';
177                         if ((cur->symlink = strdup(slink)) == NULL)
178                                 err(1, "Memory allocation error");
179                 }
180         }
181         for (cur = first; cur != NULL; cur = cur->next)
182                 cur->first = first;
183         if (closedir(dirp) == -1)
184                 err(1, "Can't closedir `%s'", dir);
185         return (first);
186 }
187
188 static fsnode *
189 create_fsnode(const char *name, struct stat *stbuf)
190 {
191         fsnode *cur;
192
193         if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
194             (cur->name = strdup(name)) == NULL ||
195             (cur->inode = calloc(1, sizeof(fsinode))) == NULL)
196                 err(1, "Memory allocation error");
197         cur->type = stbuf->st_mode & S_IFMT;
198         cur->inode->nlink = 1;
199         cur->inode->st = *stbuf;
200         return (cur);
201 }
202
203 /*
204  * apply_specfile --
205  *      read in the mtree(8) specfile, and apply it to the tree
206  *      at dir,parent. parameters in parent on equivalent types
207  *      will be changed to those found in specfile, and missing
208  *      entries will be added.
209  */
210 void
211 apply_specfile(const char *specfile, const char *dir, fsnode *parent)
212 {
213         struct timeval   start;
214         FILE    *fp;
215         NODE    *root;
216
217         assert(specfile != NULL);
218         assert(parent != NULL);
219
220         if (debug & DEBUG_APPLY_SPECFILE)
221                 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
222
223                                 /* read in the specfile */
224         if ((fp = fopen(specfile, "r")) == NULL)
225                 err(1, "Can't open `%s'", specfile);
226         TIMER_START(start);
227         root = mtree_readspec(fp);
228         TIMER_RESULTS(start, "spec");
229         if (fclose(fp) == EOF)
230                 err(1, "Can't close `%s'", specfile);
231
232                                 /* perform some sanity checks */
233         if (root == NULL)
234                 errx(1, "Specfile `%s' did not contain a tree", specfile);
235         assert(strcmp(root->name, ".") == 0);
236         assert(root->type == F_DIR);
237
238                                 /* merge in the changes */
239         apply_specdir(dir, root, parent);
240 }
241
242 static u_int
243 nodetoino(u_int type)
244 {
245
246         switch (type) {
247         case F_BLOCK:
248                 return S_IFBLK;
249         case F_CHAR:
250                 return S_IFCHR;
251         case F_DIR:
252                 return S_IFDIR;
253         case F_FIFO:
254                 return S_IFIFO;
255         case F_FILE:
256                 return S_IFREG;
257         case F_LINK:
258                 return S_IFLNK;
259         case F_SOCK:
260                 return S_IFSOCK;
261         default:
262                 printf("unknown type %d", type);
263                 abort();
264         }
265         /* NOTREACHED */
266 }
267
268 static void
269 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode)
270 {
271         char     path[MAXPATHLEN + 1];
272         NODE    *curnode;
273         fsnode  *curfsnode;
274
275         assert(specnode != NULL);
276         assert(dirnode != NULL);
277
278         if (debug & DEBUG_APPLY_SPECFILE)
279                 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
280
281         if (specnode->type != F_DIR)
282                 errx(1, "Specfile node `%s/%s' is not a directory",
283                     dir, specnode->name);
284         if (dirnode->type != S_IFDIR)
285                 errx(1, "Directory node `%s/%s' is not a directory",
286                     dir, dirnode->name);
287
288         apply_specentry(dir, specnode, dirnode);
289
290                         /* now walk specnode->child matching up with dirnode */
291         for (curnode = specnode->child; curnode != NULL;
292             curnode = curnode->next) {
293                 if (debug & DEBUG_APPLY_SPECENTRY)
294                         printf("apply_specdir:  spec %s\n",
295                             curnode->name);
296                 for (curfsnode = dirnode->next; curfsnode != NULL;
297                     curfsnode = curfsnode->next) {
298 #if 0   /* too verbose for now */
299                         if (debug & DEBUG_APPLY_SPECENTRY)
300                                 printf("apply_specdir:  dirent %s\n",
301                                     curfsnode->name);
302 #endif
303                         if (strcmp(curnode->name, curfsnode->name) == 0)
304                                 break;
305                 }
306                 if (snprintf(path, sizeof(path), "%s/%s",
307                     dir, curnode->name) >= sizeof(path))
308                         errx(1, "Pathname too long.");
309                 if (curfsnode == NULL) {        /* need new entry */
310                         struct stat     stbuf;
311
312                                             /*
313                                              * don't add optional spec entries
314                                              * that lack an existing fs entry
315                                              */
316                         if ((curnode->flags & F_OPT) &&
317                             lstat(path, &stbuf) == -1)
318                                         continue;
319
320                                         /* check that enough info is provided */
321 #define NODETEST(t, m)                                                  \
322                         if (!(t))                                       \
323                                 errx(1, "`%s': %s not provided", path, m)
324                         NODETEST(curnode->flags & F_TYPE, "type");
325                         NODETEST(curnode->flags & F_MODE, "mode");
326                                 /* XXX: require F_TIME ? */
327                         NODETEST(curnode->flags & F_GID ||
328                             curnode->flags & F_GNAME, "group");
329                         NODETEST(curnode->flags & F_UID ||
330                             curnode->flags & F_UNAME, "user");
331 #undef NODETEST
332
333                         if (debug & DEBUG_APPLY_SPECFILE)
334                                 printf("apply_specdir: adding %s\n",
335                                     curnode->name);
336                                         /* build minimal fsnode */
337                         memset(&stbuf, 0, sizeof(stbuf));
338                         stbuf.st_mode = nodetoino(curnode->type);
339                         stbuf.st_nlink = 1;
340                         stbuf.st_mtime = stbuf.st_atime =
341                             stbuf.st_ctime = start_time.tv_sec;
342 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
343                         stbuf.st_mtimensec = stbuf.st_atimensec =
344                             stbuf.st_ctimensec = start_time.tv_nsec;
345 #endif
346                         curfsnode = create_fsnode(curnode->name, &stbuf);
347                         curfsnode->parent = dirnode->parent;
348                         curfsnode->first = dirnode;
349                         curfsnode->next = dirnode->next;
350                         dirnode->next = curfsnode;
351                         if (curfsnode->type == S_IFDIR) {
352                                         /* for dirs, make "." entry as well */
353                                 curfsnode->child = create_fsnode(".", &stbuf);
354                                 curfsnode->child->parent = curfsnode;
355                                 curfsnode->child->first = curfsnode->child;
356                         }
357                         if (curfsnode->type == S_IFLNK) {
358                                 assert(curnode->slink != NULL);
359                                         /* for symlinks, copy the target */
360                                 if ((curfsnode->symlink =
361                                     strdup(curnode->slink)) == NULL)
362                                         err(1, "Memory allocation error");
363                         }
364                 }
365                 apply_specentry(dir, curnode, curfsnode);
366                 if (curnode->type == F_DIR) {
367                         if (curfsnode->type != S_IFDIR)
368                                 errx(1, "`%s' is not a directory", path);
369                         assert (curfsnode->child != NULL);
370                         apply_specdir(path, curnode, curfsnode->child);
371                 }
372         }
373 }
374
375 static void
376 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
377 {
378
379         assert(specnode != NULL);
380         assert(dirnode != NULL);
381
382         if (nodetoino(specnode->type) != dirnode->type)
383                 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
384                     dir, specnode->name, inode_type(nodetoino(specnode->type)),
385                     inode_type(dirnode->type));
386
387         if (debug & DEBUG_APPLY_SPECENTRY)
388                 printf("apply_specentry: %s/%s\n", dir, dirnode->name);
389
390 #define ASEPRINT(t, b, o, n) \
391                 if (debug & DEBUG_APPLY_SPECENTRY) \
392                         printf("\t\t\tchanging %s from " b " to " b "\n", \
393                             t, o, n)
394
395         if (specnode->flags & (F_GID | F_GNAME)) {
396                 ASEPRINT("gid", "%d",
397                     dirnode->inode->st.st_gid, specnode->st_gid);
398                 dirnode->inode->st.st_gid = specnode->st_gid;
399         }
400         if (specnode->flags & F_MODE) {
401                 ASEPRINT("mode", "%#o",
402                     dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
403                 dirnode->inode->st.st_mode &= ~ALLPERMS;
404                 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
405         }
406                 /* XXX: ignoring F_NLINK for now */
407         if (specnode->flags & F_SIZE) {
408                 ASEPRINT("size", "%lld",
409                     (long long)dirnode->inode->st.st_size,
410                     (long long)specnode->st_size);
411                 dirnode->inode->st.st_size = specnode->st_size;
412         }
413         if (specnode->flags & F_SLINK) {
414                 assert(dirnode->symlink != NULL);
415                 assert(specnode->slink != NULL);
416                 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
417                 free(dirnode->symlink);
418                 if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
419                         err(1, "Memory allocation error");
420         }
421         if (specnode->flags & F_TIME) {
422                 ASEPRINT("time", "%ld",
423                     (long)dirnode->inode->st.st_mtime,
424                     (long)specnode->st_mtimespec.tv_sec);
425                 dirnode->inode->st.st_mtime =           specnode->st_mtimespec.tv_sec;
426                 dirnode->inode->st.st_atime =           specnode->st_mtimespec.tv_sec;
427                 dirnode->inode->st.st_ctime =           start_time.tv_sec;
428 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
429                 dirnode->inode->st.st_mtimensec =       specnode->st_mtimespec.tv_nsec;
430                 dirnode->inode->st.st_atimensec =       specnode->st_mtimespec.tv_nsec;
431                 dirnode->inode->st.st_ctimensec =       start_time.tv_nsec;
432 #endif
433         }
434         if (specnode->flags & (F_UID | F_UNAME)) {
435                 ASEPRINT("uid", "%d",
436                     dirnode->inode->st.st_uid, specnode->st_uid);
437                 dirnode->inode->st.st_uid = specnode->st_uid;
438         }
439 #if HAVE_STRUCT_STAT_ST_FLAGS
440         if (specnode->flags & F_FLAGS) {
441                 ASEPRINT("flags", "%#lX",
442                     (unsigned long)dirnode->inode->st.st_flags,
443                     (unsigned long)specnode->st_flags);
444                 dirnode->inode->st.st_flags = specnode->st_flags;
445         }
446 #endif
447 #undef ASEPRINT
448
449         dirnode->flags |= FSNODE_F_HASSPEC;
450 }
451
452
453 /*
454  * dump_fsnodes --
455  *      dump the fsnodes from `cur', based in the directory `dir'
456  */
457 void
458 dump_fsnodes(const char *dir, fsnode *root)
459 {
460         fsnode  *cur;
461         char    path[MAXPATHLEN + 1];
462
463         assert (dir != NULL);
464         printf("dump_fsnodes: %s %p\n", dir, root);
465         for (cur = root; cur != NULL; cur = cur->next) {
466                 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
467                     >= sizeof(path))
468                         errx(1, "Pathname too long.");
469
470                 if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
471                         printf("cur=%8p parent=%8p first=%8p ",
472                             cur, cur->parent, cur->first);
473                 printf("%7s: %s", inode_type(cur->type), path);
474                 if (S_ISLNK(cur->type)) {
475                         assert(cur->symlink != NULL);
476                         printf(" -> %s", cur->symlink);
477                 } else {
478                         assert (cur->symlink == NULL);
479                 }
480                 if (cur->inode->nlink > 1)
481                         printf(", nlinks=%d", cur->inode->nlink);
482                 putchar('\n');
483
484                 if (cur->child) {
485                         assert (cur->type == S_IFDIR);
486                         dump_fsnodes(path, cur->child);
487                 }
488         }
489         printf("dump_fsnodes: finished %s\n", dir);
490 }
491
492
493 /*
494  * inode_type --
495  *      for a given inode type `mode', return a descriptive string.
496  */
497 const char *
498 inode_type(mode_t mode)
499 {
500
501         if (S_ISREG(mode))
502                 return ("file");
503         if (S_ISLNK(mode))
504                 return ("symlink");
505         if (S_ISDIR(mode))
506                 return ("dir");
507         if (S_ISLNK(mode))
508                 return ("link");
509         if (S_ISFIFO(mode))
510                 return ("fifo");
511         if (S_ISSOCK(mode))
512                 return ("socket");
513         /* XXX should not happen but handle them */
514         if (S_ISCHR(mode))
515                 return ("char");
516         if (S_ISBLK(mode))
517                 return ("block");
518         return ("unknown");
519 }
520
521
522 /*
523  * link_check --
524  *      return pointer to fsnode matching `entry's st_ino & st_dev if it exists,
525  *      otherwise add `entry' to table and return NULL
526  */
527 static fsinode *
528 link_check(fsinode *entry)
529 {
530         static  struct dupnode {
531                 uint32_t        dev;
532                 uint64_t        ino;
533                 fsinode         *dup;
534         } *dups, *newdups;
535         static  int     ndups, maxdups;
536
537         int     i;
538
539         assert (entry != NULL);
540
541                 /* XXX; maybe traverse in reverse for speed? */
542         for (i = 0; i < ndups; i++) {
543                 if (dups[i].dev == entry->st.st_dev &&
544                     dups[i].ino == entry->st.st_ino) {
545                         if (debug & DEBUG_WALK_DIR_LINKCHECK)
546                                 printf("link_check: found [%d,%d]\n",
547                                     entry->st.st_dev, entry->st.st_ino);
548                         return (dups[i].dup);
549                 }
550         }
551
552         if (debug & DEBUG_WALK_DIR_LINKCHECK)
553                 printf("link_check: no match for [%d, %d]\n",
554                     entry->st.st_dev, entry->st.st_ino);
555         if (ndups == maxdups) {
556                 if ((newdups = realloc(dups, sizeof(struct dupnode) * (maxdups + 128)))
557                     == NULL)
558                         err(1, "Memory allocation error");
559                 dups = newdups;
560                 maxdups += 128;
561         }
562         dups[ndups].dev = entry->st.st_dev;
563         dups[ndups].ino = entry->st.st_ino;
564         dups[ndups].dup = entry;
565         ndups++;
566
567         return (NULL);
568 }