From 14f4aa7d07454bfa6979aab9c6032696e2367e2f Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Tue, 4 Dec 2007 22:33:40 +0100 Subject: [PATCH] Init builder implementation. --- Makefile.am | 1 + src/builder.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ src/builder.h | 30 ++++++++++++-- src/libisofs.h | 11 +++++ src/node.h | 17 +++----- 5 files changed, 154 insertions(+), 14 deletions(-) create mode 100644 src/builder.c diff --git a/Makefile.am b/Makefile.am index 98d55a5..37925fd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -11,6 +11,7 @@ src_libisofs_la_LDFLAGS = \ -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) src_libisofs_la_SOURCES = \ src/builder.h \ + src/builder.c \ src/error.h \ src/node.h \ src/node.c \ diff --git a/src/builder.c b/src/builder.c new file mode 100644 index 0000000..1de35a1 --- /dev/null +++ b/src/builder.c @@ -0,0 +1,109 @@ +/* + * 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" + +#include +#include + +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 +int default_create_file(IsoNodeBuilder *builder, IsoFileSource *src, + IsoFile **file) +{ + int res; + struct stat info; + IsoStream *stream; + IsoFile *node; + + if (builder == NULL || src == NULL || file == NULL) { + return ISO_NULL_POINTER; + } + + res = src->stat(src, &info); + if (res < 0) { + return res; + } + + /* this will fail if src is a dir, is not accessible... */ + res = iso_file_source_stream_new(src, &stream); + if (res < 0) { + return res; + } + + 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; + } + + /* fill node fields */ + node->node.refcount = 1; + node->node.type = LIBISO_FILE; + node->node.name = strdup(src->get_name(src)); + 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; + + *file = node; + return ISO_SUCCESS; +} + +void default_free(IsoNodeBuilder *builder) +{ + return; +} + +int iso_node_basic_builder_new(IsoNodeBuilder **builder) +{ + IsoNodeBuilder *b; + + if (builder == NULL) { + return ISO_NULL_POINTER; + } + + b = malloc(sizeof(IsoNodeBuilder)); + if (b == NULL) { + return ISO_MEM_ERROR; + } + + b->refcount = 1; + b->data = NULL; + b->create_file = default_create_file; + b->free = default_free; + + *builder = b; + return ISO_SUCCESS; +} diff --git a/src/builder.h b/src/builder.h index c3bf15f..edd3d89 100644 --- a/src/builder.h +++ b/src/builder.h @@ -18,7 +18,8 @@ * Builder. */ -#include "node.h" +#include "libisofs.h" +#include "fsource.h" typedef struct Iso_Node_Builder IsoNodeBuilder; @@ -26,18 +27,41 @@ struct Iso_Node_Builder { /** + * Create a new IsoFile from an IsoFileSource. Name, permissions + * and other attributes are taken from src, but a regular file will + * always be created, even if src is another kind of file. + * + * In that case, if the implementation can't do the conversion, it + * should fail propertly. + * + * On sucess, the ref. to src will be owned by file, so you musn't + * unref it. * * @return * 1 on success, < 0 on error */ - int (*create_file)(const IsoFileSource *src, IsoFile **file); - + int (*create_file)(IsoNodeBuilder *builder, IsoFileSource *src, + IsoFile **file); + /** + * Free implementation specific data. Should never be called by user. + * Use iso_node_builder_unref() instead. + */ + void (*free)(IsoNodeBuilder *builder); int refcount; void *data; }; +void iso_node_builder_ref(IsoNodeBuilder *builder); +void iso_node_builder_unref(IsoNodeBuilder *builder); +/** + * Create a new basic builder ... + * + * @return + * 1 success, < 0 error + */ +int iso_node_basic_builder_new(IsoNodeBuilder **builder); #endif /*LIBISO_BUILDER_H_*/ diff --git a/src/libisofs.h b/src/libisofs.h index 9eb8f8f..a9b1acb 100644 --- a/src/libisofs.h +++ b/src/libisofs.h @@ -19,6 +19,17 @@ typedef struct Iso_File IsoFile; typedef struct Iso_Dir_Iter IsoDirIter; +/** + * The type of an IsoNode. + */ +enum IsoNodeType { + LIBISO_DIR, + LIBISO_FILE, + LIBISO_SYMLINK, + LIBISO_SPECIAL, + LIBISO_BOOT +}; + /** * Create a new image, empty. * diff --git a/src/node.h b/src/node.h index 6ec7373..9389004 100644 --- a/src/node.h +++ b/src/node.h @@ -18,17 +18,7 @@ #include #include #include - -/** - * The type of an IsoNode. - */ -enum IsoNodeType { - LIBISO_DIR, - LIBISO_FILE, - LIBISO_SYMLINK, - LIBISO_SPECIAL, - LIBISO_BOOT -}; +#include /** * Flag used to hide a file in the RR/ISO or Joliet tree. @@ -89,6 +79,11 @@ struct Iso_Dir struct Iso_File { IsoNode node; + + /** + * Location of a file extent in a ms disc, 0 for newly added file + */ + uint32_t msblock; /** * It sorts the order in which the file data is written to the CD image.