Skip to content

Commit

Permalink
Add option to overwrite existing output file
Browse files Browse the repository at this point in the history
  • Loading branch information
rmsacks committed Jul 14, 2020
1 parent 343f634 commit dedc79f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
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

0 comments on commit dedc79f

Please sign in to comment.