Add function to add an exiting file to the iso tree.

Node is created using a builder. Default builder and filesystem is added 
to IsoImage. Later we should expose setters for this atts.
This commit is contained in:
Vreixo Formoso
2007-12-07 03:02:46 +01:00
parent c93fa154d5
commit 45f4d0a29e
5 changed files with 221 additions and 7 deletions

View File

@ -13,10 +13,14 @@
#include "libisofs.h"
#include "node.h"
#include "error.h"
#include "image.h"
#include "fsource.h"
#include "builder.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
/**
* Add a new directory to the iso tree.
@ -289,3 +293,151 @@ int iso_tree_add_new_special(IsoDir *parent, const char *name, mode_t mode,
}
return ++parent->nchildren;
}
static
int iso_tree_add_node_builder(IsoDir *parent, IsoFileSource *src,
IsoNodeBuilder *builder, IsoNode **node)
{
int result;
struct stat info;
IsoNode *new;
IsoNode **pos;
char *name;
if (parent == NULL || src == NULL || builder == NULL) {
return ISO_NULL_POINTER;
}
if (node) {
*node = NULL;
}
name = src->get_name(src);
/* find place where to insert */
pos = &(parent->children);
while (*pos != NULL && strcmp((*pos)->name, name) < 0) {
pos = &((*pos)->next);
}
if (*pos != NULL && !strcmp((*pos)->name, name)) {
/* a node with same name already exists */
return ISO_NODE_NAME_NOT_UNIQUE;
}
/* get info about source */
result = src->lstat(src, &info);
if (result < 0) {
return result;
}
new = NULL;
switch (info.st_mode & S_IFMT) {
case S_IFREG:
{
/* source is a regular file */
IsoStream *stream;
IsoFile *file;
result = iso_file_source_stream_new(src, &stream);
if (result < 0) {
return result;
}
file = malloc(sizeof(IsoFile));
if (file == NULL) {
iso_stream_unref(stream);
return ISO_MEM_ERROR;
}
file->msblock = 0;
file->sort_weight = 0;
file->stream = stream;
file->node.type = LIBISO_FILE;
new = (IsoNode*) file;
}
break;
case S_IFDIR:
{
/* source is a directory */
new = calloc(1, sizeof(IsoDir));
if (new == NULL) {
return ISO_MEM_ERROR;
}
new->type = LIBISO_DIR;
}
break;
case S_IFLNK:
{
/* source is a symbolic link */
char dest[PATH_MAX];
IsoSymlink *link;
result = src->readlink(src, dest, PATH_MAX);
if (result < 0) {
return result;
}
link = malloc(sizeof(IsoSymlink));
if (link == NULL) {
return ISO_MEM_ERROR;
}
link->dest = strdup(dest);
link->node.type = LIBISO_SYMLINK;
new = (IsoNode*) link;
}
break;
case S_IFSOCK:
case S_IFBLK:
case S_IFCHR:
case S_IFIFO:
{
/* source is an special file */
IsoSpecial *special;
special = malloc(sizeof(IsoSpecial));
if (special == NULL) {
return ISO_MEM_ERROR;
}
special->dev = info.st_rdev;
special->node.type = LIBISO_SPECIAL;
new = (IsoNode*) special;
}
break;
}
/* fill fields */
new->refcount = 1;
new->name = strdup(name);
new->mode = info.st_mode;
new->uid = info.st_uid;
new->gid = info.st_gid;
new->atime = info.st_atime;
new->mtime = info.st_mtime;
new->ctime = info.st_ctime;
new->hidden = 0;
/* finally, add node to parent */
new->parent = parent;
new->next = *pos;
*pos = new;
if (node) {
*node = new;
}
return ++parent->nchildren;
}
int iso_tree_add_node(IsoImage *image, IsoDir *parent, const char *path,
IsoNode **node)
{
int result;
IsoFilesystem *fs;
IsoFileSource *file;
if (image == NULL || parent == NULL || path == NULL) {
return ISO_NULL_POINTER;
}
fs = image->fs;
result = fs->get_by_path(fs, path, &file);
if (result < 0) {
return result;
}
result = iso_tree_add_node_builder(parent, file, image->builder, node);
return result;
}