Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exclude command line option. Excludes files based on a glob string #56

Merged
merged 13 commits into from
Jan 25, 2021
18 changes: 18 additions & 0 deletions ftw.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdbool.h>
#include <fnmatch.h>

#include "export.h"
#include "mktorrent.h" /* DIRSEP_CHAR */
#include "ftw.h"
#include "msg.h"
zordtk marked this conversation as resolved.
Show resolved Hide resolved


struct dir_state {
Expand Down Expand Up @@ -135,6 +138,7 @@ EXPORT int file_tree_walk(const char *dirname, unsigned int nfds,
struct dir_state *ds = dir_state_new(NULL, NULL);
struct dir_state *first_open;
unsigned int nopen;
struct metafile *m = data;

if (ds == NULL)
return -1;
Expand Down Expand Up @@ -195,6 +199,20 @@ EXPORT int file_tree_walk(const char *dirname, unsigned int nfds,
continue;
}

bool should_skip = false;
LL_FOR(exclude_node, m->exclude_list) {
const char *exclude_pattern = LL_DATA(exclude_node);
if (fnmatch(exclude_pattern, de->d_name, 0) != FNM_NOMATCH) {
if (m->verbose)
printf("skipping %s\n", de->d_name);
zordtk marked this conversation as resolved.
Show resolved Hide resolved
should_skip = true;
break;
}
}

if (should_skip)
continue;

end = path + ds->length + 1;
p = de->d_name;
while ((*end = *p)) {
Expand Down
19 changes: 16 additions & 3 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ static void print_help()
" additional -a adds backup trackers\n"
"-c, --comment=<comment> : add a comment to the metainfo\n"
"-d, --no-date : don't write the creation date\n"
"-f, --force : overwrite output file if it exists\n"
"-e, --exclude=<pat>[,<p>]* : exclude files whose name matches the pattern <p>\n"
zordtk marked this conversation as resolved.
Show resolved Hide resolved
" see the man page glob(7)\n"
"-f, --force : overwrite output file if it exists\n"
"-h, --help : show this help screen\n"
"-l, --piece-length=<n> : set the piece length to 2^n bytes,\n"
" default is calculated from the total size\n"
Expand All @@ -280,6 +282,8 @@ static void print_help()
" additional -a adds backup trackers\n"
"-c <comment> : add a comment to the metainfo\n"
"-d : don't write the creation date\n"
"-e <pat> : exclude files whose name matches the pattern <p>\n"
" see the man page glob(7)\n"
"-f : overwrite output file if it exists\n"
"-h : show this help screen\n"
"-l <n> : set the piece length to 2^n bytes,\n"
Expand Down Expand Up @@ -429,6 +433,7 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
{"announce", 1, NULL, 'a'},
{"comment", 1, NULL, 'c'},
{"no-date", 0, NULL, 'd'},
{"exclude", 1, NULL, 'e'},
{"force", 0, NULL, 'f'},
{"help", 0, NULL, 'h'},
{"piece-length", 1, NULL, 'l'},
Expand All @@ -455,11 +460,14 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
m->file_list = ll_new();
FATAL_IF0(m->file_list == NULL, "out of memory\n");

m->exclude_list = ll_new();
FATAL_IF0(m->exclude_list == NULL, "out of memory\n");

/* now parse the command line options given */
#ifdef USE_PTHREADS
#define OPT_STRING "a:c:dfhl:n:o:ps:t:vw:x"
#define OPT_STRING "a:c:e:dfhl:n:o:ps:t:vw:x"
#else
#define OPT_STRING "a:c:dfhl:n:o:ps:vw:x"
#define OPT_STRING "a:c:e:dfhl:n:o:ps:vw:x"
#endif
#ifdef USE_LONG_OPTIONS
while ((c = getopt_long(argc, argv, OPT_STRING,
Expand All @@ -480,6 +488,9 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
case 'd':
m->no_creation_date = 1;
break;
case 'e':
ll_extend(m->exclude_list, get_slist(optarg));
break;
case 'f':
m->force_overwrite = 1;
break;
Expand Down Expand Up @@ -604,5 +615,7 @@ EXPORT void cleanup_metafile(struct metafile *m)

ll_free(m->web_seed_list, NULL);

ll_free(m->exclude_list, NULL);

free(m->metainfo_file_path);
}
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ int main(int argc, char *argv[])
0, /* cross_seed */
0, /* verbose */
0, /* force_overwrite */
NULL, /* exclude_list */
#ifdef USE_PTHREADS
0, /* threads, initialised by init() */
#endif
Expand Down
1 change: 1 addition & 0 deletions mktorrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct metafile {
int cross_seed; /* ensure info hash is unique for easier cross-seeding */
int verbose; /* be verbose */
int force_overwrite; /* overwrite existing output file */
struct ll *exclude_list; /* exclude list */
#ifdef USE_PTHREADS
long threads; /* number of threads used for hashing */
#endif
Expand Down