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
19 changes: 19 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 @@ -194,6 +198,21 @@ EXPORT int file_tree_walk(const char *dirname, unsigned int nfds,
&& de->d_name[2] == '\0'))) {
continue;
}

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

if(should_skip)
continue;

end = path + ds->length + 1;
p = de->d_name;
Expand Down
99 changes: 57 additions & 42 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,51 +253,55 @@ static void print_help()
"Usage: mktorrent [OPTIONS] <target directory or filename>\n\n"
"Options:\n"
#ifdef USE_LONG_OPTIONS
"-a, --announce=<url>[,<url>]* : specify the full announce URLs\n"
" 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"
"-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"
"-n, --name=<name> : set the name of the torrent\n"
" default is the basename of the target\n"
"-o, --output=<filename> : set the path and filename of the created file\n"
" default is <name>.torrent\n"
"-p, --private : set the private flag\n"
"-s, --source=<source> : add source string embedded in infohash\n"
"-a, --announce=<url>[,<url>]* : specify the full announce URLs\n"
" 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"
"-e, --exclude=<match>[,<match>]* : exclude files based on filename pattern\n"
zordtk marked this conversation as resolved.
Show resolved Hide resolved
" matching, see fnmatch man page\n"
zordtk marked this conversation as resolved.
Show resolved Hide resolved
"-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"
"-n, --name=<name> : set the name of the torrent\n"
" default is the basename of the target\n"
"-o, --output=<filename> : set the path and filename of the created file\n"
" default is <name>.torrent\n"
"-p, --private : set the private flag\n"
"-s, --source=<source> : add source string embedded in infohash\n"
#ifdef USE_PTHREADS
"-t, --threads=<n> : use <n> threads for calculating hashes\n"
" default is the number of CPU cores\n"
"-t, --threads=<n> : use <n> threads for calculating hashes\n"
" default is the number of CPU cores\n"
#endif
"-v, --verbose : be verbose\n"
"-w, --web-seed=<url>[,<url>]* : add web seed URLs\n"
" additional -w adds more URLs\n"
"-x, --cross-seed : ensure info hash is unique for easier cross-seeding\n"
"-v, --verbose : be verbose\n"
"-w, --web-seed=<url>[,<url>]* : add web seed URLs\n"
" additional -w adds more URLs\n"
"-x, --cross-seed : ensure info hash is unique for easier cross-seeding\n"
#else
"-a <url>[,<url>]* : specify the full announce URLs\n"
" additional -a adds backup trackers\n"
"-c <comment> : add a comment to the metainfo\n"
"-d : don't write the creation date\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"
" default is calculated from the total size\n"
"-n <name> : set the name of the torrent,\n"
" default is the basename of the target\n"
"-o <filename> : set the path and filename of the created file\n"
" default is <name>.torrent\n"
"-p : set the private flag\n"
"-s : add source string embedded in infohash\n"
"-a <url>[,<url>]* : specify the full announce URLs\n"
" additional -a adds backup trackers\n"
"-c <comment> : add a comment to the metainfo\n"
"-d : don't write the creation date\n"
"-e <match>[,<match>]* : exclude files based on filename pattern\n"
" matching, see fnmatch man page\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"
" default is calculated from the total size\n"
"-n <name> : set the name of the torrent,\n"
" default is the basename of the target\n"
"-o <filename> : set the path and filename of the created file\n"
" default is <name>.torrent\n"
"-p : set the private flag\n"
"-s : add source string embedded in infohash\n"
#ifdef USE_PTHREADS
"-t <n> : use <n> threads for calculating hashes\n"
" default is the number of CPU cores\n"
"-t <n> : use <n> threads for calculating hashes\n"
" default is the number of CPU cores\n"
#endif
"-v : be verbose\n"
"-w <url>[,<url>]* : add web seed URLs\n"
" additional -w adds more URLs\n"
"-x : ensure info hash is unique for easier cross-seeding\n"
"-v : be verbose\n"
"-w <url>[,<url>]* : add web seed URLs\n"
" additional -w adds more URLs\n"
"-x : ensure info hash is unique for easier cross-seeding\n"
#endif
"\nPlease send bug reports, patches, feature requests, praise and\n"
"general gossip about the program to: mktorrent@rudde.org\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,11 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
case 'd':
m->no_creation_date = 1;
break;
case 'e':
FATAL_IF0(
ll_append(m->exclude_list, get_slist(optarg), 0) == NULL,
"out of memory\n");
break;
case 'f':
m->force_overwrite = 1;
break;
Expand Down Expand Up @@ -604,5 +617,7 @@ EXPORT void cleanup_metafile(struct metafile *m)

ll_free(m->web_seed_list, NULL);

ll_free(m->exclude_list, free_inner_list);

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