|
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" |
44 | 46 |
|
45 | 47 | #define FAILED_RUN "failed to run %s" |
46 | 48 |
|
@@ -1345,6 +1347,131 @@ static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts |
1345 | 1347 | return 0; |
1346 | 1348 | } |
1347 | 1349 |
|
| 1350 | +static void link_or_copy_or_die(const char *src, const char *dst) |
| 1351 | +{ |
| 1352 | + if (!link(src, dst)) |
| 1353 | + return; |
| 1354 | + |
| 1355 | + warning_errno(_("failed to link '%s' to '%s'... trying copy..."), src, |
| 1356 | + dst); |
| 1357 | + |
| 1358 | + if (!copy_file(dst, src, 0644)) |
| 1359 | + return; |
| 1360 | + |
| 1361 | + die_errno(_("failed to copy '%s' to '%s'"), src, dst); |
| 1362 | +} |
| 1363 | + |
| 1364 | +static void migrate_pack(const char *srcdir, const char *dstdir, |
| 1365 | + const char *pack_filename) |
| 1366 | +{ |
| 1367 | + struct stat st; |
| 1368 | + int has_keep, has_rev, has_idx; |
| 1369 | + char *basename, *pack_src, *keep_src, *rev_src, *idx_src, |
| 1370 | + *pack_dst, *keep_dst, *rev_dst, *idx_dst; |
| 1371 | + |
| 1372 | + trace2_region_enter("maintenance", "migrate_pack", the_repository); |
| 1373 | + |
| 1374 | + basename = xstrndup(pack_filename, strlen(pack_filename) - 5 /*.pack*/); |
| 1375 | + pack_src = xstrfmt("%s/%s", srcdir, pack_filename); |
| 1376 | + pack_dst = xstrfmt("%s/%s", dstdir, pack_filename); |
| 1377 | + keep_src = xstrfmt("%s/%s.keep", srcdir, basename); |
| 1378 | + keep_dst = xstrfmt("%s/%s.keep", dstdir, basename); |
| 1379 | + rev_src = xstrfmt("%s/%s.rev", srcdir, basename); |
| 1380 | + rev_dst = xstrfmt("%s/%s.rev", dstdir, basename); |
| 1381 | + idx_src = xstrfmt("%s/%s.idx", srcdir, basename); |
| 1382 | + idx_dst = xstrfmt("%s/%s.idx", dstdir, basename); |
| 1383 | + |
| 1384 | + has_keep = !stat(keep_src, &st); |
| 1385 | + has_rev = !stat(rev_src, &st); |
| 1386 | + has_idx = !stat(idx_src, &st); |
| 1387 | + |
| 1388 | + /* A pack without an index file is not yet ready to be migrated. */ |
| 1389 | + if (!has_idx) |
| 1390 | + goto cleanup; |
| 1391 | + |
| 1392 | + /* |
| 1393 | + * Hard link (or copy if that fails) all but the index file so that |
| 1394 | + * other Git processes don't attempt to use the pack file from the new |
| 1395 | + * location yet. |
| 1396 | + */ |
| 1397 | + link_or_copy_or_die(pack_src, pack_dst); |
| 1398 | + if (has_keep) |
| 1399 | + link_or_copy_or_die(keep_src, keep_dst); |
| 1400 | + if (has_rev) |
| 1401 | + link_or_copy_or_die(rev_src, rev_dst); |
| 1402 | + |
| 1403 | + /* |
| 1404 | + * Move the index file atomically now that the other files can be found |
| 1405 | + * at the destination. |
| 1406 | + */ |
| 1407 | + if (rename(idx_src, idx_dst)) |
| 1408 | + die_errno(_("failed to move '%s' to '%s'"), idx_src, idx_dst); |
| 1409 | + |
| 1410 | + /* |
| 1411 | + * Now the pack and all associated files exist at the destination we can |
| 1412 | + * now clean up the files in the source directory. |
| 1413 | + */ |
| 1414 | + if (unlink(pack_src)) |
| 1415 | + warning_errno(_("failed to delete '%s'"), pack_src); |
| 1416 | + if (has_keep && unlink(keep_src)) |
| 1417 | + warning_errno(_("failed to delete '%s'"), keep_src); |
| 1418 | + if (has_rev & unlink(rev_src)) |
| 1419 | + warning_errno(_("failed to delete '%s'"), rev_src); |
| 1420 | + |
| 1421 | +cleanup: |
| 1422 | + free(idx_src); |
| 1423 | + free(idx_dst); |
| 1424 | + free(rev_src); |
| 1425 | + free(rev_dst); |
| 1426 | + free(keep_src); |
| 1427 | + free(keep_dst); |
| 1428 | + free(pack_src); |
| 1429 | + free(pack_dst); |
| 1430 | + free(basename); |
| 1431 | + |
| 1432 | + trace2_region_leave("maintenance", "migrate_pack", the_repository); |
| 1433 | +} |
| 1434 | + |
| 1435 | +static void move_pack_to_vfs_cache(const char *full_path, size_t full_path_len, |
| 1436 | + const char *file_name, UNUSED void *data) |
| 1437 | +{ |
| 1438 | + char *srcdir; |
| 1439 | + struct strbuf dstdir = STRBUF_INIT; |
| 1440 | + |
| 1441 | + /* We only care about the actual pack files here. |
| 1442 | + * The associated .idx, .keep, .rev files will be copied in tandem |
| 1443 | + * with the pack file, with the index file being moved last. |
| 1444 | + * The original locations of the non-index files will only deleted |
| 1445 | + * once all other files have been copied/moved. |
| 1446 | + */ |
| 1447 | + if (!ends_with(file_name, ".pack")) |
| 1448 | + return; |
| 1449 | + |
| 1450 | + srcdir = xstrndup(full_path, full_path_len - strlen(file_name) - 1); |
| 1451 | + |
| 1452 | + /* No cache or same source + desintation means there's no work to do. */ |
| 1453 | + if (!object_dir || !fspathcmp(srcdir, object_dir)) |
| 1454 | + return; |
| 1455 | + |
| 1456 | + strbuf_addf(&dstdir, "%s/pack", object_dir); |
| 1457 | + |
| 1458 | + migrate_pack(srcdir, dstdir.buf, file_name); |
| 1459 | + |
| 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 | + |
1348 | 1475 | typedef int maintenance_task_fn(struct maintenance_run_opts *opts, |
1349 | 1476 | struct gc_config *cfg); |
1350 | 1477 |
|
@@ -1374,6 +1501,7 @@ enum maintenance_task_label { |
1374 | 1501 | TASK_GC, |
1375 | 1502 | TASK_COMMIT_GRAPH, |
1376 | 1503 | TASK_PACK_REFS, |
| 1504 | + TASK_VFS_CACHE_MOVE, |
1377 | 1505 |
|
1378 | 1506 | /* Leave as final value */ |
1379 | 1507 | TASK__COUNT |
@@ -1410,6 +1538,10 @@ static struct maintenance_task tasks[] = { |
1410 | 1538 | maintenance_task_pack_refs, |
1411 | 1539 | pack_refs_condition, |
1412 | 1540 | }, |
| 1541 | + [TASK_VFS_CACHE_MOVE] = { |
| 1542 | + "vfs-cache-move", |
| 1543 | + maintenance_task_vfs_cache_move, |
| 1544 | + }, |
1413 | 1545 | }; |
1414 | 1546 |
|
1415 | 1547 | static int compare_tasks_by_selection(const void *a_, const void *b_) |
@@ -1504,6 +1636,8 @@ static void initialize_maintenance_strategy(void) |
1504 | 1636 | tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY; |
1505 | 1637 | tasks[TASK_PACK_REFS].enabled = 1; |
1506 | 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; |
1507 | 1641 | } |
1508 | 1642 | } |
1509 | 1643 |
|
|
0 commit comments