357 lines
7.4 KiB
C
Executable File
357 lines
7.4 KiB
C
Executable File
/* vim: set noet ts=8 sts=8 sw=8 : */
|
|
|
|
/**
|
|
* \file tree.c
|
|
*
|
|
* Implement filesystem trees.
|
|
*/
|
|
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <dirent.h>
|
|
#include <libgen.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <err.h>
|
|
#include <stdio.h>
|
|
|
|
#include "tree.h"
|
|
#include "exclude.h"
|
|
|
|
static void
|
|
set_default_stat(struct stat *s)
|
|
{
|
|
time_t now = time(NULL);
|
|
|
|
memset(s, 0, sizeof(struct stat));
|
|
s->st_mode = 0555;
|
|
s->st_atime = s->st_mtime = s->st_ctime = now;
|
|
}
|
|
|
|
static void
|
|
append_node(struct iso_tree_node_dir *parent,
|
|
struct iso_tree_node *child)
|
|
{
|
|
assert( parent && child);
|
|
|
|
parent->nchildren++;
|
|
parent->children =
|
|
realloc(parent->children, parent->nchildren * sizeof(void*));
|
|
parent->children[parent->nchildren-1] = child;
|
|
child->parent = parent;
|
|
}
|
|
|
|
struct iso_tree_node_dir*
|
|
iso_tree_new_root()
|
|
{
|
|
struct iso_tree_node_dir *root;
|
|
|
|
root = calloc(1, sizeof(struct iso_tree_node_dir));
|
|
|
|
set_default_stat(&root->node.attrib);
|
|
root->node.attrib.st_mode = S_IFDIR | 0777;
|
|
return root;
|
|
}
|
|
|
|
struct iso_tree_node*
|
|
iso_tree_add_file(struct iso_tree_node_dir *parent, const char *path)
|
|
{
|
|
struct iso_tree_node_file *f;
|
|
char *p;
|
|
struct stat st;
|
|
|
|
assert( parent && path);
|
|
|
|
if (lstat(path, &st) == -1) {
|
|
libisofs_errno = NO_FILE;
|
|
return NULL;
|
|
}
|
|
|
|
if ( !S_ISREG(st.st_mode) ) {
|
|
libisofs_errno = UNEXPECTED_FILE_TYPE;
|
|
return NULL;
|
|
}
|
|
|
|
if ( access(path, R_OK) ) {
|
|
libisofs_errno = NO_READ_ACCESS;
|
|
return NULL;
|
|
}
|
|
|
|
f = calloc(1, sizeof(struct iso_tree_node_file));
|
|
|
|
/* fill fields */
|
|
f->node.attrib = st;
|
|
f->path = strdup(path);
|
|
|
|
p = strdup(path); /* because basename() might modify its arg */
|
|
f->node.name = strdup( basename(p) );
|
|
free(p);
|
|
|
|
/* add to parent (this also sets f->node->parent) */
|
|
append_node(parent, (struct iso_tree_node*) f);
|
|
|
|
return (struct iso_tree_node*) f;
|
|
}
|
|
|
|
struct iso_tree_node*
|
|
iso_tree_add_symlink(struct iso_tree_node_dir *parent,
|
|
const char *name, const char *dest)
|
|
{
|
|
struct iso_tree_node_symlink *link;
|
|
|
|
assert( parent && name && dest);
|
|
|
|
link = calloc(1, sizeof(struct iso_tree_node_symlink));
|
|
|
|
/* fill fields */
|
|
set_default_stat(&link->node.attrib);
|
|
link->node.attrib.st_mode |= S_IFLNK;
|
|
link->node.name = strdup(name);
|
|
link->dest = strdup(dest);
|
|
|
|
/* add to parent (this also sets link->node->parent) */
|
|
append_node(parent, (struct iso_tree_node*) link);
|
|
|
|
return (struct iso_tree_node*) link;
|
|
}
|
|
|
|
struct iso_tree_node_dir*
|
|
iso_tree_add_dir(struct iso_tree_node_dir *parent,
|
|
const char *name)
|
|
{
|
|
struct iso_tree_node_dir *dir;
|
|
|
|
assert( parent && name );
|
|
|
|
dir = calloc(1, sizeof(struct iso_tree_node_dir));
|
|
|
|
dir->node.attrib = parent->node.attrib;
|
|
dir->node.name = strdup(name);
|
|
|
|
append_node(parent, (struct iso_tree_node*) dir);
|
|
return dir;
|
|
}
|
|
|
|
void
|
|
iso_tree_node_set_name(struct iso_tree_node *node, const char *name)
|
|
{
|
|
free(node->name);
|
|
node->name = strdup(name);
|
|
}
|
|
|
|
void
|
|
iso_tree_node_set_hidden(struct iso_tree_node *node, int hide_attrs)
|
|
{
|
|
node->hide_flags = hide_attrs;
|
|
}
|
|
|
|
struct iso_tree_node*
|
|
iso_tree_add_node(struct iso_tree_node_dir *parent,
|
|
const char *path)
|
|
{
|
|
struct stat st;
|
|
struct iso_tree_node *node;
|
|
|
|
assert( parent && path);
|
|
|
|
if (lstat(path, &st) == -1) {
|
|
libisofs_errno = NO_FILE;
|
|
return NULL;
|
|
}
|
|
|
|
if ( access(path, R_OK) ) {
|
|
libisofs_errno = NO_READ_ACCESS;
|
|
return NULL;
|
|
}
|
|
|
|
switch (st.st_mode & S_IFMT) {
|
|
case S_IFREG:
|
|
/* regular file */
|
|
node = iso_tree_add_file(parent, path);
|
|
break;
|
|
case S_IFLNK:
|
|
/* symlink */
|
|
{
|
|
char dest[PATH_MAX];
|
|
char *p;
|
|
int n;
|
|
|
|
n = readlink(path, dest, PATH_MAX);
|
|
if ( n == -1 ) {
|
|
libisofs_errno = INTERNAL_ERROR;
|
|
return NULL;
|
|
}
|
|
dest[n] = '\0';
|
|
p = strdup(path); /* because basename() might modify its arg */
|
|
node = iso_tree_add_symlink(parent, basename(p), dest);
|
|
free(p);
|
|
node->attrib = st;
|
|
}
|
|
break;
|
|
case S_IFDIR:
|
|
/* directory */
|
|
{
|
|
char *p;
|
|
p = strdup(path); /* because basename() might modify its arg */
|
|
node = (struct iso_tree_node*) iso_tree_add_dir(parent, basename(p));
|
|
free(p);
|
|
node->attrib = st;
|
|
}
|
|
break;
|
|
default:
|
|
libisofs_errno = UNEXPECTED_FILE_TYPE;
|
|
node = NULL;
|
|
break;
|
|
}
|
|
return node;
|
|
}
|
|
|
|
void
|
|
iso_tree_free(struct iso_tree_node *root)
|
|
{
|
|
if ( ISO_ISDIR(root) ) {
|
|
size_t i;
|
|
struct iso_tree_node_dir *dir;
|
|
dir = (struct iso_tree_node_dir *) root;
|
|
for (i=0; i < dir->nchildren; i++) {
|
|
iso_tree_free(dir->children[i]);
|
|
}
|
|
free(dir->children);
|
|
} else if ( ISO_ISLNK(root) ) {
|
|
struct iso_tree_node_symlink *link;
|
|
link = (struct iso_tree_node_symlink *) root;
|
|
free(link->dest);
|
|
} else if ( ISO_ISREG(root) ) {
|
|
struct iso_tree_node_file *file;
|
|
file = (struct iso_tree_node_file *) root;
|
|
free(file->path);
|
|
}
|
|
free(root->name);
|
|
free(root);
|
|
}
|
|
|
|
static struct iso_tree_node*
|
|
iso_tree_radd_dir_aux(struct iso_tree_node_dir *parent, const char *path,
|
|
struct iso_tree_radd_dir_behavior *behavior,
|
|
struct iso_hash_table *excludes)
|
|
{
|
|
struct iso_tree_node *new;
|
|
DIR *dir;
|
|
struct dirent *ent;
|
|
|
|
new = iso_tree_add_node(parent, path);
|
|
if (!new || !ISO_ISDIR(new)) {
|
|
return new;
|
|
}
|
|
|
|
dir = opendir(path);
|
|
if (!dir) {
|
|
warn("couldn't open directory %s: %s\n", path, strerror(errno));
|
|
return new;
|
|
}
|
|
|
|
while ((ent = readdir(dir))) {
|
|
char child[strlen(ent->d_name) + strlen(path) + 2];
|
|
|
|
if (strcmp(ent->d_name, ".") == 0 ||
|
|
strcmp(ent->d_name, "..") == 0)
|
|
continue;
|
|
|
|
//check if path already finished in '/'
|
|
sprintf(child, "%s/%s", path, ent->d_name);
|
|
|
|
/* see if this child is excluded. */
|
|
if (iso_exclude_lookup(excludes, child))
|
|
continue;
|
|
|
|
if ( !iso_tree_radd_dir_aux( (struct iso_tree_node_dir *) new, child,
|
|
behavior, excludes) ) {
|
|
/* error */
|
|
behavior->error = 1;
|
|
if (behavior->stop_on_error)
|
|
break;
|
|
}
|
|
}
|
|
closedir(dir);
|
|
|
|
return new;
|
|
}
|
|
|
|
struct iso_tree_node_dir*
|
|
iso_tree_radd_dir(struct iso_tree_node_dir *parent, const char *path,
|
|
struct iso_tree_radd_dir_behavior *behavior)
|
|
{
|
|
struct iso_tree_node_dir *dir;
|
|
struct iso_hash_table table = { {0,}, 0};
|
|
|
|
assert ( parent && path );
|
|
|
|
behavior->error = 0;
|
|
|
|
/* initialize exclude hash_table */
|
|
if ( behavior->excludes ) {
|
|
char *exclude;
|
|
int i = 0;
|
|
while ( (exclude = behavior->excludes[i++]) ) {
|
|
iso_exclude_add_path(&table, exclude);
|
|
}
|
|
}
|
|
|
|
/* recurse into dir */
|
|
dir = (struct iso_tree_node_dir*) iso_tree_radd_dir_aux(
|
|
parent, path, behavior, &table);
|
|
|
|
/* clear hashtable */
|
|
iso_exclude_empty(&table);
|
|
|
|
return dir;
|
|
}
|
|
|
|
void
|
|
iso_tree_print(const struct iso_tree_node *root, int spaces)
|
|
{
|
|
char sp[spaces+1];
|
|
|
|
memset(sp, ' ', spaces);
|
|
sp[spaces] = '\0';
|
|
|
|
printf("%s%s\n", sp, root->name);
|
|
|
|
if ( ISO_ISDIR(root) ) {
|
|
size_t i;
|
|
struct iso_tree_node_dir *dir;
|
|
|
|
dir = (struct iso_tree_node_dir *) root;
|
|
for (i=0; i < dir->nchildren; i++) {
|
|
iso_tree_print(dir->children[i], spaces+2);
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
iso_tree_print_verbose(const struct iso_tree_node *root,
|
|
print_dir_callback dir,
|
|
print_file_callback file,
|
|
void *callback_data,
|
|
int spaces)
|
|
{
|
|
|
|
(S_ISDIR(root->attrib.st_mode) ? dir : file)
|
|
(root, callback_data, spaces);
|
|
|
|
if ( ISO_ISDIR(root) ) {
|
|
size_t i;
|
|
struct iso_tree_node_dir *dir_node;
|
|
|
|
dir_node = (struct iso_tree_node_dir *) root;
|
|
for (i=0; i < dir_node->nchildren; i++) {
|
|
iso_tree_print_verbose(dir_node->children[i], dir,
|
|
file, callback_data, spaces+2);
|
|
}
|
|
}
|
|
}
|