Skip to content

Commit

Permalink
Merge pull request #41 from uno20001/fatal_error_utility
Browse files Browse the repository at this point in the history
Utility function for handling fatal errors
  • Loading branch information
Rudde authored Apr 19, 2020
2 parents 9fefac2 + 416f192 commit a0c182f
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 180 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ program = mktorrent
version = 1.1

HEADERS = mktorrent.h
SRCS = ftw.c init.c sha1.c hash.c output.c main.c
SRCS = ftw.c init.c sha1.c hash.c output.c main.c msg.c
20 changes: 10 additions & 10 deletions ftw.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static struct dir_state *dir_state_new(struct dir_state *prev,
struct dir_state *ds = malloc(sizeof(struct dir_state));

if (ds == NULL) {
fprintf(stderr, "Out of memory.\n");
fprintf(stderr, "fatal error: out of memory\n");
return NULL;
}

Expand All @@ -61,7 +61,7 @@ static unsigned int dir_state_open(struct dir_state *ds, const char *name,
{
ds->dir = opendir(name);
if (ds->dir == NULL) {
fprintf(stderr, "Error opening '%s': %s\n",
fprintf(stderr, "fatal error: cannot open '%s': %s\n",
name, strerror(errno));
return 1;
}
Expand All @@ -76,7 +76,7 @@ static unsigned int dir_state_reopen(struct dir_state *ds, char *name)
name[ds->length] = '\0';
ds->dir = opendir(name);
if (ds->dir == NULL) {
fprintf(stderr, "Error opening '%s': %s\n",
fprintf(stderr, "fatal error: cannot open '%s': %s\n",
name, strerror(errno));
return 1;
}
Expand All @@ -92,13 +92,13 @@ static unsigned int dir_state_close(struct dir_state *ds)
{
ds->offset = telldir(ds->dir);
if (ds->offset < 0) {
fprintf(stderr, "Error getting dir offset: %s\n",
fprintf(stderr, "fatal error: cannot obtain dir offset: %s\n",
strerror(errno));
return 1;
}

if (closedir(ds->dir)) {
fprintf(stderr, "Error closing directory: %s\n",
fprintf(stderr, "fatal error: cannot close directory: %s\n",
strerror(errno));
return 1;
}
Expand Down Expand Up @@ -141,7 +141,7 @@ EXPORT int file_tree_walk(const char *dirname, unsigned int nfds,

path = malloc(path_size);
if (path == NULL) {
fprintf(stderr, "Out of memory.\n");
fprintf(stderr, "fatal error: out of memory\n");
return cleanup(ds, NULL, -1);
}
path_max = path + path_size;
Expand All @@ -156,7 +156,7 @@ EXPORT int file_tree_walk(const char *dirname, unsigned int nfds,

new_path = realloc(path, 2*path_size);
if (new_path == NULL) {
fprintf(stderr, "Out of memory.\n");
fprintf(stderr, "fatal error: out of memory\n");
return cleanup(ds, path, -1);
}
end = new_path + path_size;
Expand Down Expand Up @@ -205,7 +205,7 @@ EXPORT int file_tree_walk(const char *dirname, unsigned int nfds,

new_path = realloc(path, 2*path_size);
if (new_path == NULL) {
fprintf(stderr, "Out of memory.\n");
fprintf(stderr, "fatal error: out of memory\n");
return cleanup(ds, path, -1);
}
end = new_path + path_size;
Expand All @@ -216,7 +216,7 @@ EXPORT int file_tree_walk(const char *dirname, unsigned int nfds,
}

if (stat(path, &sbuf)) {
fprintf(stderr, "Error stat'ing '%s': %s\n",
fprintf(stderr, "fatal error: cannot stat '%s': %s\n",
path, strerror(errno));
return cleanup(ds, path, -1);
}
Expand Down Expand Up @@ -249,7 +249,7 @@ EXPORT int file_tree_walk(const char *dirname, unsigned int nfds,
} else {
if (closedir(ds->dir)) {
path[ds->length] = '\0';
fprintf(stderr, "Error closing '%s': %s\n",
fprintf(stderr, "fatal error: cannot close '%s': %s\n",
path, strerror(errno));
return cleanup(ds, path, -1);
}
Expand Down
38 changes: 12 additions & 26 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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,
"cannot open '%s' for reading: %s\n", f->path, strerror(errno));
printf("Hashing %s.\n", f->path);
fflush(stdout);

Expand All @@ -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, "cannot read from '%s': %s\n",
f->path, strerror(errno));

if (d == 0) /* end of file */
break;
Expand All @@ -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), "cannot close '%s': %s\n",
f->path, strerror(errno));
}

/* finally append the hash of the last irregular piece to the hash string */
Expand All @@ -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 */
Expand Down
79 changes: 20 additions & 59 deletions hash_pthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, "cannot set thread cancel type: %s\n", strerror(err));

while (1) {
/* print progress and flush the buffer immediately */
Expand Down Expand Up @@ -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,
"cannot open '%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, "cannot read from '%s': %s\n",
f->path, strerror(errno));

if (d == 0) /* end of file */
break;
Expand All @@ -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), "cannot close '%s': %s\n",
f->path, strerror(errno));
}

/* finally append the hash of the last irregular piece to the hash string */
Expand All @@ -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
}

Expand All @@ -299,65 +283,42 @@ 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;

/* 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, "cannot create 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, "cannot create 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, "cannot cancel thread: %s\n", strerror(err));

/* inform workers we're done */
set_done(&q);

/* 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, "cannot join 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, "cannot join thread: %s\n", strerror(err));

/* destroy mutexes and condition variables */
pthread_mutex_destroy(&q.mutex_full);
Expand Down
Loading

0 comments on commit a0c182f

Please sign in to comment.