eliminate unnecessary fdopen() in got_object_blob_create()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
diff --git a/lib/object_create.c b/lib/object_create.c
index 222b6b1..942f76d 100644
--- a/lib/object_create.c
+++ b/lib/object_create.c
@@ -24,6 +24,7 @@
#include <string.h>
#include <stdint.h>
#include <sha1.h>
+#include <unistd.h>
#include <zlib.h>
#include "got_error.h"
@@ -48,7 +49,7 @@ got_object_blob_create(struct got_object_id **id, struct got_repository *repo,
{
const struct got_error *err = NULL, *unlock_err = NULL;
char *header = NULL, *blobpath = NULL, *objpath = NULL, *outpath = NULL;
- FILE *infile = NULL, *blobfile = NULL, *outfile = NULL;
+ FILE *blobfile = NULL, *outfile = NULL;
int fd = -1;
struct stat sb;
SHA1_CTX sha1_ctx;
@@ -76,29 +77,22 @@ got_object_blob_create(struct got_object_id **id, struct got_repository *repo,
}
SHA1Update(&sha1_ctx, header, strlen(header) + 1);
- infile = fdopen(fd, "r");
- if (infile == NULL) {
- err = got_error_from_errno();
- goto done;
- }
- fd = -1;
-
err = got_opentemp_named(&blobpath, &blobfile, "/tmp/got-blob-create");
if (err)
goto done;
while (1) {
char buf[8192];
- size_t inlen, outlen;
-
- inlen = fread(buf, 1, sizeof(buf), infile);
- if (inlen == 0) {
- if (ferror(infile)) {
- err = got_error_from_errno();
- goto done;
- }
- break; /* EOF */
+ ssize_t inlen;
+ size_t outlen;
+
+ inlen = read(fd, buf, sizeof(buf));
+ if (inlen == -1) {
+ err = got_error_from_errno();
+ goto done;
}
+ if (inlen == 0)
+ break; /* EOF */
SHA1Update(&sha1_ctx, buf, inlen);
outlen = fwrite(buf, 1, inlen, blobfile);
if (outlen != inlen) {
@@ -158,8 +152,6 @@ done:
}
if (fd != -1 && close(fd) != 0 && err == NULL)
err = got_error_from_errno();
- if (infile && fclose(infile) != 0 && err == NULL)
- err = got_error_from_errno();
if (blobfile && fclose(blobfile) != 0 && err == NULL)
err = got_error_from_errno();
if (outfile && fclose(outfile) != 0 && err == NULL)