move code for creating loose objects to a separate function
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
diff --git a/lib/object_create.c b/lib/object_create.c
index cbb6de1..35cbd8b 100644
--- a/lib/object_create.c
+++ b/lib/object_create.c
@@ -44,19 +44,82 @@
#define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
#endif
+static const struct got_error *
+create_loose_object(struct got_object_id *id, FILE *content,
+ struct got_repository *repo)
+{
+ const struct got_error *err = NULL, *unlock_err = NULL;
+ char *objpath = NULL, *outpath = NULL;
+ FILE *outfile = NULL;
+ struct got_lockfile *lf = NULL;
+ size_t outlen = 0;
+
+ err = got_object_get_path(&objpath, id, repo);
+ if (err)
+ return err;
+
+ err = got_opentemp_named(&outpath, &outfile, objpath);
+ if (err) {
+ char *parent_path;
+ if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
+ goto done;
+ err = got_path_dirname(&parent_path, objpath);
+ if (err)
+ goto done;
+ err = got_path_mkdir(parent_path);
+ free(parent_path);
+ if (err)
+ goto done;
+ err = got_opentemp_named(&outpath, &outfile, objpath);
+ if (err)
+ goto done;
+ }
+
+ err = got_deflate_to_file(&outlen, content, outfile);
+ if (err)
+ goto done;
+
+ err = got_lockfile_lock(&lf, objpath);
+ if (err)
+ goto done;
+
+ if (rename(outpath, objpath) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ free(outpath);
+ outpath = NULL;
+
+ if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+done:
+ free(objpath);
+ if (outpath) {
+ if (unlink(outpath) != 0 && err == NULL)
+ err = got_error_from_errno();
+ free(outpath);
+ }
+ if (outfile && fclose(outfile) != 0 && err == NULL)
+ err = got_error_from_errno();
+ if (lf)
+ unlock_err = got_lockfile_unlock(lf);
+ return err ? err : unlock_err;
+}
+
const struct got_error *
got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
struct got_repository *repo)
{
- const struct got_error *err = NULL, *unlock_err = NULL;
- char *header = NULL, *objpath = NULL, *outpath = NULL;
- FILE *blobfile = NULL, *outfile = NULL;
+ const struct got_error *err = NULL;
+ char *header = NULL;
+ FILE *blobfile = NULL;
int fd = -1;
struct stat sb;
SHA1_CTX sha1_ctx;
uint8_t digest[SHA1_DIGEST_LENGTH];
- struct got_lockfile *lf = NULL;
- size_t outlen = 0, headerlen = 0;
+ size_t headerlen = 0, n;
*id = NULL;
@@ -85,15 +148,14 @@ got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
goto done;
}
- outlen = fwrite(header, 1, headerlen, blobfile);
- if (outlen != headerlen) {
+ n = fwrite(header, 1, headerlen, blobfile);
+ if (n != headerlen) {
err = got_ferror(blobfile, GOT_ERR_IO);
goto done;
}
while (1) {
char buf[8192];
ssize_t inlen;
- size_t outlen;
inlen = read(fd, buf, sizeof(buf));
if (inlen == -1) {
@@ -103,8 +165,8 @@ got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
if (inlen == 0)
break; /* EOF */
SHA1Update(&sha1_ctx, buf, inlen);
- outlen = fwrite(buf, 1, inlen, blobfile);
- if (outlen != inlen) {
+ n = fwrite(buf, 1, inlen, blobfile);
+ if (n != inlen) {
err = got_ferror(blobfile, GOT_ERR_IO);
goto done;
}
@@ -124,65 +186,16 @@ got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
}
rewind(blobfile);
- err = got_object_get_path(&objpath, *id, repo);
- if (err)
- goto done;
-
- err = got_opentemp_named(&outpath, &outfile, objpath);
- if (err) {
- char *parent_path;
- if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
- goto done;
- err = got_path_dirname(&parent_path, objpath);
- if (err)
- goto done;
- err = got_path_mkdir(parent_path);
- free(parent_path);
- if (err)
- goto done;
- err = got_opentemp_named(&outpath, &outfile, objpath);
- if (err)
- goto done;
- }
-
- err = got_deflate_to_file(&outlen, blobfile, outfile);
- if (err)
- goto done;
-
- err = got_lockfile_lock(&lf, objpath);
- if (err)
- goto done;
-
- if (rename(outpath, objpath) != 0) {
- err = got_error_from_errno();
- goto done;
- }
- free(outpath);
- outpath = NULL;
-
- if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
- err = got_error_from_errno();
- goto done;
- }
+ err = create_loose_object(*id, blobfile, repo);
done:
free(header);
- free(objpath);
- if (outpath) {
- if (unlink(outpath) != 0 && err == NULL)
- err = got_error_from_errno();
- free(outpath);
- }
if (fd != -1 && close(fd) != 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)
- err = got_error_from_errno();
if (err) {
free(*id);
*id = NULL;
}
- if (lf)
- unlock_err = got_lockfile_unlock(lf);
- return err ? err : unlock_err;
+ return err;
}