|
10 | 10 | * Copyright (c) 2006 Shawn O. Pearce |
11 | 11 | */ |
12 | 12 | #define USE_THE_REPOSITORY_VARIABLE |
| 13 | +#include "git-compat-util.h" |
13 | 14 | #include "builtin.h" |
14 | 15 | #include "abspath.h" |
15 | 16 | #include "date.h" |
|
41 | 42 | #include "hook.h" |
42 | 43 | #include "setup.h" |
43 | 44 | #include "trace2.h" |
| 45 | +#include "copy.h" |
| 46 | +#include "dir.h" |
44 | 47 |
|
45 | 48 | #define FAILED_RUN "failed to run %s" |
46 | 49 |
|
@@ -1347,6 +1350,128 @@ static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts |
1347 | 1350 | return 0; |
1348 | 1351 | } |
1349 | 1352 |
|
| 1353 | +static void link_or_copy_or_die(const char *src, const char *dst) |
| 1354 | +{ |
| 1355 | + if (!link(src, dst)) |
| 1356 | + return; |
| 1357 | + |
| 1358 | + warning_errno(_("failed to link '%s' to '%s'"), src, dst); |
| 1359 | + |
| 1360 | + if (!copy_file(dst, src, 0644)) |
| 1361 | + return; |
| 1362 | + |
| 1363 | + die_errno(_("failed to copy '%s' to '%s'"), src, dst); |
| 1364 | +} |
| 1365 | + |
| 1366 | +static void migrate_pack(const char *srcdir, const char *dstdir, |
| 1367 | + const char *pack_filename) |
| 1368 | +{ |
| 1369 | + struct stat st; |
| 1370 | + char *basename; |
| 1371 | + struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT; |
| 1372 | + struct { |
| 1373 | + const char *ext; |
| 1374 | + unsigned move:1; |
| 1375 | + } files[] = { |
| 1376 | + {".pack", 0}, |
| 1377 | + {".keep", 0}, |
| 1378 | + {".rev", 0}, |
| 1379 | + {".idx", 1}, /* The index file must be atomically moved last. */ |
| 1380 | + }; |
| 1381 | + |
| 1382 | + trace2_region_enter("maintenance", "migrate_pack", the_repository); |
| 1383 | + |
| 1384 | + basename = xstrndup(pack_filename, strlen(pack_filename) - 5 /*.pack*/); |
| 1385 | + |
| 1386 | + /* A pack without an index file is not yet ready to be migrated. */ |
| 1387 | + strbuf_addf(&src, "%s/%s%s", srcdir, basename, ".idx"); |
| 1388 | + if (stat(src.buf, &st)) |
| 1389 | + goto cleanup; |
| 1390 | + |
| 1391 | + /* Move or copy files from the source directory to the destination. */ |
| 1392 | + for (size_t i = 0; i < ARRAY_SIZE(files); i++) { |
| 1393 | + strbuf_reset(&src); |
| 1394 | + strbuf_addf(&src, "%s/%s%s", srcdir, basename, files[i].ext); |
| 1395 | + |
| 1396 | + if (stat(src.buf, &st)) |
| 1397 | + continue; |
| 1398 | + |
| 1399 | + strbuf_reset(&dst); |
| 1400 | + strbuf_addf(&dst, "%s/%s%s", dstdir, basename, files[i].ext); |
| 1401 | + |
| 1402 | + if (files[i].move) { |
| 1403 | + if (rename(src.buf, dst.buf)) |
| 1404 | + die_errno(_("failed to move '%s' to '%s'"), |
| 1405 | + src.buf, dst.buf); |
| 1406 | + } else { |
| 1407 | + link_or_copy_or_die(src.buf, dst.buf); |
| 1408 | + } |
| 1409 | + } |
| 1410 | + |
| 1411 | + /* |
| 1412 | + * Now the pack and all associated files exist at the destination we can |
| 1413 | + * now clean up the files in the source directory. |
| 1414 | + */ |
| 1415 | + for (size_t i = 0; i < ARRAY_SIZE(files); i++) { |
| 1416 | + /* Files that were moved rather than copied have no clean up. */ |
| 1417 | + if (files[i].move) |
| 1418 | + continue; |
| 1419 | + |
| 1420 | + strbuf_reset(&src); |
| 1421 | + strbuf_addf(&src, "%s/%s%s", srcdir, basename, files[i].ext); |
| 1422 | + if (unlink(src.buf)) |
| 1423 | + warning_errno(_("failed to delete '%s'"), src.buf); |
| 1424 | + } |
| 1425 | + |
| 1426 | +cleanup: |
| 1427 | + free(basename); |
| 1428 | + strbuf_release(&src); |
| 1429 | + strbuf_release(&dst); |
| 1430 | + |
| 1431 | + trace2_region_leave("maintenance", "migrate_pack", the_repository); |
| 1432 | +} |
| 1433 | + |
| 1434 | +static void move_pack_to_vfs_cache(const char *full_path, size_t full_path_len, |
| 1435 | + const char *file_name, UNUSED void *data) |
| 1436 | +{ |
| 1437 | + char *srcdir; |
| 1438 | + struct strbuf dstdir = STRBUF_INIT; |
| 1439 | + |
| 1440 | + /* We only care about the actual pack files here. |
| 1441 | + * The associated .idx, .keep, .rev files will be copied in tandem |
| 1442 | + * with the pack file, with the index file being moved last. |
| 1443 | + * The original locations of the non-index files will only deleted |
| 1444 | + * once all other files have been copied/moved. |
| 1445 | + */ |
| 1446 | + if (!ends_with(file_name, ".pack")) |
| 1447 | + return; |
| 1448 | + |
| 1449 | + srcdir = xstrndup(full_path, full_path_len - strlen(file_name) - 1); |
| 1450 | + |
| 1451 | + /* No cache or same source + desintation means there's no work to do. */ |
| 1452 | + if (!vfs_object_dir || !fspathcmp(srcdir, vfs_object_dir)) |
| 1453 | + goto cleanup; |
| 1454 | + |
| 1455 | + strbuf_addf(&dstdir, "%s/pack", vfs_object_dir); |
| 1456 | + |
| 1457 | + migrate_pack(srcdir, dstdir.buf, file_name); |
| 1458 | + |
| 1459 | +cleanup: |
| 1460 | + free(srcdir); |
| 1461 | + strbuf_release(&dstdir); |
| 1462 | +} |
| 1463 | + |
| 1464 | +static int maintenance_task_vfs_cache_move(UNUSED struct maintenance_run_opts *opts, |
| 1465 | + UNUSED struct gc_config *cfg) |
| 1466 | +{ |
| 1467 | + struct repository *r = the_repository; |
| 1468 | + |
| 1469 | + for_each_file_in_pack_dir(r->objects->odb->path, move_pack_to_vfs_cache, |
| 1470 | + NULL); |
| 1471 | + |
| 1472 | + return 0; |
| 1473 | +} |
| 1474 | + |
1350 | 1475 | typedef int maintenance_task_fn(struct maintenance_run_opts *opts, |
1351 | 1476 | struct gc_config *cfg); |
1352 | 1477 |
|
@@ -1376,6 +1501,7 @@ enum maintenance_task_label { |
1376 | 1501 | TASK_GC, |
1377 | 1502 | TASK_COMMIT_GRAPH, |
1378 | 1503 | TASK_PACK_REFS, |
| 1504 | + TASK_VFS_CACHE_MOVE, |
1379 | 1505 |
|
1380 | 1506 | /* Leave as final value */ |
1381 | 1507 | TASK__COUNT |
@@ -1412,6 +1538,10 @@ static struct maintenance_task tasks[] = { |
1412 | 1538 | maintenance_task_pack_refs, |
1413 | 1539 | pack_refs_condition, |
1414 | 1540 | }, |
| 1541 | + [TASK_VFS_CACHE_MOVE] = { |
| 1542 | + "vfs-cache-move", |
| 1543 | + maintenance_task_vfs_cache_move, |
| 1544 | + }, |
1415 | 1545 | }; |
1416 | 1546 |
|
1417 | 1547 | static int compare_tasks_by_selection(const void *a_, const void *b_) |
@@ -1506,6 +1636,8 @@ static void initialize_maintenance_strategy(void) |
1506 | 1636 | tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY; |
1507 | 1637 | tasks[TASK_PACK_REFS].enabled = 1; |
1508 | 1638 | tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY; |
| 1639 | + tasks[TASK_VFS_CACHE_MOVE].enabled = 1; |
| 1640 | + tasks[TASK_VFS_CACHE_MOVE].schedule = SCHEDULE_WEEKLY; |
1509 | 1641 | } |
1510 | 1642 | } |
1511 | 1643 |
|
|
0 commit comments