From dbac2676d21af8f0a89135903e05a82d576012c2 Mon Sep 17 00:00:00 2001 From: uno20001 Date: Sun, 19 Apr 2020 19:49:24 +0200 Subject: [PATCH] make rest of code use FATAL_IF macros --- hash.c | 38 +++++++-------------- hash_pthreads.c | 79 +++++++++++------------------------------- init.c | 91 +++++++++++++------------------------------------ main.c | 22 ++++-------- 4 files changed, 62 insertions(+), 168 deletions(-) diff --git a/hash.c b/hash.c index 7967359..f4590b0 100644 --- a/hash.c +++ b/hash.c @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include "export.h" #include "mktorrent.h" #include "hash.h" +#include "msg.h" #ifndef O_BINARY #define O_BINARY 0 @@ -75,10 +76,7 @@ EXPORT unsigned char *make_hash(struct metafile *m) read_buf = malloc(m->piece_length); /* check if we've run out of memory */ - if (hash_string == NULL || read_buf == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(hash_string == NULL || read_buf == NULL, "Out of memory.\n"); /* initiate pos to point to the beginning of hash_string */ pos = hash_string; @@ -88,11 +86,8 @@ EXPORT unsigned char *make_hash(struct metafile *m) for (f = m->file_list; f; f = f->next) { /* open the current file for reading */ - if ((fd = open(f->path, OPENFLAGS)) == -1) { - fprintf(stderr, "Error opening '%s' for reading: %s\n", - f->path, strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF((fd = open(f->path, OPENFLAGS)) == -1, + "Error opening '%s' for reading: %s\n", f->path, strerror(errno)); printf("Hashing %s.\n", f->path); fflush(stdout); @@ -102,12 +97,8 @@ EXPORT unsigned char *make_hash(struct metafile *m) to the end of the file */ while (1) { ssize_t d = read(fd, read_buf + r, m->piece_length - r); - - if (d < 0) { - fprintf(stderr, "Error reading from '%s': %s\n", - f->path, strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF(d < 0, "Error reading from '%s': %s\n", + f->path, strerror(errno)); if (d == 0) /* end of file */ break; @@ -127,11 +118,8 @@ EXPORT unsigned char *make_hash(struct metafile *m) } /* now close the file */ - if (close(fd)) { - fprintf(stderr, "Error closing '%s': %s\n", - f->path, strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF(close(fd), "Error closing '%s': %s\n", + f->path, strerror(errno)); } /* finally append the hash of the last irregular piece to the hash string */ @@ -143,12 +131,10 @@ EXPORT unsigned char *make_hash(struct metafile *m) #ifndef NO_HASH_CHECK counter += r; - if (counter != m->size) { - fprintf(stderr, "Counted %" PRId64 " bytes, " - "but hashed %" PRId64 " bytes. " - "Something is wrong...\n", m->size, counter); - exit(EXIT_FAILURE); - } + FATAL_IF(counter != m->size, + "Counted %" PRId64 " bytes, but hashed %" PRId64 " bytes. " + "Something is wrong...\n", + m->size, counter); #endif /* free the read buffer before we return */ diff --git a/hash_pthreads.c b/hash_pthreads.c index 73e1b2f..73ca718 100644 --- a/hash_pthreads.c +++ b/hash_pthreads.c @@ -37,6 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include "export.h" #include "mktorrent.h" #include "hash.h" +#include "msg.h" #ifndef PROGRESS_PERIOD #define PROGRESS_PERIOD 200000 @@ -84,10 +85,8 @@ static struct piece *get_free(struct queue *q, size_t piece_length) q->free = r->next; } else if (q->buffers < q->buffers_max) { r = malloc(sizeof(struct piece) - 1 + piece_length); - if (r == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(r == NULL, "Out of memory.\n"); + q->buffers++; } else { @@ -172,11 +171,7 @@ static void *print_progress(void *data) int err; err = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); - if (err) { - fprintf(stderr, "Error setting thread cancel type: %s\n", - strerror(err)); - exit(EXIT_FAILURE); - } + FATAL_IF(err, "Error setting thread cancel type: %s\n", strerror(err)); while (1) { /* print progress and flush the buffer immediately */ @@ -221,20 +216,14 @@ static void read_files(struct metafile *m, struct queue *q, unsigned char *pos) for (f = m->file_list; f; f = f->next) { /* open the current file for reading */ - if ((fd = open(f->path, OPENFLAGS)) == -1) { - fprintf(stderr, "Error opening '%s' for reading: %s\n", - f->path, strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF((fd = open(f->path, OPENFLAGS)) == -1, + "Error opening '%s' for reading: %s\n", f->path, strerror(errno)); while (1) { ssize_t d = read(fd, p->data + r, m->piece_length - r); - if (d < 0) { - fprintf(stderr, "Error reading from '%s': %s\n", - f->path, strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF(d < 0, "Error reading from '%s': %s\n", + f->path, strerror(errno)); if (d == 0) /* end of file */ break; @@ -255,11 +244,8 @@ static void read_files(struct metafile *m, struct queue *q, unsigned char *pos) } /* now close the file */ - if (close(fd)) { - fprintf(stderr, "Error closing '%s': %s\n", - f->path, strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF(close(fd), "Error closing '%s': %s\n", + f->path, strerror(errno)); } /* finally append the hash of the last irregular piece to the hash string */ @@ -272,12 +258,10 @@ static void read_files(struct metafile *m, struct queue *q, unsigned char *pos) #ifndef NO_HASH_CHECK counter += r; - if (counter != m->size) { - fprintf(stderr, "Counted %" PRId64 " bytes, " - "but hashed %" PRId64 " bytes. " - "Something is wrong...\n", m->size, counter); - exit(EXIT_FAILURE); - } + FATAL_IF(counter != m->size, + "Counted %" PRId64 " bytes, but hashed %" PRId64 " bytes. " + "Something is wrong...\n", + m->size, counter); #endif } @@ -299,10 +283,7 @@ EXPORT unsigned char *make_hash(struct metafile *m) workers = malloc(m->threads * sizeof(pthread_t)); hash_string = malloc(m->pieces * SHA_DIGEST_LENGTH); - if (workers == NULL || hash_string == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(workers == NULL || hash_string == NULL, "Out of memory.\n"); q.pieces = m->pieces; q.buffers_max = 3*m->threads; @@ -310,31 +291,19 @@ EXPORT unsigned char *make_hash(struct metafile *m) /* create worker threads */ for (i = 0; i < m->threads; i++) { err = pthread_create(&workers[i], NULL, worker, &q); - if (err) { - fprintf(stderr, "Error creating thread: %s\n", - strerror(err)); - exit(EXIT_FAILURE); - } + FATAL_IF(err, "Error creating thread: %s\n", strerror(err)); } /* now set off the progress printer */ err = pthread_create(&print_progress_thread, NULL, print_progress, &q); - if (err) { - fprintf(stderr, "Error creating thread: %s\n", - strerror(err)); - exit(EXIT_FAILURE); - } + FATAL_IF(err, "Error creating thread: %s\n", strerror(err)); /* read files and feed pieces to the workers */ read_files(m, &q, hash_string); /* we're done so stop printing our progress. */ err = pthread_cancel(print_progress_thread); - if (err) { - fprintf(stderr, "Error cancelling thread: %s\n", - strerror(err)); - exit(EXIT_FAILURE); - } + FATAL_IF(err, "Error cancelling thread: %s\n", strerror(err)); /* inform workers we're done */ set_done(&q); @@ -342,22 +311,14 @@ EXPORT unsigned char *make_hash(struct metafile *m) /* wait for workers to finish */ for (i = 0; i < m->threads; i++) { err = pthread_join(workers[i], NULL); - if (err) { - fprintf(stderr, "Error joining thread: %s\n", - strerror(err)); - exit(EXIT_FAILURE); - } + FATAL_IF(err, "Error joining thread: %s\n", strerror(err)); } free(workers); /* the progress printer should be done by now too */ err = pthread_join(print_progress_thread, NULL); - if (err) { - fprintf(stderr, "Error joining thread: %s\n", - strerror(err)); - exit(EXIT_FAILURE); - } + FATAL_IF(err, "Error joining thread: %s\n", strerror(err)); /* destroy mutexes and condition variables */ pthread_mutex_destroy(&q.mutex_full); diff --git a/init.c b/init.c index b6dc1de..7380f53 100644 --- a/init.c +++ b/init.c @@ -36,6 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include "export.h" #include "mktorrent.h" #include "ftw.h" +#include "msg.h" #ifndef MAX_OPENFD #define MAX_OPENFD 100 /* Maximum number of file descriptors @@ -82,10 +83,8 @@ static void set_absolute_file_path(struct metafile *m) using getcwd is a bit of a PITA */ /* allocate initial string */ string = malloc(length); - if (string == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(string == NULL, "Out of memory.\n"); + /* while our allocated memory for the working dir isn't big enough */ while (getcwd(string, length) == NULL) { /* double the buffer size */ @@ -94,10 +93,7 @@ static void set_absolute_file_path(struct metafile *m) free(string); /* and allocate a new one twice as big muahaha */ string = malloc(length); - if (string == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(string == NULL, "Out of memory.\n"); } /* now set length to the proper length of the working dir */ @@ -107,20 +103,14 @@ static void set_absolute_file_path(struct metafile *m) /* append .torrent to the working dir */ string = realloc(string, length + strlen(m->torrent_name) + 10); - if (string == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(string == NULL, "Out of memory.\n"); sprintf(string + length, DIRSEP "%s.torrent", m->torrent_name); } else { /* otherwise append the torrent path to the working dir */ string = realloc(string, length + strlen(m->metainfo_file_path) + 2); - if (string == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(string == NULL, "Out of memory.\n"); sprintf(string + length, DIRSEP "%s", m->metainfo_file_path); } @@ -138,10 +128,7 @@ static slist_t *get_slist(char *s) /* allocate memory for the first node in the list */ list = last = malloc(sizeof(slist_t)); - if (list == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(list == NULL, "Out of memory.\n"); /* add URLs to the list while there are commas in the string */ while ((e = strchr(s, ','))) { @@ -156,10 +143,7 @@ static slist_t *get_slist(char *s) /* append another node to the list */ last->next = malloc(sizeof(slist_t)); last = last->next; - if (last == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(last == NULL, "Out of memory.\n"); } /* set the last string in the list */ @@ -179,31 +163,21 @@ static int is_dir(struct metafile *m, char *target) struct stat s; /* stat structure for stat() to fill */ /* stat the target */ - if (stat(target, &s)) { - fprintf(stderr, "Error stat'ing '%s': %s\n", - target, strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF(stat(target, &s), "Error stat'ing '%s': %s\n", + target, strerror(errno)); /* if it is a directory, just return 1 */ if (S_ISDIR(s.st_mode)) return 1; /* if it isn't a regular file either, something is wrong.. */ - if (!S_ISREG(s.st_mode)) { - fprintf(stderr, - "'%s' is neither a directory nor regular file.\n", - target); - exit(EXIT_FAILURE); - } - + FATAL_IF(!S_ISREG(s.st_mode), + "'%s' is neither a directory nor regular file.\n", target); + /* since we know the torrent is just a single file and we've already stat'ed it, we might as well set the file list */ m->file_list = malloc(sizeof(flist_t)); - if (m->file_list == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(m->file_list == NULL, "Out of memory.\n"); m->file_list->path = target; m->file_list->size = s.st_size; m->file_list->next = NULL; @@ -455,10 +429,7 @@ EXPORT void init(struct metafile *m, int argc, char *argv[]) announce_last = announce_last->next; } - if (announce_last == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(announce_last == NULL, "Out of memory.\n"); announce_last->l = get_slist(optarg); break; case 'c': @@ -506,39 +477,28 @@ EXPORT void init(struct metafile *m, int argc, char *argv[]) web_seed_last = web_seed_last->next; break; case '?': - fprintf(stderr, "Use -h for help.\n"); - exit(EXIT_FAILURE); + fatal("Use -h for help.\n"); } } /* set the correct piece length. default is 2^18 = 256kb. */ - if (m->piece_length < 15 || m->piece_length > 28) { - fprintf(stderr, - "The piece length must be a number between " - "15 and 28.\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(m->piece_length < 15 || m->piece_length > 28, + "The piece length must be a number between 15 and 28.\n"); m->piece_length = 1 << m->piece_length; if (announce_last != NULL) announce_last->next = NULL; /* ..and a file or directory from which to create the torrent */ - if (optind >= argc) { - fprintf(stderr, "Must specify the contents, " - "use -h for help\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(optind >= argc, + "Must specify the contents, use -h for help\n"); #ifdef USE_PTHREADS /* check the number of threads */ if (m->threads) { - if (m->threads > 20) { - fprintf(stderr, "The number of threads is limited to " - "at most 20\n"); - exit(EXIT_FAILURE); - } + FATAL_IF0(m->threads > 20, + "The number of threads is limited to at most 20\n"); } else { #ifdef _SC_NPROCESSORS_ONLN m->threads = sysconf(_SC_NPROCESSORS_ONLN); @@ -567,11 +527,8 @@ EXPORT void init(struct metafile *m, int argc, char *argv[]) m->target_is_directory = is_dir(m, argv[optind]); if (m->target_is_directory) { /* change to the specified directory */ - if (chdir(argv[optind])) { - fprintf(stderr, "Error changing directory to '%s': %s\n", - argv[optind], strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF(chdir(argv[optind]), "Error changing directory to '%s': %s\n", + argv[optind], strerror(errno)); if (file_tree_walk("." DIRSEP, MAX_OPENFD, process_node, m)) exit(EXIT_FAILURE); diff --git a/main.c b/main.c index 99ecaad..2fb1a59 100644 --- a/main.c +++ b/main.c @@ -30,6 +30,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include "init.h" #include "hash.h" #include "output.h" +#include "msg.h" #ifdef ALLINONE /* include all .c files in alphabetical order */ @@ -76,19 +77,12 @@ static FILE *open_file(const char *path) /* 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); - if (fd < 0) { - fprintf(stderr, "Error creating '%s': %s\n", - path, strerror(errno)); - exit(EXIT_FAILURE); - } - + FATAL_IF(fd < 0, "Error creating '%s': %s\n", path, strerror(errno)); + /* create the stream from this filedescriptor */ f = fdopen(fd, "wb"); - if (f == NULL) { - fprintf(stderr, "Error creating stream for '%s': %s\n", - path, strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF(f == NULL, "Error creating stream for '%s': %s\n", + path, strerror(errno)); return f; } @@ -99,11 +93,7 @@ static FILE *open_file(const char *path) static void close_file(FILE *f) { /* close the metainfo file */ - if (fclose(f)) { - fprintf(stderr, "Error closing stream: %s\n", - strerror(errno)); - exit(EXIT_FAILURE); - } + FATAL_IF(fclose(f), "Error closing stream: %s\n", strerror(errno)); } /*