|
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"
|
| 19 | +#include "copy.h" |
18 | 20 | #include "date.h"
|
19 | 21 | #include "dir.h"
|
20 | 22 | #include "environment.h"
|
@@ -262,6 +264,7 @@ enum maintenance_task_label {
|
262 | 264 | TASK_REFLOG_EXPIRE,
|
263 | 265 | TASK_WORKTREE_PRUNE,
|
264 | 266 | TASK_RERERE_GC,
|
| 267 | + TASK_CACHE_LOCAL_OBJS, |
265 | 268 |
|
266 | 269 | /* Leave as final value */
|
267 | 270 | TASK__COUNT
|
@@ -1561,6 +1564,186 @@ static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts
|
1561 | 1564 | return 0;
|
1562 | 1565 | }
|
1563 | 1566 |
|
| 1567 | +static void link_or_copy_or_die(const char *src, const char *dst) |
| 1568 | +{ |
| 1569 | + if (!link(src, dst)) |
| 1570 | + return; |
| 1571 | + |
| 1572 | + /* Use copy operation if src and dst are on different file systems. */ |
| 1573 | + if (errno != EXDEV) |
| 1574 | + warning_errno(_("failed to link '%s' to '%s'"), src, dst); |
| 1575 | + |
| 1576 | + if (copy_file(dst, src, 0444)) |
| 1577 | + die_errno(_("failed to copy '%s' to '%s'"), src, dst); |
| 1578 | +} |
| 1579 | + |
| 1580 | +static void rename_or_copy_or_die(const char *src, const char *dst) |
| 1581 | +{ |
| 1582 | + if (!rename(src, dst)) |
| 1583 | + return; |
| 1584 | + |
| 1585 | + /* Use copy and delete if src and dst are on different file systems. */ |
| 1586 | + if (errno != EXDEV) |
| 1587 | + warning_errno(_("failed to move '%s' to '%s'"), src, dst); |
| 1588 | + |
| 1589 | + if (copy_file(dst, src, 0444)) |
| 1590 | + die_errno(_("failed to copy '%s' to '%s'"), src, dst); |
| 1591 | + |
| 1592 | + if (unlink(src)) |
| 1593 | + die_errno(_("failed to delete '%s'"), src); |
| 1594 | +} |
| 1595 | + |
| 1596 | +static void migrate_pack(const char *srcdir, const char *dstdir, |
| 1597 | + const char *pack_filename) |
| 1598 | +{ |
| 1599 | + size_t basenamelen, srclen, dstlen; |
| 1600 | + struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT; |
| 1601 | + struct { |
| 1602 | + const char *ext; |
| 1603 | + unsigned move:1; |
| 1604 | + } files[] = { |
| 1605 | + {".pack", 0}, |
| 1606 | + {".keep", 0}, |
| 1607 | + {".rev", 0}, |
| 1608 | + {".idx", 1}, /* The index file must be atomically moved last. */ |
| 1609 | + }; |
| 1610 | + |
| 1611 | + trace2_region_enter("maintenance", "migrate_pack", the_repository); |
| 1612 | + |
| 1613 | + basenamelen = strlen(pack_filename) - 5; /* .pack */ |
| 1614 | + strbuf_addstr(&src, srcdir); |
| 1615 | + strbuf_addch(&src, '/'); |
| 1616 | + strbuf_add(&src, pack_filename, basenamelen); |
| 1617 | + strbuf_addstr(&src, ".idx"); |
| 1618 | + |
| 1619 | + /* A pack without an index file is not yet ready to be migrated. */ |
| 1620 | + if (!file_exists(src.buf)) |
| 1621 | + goto cleanup; |
| 1622 | + |
| 1623 | + strbuf_setlen(&src, src.len - 4 /* .idx */); |
| 1624 | + strbuf_addstr(&dst, dstdir); |
| 1625 | + strbuf_addch(&dst, '/'); |
| 1626 | + strbuf_add(&dst, pack_filename, basenamelen); |
| 1627 | + |
| 1628 | + srclen = src.len; |
| 1629 | + dstlen = dst.len; |
| 1630 | + |
| 1631 | + /* Move or copy files from the source directory to the destination. */ |
| 1632 | + for (size_t i = 0; i < ARRAY_SIZE(files); i++) { |
| 1633 | + strbuf_setlen(&src, srclen); |
| 1634 | + strbuf_addstr(&src, files[i].ext); |
| 1635 | + |
| 1636 | + if (!file_exists(src.buf)) |
| 1637 | + continue; |
| 1638 | + |
| 1639 | + strbuf_setlen(&dst, dstlen); |
| 1640 | + strbuf_addstr(&dst, files[i].ext); |
| 1641 | + |
| 1642 | + if (files[i].move) |
| 1643 | + rename_or_copy_or_die(src.buf, dst.buf); |
| 1644 | + else |
| 1645 | + link_or_copy_or_die(src.buf, dst.buf); |
| 1646 | + } |
| 1647 | + |
| 1648 | + /* |
| 1649 | + * Now the pack and all associated files exist at the destination we can |
| 1650 | + * now clean up the files in the source directory. |
| 1651 | + */ |
| 1652 | + for (size_t i = 0; i < ARRAY_SIZE(files); i++) { |
| 1653 | + /* Files that were moved rather than copied have no clean up. */ |
| 1654 | + if (files[i].move) |
| 1655 | + continue; |
| 1656 | + |
| 1657 | + strbuf_setlen(&src, srclen); |
| 1658 | + strbuf_addstr(&src, files[i].ext); |
| 1659 | + |
| 1660 | + /* Files that never existed in originally have no clean up.*/ |
| 1661 | + if (!file_exists(src.buf)) |
| 1662 | + continue; |
| 1663 | + |
| 1664 | + if (unlink(src.buf)) |
| 1665 | + warning_errno(_("failed to delete '%s'"), src.buf); |
| 1666 | + } |
| 1667 | + |
| 1668 | +cleanup: |
| 1669 | + strbuf_release(&src); |
| 1670 | + strbuf_release(&dst); |
| 1671 | + |
| 1672 | + trace2_region_leave("maintenance", "migrate_pack", the_repository); |
| 1673 | +} |
| 1674 | + |
| 1675 | +static void move_pack_to_shared_cache(const char *full_path, size_t full_path_len, |
| 1676 | + const char *file_name, void *data) |
| 1677 | +{ |
| 1678 | + char *srcdir; |
| 1679 | + const char *dstdir = (const char *)data; |
| 1680 | + |
| 1681 | + /* We only care about the actual pack files here. |
| 1682 | + * The associated .idx, .keep, .rev files will be copied in tandem |
| 1683 | + * with the pack file, with the index file being moved last. |
| 1684 | + * The original locations of the non-index files will only deleted |
| 1685 | + * once all other files have been copied/moved. |
| 1686 | + */ |
| 1687 | + if (!ends_with(file_name, ".pack")) |
| 1688 | + return; |
| 1689 | + |
| 1690 | + srcdir = xstrndup(full_path, full_path_len - strlen(file_name) - 1); |
| 1691 | + |
| 1692 | + migrate_pack(srcdir, dstdir, file_name); |
| 1693 | + |
| 1694 | + free(srcdir); |
| 1695 | +} |
| 1696 | + |
| 1697 | +static int move_loose_object_to_shared_cache(const struct object_id *oid, |
| 1698 | + const char *path, |
| 1699 | + UNUSED void *data) |
| 1700 | +{ |
| 1701 | + struct stat st; |
| 1702 | + struct strbuf dst = STRBUF_INIT; |
| 1703 | + char *hex = oid_to_hex(oid); |
| 1704 | + |
| 1705 | + strbuf_addf(&dst, "%s/%.2s/", shared_object_dir, hex); |
| 1706 | + |
| 1707 | + if (stat(dst.buf, &st)) { |
| 1708 | + if (mkdir(dst.buf, 0777)) |
| 1709 | + die_errno(_("failed to create directory '%s'"), dst.buf); |
| 1710 | + } else if (!S_ISDIR(st.st_mode)) |
| 1711 | + die(_("expected '%s' to be a directory"), dst.buf); |
| 1712 | + |
| 1713 | + strbuf_addstr(&dst, hex+2); |
| 1714 | + rename_or_copy_or_die(path, dst.buf); |
| 1715 | + |
| 1716 | + strbuf_release(&dst); |
| 1717 | + return 0; |
| 1718 | +} |
| 1719 | + |
| 1720 | +static int maintenance_task_cache_local_objs(UNUSED struct maintenance_run_opts *opts, |
| 1721 | + UNUSED struct gc_config *cfg) |
| 1722 | +{ |
| 1723 | + struct strbuf dstdir = STRBUF_INIT; |
| 1724 | + struct repository *r = the_repository; |
| 1725 | + |
| 1726 | + /* This task is only applicable with a VFS/Scalar shared cache. */ |
| 1727 | + if (!shared_object_dir) |
| 1728 | + return 0; |
| 1729 | + |
| 1730 | + /* If the dest is the same as the local odb path then we do nothing. */ |
| 1731 | + if (!fspathcmp(r->objects->sources->path, shared_object_dir)) |
| 1732 | + goto cleanup; |
| 1733 | + |
| 1734 | + strbuf_addf(&dstdir, "%s/pack", shared_object_dir); |
| 1735 | + |
| 1736 | + for_each_file_in_pack_dir(r->objects->sources->path, move_pack_to_shared_cache, |
| 1737 | + dstdir.buf); |
| 1738 | + |
| 1739 | + for_each_loose_object(move_loose_object_to_shared_cache, NULL, |
| 1740 | + FOR_EACH_OBJECT_LOCAL_ONLY); |
| 1741 | + |
| 1742 | +cleanup: |
| 1743 | + strbuf_release(&dstdir); |
| 1744 | + return 0; |
| 1745 | +} |
| 1746 | + |
1564 | 1747 | typedef int (*maintenance_task_fn)(struct maintenance_run_opts *opts,
|
1565 | 1748 | struct gc_config *cfg);
|
1566 | 1749 | typedef int (*maintenance_auto_fn)(struct gc_config *cfg);
|
@@ -1634,6 +1817,10 @@ static const struct maintenance_task tasks[] = {
|
1634 | 1817 | .background = maintenance_task_rerere_gc,
|
1635 | 1818 | .auto_condition = rerere_gc_condition,
|
1636 | 1819 | },
|
| 1820 | + [TASK_CACHE_LOCAL_OBJS] = { |
| 1821 | + "cache-local-objects", |
| 1822 | + maintenance_task_cache_local_objs, |
| 1823 | + }, |
1637 | 1824 | };
|
1638 | 1825 |
|
1639 | 1826 | enum task_phase {
|
@@ -1738,6 +1925,8 @@ static const struct maintenance_strategy incremental_strategy = {
|
1738 | 1925 | [TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY,
|
1739 | 1926 | [TASK_PACK_REFS].enabled = 1,
|
1740 | 1927 | [TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY,
|
| 1928 | + [TASK_CACHE_LOCAL_OBJS].enabled = 1, |
| 1929 | + [TASK_CACHE_LOCAL_OBJS].schedule = SCHEDULE_WEEKLY, |
1741 | 1930 | },
|
1742 | 1931 | };
|
1743 | 1932 |
|
|
0 commit comments