Add wildcard support to excludes.

This commit is contained in:
Vreixo Formoso 2008-01-30 21:40:26 +01:00
parent 04078d12ef
commit b6362a8680
3 changed files with 56 additions and 7 deletions

View File

@ -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;

View File

@ -1825,6 +1825,40 @@ int iso_tree_get_ignore_special(IsoImage *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
*/

View File

@ -24,6 +24,7 @@
#include <time.h>
#include <limits.h>
#include <stdio.h>
#include <fnmatch.h>
/**
* Add a new directory to the iso tree.
@ -479,10 +480,25 @@ 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) {
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;
}