1*e425abdcSguenther /* $OpenBSD: walk.c,v 1.12 2023/08/19 04:21:06 guenther Exp $ */
26163fc9cSnatano /* $NetBSD: walk.c,v 1.29 2015/11/25 00:48:49 christos Exp $ */
36163fc9cSnatano
46163fc9cSnatano /*
56163fc9cSnatano * Copyright (c) 2001 Wasabi Systems, Inc.
66163fc9cSnatano * All rights reserved.
76163fc9cSnatano *
86163fc9cSnatano * Written by Luke Mewburn for Wasabi Systems, Inc.
96163fc9cSnatano *
106163fc9cSnatano * Redistribution and use in source and binary forms, with or without
116163fc9cSnatano * modification, are permitted provided that the following conditions
126163fc9cSnatano * are met:
136163fc9cSnatano * 1. Redistributions of source code must retain the above copyright
146163fc9cSnatano * notice, this list of conditions and the following disclaimer.
156163fc9cSnatano * 2. Redistributions in binary form must reproduce the above copyright
166163fc9cSnatano * notice, this list of conditions and the following disclaimer in the
176163fc9cSnatano * documentation and/or other materials provided with the distribution.
186163fc9cSnatano * 3. All advertising materials mentioning features or use of this software
196163fc9cSnatano * must display the following acknowledgement:
206163fc9cSnatano * This product includes software developed for the NetBSD Project by
216163fc9cSnatano * Wasabi Systems, Inc.
226163fc9cSnatano * 4. The name of Wasabi Systems, Inc. may not be used to endorse
236163fc9cSnatano * or promote products derived from this software without specific prior
246163fc9cSnatano * written permission.
256163fc9cSnatano *
266163fc9cSnatano * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
276163fc9cSnatano * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
286163fc9cSnatano * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
296163fc9cSnatano * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
306163fc9cSnatano * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
316163fc9cSnatano * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
326163fc9cSnatano * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
336163fc9cSnatano * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
346163fc9cSnatano * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
356163fc9cSnatano * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
366163fc9cSnatano * POSSIBILITY OF SUCH DAMAGE.
376163fc9cSnatano */
386163fc9cSnatano
39b7df7881Sderaadt #include <sys/types.h>
406163fc9cSnatano #include <sys/stat.h>
416163fc9cSnatano
426163fc9cSnatano #include <assert.h>
436163fc9cSnatano #include <stdio.h>
446163fc9cSnatano #include <dirent.h>
456163fc9cSnatano #include <stdlib.h>
46b7df7881Sderaadt #include <limits.h>
476163fc9cSnatano #include <string.h>
486163fc9cSnatano #include <unistd.h>
496163fc9cSnatano
506163fc9cSnatano #include "makefs.h"
516163fc9cSnatano
526163fc9cSnatano static fsnode *create_fsnode(const char *, const char *, const char *,
536163fc9cSnatano struct stat *);
546163fc9cSnatano static fsinode *link_check(fsinode *);
556163fc9cSnatano
566163fc9cSnatano
576163fc9cSnatano /*
586163fc9cSnatano * walk_dir --
596163fc9cSnatano * build a tree of fsnodes from `root' and `dir', with a parent
606163fc9cSnatano * fsnode of `parent' (which may be NULL for the root of the tree).
616163fc9cSnatano * append the tree to a fsnode of `join' if it is not NULL.
626163fc9cSnatano * each "level" is a directory, with the "." entry guaranteed to be
636163fc9cSnatano * at the start of the list, and without ".." entries.
646163fc9cSnatano */
656163fc9cSnatano fsnode *
walk_dir(const char * root,const char * dir,fsnode * parent,fsnode * join)664a445526Snatano walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join)
676163fc9cSnatano {
686163fc9cSnatano fsnode *first, *cur, *prev, *last;
696163fc9cSnatano DIR *dirp;
706163fc9cSnatano struct dirent *dent;
71b7df7881Sderaadt char path[PATH_MAX+1];
726163fc9cSnatano struct stat stbuf;
736163fc9cSnatano char *name, *rp;
746163fc9cSnatano int dot, len;
756163fc9cSnatano
766163fc9cSnatano assert(root != NULL);
776163fc9cSnatano assert(dir != NULL);
786163fc9cSnatano
796163fc9cSnatano len = snprintf(path, sizeof(path), "%s/%s", root, dir);
806163fc9cSnatano if (len >= (int)sizeof(path))
816163fc9cSnatano errx(1, "Pathname too long.");
826163fc9cSnatano if ((dirp = opendir(path)) == NULL)
836163fc9cSnatano err(1, "Can't opendir `%s'", path);
846163fc9cSnatano rp = path + strlen(root) + 1;
856163fc9cSnatano if (join != NULL) {
866163fc9cSnatano first = cur = join;
876163fc9cSnatano while (cur->next != NULL)
886163fc9cSnatano cur = cur->next;
896163fc9cSnatano prev = last = cur;
906163fc9cSnatano } else
916163fc9cSnatano last = first = prev = NULL;
926163fc9cSnatano while ((dent = readdir(dirp)) != NULL) {
936163fc9cSnatano name = dent->d_name;
946163fc9cSnatano dot = 0;
956163fc9cSnatano if (name[0] == '.')
966163fc9cSnatano switch (name[1]) {
976163fc9cSnatano case '\0': /* "." */
986163fc9cSnatano if (join != NULL)
996163fc9cSnatano continue;
1006163fc9cSnatano dot = 1;
1016163fc9cSnatano break;
1026163fc9cSnatano case '.': /* ".." */
1036163fc9cSnatano if (name[2] == '\0')
1046163fc9cSnatano continue;
1056163fc9cSnatano /* FALLTHROUGH */
1066163fc9cSnatano default:
1076163fc9cSnatano dot = 0;
1086163fc9cSnatano }
1096163fc9cSnatano if (snprintf(path + len, sizeof(path) - len, "/%s", name) >=
1106163fc9cSnatano (int)sizeof(path) - len)
1116163fc9cSnatano errx(1, "Pathname too long.");
1126163fc9cSnatano if (lstat(path, &stbuf) == -1)
1136163fc9cSnatano err(1, "Can't lstat `%s'", path);
114913395cbSnatano if (S_ISSOCK(stbuf.st_mode & S_IFMT))
1156163fc9cSnatano continue;
1166163fc9cSnatano
1176163fc9cSnatano if (join != NULL) {
1186163fc9cSnatano cur = join->next;
1196163fc9cSnatano for (;;) {
1206163fc9cSnatano if (cur == NULL || strcmp(cur->name, name) == 0)
1216163fc9cSnatano break;
1226163fc9cSnatano if (cur == last) {
1236163fc9cSnatano cur = NULL;
1246163fc9cSnatano break;
1256163fc9cSnatano }
1266163fc9cSnatano cur = cur->next;
1276163fc9cSnatano }
1286163fc9cSnatano if (cur != NULL) {
1296163fc9cSnatano if (S_ISDIR(cur->type) &&
1306163fc9cSnatano S_ISDIR(stbuf.st_mode)) {
1316163fc9cSnatano cur->child = walk_dir(root, rp, cur,
1324a445526Snatano cur->child);
1336163fc9cSnatano continue;
1346163fc9cSnatano }
1356163fc9cSnatano errx(1, "Can't merge %s `%s' with "
1366163fc9cSnatano "existing %s",
1376163fc9cSnatano inode_type(stbuf.st_mode), path,
1386163fc9cSnatano inode_type(cur->type));
1396163fc9cSnatano }
1406163fc9cSnatano }
1416163fc9cSnatano
1426163fc9cSnatano cur = create_fsnode(root, dir, name, &stbuf);
1436163fc9cSnatano cur->parent = parent;
1446163fc9cSnatano if (dot) {
1456163fc9cSnatano /* ensure "." is at the start of the list */
1466163fc9cSnatano cur->next = first;
1476163fc9cSnatano first = cur;
1486163fc9cSnatano if (! prev)
1496163fc9cSnatano prev = cur;
1506163fc9cSnatano cur->first = first;
1516163fc9cSnatano } else { /* not "." */
1526163fc9cSnatano if (prev)
1536163fc9cSnatano prev->next = cur;
1546163fc9cSnatano prev = cur;
1556163fc9cSnatano if (!first)
1566163fc9cSnatano first = cur;
1576163fc9cSnatano cur->first = first;
1586163fc9cSnatano if (S_ISDIR(cur->type)) {
1594a445526Snatano cur->child = walk_dir(root, rp, cur, NULL);
1606163fc9cSnatano continue;
1616163fc9cSnatano }
1626163fc9cSnatano }
1636163fc9cSnatano if (stbuf.st_nlink > 1) {
1646163fc9cSnatano fsinode *curino;
1656163fc9cSnatano
1666163fc9cSnatano curino = link_check(cur->inode);
1676163fc9cSnatano if (curino != NULL) {
1686163fc9cSnatano free(cur->inode);
1696163fc9cSnatano cur->inode = curino;
1706163fc9cSnatano cur->inode->nlink++;
1716163fc9cSnatano }
1726163fc9cSnatano }
1736163fc9cSnatano if (S_ISLNK(cur->type)) {
1746163fc9cSnatano char slink[PATH_MAX+1];
1756163fc9cSnatano int llen;
1766163fc9cSnatano
1776163fc9cSnatano llen = readlink(path, slink, sizeof(slink) - 1);
1786163fc9cSnatano if (llen == -1)
1796163fc9cSnatano err(1, "Readlink `%s'", path);
1806163fc9cSnatano slink[llen] = '\0';
1816163fc9cSnatano cur->symlink = estrdup(slink);
1826163fc9cSnatano }
1836163fc9cSnatano }
1846163fc9cSnatano assert(first != NULL);
1856163fc9cSnatano if (join == NULL)
1866163fc9cSnatano for (cur = first->next; cur != NULL; cur = cur->next)
1876163fc9cSnatano cur->first = first;
1886163fc9cSnatano if (closedir(dirp) == -1)
1896163fc9cSnatano err(1, "Can't closedir `%s/%s'", root, dir);
1906163fc9cSnatano return (first);
1916163fc9cSnatano }
1926163fc9cSnatano
1936163fc9cSnatano static fsnode *
create_fsnode(const char * root,const char * path,const char * name,struct stat * stbuf)1946163fc9cSnatano create_fsnode(const char *root, const char *path, const char *name,
1956163fc9cSnatano struct stat *stbuf)
1966163fc9cSnatano {
1976163fc9cSnatano fsnode *cur;
1986163fc9cSnatano
1996163fc9cSnatano cur = ecalloc(1, sizeof(*cur));
2006163fc9cSnatano cur->path = estrdup(path);
2016163fc9cSnatano cur->name = estrdup(name);
2026163fc9cSnatano cur->inode = ecalloc(1, sizeof(*cur->inode));
2036163fc9cSnatano cur->root = root;
2046163fc9cSnatano cur->type = stbuf->st_mode & S_IFMT;
2056163fc9cSnatano cur->inode->nlink = 1;
2066163fc9cSnatano cur->inode->st = *stbuf;
207bc67c994Snatano if (Tflag) {
208*e425abdcSguenther cur->inode->st.st_atim.tv_sec = stampts;
209*e425abdcSguenther cur->inode->st.st_atim.tv_nsec = 0;
210*e425abdcSguenther cur->inode->st.st_mtim = cur->inode->st.st_ctim =
211*e425abdcSguenther cur->inode->st.st_atim;
2126163fc9cSnatano }
2136163fc9cSnatano return (cur);
2146163fc9cSnatano }
2156163fc9cSnatano
2166163fc9cSnatano /*
2176163fc9cSnatano * free_fsnodes --
2186163fc9cSnatano * Removes node from tree and frees it and all of
2198e544e80Sjsg * its descendants.
2206163fc9cSnatano */
2216163fc9cSnatano void
free_fsnodes(fsnode * node)2226163fc9cSnatano free_fsnodes(fsnode *node)
2236163fc9cSnatano {
2246163fc9cSnatano fsnode *cur, *next;
2256163fc9cSnatano
2266163fc9cSnatano assert(node != NULL);
2276163fc9cSnatano
2286163fc9cSnatano /* for ".", start with actual parent node */
2296163fc9cSnatano if (node->first == node) {
2306163fc9cSnatano assert(node->name[0] == '.' && node->name[1] == '\0');
2316163fc9cSnatano if (node->parent) {
2326163fc9cSnatano assert(node->parent->child == node);
2336163fc9cSnatano node = node->parent;
2346163fc9cSnatano }
2356163fc9cSnatano }
2366163fc9cSnatano
2376163fc9cSnatano /* Find ourselves in our sibling list and unlink */
2386163fc9cSnatano if (node->first != node) {
2396163fc9cSnatano for (cur = node->first; cur->next; cur = cur->next) {
2406163fc9cSnatano if (cur->next == node) {
2416163fc9cSnatano cur->next = node->next;
2426163fc9cSnatano node->next = NULL;
2436163fc9cSnatano break;
2446163fc9cSnatano }
2456163fc9cSnatano }
2466163fc9cSnatano }
2476163fc9cSnatano
2486163fc9cSnatano for (cur = node; cur != NULL; cur = next) {
2496163fc9cSnatano next = cur->next;
2506163fc9cSnatano if (cur->child) {
2516163fc9cSnatano cur->child->parent = NULL;
2526163fc9cSnatano free_fsnodes(cur->child);
2536163fc9cSnatano }
2546163fc9cSnatano if (cur->inode->nlink-- == 1)
2556163fc9cSnatano free(cur->inode);
2566163fc9cSnatano if (cur->symlink)
2576163fc9cSnatano free(cur->symlink);
2586163fc9cSnatano free(cur->path);
2596163fc9cSnatano free(cur->name);
2606163fc9cSnatano free(cur);
2616163fc9cSnatano }
2626163fc9cSnatano }
2636163fc9cSnatano
2646163fc9cSnatano
2656163fc9cSnatano /*
2666163fc9cSnatano * inode_type --
2676163fc9cSnatano * for a given inode type `mode', return a descriptive string.
2686163fc9cSnatano * for most cases, uses inotype() from mtree/misc.c
2696163fc9cSnatano */
2706163fc9cSnatano const char *
inode_type(mode_t mode)2716163fc9cSnatano inode_type(mode_t mode)
2726163fc9cSnatano {
2736163fc9cSnatano switch (mode & S_IFMT) {
2746163fc9cSnatano case S_IFBLK:
2756163fc9cSnatano return ("block");
2766163fc9cSnatano case S_IFCHR:
2776163fc9cSnatano return ("char");
2786163fc9cSnatano case S_IFDIR:
2796163fc9cSnatano return ("dir");
2806163fc9cSnatano case S_IFIFO:
2816163fc9cSnatano return ("fifo");
2826163fc9cSnatano case S_IFREG:
2836163fc9cSnatano return ("file");
2846163fc9cSnatano case S_IFLNK:
2856163fc9cSnatano return ("symlink");
2866163fc9cSnatano case S_IFSOCK:
2876163fc9cSnatano return ("socket");
2886163fc9cSnatano default:
2896163fc9cSnatano return ("unknown");
2906163fc9cSnatano }
2916163fc9cSnatano }
2926163fc9cSnatano
2936163fc9cSnatano
2946163fc9cSnatano /*
2956163fc9cSnatano * link_check --
2966163fc9cSnatano * return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
2976163fc9cSnatano * otherwise add `entry' to table and return NULL
2986163fc9cSnatano */
2996163fc9cSnatano /* This was borrowed from du.c and tweaked to keep an fsnode
3006163fc9cSnatano * pointer instead. -- dbj@netbsd.org
3016163fc9cSnatano */
3026163fc9cSnatano static fsinode *
link_check(fsinode * entry)3036163fc9cSnatano link_check(fsinode *entry)
3046163fc9cSnatano {
3056163fc9cSnatano static struct entry {
3066163fc9cSnatano fsinode *data;
3076163fc9cSnatano } *htable;
3086163fc9cSnatano static int htshift; /* log(allocated size) */
3096163fc9cSnatano static int htmask; /* allocated size - 1 */
3106163fc9cSnatano static int htused; /* 2*number of insertions */
3116163fc9cSnatano int h, h2;
3126163fc9cSnatano uint64_t tmp;
3136163fc9cSnatano /* this constant is (1<<64)/((1+sqrt(5))/2)
3146163fc9cSnatano * aka (word size)/(golden ratio)
3156163fc9cSnatano */
3166163fc9cSnatano const uint64_t HTCONST = 11400714819323198485ULL;
3176163fc9cSnatano const int HTBITS = 64;
3186163fc9cSnatano
3196163fc9cSnatano /* Never store zero in hashtable */
3206163fc9cSnatano assert(entry);
3216163fc9cSnatano
3226163fc9cSnatano /* Extend hash table if necessary, keep load under 0.5 */
3236163fc9cSnatano if (htused<<1 >= htmask) {
3246163fc9cSnatano struct entry *ohtable;
3256163fc9cSnatano
3266163fc9cSnatano if (!htable)
3276163fc9cSnatano htshift = 10; /* starting hashtable size */
3286163fc9cSnatano else
3296163fc9cSnatano htshift++; /* exponential hashtable growth */
3306163fc9cSnatano
3316163fc9cSnatano htmask = (1 << htshift) - 1;
3326163fc9cSnatano htused = 0;
3336163fc9cSnatano
3346163fc9cSnatano ohtable = htable;
3356163fc9cSnatano htable = ecalloc(htmask+1, sizeof(*htable));
3366163fc9cSnatano /* populate newly allocated hashtable */
3376163fc9cSnatano if (ohtable) {
3386163fc9cSnatano int i;
3396163fc9cSnatano for (i = 0; i <= htmask>>1; i++)
3406163fc9cSnatano if (ohtable[i].data)
3416163fc9cSnatano link_check(ohtable[i].data);
3426163fc9cSnatano free(ohtable);
3436163fc9cSnatano }
3446163fc9cSnatano }
3456163fc9cSnatano
3466163fc9cSnatano /* multiplicative hashing */
3476163fc9cSnatano tmp = entry->st.st_dev;
3486163fc9cSnatano tmp <<= HTBITS>>1;
3496163fc9cSnatano tmp |= entry->st.st_ino;
3506163fc9cSnatano tmp *= HTCONST;
3516163fc9cSnatano h = tmp >> (HTBITS - htshift);
3526163fc9cSnatano h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
3536163fc9cSnatano
3546163fc9cSnatano /* open address hashtable search with double hash probing */
3556163fc9cSnatano while (htable[h].data) {
3566163fc9cSnatano if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
3576163fc9cSnatano (htable[h].data->st.st_dev == entry->st.st_dev)) {
3586163fc9cSnatano return htable[h].data;
3596163fc9cSnatano }
3606163fc9cSnatano h = (h + h2) & htmask;
3616163fc9cSnatano }
3626163fc9cSnatano
3636163fc9cSnatano /* Insert the current entry into hashtable */
3646163fc9cSnatano htable[h].data = entry;
3656163fc9cSnatano htused++;
3666163fc9cSnatano return NULL;
3676163fc9cSnatano }
368