2012-01-28 06:35:46 +04:00
|
|
|
/* $NetBSD: walk.c,v 1.25 2012/01/28 02:35:46 christos Exp $ */
|
2001-10-26 10:16:19 +04:00
|
|
|
|
|
|
|
/*
|
2001-11-02 06:12:48 +03:00
|
|
|
* Copyright (c) 2001 Wasabi Systems, Inc.
|
2001-10-26 10:16:19 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Written by Luke Mewburn for Wasabi Systems, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed for the NetBSD Project by
|
|
|
|
* Wasabi Systems, Inc.
|
|
|
|
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-06-21 02:20:14 +04:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
2001-10-28 16:14:05 +03:00
|
|
|
#include <sys/cdefs.h>
|
2002-02-01 01:43:33 +03:00
|
|
|
#if defined(__RCSID) && !defined(__lint)
|
2012-01-28 06:35:46 +04:00
|
|
|
__RCSID("$NetBSD: walk.c,v 1.25 2012/01/28 02:35:46 christos Exp $");
|
2001-10-28 16:14:05 +03:00
|
|
|
#endif /* !__lint */
|
|
|
|
|
2001-10-26 10:16:19 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2006-02-02 01:19:34 +03:00
|
|
|
#include <sys/stat.h>
|
2001-10-26 10:16:19 +04:00
|
|
|
|
|
|
|
#include "makefs.h"
|
|
|
|
#include "mtree.h"
|
|
|
|
|
2006-10-10 05:55:45 +04:00
|
|
|
static void apply_specdir(const char *, NODE *, fsnode *, int);
|
2001-10-26 10:16:19 +04:00
|
|
|
static void apply_specentry(const char *, NODE *, fsnode *);
|
2012-01-28 06:35:46 +04:00
|
|
|
static fsnode *create_fsnode(const char *, const char *, const char *,
|
|
|
|
struct stat *);
|
2001-12-05 14:08:53 +03:00
|
|
|
static fsinode *link_check(fsinode *);
|
2001-10-26 10:16:19 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* walk_dir --
|
2012-01-28 06:35:46 +04:00
|
|
|
* build a tree of fsnodes from `root' and `dir', with a parent
|
|
|
|
* fsnode of `parent' (which may be NULL for the root of the tree).
|
|
|
|
* append the tree to a fsnode of `join' if it is not NULL.
|
2001-10-26 10:16:19 +04:00
|
|
|
* each "level" is a directory, with the "." entry guaranteed to be
|
|
|
|
* at the start of the list, and without ".." entries.
|
|
|
|
*/
|
|
|
|
fsnode *
|
2012-01-28 06:35:46 +04:00
|
|
|
walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join)
|
2001-10-26 10:16:19 +04:00
|
|
|
{
|
2012-01-28 06:35:46 +04:00
|
|
|
fsnode *first, *cur, *prev, *last;
|
2001-10-26 10:16:19 +04:00
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dent;
|
|
|
|
char path[MAXPATHLEN + 1];
|
|
|
|
struct stat stbuf;
|
2012-01-28 06:35:46 +04:00
|
|
|
char *name, *rp;
|
|
|
|
int dot, len;
|
2001-10-26 10:16:19 +04:00
|
|
|
|
2012-01-28 06:35:46 +04:00
|
|
|
assert(root != NULL);
|
2001-10-26 10:16:19 +04:00
|
|
|
assert(dir != NULL);
|
|
|
|
|
2012-01-28 06:35:46 +04:00
|
|
|
len = snprintf(path, sizeof(path), "%s/%s", root, dir);
|
|
|
|
if (len >= (int)sizeof(path))
|
|
|
|
errx(1, "Pathname too long.");
|
2001-10-26 10:16:19 +04:00
|
|
|
if (debug & DEBUG_WALK_DIR)
|
2012-01-28 06:35:46 +04:00
|
|
|
printf("walk_dir: %s %p\n", path, parent);
|
|
|
|
if ((dirp = opendir(path)) == NULL)
|
|
|
|
err(1, "Can't opendir `%s'", path);
|
|
|
|
rp = path + strlen(root) + 1;
|
|
|
|
if (join != NULL) {
|
|
|
|
first = cur = join;
|
|
|
|
while (cur->next != NULL)
|
|
|
|
cur = cur->next;
|
|
|
|
prev = last = cur;
|
|
|
|
} else
|
|
|
|
last = first = prev = NULL;
|
2001-10-26 10:16:19 +04:00
|
|
|
while ((dent = readdir(dirp)) != NULL) {
|
2012-01-28 06:35:46 +04:00
|
|
|
name = dent->d_name;
|
|
|
|
dot = 0;
|
|
|
|
if (name[0] == '.')
|
|
|
|
switch (name[1]) {
|
|
|
|
case '\0': /* "." */
|
|
|
|
if (join != NULL)
|
|
|
|
continue;
|
|
|
|
dot = 1;
|
|
|
|
break;
|
|
|
|
case '.': /* ".." */
|
|
|
|
if (name[2] == '\0')
|
|
|
|
continue;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
dot = 0;
|
|
|
|
}
|
2001-10-26 10:16:19 +04:00
|
|
|
if (debug & DEBUG_WALK_DIR_NODE)
|
2012-01-28 06:35:46 +04:00
|
|
|
printf("scanning %s/%s/%s\n", root, dir, name);
|
|
|
|
if (snprintf(path + len, sizeof(path) - len, "/%s", name) >=
|
|
|
|
(int)sizeof(path) - len)
|
2001-10-26 10:16:19 +04:00
|
|
|
errx(1, "Pathname too long.");
|
|
|
|
if (lstat(path, &stbuf) == -1)
|
|
|
|
err(1, "Can't lstat `%s'", path);
|
2004-06-21 02:20:14 +04:00
|
|
|
#ifdef S_ISSOCK
|
2001-10-26 10:16:19 +04:00
|
|
|
if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
|
|
|
|
if (debug & DEBUG_WALK_DIR_NODE)
|
|
|
|
printf(" skipping socket %s\n", path);
|
|
|
|
continue;
|
|
|
|
}
|
2004-06-21 02:20:14 +04:00
|
|
|
#endif
|
2001-10-26 10:16:19 +04:00
|
|
|
|
2012-01-28 06:35:46 +04:00
|
|
|
if (join != NULL) {
|
|
|
|
cur = join->next;
|
|
|
|
for (;;) {
|
|
|
|
if (cur == NULL || strcmp(cur->name, name) == 0)
|
|
|
|
break;
|
|
|
|
if (cur == last) {
|
|
|
|
cur = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
if (cur != NULL) {
|
|
|
|
if (S_ISDIR(cur->type) &&
|
|
|
|
S_ISDIR(stbuf.st_mode)) {
|
|
|
|
if (debug & DEBUG_WALK_DIR_NODE)
|
|
|
|
printf("merging %s with %p\n",
|
|
|
|
path, cur->child);
|
|
|
|
cur->child = walk_dir(root, rp, cur,
|
|
|
|
cur->child);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
errx(1, "Can't merge %s `%s' with existing %s",
|
|
|
|
inode_type(stbuf.st_mode), path,
|
|
|
|
inode_type(cur->type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = create_fsnode(root, dir, name, &stbuf);
|
2001-10-26 10:16:19 +04:00
|
|
|
cur->parent = parent;
|
2012-01-28 06:35:46 +04:00
|
|
|
if (dot) {
|
2001-10-26 10:16:19 +04:00
|
|
|
/* ensure "." is at the start of the list */
|
|
|
|
cur->next = first;
|
|
|
|
first = cur;
|
|
|
|
if (! prev)
|
|
|
|
prev = cur;
|
2012-01-28 06:35:46 +04:00
|
|
|
cur->first = first;
|
2001-10-26 10:16:19 +04:00
|
|
|
} else { /* not "." */
|
|
|
|
if (prev)
|
|
|
|
prev->next = cur;
|
|
|
|
prev = cur;
|
|
|
|
if (!first)
|
|
|
|
first = cur;
|
2012-01-28 06:35:46 +04:00
|
|
|
cur->first = first;
|
2001-10-26 10:16:19 +04:00
|
|
|
if (S_ISDIR(cur->type)) {
|
2012-01-28 06:35:46 +04:00
|
|
|
cur->child = walk_dir(root, rp, cur, NULL);
|
2001-10-26 10:16:19 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2001-12-05 14:08:53 +03:00
|
|
|
if (stbuf.st_nlink > 1) {
|
|
|
|
fsinode *curino;
|
|
|
|
|
|
|
|
curino = link_check(cur->inode);
|
|
|
|
if (curino != NULL) {
|
|
|
|
free(cur->inode);
|
|
|
|
cur->inode = curino;
|
|
|
|
cur->inode->nlink++;
|
2006-10-10 05:39:10 +04:00
|
|
|
if (debug & DEBUG_WALK_DIR_LINKCHECK)
|
2008-12-29 00:51:46 +03:00
|
|
|
printf("link_check: found [%llu, %llu]\n",
|
|
|
|
(unsigned long long)curino->st.st_dev,
|
2006-10-10 05:39:10 +04:00
|
|
|
(unsigned long long)curino->st.st_ino);
|
2001-12-05 14:08:53 +03:00
|
|
|
}
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
if (S_ISLNK(cur->type)) {
|
|
|
|
char slink[PATH_MAX+1];
|
|
|
|
int llen;
|
|
|
|
|
2003-05-09 04:48:59 +04:00
|
|
|
llen = readlink(path, slink, sizeof(slink) - 1);
|
2001-10-26 10:16:19 +04:00
|
|
|
if (llen == -1)
|
|
|
|
err(1, "Readlink `%s'", path);
|
|
|
|
slink[llen] = '\0';
|
|
|
|
if ((cur->symlink = strdup(slink)) == NULL)
|
|
|
|
err(1, "Memory allocation error");
|
|
|
|
}
|
|
|
|
}
|
2012-01-28 06:35:46 +04:00
|
|
|
assert(first != NULL);
|
|
|
|
if (join == NULL)
|
|
|
|
for (cur = first->next; cur != NULL; cur = cur->next)
|
|
|
|
cur->first = first;
|
2001-10-26 10:16:19 +04:00
|
|
|
if (closedir(dirp) == -1)
|
2012-01-28 06:35:46 +04:00
|
|
|
err(1, "Can't closedir `%s/%s'", root, dir);
|
2001-10-26 10:16:19 +04:00
|
|
|
return (first);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fsnode *
|
2012-01-28 06:35:46 +04:00
|
|
|
create_fsnode(const char *root, const char *path, const char *name,
|
|
|
|
struct stat *stbuf)
|
2001-10-26 10:16:19 +04:00
|
|
|
{
|
|
|
|
fsnode *cur;
|
|
|
|
|
|
|
|
if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
|
2012-01-28 06:35:46 +04:00
|
|
|
(cur->path = strdup(path)) == NULL ||
|
2002-01-23 05:26:21 +03:00
|
|
|
(cur->name = strdup(name)) == NULL ||
|
|
|
|
(cur->inode = calloc(1, sizeof(fsinode))) == NULL)
|
2001-10-26 10:16:19 +04:00
|
|
|
err(1, "Memory allocation error");
|
2012-01-28 06:35:46 +04:00
|
|
|
cur->root = root;
|
2001-12-05 14:08:53 +03:00
|
|
|
cur->type = stbuf->st_mode & S_IFMT;
|
2002-01-23 05:26:21 +03:00
|
|
|
cur->inode->nlink = 1;
|
|
|
|
cur->inode->st = *stbuf;
|
2001-12-05 14:08:53 +03:00
|
|
|
return (cur);
|
|
|
|
}
|
2001-10-26 10:16:19 +04:00
|
|
|
|
2006-10-10 05:46:49 +04:00
|
|
|
/*
|
|
|
|
* free_fsnodes --
|
|
|
|
* Removes node from tree and frees it and all of
|
|
|
|
* its decendents.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
free_fsnodes(fsnode *node)
|
|
|
|
{
|
|
|
|
fsnode *cur, *next;
|
|
|
|
|
|
|
|
assert(node != NULL);
|
|
|
|
|
|
|
|
/* for ".", start with actual parent node */
|
|
|
|
if (node->first == node) {
|
|
|
|
assert(node->name[0] == '.' && node->name[1] == '\0');
|
|
|
|
if (node->parent) {
|
|
|
|
assert(node->parent->child == node);
|
|
|
|
node = node->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find ourselves in our sibling list and unlink */
|
|
|
|
if (node->first != node) {
|
|
|
|
for (cur = node->first; cur->next; cur = cur->next) {
|
|
|
|
if (cur->next == node) {
|
|
|
|
cur->next = node->next;
|
|
|
|
node->next = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (cur = node; cur != NULL; cur = next) {
|
|
|
|
next = cur->next;
|
|
|
|
if (cur->child) {
|
|
|
|
cur->child->parent = NULL;
|
|
|
|
free_fsnodes(cur->child);
|
|
|
|
}
|
|
|
|
if (cur->inode->nlink-- == 1)
|
|
|
|
free(cur->inode);
|
|
|
|
if (cur->symlink)
|
|
|
|
free(cur->symlink);
|
2012-01-28 06:35:46 +04:00
|
|
|
free(cur->path);
|
2006-10-10 05:46:49 +04:00
|
|
|
free(cur->name);
|
|
|
|
free(cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-26 10:16:19 +04:00
|
|
|
/*
|
|
|
|
* apply_specfile --
|
|
|
|
* read in the mtree(8) specfile, and apply it to the tree
|
|
|
|
* at dir,parent. parameters in parent on equivalent types
|
|
|
|
* will be changed to those found in specfile, and missing
|
|
|
|
* entries will be added.
|
|
|
|
*/
|
|
|
|
void
|
2006-10-10 05:55:45 +04:00
|
|
|
apply_specfile(const char *specfile, const char *dir, fsnode *parent, int speconly)
|
2001-10-26 10:16:19 +04:00
|
|
|
{
|
|
|
|
struct timeval start;
|
|
|
|
FILE *fp;
|
|
|
|
NODE *root;
|
|
|
|
|
|
|
|
assert(specfile != NULL);
|
|
|
|
assert(parent != NULL);
|
|
|
|
|
|
|
|
if (debug & DEBUG_APPLY_SPECFILE)
|
|
|
|
printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
|
|
|
|
|
|
|
|
/* read in the specfile */
|
|
|
|
if ((fp = fopen(specfile, "r")) == NULL)
|
|
|
|
err(1, "Can't open `%s'", specfile);
|
|
|
|
TIMER_START(start);
|
|
|
|
root = spec(fp);
|
|
|
|
TIMER_RESULTS(start, "spec");
|
|
|
|
if (fclose(fp) == EOF)
|
|
|
|
err(1, "Can't close `%s'", specfile);
|
|
|
|
|
|
|
|
/* perform some sanity checks */
|
|
|
|
if (root == NULL)
|
|
|
|
errx(1, "Specfile `%s' did not contain a tree", specfile);
|
|
|
|
assert(strcmp(root->name, ".") == 0);
|
|
|
|
assert(root->type == F_DIR);
|
|
|
|
|
|
|
|
/* merge in the changes */
|
2006-10-10 05:55:45 +04:00
|
|
|
apply_specdir(dir, root, parent, speconly);
|
2006-10-10 05:41:14 +04:00
|
|
|
|
|
|
|
free_nodes(root);
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-10-10 05:55:45 +04:00
|
|
|
apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly)
|
2001-10-26 10:16:19 +04:00
|
|
|
{
|
|
|
|
char path[MAXPATHLEN + 1];
|
|
|
|
NODE *curnode;
|
|
|
|
fsnode *curfsnode;
|
|
|
|
|
|
|
|
assert(specnode != NULL);
|
|
|
|
assert(dirnode != NULL);
|
|
|
|
|
|
|
|
if (debug & DEBUG_APPLY_SPECFILE)
|
|
|
|
printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
|
|
|
|
|
|
|
|
if (specnode->type != F_DIR)
|
|
|
|
errx(1, "Specfile node `%s/%s' is not a directory",
|
|
|
|
dir, specnode->name);
|
|
|
|
if (dirnode->type != S_IFDIR)
|
|
|
|
errx(1, "Directory node `%s/%s' is not a directory",
|
|
|
|
dir, dirnode->name);
|
|
|
|
|
|
|
|
apply_specentry(dir, specnode, dirnode);
|
|
|
|
|
2006-10-10 05:55:45 +04:00
|
|
|
/* Remove any filesystem nodes not found in specfile */
|
|
|
|
/* XXX inefficient. This is O^2 in each dir and it would
|
|
|
|
* have been better never to have walked this part of the tree
|
|
|
|
* to begin with
|
|
|
|
*/
|
|
|
|
if (speconly) {
|
|
|
|
fsnode *next;
|
|
|
|
assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0');
|
|
|
|
for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) {
|
|
|
|
next = curfsnode->next;
|
|
|
|
for (curnode = specnode->child; curnode != NULL;
|
|
|
|
curnode = curnode->next) {
|
|
|
|
if (strcmp(curnode->name, curfsnode->name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (curnode == NULL) {
|
|
|
|
if (debug & DEBUG_APPLY_SPECONLY) {
|
|
|
|
printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode);
|
|
|
|
}
|
|
|
|
free_fsnodes(curfsnode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-26 10:16:19 +04:00
|
|
|
/* now walk specnode->child matching up with dirnode */
|
|
|
|
for (curnode = specnode->child; curnode != NULL;
|
|
|
|
curnode = curnode->next) {
|
|
|
|
if (debug & DEBUG_APPLY_SPECENTRY)
|
|
|
|
printf("apply_specdir: spec %s\n",
|
|
|
|
curnode->name);
|
|
|
|
for (curfsnode = dirnode->next; curfsnode != NULL;
|
|
|
|
curfsnode = curfsnode->next) {
|
2001-10-29 08:38:09 +03:00
|
|
|
#if 0 /* too verbose for now */
|
2001-10-26 10:16:19 +04:00
|
|
|
if (debug & DEBUG_APPLY_SPECENTRY)
|
|
|
|
printf("apply_specdir: dirent %s\n",
|
|
|
|
curfsnode->name);
|
2001-10-29 08:38:09 +03:00
|
|
|
#endif
|
2001-10-26 10:16:19 +04:00
|
|
|
if (strcmp(curnode->name, curfsnode->name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2002-02-08 04:17:32 +03:00
|
|
|
if (snprintf(path, sizeof(path), "%s/%s",
|
|
|
|
dir, curnode->name) >= sizeof(path))
|
|
|
|
errx(1, "Pathname too long.");
|
2001-10-26 10:16:19 +04:00
|
|
|
if (curfsnode == NULL) { /* need new entry */
|
|
|
|
struct stat stbuf;
|
|
|
|
|
2002-02-08 04:17:32 +03:00
|
|
|
/*
|
|
|
|
* don't add optional spec entries
|
|
|
|
* that lack an existing fs entry
|
|
|
|
*/
|
|
|
|
if ((curnode->flags & F_OPT) &&
|
|
|
|
lstat(path, &stbuf) == -1)
|
|
|
|
continue;
|
|
|
|
|
2001-10-26 10:16:19 +04:00
|
|
|
/* check that enough info is provided */
|
|
|
|
#define NODETEST(t, m) \
|
|
|
|
if (!(t)) \
|
2002-02-08 04:17:32 +03:00
|
|
|
errx(1, "`%s': %s not provided", path, m)
|
2001-10-26 10:16:19 +04:00
|
|
|
NODETEST(curnode->flags & F_TYPE, "type");
|
|
|
|
NODETEST(curnode->flags & F_MODE, "mode");
|
|
|
|
/* XXX: require F_TIME ? */
|
|
|
|
NODETEST(curnode->flags & F_GID ||
|
|
|
|
curnode->flags & F_GNAME, "group");
|
|
|
|
NODETEST(curnode->flags & F_UID ||
|
|
|
|
curnode->flags & F_UNAME, "user");
|
|
|
|
if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
|
|
|
|
NODETEST(curnode->flags & F_DEV,
|
|
|
|
"device number");
|
|
|
|
#undef NODETEST
|
|
|
|
|
|
|
|
if (debug & DEBUG_APPLY_SPECFILE)
|
|
|
|
printf("apply_specdir: adding %s\n",
|
|
|
|
curnode->name);
|
|
|
|
/* build minimal fsnode */
|
|
|
|
memset(&stbuf, 0, sizeof(stbuf));
|
|
|
|
stbuf.st_mode = nodetoino(curnode->type);
|
2001-12-05 14:08:53 +03:00
|
|
|
stbuf.st_nlink = 1;
|
2001-10-26 10:16:19 +04:00
|
|
|
stbuf.st_mtime = stbuf.st_atime =
|
|
|
|
stbuf.st_ctime = start_time.tv_sec;
|
2002-02-01 01:43:33 +03:00
|
|
|
#if HAVE_STRUCT_STAT_ST_MTIMENSEC
|
2001-10-26 10:16:19 +04:00
|
|
|
stbuf.st_mtimensec = stbuf.st_atimensec =
|
|
|
|
stbuf.st_ctimensec = start_time.tv_nsec;
|
2002-02-01 01:43:33 +03:00
|
|
|
#endif
|
2012-01-28 06:35:46 +04:00
|
|
|
curfsnode = create_fsnode(".", ".", curnode->name,
|
|
|
|
&stbuf);
|
2001-10-26 10:16:19 +04:00
|
|
|
curfsnode->parent = dirnode->parent;
|
|
|
|
curfsnode->first = dirnode;
|
|
|
|
curfsnode->next = dirnode->next;
|
|
|
|
dirnode->next = curfsnode;
|
|
|
|
if (curfsnode->type == S_IFDIR) {
|
|
|
|
/* for dirs, make "." entry as well */
|
2012-01-28 06:35:46 +04:00
|
|
|
curfsnode->child = create_fsnode(".", ".", ".",
|
|
|
|
&stbuf);
|
2001-10-26 10:16:19 +04:00
|
|
|
curfsnode->child->parent = curfsnode;
|
|
|
|
curfsnode->child->first = curfsnode->child;
|
|
|
|
}
|
|
|
|
if (curfsnode->type == S_IFLNK) {
|
2001-10-29 08:38:09 +03:00
|
|
|
assert(curnode->slink != NULL);
|
2001-10-26 10:16:19 +04:00
|
|
|
/* for symlinks, copy the target */
|
|
|
|
if ((curfsnode->symlink =
|
|
|
|
strdup(curnode->slink)) == NULL)
|
|
|
|
err(1, "Memory allocation error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
apply_specentry(dir, curnode, curfsnode);
|
|
|
|
if (curnode->type == F_DIR) {
|
|
|
|
if (curfsnode->type != S_IFDIR)
|
2002-02-08 04:17:32 +03:00
|
|
|
errx(1, "`%s' is not a directory", path);
|
2001-10-26 10:16:19 +04:00
|
|
|
assert (curfsnode->child != NULL);
|
2006-10-10 05:55:45 +04:00
|
|
|
apply_specdir(path, curnode, curfsnode->child, speconly);
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(specnode != NULL);
|
|
|
|
assert(dirnode != NULL);
|
|
|
|
|
|
|
|
if (nodetoino(specnode->type) != dirnode->type)
|
|
|
|
errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
|
|
|
|
dir, specnode->name, inode_type(nodetoino(specnode->type)),
|
|
|
|
inode_type(dirnode->type));
|
|
|
|
|
|
|
|
if (debug & DEBUG_APPLY_SPECENTRY)
|
|
|
|
printf("apply_specentry: %s/%s\n", dir, dirnode->name);
|
|
|
|
|
|
|
|
#define ASEPRINT(t, b, o, n) \
|
|
|
|
if (debug & DEBUG_APPLY_SPECENTRY) \
|
|
|
|
printf("\t\t\tchanging %s from " b " to " b "\n", \
|
|
|
|
t, o, n)
|
|
|
|
|
|
|
|
if (specnode->flags & (F_GID | F_GNAME)) {
|
|
|
|
ASEPRINT("gid", "%d",
|
2001-12-05 14:08:53 +03:00
|
|
|
dirnode->inode->st.st_gid, specnode->st_gid);
|
|
|
|
dirnode->inode->st.st_gid = specnode->st_gid;
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
if (specnode->flags & F_MODE) {
|
|
|
|
ASEPRINT("mode", "%#o",
|
2001-12-05 14:08:53 +03:00
|
|
|
dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
|
|
|
|
dirnode->inode->st.st_mode &= ~ALLPERMS;
|
|
|
|
dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
/* XXX: ignoring F_NLINK for now */
|
|
|
|
if (specnode->flags & F_SIZE) {
|
|
|
|
ASEPRINT("size", "%lld",
|
2001-12-05 14:08:53 +03:00
|
|
|
(long long)dirnode->inode->st.st_size,
|
2001-10-26 10:16:19 +04:00
|
|
|
(long long)specnode->st_size);
|
2001-12-05 14:08:53 +03:00
|
|
|
dirnode->inode->st.st_size = specnode->st_size;
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
if (specnode->flags & F_SLINK) {
|
|
|
|
assert(dirnode->symlink != NULL);
|
|
|
|
assert(specnode->slink != NULL);
|
|
|
|
ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
|
|
|
|
free(dirnode->symlink);
|
|
|
|
if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
|
|
|
|
err(1, "Memory allocation error");
|
|
|
|
}
|
|
|
|
if (specnode->flags & F_TIME) {
|
|
|
|
ASEPRINT("time", "%ld",
|
2001-12-05 14:08:53 +03:00
|
|
|
(long)dirnode->inode->st.st_mtime,
|
2002-02-01 01:43:33 +03:00
|
|
|
(long)specnode->st_mtimespec.tv_sec);
|
|
|
|
dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec;
|
|
|
|
dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec;
|
|
|
|
dirnode->inode->st.st_ctime = start_time.tv_sec;
|
|
|
|
#if HAVE_STRUCT_STAT_ST_MTIMENSEC
|
2004-06-21 02:20:14 +04:00
|
|
|
dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec;
|
|
|
|
dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec;
|
2001-12-05 14:08:53 +03:00
|
|
|
dirnode->inode->st.st_ctimensec = start_time.tv_nsec;
|
2002-02-01 01:43:33 +03:00
|
|
|
#endif
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
if (specnode->flags & (F_UID | F_UNAME)) {
|
|
|
|
ASEPRINT("uid", "%d",
|
2001-12-05 14:08:53 +03:00
|
|
|
dirnode->inode->st.st_uid, specnode->st_uid);
|
|
|
|
dirnode->inode->st.st_uid = specnode->st_uid;
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
2002-02-01 01:43:33 +03:00
|
|
|
#if HAVE_STRUCT_STAT_ST_FLAGS
|
2001-10-26 10:16:19 +04:00
|
|
|
if (specnode->flags & F_FLAGS) {
|
|
|
|
ASEPRINT("flags", "%#lX",
|
2003-01-27 04:34:09 +03:00
|
|
|
(unsigned long)dirnode->inode->st.st_flags,
|
|
|
|
(unsigned long)specnode->st_flags);
|
2001-12-05 14:08:53 +03:00
|
|
|
dirnode->inode->st.st_flags = specnode->st_flags;
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
2002-02-01 01:43:33 +03:00
|
|
|
#endif
|
2001-10-26 10:16:19 +04:00
|
|
|
if (specnode->flags & F_DEV) {
|
2008-12-29 00:51:46 +03:00
|
|
|
ASEPRINT("rdev", "%#llx",
|
|
|
|
(unsigned long long)dirnode->inode->st.st_rdev,
|
|
|
|
(unsigned long long)specnode->st_rdev);
|
2001-12-05 14:08:53 +03:00
|
|
|
dirnode->inode->st.st_rdev = specnode->st_rdev;
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
#undef ASEPRINT
|
2003-03-29 03:12:12 +03:00
|
|
|
|
|
|
|
dirnode->flags |= FSNODE_F_HASSPEC;
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dump_fsnodes --
|
2012-01-28 06:35:46 +04:00
|
|
|
* dump the fsnodes from `cur'
|
2001-10-26 10:16:19 +04:00
|
|
|
*/
|
|
|
|
void
|
2012-01-28 06:35:46 +04:00
|
|
|
dump_fsnodes(fsnode *root)
|
2001-10-26 10:16:19 +04:00
|
|
|
{
|
|
|
|
fsnode *cur;
|
|
|
|
char path[MAXPATHLEN + 1];
|
|
|
|
|
2012-01-28 06:35:46 +04:00
|
|
|
printf("dump_fsnodes: %s %p\n", root->path, root);
|
2001-10-26 10:16:19 +04:00
|
|
|
for (cur = root; cur != NULL; cur = cur->next) {
|
2012-01-28 06:35:46 +04:00
|
|
|
if (snprintf(path, sizeof(path), "%s/%s", cur->path,
|
|
|
|
cur->name) >= (int)sizeof(path))
|
2001-10-26 10:16:19 +04:00
|
|
|
errx(1, "Pathname too long.");
|
|
|
|
|
|
|
|
if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
|
|
|
|
printf("cur=%8p parent=%8p first=%8p ",
|
|
|
|
cur, cur->parent, cur->first);
|
|
|
|
printf("%7s: %s", inode_type(cur->type), path);
|
|
|
|
if (S_ISLNK(cur->type)) {
|
|
|
|
assert(cur->symlink != NULL);
|
|
|
|
printf(" -> %s", cur->symlink);
|
|
|
|
} else {
|
|
|
|
assert (cur->symlink == NULL);
|
|
|
|
}
|
2001-12-05 14:08:53 +03:00
|
|
|
if (cur->inode->nlink > 1)
|
|
|
|
printf(", nlinks=%d", cur->inode->nlink);
|
2001-10-26 10:16:19 +04:00
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
if (cur->child) {
|
|
|
|
assert (cur->type == S_IFDIR);
|
2012-01-28 06:35:46 +04:00
|
|
|
dump_fsnodes(cur->child);
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
}
|
2012-01-28 06:35:46 +04:00
|
|
|
printf("dump_fsnodes: finished %s/%s\n", root->path, root->name);
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* inode_type --
|
|
|
|
* for a given inode type `mode', return a descriptive string.
|
|
|
|
* for most cases, uses inotype() from mtree/misc.c
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
inode_type(mode_t mode)
|
|
|
|
{
|
|
|
|
|
2001-10-29 08:38:09 +03:00
|
|
|
if (S_ISLNK(mode))
|
2001-10-26 10:16:19 +04:00
|
|
|
return ("symlink"); /* inotype() returns "link"... */
|
|
|
|
return (inotype(mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* link_check --
|
2006-10-10 05:39:10 +04:00
|
|
|
* return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
|
2001-10-26 10:16:19 +04:00
|
|
|
* otherwise add `entry' to table and return NULL
|
|
|
|
*/
|
2006-10-10 05:39:10 +04:00
|
|
|
/* This was borrowed from du.c and tweaked to keep an fsnode
|
|
|
|
* pointer instead. -- dbj@netbsd.org
|
|
|
|
*/
|
2001-12-05 14:08:53 +03:00
|
|
|
static fsinode *
|
|
|
|
link_check(fsinode *entry)
|
2001-10-26 10:16:19 +04:00
|
|
|
{
|
2006-10-10 05:39:10 +04:00
|
|
|
static struct entry {
|
|
|
|
fsinode *data;
|
|
|
|
} *htable;
|
|
|
|
static int htshift; /* log(allocated size) */
|
|
|
|
static int htmask; /* allocated size - 1 */
|
|
|
|
static int htused; /* 2*number of insertions */
|
|
|
|
int h, h2;
|
|
|
|
uint64_t tmp;
|
|
|
|
/* this constant is (1<<64)/((1+sqrt(5))/2)
|
|
|
|
* aka (word size)/(golden ratio)
|
|
|
|
*/
|
|
|
|
const uint64_t HTCONST = 11400714819323198485ULL;
|
|
|
|
const int HTBITS = 64;
|
|
|
|
|
|
|
|
/* Never store zero in hashtable */
|
|
|
|
assert(entry);
|
|
|
|
|
|
|
|
/* Extend hash table if necessary, keep load under 0.5 */
|
|
|
|
if (htused<<1 >= htmask) {
|
|
|
|
struct entry *ohtable;
|
|
|
|
|
|
|
|
if (!htable)
|
|
|
|
htshift = 10; /* starting hashtable size */
|
|
|
|
else
|
|
|
|
htshift++; /* exponential hashtable growth */
|
|
|
|
|
|
|
|
htmask = (1 << htshift) - 1;
|
|
|
|
htused = 0;
|
|
|
|
|
|
|
|
ohtable = htable;
|
|
|
|
htable = calloc(htmask+1, sizeof(*htable));
|
|
|
|
if (!htable)
|
|
|
|
err(1, "Memory allocation error");
|
|
|
|
|
|
|
|
/* populate newly allocated hashtable */
|
|
|
|
if (ohtable) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i <= htmask>>1; i++)
|
|
|
|
if (ohtable[i].data)
|
|
|
|
link_check(ohtable[i].data);
|
|
|
|
free(ohtable);
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-10 05:39:10 +04:00
|
|
|
/* multiplicative hashing */
|
|
|
|
tmp = entry->st.st_dev;
|
|
|
|
tmp <<= HTBITS>>1;
|
|
|
|
tmp |= entry->st.st_ino;
|
|
|
|
tmp *= HTCONST;
|
|
|
|
h = tmp >> (HTBITS - htshift);
|
|
|
|
h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
|
|
|
|
|
|
|
|
/* open address hashtable search with double hash probing */
|
|
|
|
while (htable[h].data) {
|
|
|
|
if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
|
|
|
|
(htable[h].data->st.st_dev == entry->st.st_dev)) {
|
|
|
|
return htable[h].data;
|
|
|
|
}
|
|
|
|
h = (h + h2) & htmask;
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|
|
|
|
|
2006-10-10 05:39:10 +04:00
|
|
|
/* Insert the current entry into hashtable */
|
|
|
|
htable[h].data = entry;
|
|
|
|
htused++;
|
|
|
|
return NULL;
|
2001-10-26 10:16:19 +04:00
|
|
|
}
|