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 option to overwrite existing output file #49

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ 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"
"-h, --help : show this help screen\n"
"-l, --piece-length=<n> : set the piece length to 2^n bytes,\n"
" default is 18, that is 2^18 = 256kb\n"
Expand All @@ -278,6 +279,7 @@ 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"
"-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 18, that is 2^18 = 256kb\n"
Expand Down Expand Up @@ -413,6 +415,7 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
{"announce", 1, NULL, 'a'},
{"comment", 1, NULL, 'c'},
{"no-date", 0, NULL, 'd'},
{"force", 0, NULL, 'f'},
{"help", 0, NULL, 'h'},
{"piece-length", 1, NULL, 'l'},
{"name", 1, NULL, 'n'},
Expand All @@ -439,9 +442,9 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])

/* now parse the command line options given */
#ifdef USE_PTHREADS
#define OPT_STRING "a:c:dhl:n:o:ps:t:vw:"
#define OPT_STRING "a:c:dfhl:n:o:ps:t:vw:"
#else
#define OPT_STRING "a:c:dhl:n:o:ps:vw:"
#define OPT_STRING "a:c:dfhl:n:o:ps:vw:"
#endif
#ifdef USE_LONG_OPTIONS
while ((c = getopt_long(argc, argv, OPT_STRING,
Expand All @@ -462,6 +465,9 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
case 'd':
m->no_creation_date = 1;
break;
case 'f':
m->force_overwrite = 1;
break;
case 'h':
print_help();
exit(EXIT_SUCCESS);
Expand Down
13 changes: 9 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
/*
* create and open the metainfo file for writing and create a stream for it
* we don't want to overwrite anything, so abort if the file is already there
* and force is false
*/
static FILE *open_file(const char *path)
static FILE *open_file(const char *path, int force)
{
int fd; /* file descriptor */
FILE *f; /* file stream */

int flags = O_WRONLY | O_BINARY | O_CREAT;
if (!force)
flags |= O_EXCL;

/* open and create the file if it doesn't exist already */
fd = open(path, O_WRONLY | O_BINARY | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
fd = open(path, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
FATAL_IF(fd < 0, "cannot create '%s': %s\n", path, strerror(errno));

/* create the stream from this filedescriptor */
Expand Down Expand Up @@ -118,6 +122,7 @@ int main(int argc, char *argv[])
0, /* private */
NULL, /* source string */
0, /* verbose */
0, /* force_overwrite */
#ifdef USE_PTHREADS
0, /* threads, initialised by init() */
#endif
Expand All @@ -136,7 +141,7 @@ int main(int argc, char *argv[])

/* open the file stream now, so we don't have to abort
_after_ we did all the hashing in case we fail */
file = open_file(m.metainfo_file_path);
file = open_file(m.metainfo_file_path, m.force_overwrite);

/* calculate hash string... */
unsigned char *hash = make_hash(&m);
Expand Down
1 change: 1 addition & 0 deletions mktorrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct metafile {
int private; /* set the private flag */
char *source; /* set source for private trackers */
int verbose; /* be verbose */
int force_overwrite; /* overwrite existing output file */
#ifdef USE_PTHREADS
long threads; /* number of threads used for hashing */
#endif
Expand Down