src/mime.c
changeset 62 ad5e484720e1
parent 59 88429085f8da
child 63 f5b2641f4ae7
     1.1 --- a/src/mime.c	Tue Feb 10 08:27:36 2015 -0500
     1.2 +++ b/src/mime.c	Sat Feb 21 16:55:43 2015 +0100
     1.3 @@ -1,6 +1,6 @@
     1.4  #include "mime.h"
     1.5  
     1.6 -#include <libetpan/libetpan.h>
     1.7 +#include <libetpan/mailmime.h>
     1.8  #include <string.h>
     1.9  #include <stdlib.h>
    1.10  #include <assert.h>
    1.11 @@ -179,8 +179,14 @@
    1.12  
    1.13      errno = 0;
    1.14      rewind(file);
    1.15 +
    1.16      assert(errno == 0);
    1.17 -    clearerr(file);
    1.18 +    switch (errno) {
    1.19 +        case ENOMEM:
    1.20 +            goto enomem;
    1.21 +        default:
    1.22 +            goto err_file;
    1.23 +    }
    1.24  
    1.25      buf = calloc(1, size + 1);
    1.26      assert(buf);
    1.27 @@ -190,15 +196,23 @@
    1.28      char *_buf = buf;
    1.29      size_t rest = size;
    1.30      for (size_t bytes_read = 0; rest > 0; rest -= bytes_read, _buf += rest) {
    1.31 -        assert(feof(file) == 0);
    1.32 +        clearerr(file);
    1.33 +        bytes_read = rest * fread(_buf, rest, 1, file);
    1.34 +
    1.35 +        assert(ferror(file) == 0 || ferror(file) == EINTR);
    1.36 +        if (ferror(file) != 0 && ferror(file) != EINTR)
    1.37 +            goto err_file;
    1.38 +
    1.39 +        assert(!feof(file));
    1.40          if (feof(file))
    1.41              goto err_file;
    1.42 -        bytes_read = rest * fread(_buf, rest, 1, file);
    1.43 -        if (ferror(file))
    1.44 -            goto err_file;
    1.45      }
    1.46  
    1.47 -    fclose(file);
    1.48 +    do {
    1.49 +        r = fclose(file);
    1.50 +    } while (r == -1 && errno == EINTR);
    1.51 +    assert(r == 0);
    1.52 +
    1.53      mailmime_free(mime);
    1.54      *resulttext = buf;
    1.55      return PEP_STATUS_OK;
    1.56 @@ -218,10 +232,18 @@
    1.57      free(buf);
    1.58      free(template);
    1.59  
    1.60 -    if (file)
    1.61 -        fclose(file);
    1.62 -    else if (fd != -1)
    1.63 -        close(fd);
    1.64 +    if (file) {
    1.65 +        do {
    1.66 +            r = fclose(file);
    1.67 +        } while (r == -1 && errno == EINTR);
    1.68 +        assert(r == 0);
    1.69 +    }
    1.70 +    else if (fd != -1) {
    1.71 +        do {
    1.72 +            r = close(fd);
    1.73 +        } while (r == -1 && errno == EINTR);
    1.74 +        assert(r == 0);
    1.75 +    }
    1.76  
    1.77      if (mime)
    1.78          mailmime_free(mime);