|
13 | 13 | #define USE_THE_REPOSITORY_VARIABLE
|
14 | 14 | #define DISABLE_SIGN_COMPARE_WARNINGS
|
15 | 15 |
|
| 16 | +#include "git-compat-util.h" |
16 | 17 | #include "builtin.h"
|
17 | 18 | #include "abspath.h"
|
18 | 19 | #include "date.h"
|
|
44 | 45 | #include "hook.h"
|
45 | 46 | #include "setup.h"
|
46 | 47 | #include "trace2.h"
|
| 48 | +#include "copy.h" |
| 49 | +#include "dir.h" |
47 | 50 |
|
48 | 51 | #define FAILED_RUN "failed to run %s"
|
49 | 52 |
|
@@ -1154,17 +1157,19 @@ static int write_loose_object_to_stdin(const struct object_id *oid,
|
1154 | 1157 | return ++(d->count) > d->batch_size;
|
1155 | 1158 | }
|
1156 | 1159 |
|
1157 |
| -static const char *object_dir = NULL; |
| 1160 | +static const char *shared_object_dir = NULL; |
1158 | 1161 |
|
1159 | 1162 | static int pack_loose(struct maintenance_run_opts *opts)
|
1160 | 1163 | {
|
1161 | 1164 | struct repository *r = the_repository;
|
1162 | 1165 | int result = 0;
|
1163 | 1166 | struct write_loose_object_data data;
|
1164 | 1167 | struct child_process pack_proc = CHILD_PROCESS_INIT;
|
| 1168 | + const char *object_dir = r->objects->odb->path; |
1165 | 1169 |
|
1166 |
| - if (!object_dir) |
1167 |
| - object_dir = r->objects->odb->path; |
| 1170 | + /* If set, use the shared object directory. */ |
| 1171 | + if (shared_object_dir) |
| 1172 | + object_dir = shared_object_dir; |
1168 | 1173 |
|
1169 | 1174 | /*
|
1170 | 1175 | * Do not start pack-objects process
|
@@ -1358,6 +1363,186 @@ static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts
|
1358 | 1363 | return 0;
|
1359 | 1364 | }
|
1360 | 1365 |
|
| 1366 | +static void link_or_copy_or_die(const char *src, const char *dst) |
| 1367 | +{ |
| 1368 | + if (!link(src, dst)) |
| 1369 | + return; |
| 1370 | + |
| 1371 | + /* Use copy operation if src and dst are on different file systems. */ |
| 1372 | + if (errno != EXDEV) |
| 1373 | + warning_errno(_("failed to link '%s' to '%s'"), src, dst); |
| 1374 | + |
| 1375 | + if (copy_file(dst, src, 0444)) |
| 1376 | + die_errno(_("failed to copy '%s' to '%s'"), src, dst); |
| 1377 | +} |
| 1378 | + |
| 1379 | +static void rename_or_copy_or_die(const char *src, const char *dst) |
| 1380 | +{ |
| 1381 | + if (!rename(src, dst)) |
| 1382 | + return; |
| 1383 | + |
| 1384 | + /* Use copy and delete if src and dst are on different file systems. */ |
| 1385 | + if (errno != EXDEV) |
| 1386 | + warning_errno(_("failed to move '%s' to '%s'"), src, dst); |
| 1387 | + |
| 1388 | + if (copy_file(dst, src, 0444)) |
| 1389 | + die_errno(_("failed to copy '%s' to '%s'"), src, dst); |
| 1390 | + |
| 1391 | + if (unlink(src)) |
| 1392 | + die_errno(_("failed to delete '%s'"), src); |
| 1393 | +} |
| 1394 | + |
| 1395 | +static void migrate_pack(const char *srcdir, const char *dstdir, |
| 1396 | + const char *pack_filename) |
| 1397 | +{ |
| 1398 | + size_t basenamelen, srclen, dstlen; |
| 1399 | + struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT; |
| 1400 | + struct { |
| 1401 | + const char *ext; |
| 1402 | + unsigned move:1; |
| 1403 | + } files[] = { |
| 1404 | + {".pack", 0}, |
| 1405 | + {".keep", 0}, |
| 1406 | + {".rev", 0}, |
| 1407 | + {".idx", 1}, /* The index file must be atomically moved last. */ |
| 1408 | + }; |
| 1409 | + |
| 1410 | + trace2_region_enter("maintenance", "migrate_pack", the_repository); |
| 1411 | + |
| 1412 | + basenamelen = strlen(pack_filename) - 5; /* .pack */ |
| 1413 | + strbuf_addstr(&src, srcdir); |
| 1414 | + strbuf_addch(&src, '/'); |
| 1415 | + strbuf_add(&src, pack_filename, basenamelen); |
| 1416 | + strbuf_addstr(&src, ".idx"); |
| 1417 | + |
| 1418 | + /* A pack without an index file is not yet ready to be migrated. */ |
| 1419 | + if (!file_exists(src.buf)) |
| 1420 | + goto cleanup; |
| 1421 | + |
| 1422 | + strbuf_setlen(&src, src.len - 4 /* .idx */); |
| 1423 | + strbuf_addstr(&dst, dstdir); |
| 1424 | + strbuf_addch(&dst, '/'); |
| 1425 | + strbuf_add(&dst, pack_filename, basenamelen); |
| 1426 | + |
| 1427 | + srclen = src.len; |
| 1428 | + dstlen = dst.len; |
| 1429 | + |
| 1430 | + /* Move or copy files from the source directory to the destination. */ |
| 1431 | + for (size_t i = 0; i < ARRAY_SIZE(files); i++) { |
| 1432 | + strbuf_setlen(&src, srclen); |
| 1433 | + strbuf_addstr(&src, files[i].ext); |
| 1434 | + |
| 1435 | + if (!file_exists(src.buf)) |
| 1436 | + continue; |
| 1437 | + |
| 1438 | + strbuf_setlen(&dst, dstlen); |
| 1439 | + strbuf_addstr(&dst, files[i].ext); |
| 1440 | + |
| 1441 | + if (files[i].move) |
| 1442 | + rename_or_copy_or_die(src.buf, dst.buf); |
| 1443 | + else |
| 1444 | + link_or_copy_or_die(src.buf, dst.buf); |
| 1445 | + } |
| 1446 | + |
| 1447 | + /* |
| 1448 | + * Now the pack and all associated files exist at the destination we can |
| 1449 | + * now clean up the files in the source directory. |
| 1450 | + */ |
| 1451 | + for (size_t i = 0; i < ARRAY_SIZE(files); i++) { |
| 1452 | + /* Files that were moved rather than copied have no clean up. */ |
| 1453 | + if (files[i].move) |
| 1454 | + continue; |
| 1455 | + |
| 1456 | + strbuf_setlen(&src, srclen); |
| 1457 | + strbuf_addstr(&src, files[i].ext); |
| 1458 | + |
| 1459 | + /* Files that never existed in originally have no clean up.*/ |
| 1460 | + if (!file_exists(src.buf)) |
| 1461 | + continue; |
| 1462 | + |
| 1463 | + if (unlink(src.buf)) |
| 1464 | + warning_errno(_("failed to delete '%s'"), src.buf); |
| 1465 | + } |
| 1466 | + |
| 1467 | +cleanup: |
| 1468 | + strbuf_release(&src); |
| 1469 | + strbuf_release(&dst); |
| 1470 | + |
| 1471 | + trace2_region_leave("maintenance", "migrate_pack", the_repository); |
| 1472 | +} |
| 1473 | + |
| 1474 | +static void move_pack_to_shared_cache(const char *full_path, size_t full_path_len, |
| 1475 | + const char *file_name, void *data) |
| 1476 | +{ |
| 1477 | + char *srcdir; |
| 1478 | + const char *dstdir = (const char *)data; |
| 1479 | + |
| 1480 | + /* We only care about the actual pack files here. |
| 1481 | + * The associated .idx, .keep, .rev files will be copied in tandem |
| 1482 | + * with the pack file, with the index file being moved last. |
| 1483 | + * The original locations of the non-index files will only deleted |
| 1484 | + * once all other files have been copied/moved. |
| 1485 | + */ |
| 1486 | + if (!ends_with(file_name, ".pack")) |
| 1487 | + return; |
| 1488 | + |
| 1489 | + srcdir = xstrndup(full_path, full_path_len - strlen(file_name) - 1); |
| 1490 | + |
| 1491 | + migrate_pack(srcdir, dstdir, file_name); |
| 1492 | + |
| 1493 | + free(srcdir); |
| 1494 | +} |
| 1495 | + |
| 1496 | +static int move_loose_object_to_shared_cache(const struct object_id *oid, |
| 1497 | + const char *path, |
| 1498 | + UNUSED void *data) |
| 1499 | +{ |
| 1500 | + struct stat st; |
| 1501 | + struct strbuf dst = STRBUF_INIT; |
| 1502 | + char *hex = oid_to_hex(oid); |
| 1503 | + |
| 1504 | + strbuf_addf(&dst, "%s/%.2s/", shared_object_dir, hex); |
| 1505 | + |
| 1506 | + if (stat(dst.buf, &st)) { |
| 1507 | + if (mkdir(dst.buf, 0777)) |
| 1508 | + die_errno(_("failed to create directory '%s'"), dst.buf); |
| 1509 | + } else if (!S_ISDIR(st.st_mode)) |
| 1510 | + die(_("expected '%s' to be a directory"), dst.buf); |
| 1511 | + |
| 1512 | + strbuf_addstr(&dst, hex+2); |
| 1513 | + rename_or_copy_or_die(path, dst.buf); |
| 1514 | + |
| 1515 | + strbuf_release(&dst); |
| 1516 | + return 0; |
| 1517 | +} |
| 1518 | + |
| 1519 | +static int maintenance_task_cache_local_objs(UNUSED struct maintenance_run_opts *opts, |
| 1520 | + UNUSED struct gc_config *cfg) |
| 1521 | +{ |
| 1522 | + struct strbuf dstdir = STRBUF_INIT; |
| 1523 | + struct repository *r = the_repository; |
| 1524 | + |
| 1525 | + /* This task is only applicable with a VFS/Scalar shared cache. */ |
| 1526 | + if (!shared_object_dir) |
| 1527 | + return 0; |
| 1528 | + |
| 1529 | + /* If the dest is the same as the local odb path then we do nothing. */ |
| 1530 | + if (!fspathcmp(r->objects->odb->path, shared_object_dir)) |
| 1531 | + goto cleanup; |
| 1532 | + |
| 1533 | + strbuf_addf(&dstdir, "%s/pack", shared_object_dir); |
| 1534 | + |
| 1535 | + for_each_file_in_pack_dir(r->objects->odb->path, move_pack_to_shared_cache, |
| 1536 | + dstdir.buf); |
| 1537 | + |
| 1538 | + for_each_loose_object(move_loose_object_to_shared_cache, NULL, |
| 1539 | + FOR_EACH_OBJECT_LOCAL_ONLY); |
| 1540 | + |
| 1541 | +cleanup: |
| 1542 | + strbuf_release(&dstdir); |
| 1543 | + return 0; |
| 1544 | +} |
| 1545 | + |
1361 | 1546 | typedef int maintenance_task_fn(struct maintenance_run_opts *opts,
|
1362 | 1547 | struct gc_config *cfg);
|
1363 | 1548 |
|
@@ -1387,6 +1572,7 @@ enum maintenance_task_label {
|
1387 | 1572 | TASK_GC,
|
1388 | 1573 | TASK_COMMIT_GRAPH,
|
1389 | 1574 | TASK_PACK_REFS,
|
| 1575 | + TASK_CACHE_LOCAL_OBJS, |
1390 | 1576 |
|
1391 | 1577 | /* Leave as final value */
|
1392 | 1578 | TASK__COUNT
|
@@ -1423,6 +1609,10 @@ static struct maintenance_task tasks[] = {
|
1423 | 1609 | maintenance_task_pack_refs,
|
1424 | 1610 | pack_refs_condition,
|
1425 | 1611 | },
|
| 1612 | + [TASK_CACHE_LOCAL_OBJS] = { |
| 1613 | + "cache-local-objects", |
| 1614 | + maintenance_task_cache_local_objs, |
| 1615 | + }, |
1426 | 1616 | };
|
1427 | 1617 |
|
1428 | 1618 | static int compare_tasks_by_selection(const void *a_, const void *b_)
|
@@ -1517,6 +1707,8 @@ static void initialize_maintenance_strategy(void)
|
1517 | 1707 | tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
|
1518 | 1708 | tasks[TASK_PACK_REFS].enabled = 1;
|
1519 | 1709 | tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
|
| 1710 | + tasks[TASK_CACHE_LOCAL_OBJS].enabled = 1; |
| 1711 | + tasks[TASK_CACHE_LOCAL_OBJS].schedule = SCHEDULE_WEEKLY; |
1520 | 1712 | }
|
1521 | 1713 | }
|
1522 | 1714 |
|
@@ -1634,8 +1826,8 @@ static int maintenance_run(int argc, const char **argv, const char *prefix,
|
1634 | 1826 | */
|
1635 | 1827 | if (!git_config_get_value("gvfs.sharedcache", &tmp_obj_dir) &&
|
1636 | 1828 | tmp_obj_dir) {
|
1637 |
| - object_dir = xstrdup(tmp_obj_dir); |
1638 |
| - setenv(DB_ENVIRONMENT, object_dir, 1); |
| 1829 | + shared_object_dir = xstrdup(tmp_obj_dir); |
| 1830 | + setenv(DB_ENVIRONMENT, shared_object_dir, 1); |
1639 | 1831 | }
|
1640 | 1832 |
|
1641 | 1833 | ret = maintenance_run_tasks(&opts, &cfg);
|
|
0 commit comments