diff --git a/libisofs/image.h b/libisofs/image.h index a0b79d8..b86aca1 100644 --- a/libisofs/image.h +++ b/libisofs/image.h @@ -77,8 +77,7 @@ struct Iso_Image int ignore_special; /** - * Files to exclude - * TODO add wildcard support + * Files to exclude. Wildcard support is included. */ char** excludes; int nexcludes; diff --git a/libisofs/libisofs.h b/libisofs/libisofs.h index 72e0bea..04b57b2 100644 --- a/libisofs/libisofs.h +++ b/libisofs/libisofs.h @@ -1820,10 +1820,44 @@ int iso_tree_get_ignore_special(IsoImage *image); * * For example, in * - * iso_tree_add_exclude(image, "/home/user/data/private"); - * iso_tree_add_dir_rec(image, root, "/home/user/data"); + * 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. + * the directory /home/user/data/private won't be added to image. + * + * However, if you explicity add a deeper dir, it won't be excluded. i.e., + * in the following example. + * + * iso_tree_add_exclude(image, "/home/user/data"); + * iso_tree_add_dir_rec(image, root, "/home/user/data/private"); + * + * the directory /home/user/data/private is added. On the other, side, and + * foollowing the the example above, + * + * iso_tree_add_dir_rec(image, root, "/home/user"); + * + * will exclude the directory "/home/user/data". + * + * Absolute paths are not mandatory, you can, for example, add a relative + * path such as: + * + * iso_tree_add_exclude(image, "private"); + * iso_tree_add_exclude(image, "user/data"); + * + * to excluve, respectively, all files or dirs named private, and also all + * files or dirs named data that belong to a folder named "user". Not that the + * above rule about deeper dirs is still valid. i.e., if you call + * + * iso_tree_add_dir_rec(image, root, "/home/user/data/music"); + * + * it is included even containing "user/data" string. However, a possible + * "/home/user/data/music/user/data" is not added. + * + * Usual wildcards, such as * or ? are also supported, with the usual meaning + * as stated in "man 7 glob". For example + * + * // to exclude backup text files + * iso_tree_add_exclude(image, "*.~"); * * @return * 1 on success, < 0 on error diff --git a/libisofs/tree.c b/libisofs/tree.c index f8c4cc7..aba8683 100644 --- a/libisofs/tree.c +++ b/libisofs/tree.c @@ -24,6 +24,7 @@ #include #include #include +#include /** * Add a new directory to the iso tree. @@ -479,9 +480,24 @@ static int check_excludes(IsoImage *image, const char *path) { int i; + for (i = 0; i < image->nexcludes; ++i) { - if (strcmp(image->excludes[i], path) == 0) { - return 1; + char *exclude = image->excludes[i]; + if (exclude[0] == '/') { + /* absolute exclude, must completely match path */ + if (!fnmatch(exclude, path, FNM_PERIOD|FNM_FILE_NAME)) { + return 1; + } + } else { + /* relative exclude, it is enought if a part of the path matches */ + char *pos = (char*)path; + while (pos != NULL) { + pos++; + if (!fnmatch(exclude, pos, FNM_PERIOD|FNM_FILE_NAME)) { + return 1; + } + pos = strchr(pos, '/'); + } } } return 0;