@matheusbsbs / matheus-bagliano / commits / 4b37666ccb
Fork 0

builtin-bundle create - use lock_file

"git bundle create" left an invalid, partially written bundle if an error occured during creation. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 12, 2007 at 10:44 UTC 4b37666ccbab015e75fda745c29a2f5d58a54482
1 file changed +10 -8
builtin-bundle.c
+10 -8
@@ -189,7 +189,9 @@ static int list_heads(struct bundle_header *header, int argc, const char **argv)
189 static int create_bundle(struct bundle_header *header, const char *path,
190 int argc, const char **argv)
191 {
192 + static struct lock_file lock;
193 int bundle_fd = -1;
194 + int bundle_to_stdout;
195 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
196 const char **argv_pack = xmalloc(5 * sizeof(const char *));
197 int i, ref_count = 0;
@@ -198,14 +200,11 @@ static int create_bundle(struct bundle_header *header, const char *path,
200 struct child_process rls;
201 FILE *rls_fout;
202
201 - /*
202 - * NEEDSWORK: this should use something like lock-file
203 - * to create temporary that is cleaned up upon error.
204 - */
205 - bundle_fd = (!strcmp(path, "-") ? 1 :
206 - open(path, O_CREAT | O_EXCL | O_WRONLY, 0666));
207 - if (bundle_fd < 0)
208 - return error("Could not create '%s': %s", path, strerror(errno));
203 + bundle_to_stdout = !strcmp(path, "-");
204 + if (bundle_to_stdout)
205 + bundle_fd = 1;
206 + else
207 + bundle_fd = hold_lock_file_for_update(&lock, path, 1);
208
209 /* write signature */
210 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
@@ -341,6 +340,9 @@ static int create_bundle(struct bundle_header *header, const char *path,
340 }
341 if (finish_command(&rls))
342 return error ("pack-objects died");
343 + close(bundle_fd);
344 + if (!bundle_to_stdout)
345 + commit_lock_file(&lock);
346 return 0;
347 }
348