Use atomic updates to the fast-import mark file
When we allow fast-import frontends to reload a mark file from a prior session we want to let them use the same file as they exported the marks to. This makes it very simple for the frontend to save state across incremental imports. But we don't want to lose the old marks table if anything goes wrong while writing our current marks table. So instead of truncating and overwriting the path specified to --export-marks we use the standard lockfile code to write the current marks out to a temporary file, then rename it over the old marks table. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Mar 7, 2007 at 18:05 UTC
60b9004cdb106ce56589cbe587b6b5e7d6ad3cf9
1 file changed
+26
-9
fast-import.c
+26
-9
@@ -1375,16 +1375,33 @@ static void dump_marks_helper(FILE *f,
1375
1376
static void dump_marks(void)
1377
{
1378
- if (mark_file)
1379
- {
1380
- FILE *f = fopen(mark_file, "w");
1381
- if (f) {
1382
- dump_marks_helper(f, 0, marks);
1383
- fclose(f);
1384
- } else
1385
- failure |= error("Unable to write marks file %s: %s",
1386
- mark_file, strerror(errno));
1378
+ static struct lock_file mark_lock;
1379
+ int mark_fd;
1380
+ FILE *f;
1381
+
1382
+ if (!mark_file)
1383
+ return;
1384
+
1385
+ mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
1386
+ if (mark_fd < 0) {
1387
+ failure |= error("Unable to write marks file %s: %s",
1388
+ mark_file, strerror(errno));
1389
+ return;
1390
}
1391
+
1392
+ f = fdopen(mark_fd, "w");
1393
+ if (!f) {
1394
+ rollback_lock_file(&mark_lock);
1395
+ failure |= error("Unable to write marks file %s: %s",
1396
+ mark_file, strerror(errno));
1397
+ return;
1398
+ }
1399
+
1400
+ dump_marks_helper(f, 0, marks);
1401
+ fclose(f);
1402
+ if (commit_lock_file(&mark_lock))
1403
+ failure |= error("Unable to write marks file %s: %s",
1404
+ mark_file, strerror(errno));
1405
}
1406
1407
static void read_next_command(void)