From ff0dd38e9f139329c14d83966a88645941d913c5 Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Tue, 29 Jan 2008 20:43:59 +0100 Subject: [PATCH] Add support for excluding arbitrary files when adding a dir recursively. --- libisofs/image.c | 8 +++++ libisofs/image.h | 1 + libisofs/libisofs.h | 25 ++++++++++++++++ libisofs/tree.c | 73 ++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 99 insertions(+), 8 deletions(-) diff --git a/libisofs/image.c b/libisofs/image.c index 0f85290..569393b 100644 --- a/libisofs/image.c +++ b/libisofs/image.c @@ -92,11 +92,19 @@ void iso_image_ref(IsoImage *image) void iso_image_unref(IsoImage *image) { if (--image->refcount == 0) { + int nexcl; + /* we need to free the image */ if (image->user_data != NULL) { /* free attached data */ image->user_data_free(image->user_data); } + + for (nexcl = 0; nexcl < image->nexcludes; ++nexcl) { + free(image->excludes[nexcl]); + } + free(image->excludes); + iso_node_unref((IsoNode*)image->root); iso_node_builder_unref(image->builder); iso_filesystem_unref(image->fs); diff --git a/libisofs/image.h b/libisofs/image.h index aae7bc2..a0b79d8 100644 --- a/libisofs/image.h +++ b/libisofs/image.h @@ -81,6 +81,7 @@ struct Iso_Image * TODO add wildcard support */ char** excludes; + int nexcludes; /** * if the dir already contains a node with the same name, whether to diff --git a/libisofs/libisofs.h b/libisofs/libisofs.h index fe39117..b4e95e2 100644 --- a/libisofs/libisofs.h +++ b/libisofs/libisofs.h @@ -1784,6 +1784,31 @@ void iso_tree_set_ignore_special(IsoImage *image, int skip); */ int iso_tree_get_ignore_special(IsoImage *image); +/** + * Add a excluded path. These are paths that won't never added to image, + * and will be excluded even when adding recursively its parent directory. + * + * For example, in + * + * iso_tree_add_exclude(image, "/home/user/data/private"); + * iso_tree_add_dir_rec(image, root, "/home/user/data"); + * + * the directory /home/user/data/private won't be added to image. + * + * @return + * 1 on success, < 0 on error + */ +int iso_tree_add_exclude(IsoImage *image, const char *path); + +/** + * Remove a previously added exclude. + * + * @see iso_tree_add_exclude + * @return + * 1 on success, 0 exclude do not exists, < 0 on error + */ +int iso_tree_remove_exclude(IsoImage *image, const char *path); + /** * Set a callback function that libisofs will call for each file that is * added to the given image by a recursive addition function. This includes diff --git a/libisofs/tree.c b/libisofs/tree.c index 09853b8..f8c4cc7 100644 --- a/libisofs/tree.c +++ b/libisofs/tree.c @@ -350,6 +350,68 @@ void iso_tree_set_report_callback(IsoImage *image, image->report = report; } +/** + * Add a excluded path. These are paths that won't never added to image, + * and will be excluded even when adding recursively its parent directory. + * + * For example, in + * + * iso_tree_add_exclude(image, "/home/user/data/private"); + * iso_tree_add_dir_rec(image, root, "/home/user/data"); + * + * the directory /home/user/data/private won't be added to image. + * + * @return + * 1 on success, < 0 on error + */ +int iso_tree_add_exclude(IsoImage *image, const char *path) +{ + if (image == NULL || path == NULL) { + return ISO_NULL_POINTER; + } + image->excludes = realloc(image->excludes, ++image->nexcludes * + sizeof(void*)); + if (image->excludes == NULL) { + return ISO_OUT_OF_MEM; + } + image->excludes[image->nexcludes - 1] = strdup(path); + if (image->excludes[image->nexcludes - 1] == NULL) { + return ISO_OUT_OF_MEM; + } + return ISO_SUCCESS; +} + +/** + * Remove a previously added exclude. + * + * @see iso_tree_add_exclude + * @return + * 1 on success, 0 exclude do not exists, < 0 on error + */ +int iso_tree_remove_exclude(IsoImage *image, const char *path) +{ + size_t i, j; + + if (image == NULL || path == NULL) { + return ISO_NULL_POINTER; + } + + for (i = 0; i < image->nexcludes; ++i) { + if (strcmp(image->excludes[i], path) == 0) { + /* exclude found */ + free(image->excludes[i]); + --image->nexcludes; + for (j = i; j < image->nexcludes; ++j) { + image->excludes[j] = image->excludes[j+1]; + } + image->excludes = realloc(image->excludes, image->nexcludes * + sizeof(void*)); + return ISO_SUCCESS; + } + } + return 0; +} + static int iso_tree_add_node_builder(IsoImage *image, IsoDir *parent, IsoFileSource *src, IsoNodeBuilder *builder, @@ -416,16 +478,11 @@ int iso_tree_add_node(IsoImage *image, IsoDir *parent, const char *path, static int check_excludes(IsoImage *image, const char *path) { - char **exclude; - if (image->excludes == NULL) { - return 0; - } - exclude = image->excludes; - while (*exclude) { - if (strcmp(*exclude, path) == 0) { + int i; + for (i = 0; i < image->nexcludes; ++i) { + if (strcmp(image->excludes[i], path) == 0) { return 1; } - ++exclude; } return 0; }