|
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" |
@@ -263,6 +265,7 @@ enum maintenance_task_label { |
263 | 265 | TASK_REFLOG_EXPIRE, |
264 | 266 | TASK_WORKTREE_PRUNE, |
265 | 267 | TASK_RERERE_GC, |
| 268 | + TASK_CACHE_LOCAL_OBJS, |
266 | 269 |
|
267 | 270 | /* Leave as final value */ |
268 | 271 | TASK__COUNT |
@@ -1696,6 +1699,186 @@ static int geometric_repack_auto_condition(struct gc_config *cfg UNUSED) |
1696 | 1699 | return ret; |
1697 | 1700 | } |
1698 | 1701 |
|
| 1702 | +static void link_or_copy_or_die(const char *src, const char *dst) |
| 1703 | +{ |
| 1704 | + if (!link(src, dst)) |
| 1705 | + return; |
| 1706 | + |
| 1707 | + /* Use copy operation if src and dst are on different file systems. */ |
| 1708 | + if (errno != EXDEV) |
| 1709 | + warning_errno(_("failed to link '%s' to '%s'"), src, dst); |
| 1710 | + |
| 1711 | + if (copy_file(dst, src, 0444)) |
| 1712 | + die_errno(_("failed to copy '%s' to '%s'"), src, dst); |
| 1713 | +} |
| 1714 | + |
| 1715 | +static void rename_or_copy_or_die(const char *src, const char *dst) |
| 1716 | +{ |
| 1717 | + if (!rename(src, dst)) |
| 1718 | + return; |
| 1719 | + |
| 1720 | + /* Use copy and delete if src and dst are on different file systems. */ |
| 1721 | + if (errno != EXDEV) |
| 1722 | + warning_errno(_("failed to move '%s' to '%s'"), src, dst); |
| 1723 | + |
| 1724 | + if (copy_file(dst, src, 0444)) |
| 1725 | + die_errno(_("failed to copy '%s' to '%s'"), src, dst); |
| 1726 | + |
| 1727 | + if (unlink(src)) |
| 1728 | + die_errno(_("failed to delete '%s'"), src); |
| 1729 | +} |
| 1730 | + |
| 1731 | +static void migrate_pack(const char *srcdir, const char *dstdir, |
| 1732 | + const char *pack_filename) |
| 1733 | +{ |
| 1734 | + size_t basenamelen, srclen, dstlen; |
| 1735 | + struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT; |
| 1736 | + struct { |
| 1737 | + const char *ext; |
| 1738 | + unsigned move:1; |
| 1739 | + } files[] = { |
| 1740 | + {".pack", 0}, |
| 1741 | + {".keep", 0}, |
| 1742 | + {".rev", 0}, |
| 1743 | + {".idx", 1}, /* The index file must be atomically moved last. */ |
| 1744 | + }; |
| 1745 | + |
| 1746 | + trace2_region_enter("maintenance", "migrate_pack", the_repository); |
| 1747 | + |
| 1748 | + basenamelen = strlen(pack_filename) - 5; /* .pack */ |
| 1749 | + strbuf_addstr(&src, srcdir); |
| 1750 | + strbuf_addch(&src, '/'); |
| 1751 | + strbuf_add(&src, pack_filename, basenamelen); |
| 1752 | + strbuf_addstr(&src, ".idx"); |
| 1753 | + |
| 1754 | + /* A pack without an index file is not yet ready to be migrated. */ |
| 1755 | + if (!file_exists(src.buf)) |
| 1756 | + goto cleanup; |
| 1757 | + |
| 1758 | + strbuf_setlen(&src, src.len - 4 /* .idx */); |
| 1759 | + strbuf_addstr(&dst, dstdir); |
| 1760 | + strbuf_addch(&dst, '/'); |
| 1761 | + strbuf_add(&dst, pack_filename, basenamelen); |
| 1762 | + |
| 1763 | + srclen = src.len; |
| 1764 | + dstlen = dst.len; |
| 1765 | + |
| 1766 | + /* Move or copy files from the source directory to the destination. */ |
| 1767 | + for (size_t i = 0; i < ARRAY_SIZE(files); i++) { |
| 1768 | + strbuf_setlen(&src, srclen); |
| 1769 | + strbuf_addstr(&src, files[i].ext); |
| 1770 | + |
| 1771 | + if (!file_exists(src.buf)) |
| 1772 | + continue; |
| 1773 | + |
| 1774 | + strbuf_setlen(&dst, dstlen); |
| 1775 | + strbuf_addstr(&dst, files[i].ext); |
| 1776 | + |
| 1777 | + if (files[i].move) |
| 1778 | + rename_or_copy_or_die(src.buf, dst.buf); |
| 1779 | + else |
| 1780 | + link_or_copy_or_die(src.buf, dst.buf); |
| 1781 | + } |
| 1782 | + |
| 1783 | + /* |
| 1784 | + * Now the pack and all associated files exist at the destination we can |
| 1785 | + * now clean up the files in the source directory. |
| 1786 | + */ |
| 1787 | + for (size_t i = 0; i < ARRAY_SIZE(files); i++) { |
| 1788 | + /* Files that were moved rather than copied have no clean up. */ |
| 1789 | + if (files[i].move) |
| 1790 | + continue; |
| 1791 | + |
| 1792 | + strbuf_setlen(&src, srclen); |
| 1793 | + strbuf_addstr(&src, files[i].ext); |
| 1794 | + |
| 1795 | + /* Files that never existed in originally have no clean up.*/ |
| 1796 | + if (!file_exists(src.buf)) |
| 1797 | + continue; |
| 1798 | + |
| 1799 | + if (unlink(src.buf)) |
| 1800 | + warning_errno(_("failed to delete '%s'"), src.buf); |
| 1801 | + } |
| 1802 | + |
| 1803 | +cleanup: |
| 1804 | + strbuf_release(&src); |
| 1805 | + strbuf_release(&dst); |
| 1806 | + |
| 1807 | + trace2_region_leave("maintenance", "migrate_pack", the_repository); |
| 1808 | +} |
| 1809 | + |
| 1810 | +static void move_pack_to_shared_cache(const char *full_path, size_t full_path_len, |
| 1811 | + const char *file_name, void *data) |
| 1812 | +{ |
| 1813 | + char *srcdir; |
| 1814 | + const char *dstdir = (const char *)data; |
| 1815 | + |
| 1816 | + /* We only care about the actual pack files here. |
| 1817 | + * The associated .idx, .keep, .rev files will be copied in tandem |
| 1818 | + * with the pack file, with the index file being moved last. |
| 1819 | + * The original locations of the non-index files will only deleted |
| 1820 | + * once all other files have been copied/moved. |
| 1821 | + */ |
| 1822 | + if (!ends_with(file_name, ".pack")) |
| 1823 | + return; |
| 1824 | + |
| 1825 | + srcdir = xstrndup(full_path, full_path_len - strlen(file_name) - 1); |
| 1826 | + |
| 1827 | + migrate_pack(srcdir, dstdir, file_name); |
| 1828 | + |
| 1829 | + free(srcdir); |
| 1830 | +} |
| 1831 | + |
| 1832 | +static int move_loose_object_to_shared_cache(const struct object_id *oid, |
| 1833 | + const char *path, |
| 1834 | + UNUSED void *data) |
| 1835 | +{ |
| 1836 | + struct stat st; |
| 1837 | + struct strbuf dst = STRBUF_INIT; |
| 1838 | + char *hex = oid_to_hex(oid); |
| 1839 | + |
| 1840 | + strbuf_addf(&dst, "%s/%.2s/", shared_object_dir, hex); |
| 1841 | + |
| 1842 | + if (stat(dst.buf, &st)) { |
| 1843 | + if (mkdir(dst.buf, 0777)) |
| 1844 | + die_errno(_("failed to create directory '%s'"), dst.buf); |
| 1845 | + } else if (!S_ISDIR(st.st_mode)) |
| 1846 | + die(_("expected '%s' to be a directory"), dst.buf); |
| 1847 | + |
| 1848 | + strbuf_addstr(&dst, hex+2); |
| 1849 | + rename_or_copy_or_die(path, dst.buf); |
| 1850 | + |
| 1851 | + strbuf_release(&dst); |
| 1852 | + return 0; |
| 1853 | +} |
| 1854 | + |
| 1855 | +static int maintenance_task_cache_local_objs(UNUSED struct maintenance_run_opts *opts, |
| 1856 | + UNUSED struct gc_config *cfg) |
| 1857 | +{ |
| 1858 | + struct strbuf dstdir = STRBUF_INIT; |
| 1859 | + struct repository *r = the_repository; |
| 1860 | + |
| 1861 | + /* This task is only applicable with a VFS/Scalar shared cache. */ |
| 1862 | + if (!shared_object_dir) |
| 1863 | + return 0; |
| 1864 | + |
| 1865 | + /* If the dest is the same as the local odb path then we do nothing. */ |
| 1866 | + if (!fspathcmp(r->objects->sources->path, shared_object_dir)) |
| 1867 | + goto cleanup; |
| 1868 | + |
| 1869 | + strbuf_addf(&dstdir, "%s/pack", shared_object_dir); |
| 1870 | + |
| 1871 | + for_each_file_in_pack_dir(r->objects->sources->path, move_pack_to_shared_cache, |
| 1872 | + dstdir.buf); |
| 1873 | + |
| 1874 | + for_each_loose_object(r->objects, move_loose_object_to_shared_cache, NULL, |
| 1875 | + FOR_EACH_OBJECT_LOCAL_ONLY); |
| 1876 | + |
| 1877 | +cleanup: |
| 1878 | + strbuf_release(&dstdir); |
| 1879 | + return 0; |
| 1880 | +} |
| 1881 | + |
1699 | 1882 | typedef int (*maintenance_task_fn)(struct maintenance_run_opts *opts, |
1700 | 1883 | struct gc_config *cfg); |
1701 | 1884 | typedef int (*maintenance_auto_fn)(struct gc_config *cfg); |
@@ -1774,6 +1957,10 @@ static const struct maintenance_task tasks[] = { |
1774 | 1957 | .background = maintenance_task_rerere_gc, |
1775 | 1958 | .auto_condition = rerere_gc_condition, |
1776 | 1959 | }, |
| 1960 | + [TASK_CACHE_LOCAL_OBJS] = { |
| 1961 | + "cache-local-objects", |
| 1962 | + maintenance_task_cache_local_objs, |
| 1963 | + }, |
1777 | 1964 | }; |
1778 | 1965 |
|
1779 | 1966 | enum task_phase { |
@@ -1900,6 +2087,10 @@ static const struct maintenance_strategy incremental_strategy = { |
1900 | 2087 | .type = MAINTENANCE_TYPE_SCHEDULED, |
1901 | 2088 | .schedule = SCHEDULE_WEEKLY, |
1902 | 2089 | }, |
| 2090 | + [TASK_CACHE_LOCAL_OBJS] = { |
| 2091 | + .type = MAINTENANCE_TYPE_SCHEDULED, |
| 2092 | + .schedule = SCHEDULE_WEEKLY, |
| 2093 | + }, |
1903 | 2094 | /* |
1904 | 2095 | * Historically, the "incremental" strategy was only available |
1905 | 2096 | * in the context of scheduled maintenance when set up via |
|
0 commit comments