2007-12-04 21:33:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Vreixo Formoso
|
|
|
|
*
|
|
|
|
* This file is part of the libisofs project; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation. See COPYING file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "builder.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "fsource.h"
|
2007-12-11 21:47:04 +00:00
|
|
|
#include "image.h"
|
2007-12-04 21:33:40 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-12-09 17:47:29 +00:00
|
|
|
#include <limits.h>
|
2007-12-04 21:33:40 +00:00
|
|
|
|
|
|
|
void iso_node_builder_ref(IsoNodeBuilder *builder)
|
|
|
|
{
|
|
|
|
++builder->refcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void iso_node_builder_unref(IsoNodeBuilder *builder)
|
|
|
|
{
|
|
|
|
if (--builder->refcount == 0) {
|
|
|
|
/* free private data */
|
|
|
|
builder->free(builder);
|
|
|
|
free(builder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2007-12-28 21:10:17 +00:00
|
|
|
int default_create_file(IsoNodeBuilder *builder, IsoImage *image,
|
2007-12-09 17:47:29 +00:00
|
|
|
IsoFileSource *src, IsoFile **file)
|
2007-12-04 21:33:40 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct stat info;
|
|
|
|
IsoStream *stream;
|
|
|
|
IsoFile *node;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-04 21:33:40 +00:00
|
|
|
if (builder == NULL || src == NULL || file == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 19:47:39 +00:00
|
|
|
res = iso_file_source_stat(src, &info);
|
2007-12-04 21:33:40 +00:00
|
|
|
if (res < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-04 21:33:40 +00:00
|
|
|
/* this will fail if src is a dir, is not accessible... */
|
|
|
|
res = iso_file_source_stream_new(src, &stream);
|
|
|
|
if (res < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-04 21:33:40 +00:00
|
|
|
node = malloc(sizeof(IsoFile));
|
|
|
|
if (node == NULL) {
|
|
|
|
/* the stream has taken our ref to src, so we need to add one */
|
|
|
|
iso_file_source_ref(src);
|
|
|
|
iso_stream_unref(stream);
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-04 21:33:40 +00:00
|
|
|
/* fill node fields */
|
|
|
|
node->node.refcount = 1;
|
|
|
|
node->node.type = LIBISO_FILE;
|
2007-12-28 21:45:56 +00:00
|
|
|
node->node.name = iso_file_source_get_name(src);
|
2007-12-04 21:33:40 +00:00
|
|
|
node->node.mode = S_IFREG | (info.st_mode & ~S_IFMT);
|
|
|
|
node->node.uid = info.st_uid;
|
|
|
|
node->node.gid = info.st_gid;
|
|
|
|
node->node.atime = info.st_atime;
|
|
|
|
node->node.ctime = info.st_ctime;
|
|
|
|
node->node.mtime = info.st_mtime;
|
|
|
|
node->node.hidden = 0;
|
|
|
|
node->node.parent = NULL;
|
|
|
|
node->node.next = NULL;
|
|
|
|
node->sort_weight = 0;
|
|
|
|
node->stream = stream;
|
|
|
|
node->msblock = 0;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-04 21:33:40 +00:00
|
|
|
*file = node;
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-12-09 17:47:29 +00:00
|
|
|
static
|
2007-12-28 21:10:17 +00:00
|
|
|
int default_create_node(IsoNodeBuilder *builder, IsoImage *image,
|
2007-12-09 17:47:29 +00:00
|
|
|
IsoFileSource *src, IsoNode **node)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct stat info;
|
|
|
|
IsoNode *new;
|
|
|
|
char *name;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-09 17:47:29 +00:00
|
|
|
if (builder == NULL || src == NULL || node == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 19:47:39 +00:00
|
|
|
name = iso_file_source_get_name(src);
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-09 17:47:29 +00:00
|
|
|
/* get info about source */
|
2007-12-11 21:47:04 +00:00
|
|
|
if (image->recOpts->follow_symlinks) {
|
2007-12-20 19:47:39 +00:00
|
|
|
result = iso_file_source_stat(src, &info);
|
2007-12-11 21:47:04 +00:00
|
|
|
} else {
|
2007-12-20 19:47:39 +00:00
|
|
|
result = iso_file_source_lstat(src, &info);
|
2007-12-11 21:47:04 +00:00
|
|
|
}
|
2007-12-09 17:47:29 +00:00
|
|
|
if (result < 0) {
|
|
|
|
return result;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-09 17:47:29 +00:00
|
|
|
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) {
|
2007-12-28 21:45:56 +00:00
|
|
|
free(name);
|
2007-12-09 17:47:29 +00:00
|
|
|
return result;
|
|
|
|
}
|
2007-12-11 21:47:04 +00:00
|
|
|
/* take a ref to the src, as stream has taken our ref */
|
|
|
|
iso_file_source_ref(src);
|
2007-12-09 17:47:29 +00:00
|
|
|
file = calloc(1, sizeof(IsoFile));
|
|
|
|
if (file == NULL) {
|
2007-12-28 21:45:56 +00:00
|
|
|
free(name);
|
2007-12-09 17:47:29 +00:00
|
|
|
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) {
|
2007-12-28 21:45:56 +00:00
|
|
|
free(name);
|
2007-12-09 17:47:29 +00:00
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
new->type = LIBISO_DIR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_IFLNK:
|
|
|
|
{
|
|
|
|
/* source is a symbolic link */
|
|
|
|
char dest[PATH_MAX];
|
|
|
|
IsoSymlink *link;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 19:47:39 +00:00
|
|
|
result = iso_file_source_readlink(src, dest, PATH_MAX);
|
2007-12-09 17:47:29 +00:00
|
|
|
if (result < 0) {
|
2007-12-28 21:45:56 +00:00
|
|
|
free(name);
|
2007-12-09 17:47:29 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
link = malloc(sizeof(IsoSymlink));
|
|
|
|
if (link == NULL) {
|
2007-12-28 21:45:56 +00:00
|
|
|
free(name);
|
2007-12-09 17:47:29 +00:00
|
|
|
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) {
|
2007-12-28 21:45:56 +00:00
|
|
|
free(name);
|
2007-12-09 17:47:29 +00:00
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
special->dev = info.st_rdev;
|
|
|
|
special->node.type = LIBISO_SPECIAL;
|
|
|
|
new = (IsoNode*) special;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-09 17:47:29 +00:00
|
|
|
/* fill fields */
|
|
|
|
new->refcount = 1;
|
2007-12-28 21:45:56 +00:00
|
|
|
new->name = name;
|
2007-12-09 17:47:29 +00:00
|
|
|
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;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-09 17:47:29 +00:00
|
|
|
new->hidden = 0;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-09 17:47:29 +00:00
|
|
|
new->parent = NULL;
|
|
|
|
new->next = NULL;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-09 17:47:29 +00:00
|
|
|
*node = new;
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2007-12-04 21:33:40 +00:00
|
|
|
void default_free(IsoNodeBuilder *builder)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int iso_node_basic_builder_new(IsoNodeBuilder **builder)
|
|
|
|
{
|
|
|
|
IsoNodeBuilder *b;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-04 21:33:40 +00:00
|
|
|
if (builder == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-04 21:33:40 +00:00
|
|
|
b = malloc(sizeof(IsoNodeBuilder));
|
|
|
|
if (b == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-04 21:33:40 +00:00
|
|
|
b->refcount = 1;
|
2007-12-09 17:47:29 +00:00
|
|
|
b->create_file_data = NULL;
|
|
|
|
b->create_node_data = NULL;
|
2007-12-04 21:33:40 +00:00
|
|
|
b->create_file = default_create_file;
|
2007-12-09 17:47:29 +00:00
|
|
|
b->create_node = default_create_node;
|
2007-12-04 21:33:40 +00:00
|
|
|
b->free = default_free;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-04 21:33:40 +00:00
|
|
|
*builder = b;
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|