Skip to content

Commit 1e0756d

Browse files
authored
add a cachedir file to various directories that shouldnt be backed up (#4438)
1 parent 4884d21 commit 1e0756d

File tree

8 files changed

+34
-8
lines changed

8 files changed

+34
-8
lines changed

src/Artifacts.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Tar: can_symlink
66
using FileWatching: FileWatching
77

88
import ..set_readonly, ..GitTools, ..TOML, ..pkg_server, ..can_fancyprint,
9-
..stderr_f, ..printpkgstyle, ..mv_temp_dir_retries, ..atomic_toml_write
9+
..stderr_f, ..printpkgstyle, ..mv_temp_dir_retries, ..atomic_toml_write, ..create_cachedir_tag
1010

1111
import Base: get, SHA1
1212
import Artifacts: artifact_names, ARTIFACTS_DIR_OVERRIDE, ARTIFACT_OVERRIDES, artifact_paths,
@@ -31,6 +31,7 @@ function create_artifact(f::Function)
3131
# Ensure the `artifacts` directory exists in our default depot
3232
artifacts_dir = first(artifacts_dirs())
3333
mkpath(artifacts_dir)
34+
create_cachedir_tag(artifacts_dir)
3435

3536
# Temporary directory where we'll do our creation business
3637
temp_dir = mktempdir(artifacts_dir)
@@ -346,6 +347,7 @@ function download_artifact(
346347
# Ensure the `artifacts` directory exists in our default depot
347348
artifacts_dir = first(artifacts_dirs())
348349
mkpath(artifacts_dir)
350+
create_cachedir_tag(artifacts_dir)
349351
# expected artifact path
350352
dst = joinpath(artifacts_dir, bytes2hex(tree_hash.bytes))
351353

src/Operations.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ using Base.BinaryPlatforms
1616
import ...Pkg
1717
import ...Pkg: pkg_server, Registry, pathrepr, can_fancyprint, printpkgstyle, stderr_f, OFFLINE_MODE
1818
import ...Pkg: UPDATED_REGISTRY_THIS_SESSION, RESPECT_SYSIMAGE_VERSIONS, should_autoprecompile
19-
import ...Pkg: usable_io, discover_repo
19+
import ...Pkg: usable_io, discover_repo, create_cachedir_tag
2020

2121
#########
2222
# Utils #
@@ -805,6 +805,7 @@ function install_archive(
805805
# files are on a different fs. So use a temp dir in the same depot dir as some systems might
806806
# be serving different parts of the depot on different filesystems via links i.e. pkgeval does this.
807807
depot_temp = mkpath(joinpath(dirname(dirname(version_path)), "temp")) # .julia/packages/temp
808+
create_cachedir_tag(dirname(dirname(version_path)))
808809

809810
tmp_objects = String[]
810811
url_success = false
@@ -886,6 +887,7 @@ function install_git(
886887
try
887888
clones_dir = joinpath(depots1(), "clones")
888889
ispath(clones_dir) || mkpath(clones_dir)
890+
create_cachedir_tag(clones_dir)
889891
repo_path = joinpath(clones_dir, string(uuid))
890892
first_url = first(urls)
891893
repo = GitTools.ensure_clone(
@@ -912,6 +914,7 @@ function install_git(
912914
tree isa LibGit2.GitTree ||
913915
error("$name: git object $(string(hash)) should be a tree, not $(typeof(tree))")
914916
mkpath(version_path)
917+
create_cachedir_tag(dirname(dirname(version_path)))
915918
GitTools.checkout_tree_to_path(repo, tree, version_path)
916919
return
917920
finally
@@ -1541,6 +1544,7 @@ function build_versions(ctx::Context, uuids::Set{UUID}; verbose = false, allow_r
15411544
key = string(entry.tree_hash)
15421545
scratch = joinpath(pkg_scratchpath(), key)
15431546
mkpath(scratch)
1547+
create_cachedir_tag(joinpath(depots1(), "scratchspaces"))
15441548
log_file = joinpath(scratch, "build.log")
15451549
# Associate the logfile with the package being built
15461550
dict = Dict{String, Any}(

src/Pkg.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ end
4646
logdir(depot = depots1()) = joinpath(depot, "logs")
4747
devdir(depot = depots1()) = get(ENV, "JULIA_PKG_DEVDIR", joinpath(depot, "dev"))
4848
envdir(depot = depots1()) = joinpath(depot, "environments")
49+
50+
function create_cachedir_tag(cache_dir::AbstractString)
51+
return try
52+
tag_file = joinpath(cache_dir, "CACHEDIR.TAG")
53+
if !isfile(tag_file)
54+
write(tag_file, "Signature: 8a477f597d28d172789f06886806bc55\n# This file is a cache directory tag created by Julia Pkg.\n# See https://bford.info/cachedir/\n")
55+
end
56+
catch
57+
# Ignore errors to avoid failing operations on read-only filesystems
58+
end
59+
end
4960
const UPDATED_REGISTRY_THIS_SESSION = Ref(false)
5061
const OFFLINE_MODE = Ref(false)
5162
const RESPECT_SYSIMAGE_VERSIONS = Ref(true)

src/Registry/Registry.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module Registry
4040

4141
import ..Pkg
4242
using ..Pkg: depots, depots1, printpkgstyle, stderr_f, isdir_nothrow, pathrepr, pkg_server,
43-
GitTools, atomic_toml_write
43+
GitTools, atomic_toml_write, create_cachedir_tag
4444
using ..Pkg.PlatformEngines: download_verify_unpack, download, download_verify, exe7z, verify_archive_tree_hash
4545
using UUIDs, LibGit2, TOML, Dates
4646
import FileWatching
@@ -247,6 +247,7 @@ function download_registries(io::IO, regs::Vector{RegistrySpec}, depots::Union{S
247247
registry_update_log = get_registry_update_log()
248248
regdir = joinpath(target_depot, "registries")
249249
isdir(regdir) || mkpath(regdir)
250+
create_cachedir_tag(regdir)
250251
# only allow one julia process to download and install registries at a time
251252
FileWatching.mkpidlock(joinpath(regdir, ".pid"), stale_age = 10) do
252253
# once we're pidlocked check if another process has installed any of the registries
@@ -459,6 +460,7 @@ end
459460
function save_registry_update_log(d::Dict)
460461
pkg_scratch_space = joinpath(DEPOT_PATH[1], "scratchspaces", "44cfe95a-1eb2-52ea-b672-e2afdf69b78f")
461462
mkpath(pkg_scratch_space)
463+
create_cachedir_tag(joinpath(DEPOT_PATH[1], "scratchspaces"))
462464
pkg_reg_updated_file = joinpath(pkg_scratch_space, "registry_updates.toml")
463465
return atomic_toml_write(pkg_reg_updated_file, d)
464466
end
@@ -493,6 +495,7 @@ function update(regs::Vector{RegistrySpec}; io::IO = stderr_f(), force::Bool = t
493495
depot_regs = isempty(regs) ? reachable_registries(; depots = depot) : regs
494496
regdir = joinpath(depot, "registries")
495497
isdir(regdir) || mkpath(regdir)
498+
create_cachedir_tag(regdir)
496499
# only allow one julia process to update registries in this depot at a time
497500
FileWatching.mkpidlock(joinpath(regdir, ".pid"), stale_age = 10) do
498501
errors = Tuple{String, String}[]

src/Types.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Base.string
1010

1111
using TOML
1212
import ..Pkg, ..Registry
13-
import ..Pkg: GitTools, depots, depots1, logdir, set_readonly, safe_realpath, pkg_server, stdlib_dir, stdlib_path, isurl, stderr_f, RESPECT_SYSIMAGE_VERSIONS, atomic_toml_write
13+
import ..Pkg: GitTools, depots, depots1, logdir, set_readonly, safe_realpath, pkg_server, stdlib_dir, stdlib_path, isurl, stderr_f, RESPECT_SYSIMAGE_VERSIONS, atomic_toml_write, create_cachedir_tag
1414
import Base.BinaryPlatforms: Platform
1515
using ..Pkg.Versions
1616
import FileWatching
@@ -955,7 +955,7 @@ function handle_repo_add!(ctx::Context, pkg::PackageSpec)
955955
LibGit2.with(GitTools.ensure_clone(ctx.io, add_repo_cache_path(repo_source::Union{Nothing, String}), repo_source::Union{Nothing, String}; isbare = true)) do repo
956956
repo_source_typed = repo_source::Union{Nothing, String}
957957
GitTools.check_valid_HEAD(repo)
958-
958+
create_cachedir_tag(dirname(add_repo_cache_path(repo_source)))
959959
# If the user didn't specify rev, assume they want the default (master) branch if on a branch, otherwise the current commit
960960
if pkg.repo.rev === nothing
961961
pkg.repo.rev = LibGit2.isattached(repo) ? LibGit2.branch(repo) : string(LibGit2.GitHash(LibGit2.head(repo)))
@@ -1020,6 +1020,7 @@ function handle_repo_add!(ctx::Context, pkg::PackageSpec)
10201020
# Otherwise, move the temporary path into its correct place and set read only
10211021
mkpath(version_path)
10221022
mv(temp_path, version_path; force = true)
1023+
create_cachedir_tag(dirname(dirname(version_path)))
10231024
set_readonly(version_path)
10241025
return true
10251026
end

test/new.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ Pkg._auto_gc_enabled[] = false
4646
reg = regs[1]
4747
@test reg.name == "General"
4848
@test reg.uuid == general_uuid
49+
# - Check that CACHEDIR.TAG files exist in cache directories
50+
@test isfile(joinpath(LOADED_DEPOT, "registries", "CACHEDIR.TAG"))
51+
@test isfile(joinpath(LOADED_DEPOT, "packages", "CACHEDIR.TAG"))
4952
# - The package should be installed correctly.
5053
source053, source053_time = nothing, nothing
5154
Pkg.dependencies(exuuid) do pkg
@@ -90,6 +93,7 @@ Pkg._auto_gc_enabled[] = false
9093
end
9194
# Now check packages which track repos instead of registered versions
9295
Pkg.add(url = "https://github.com/JuliaLang/Example.jl", rev = "v0.5.3")
96+
@test isfile(joinpath(LOADED_DEPOT, "clones", "CACHEDIR.TAG"))
9397
Pkg.dependencies(exuuid) do pkg
9498
@test !pkg.is_tracking_registry
9599
@test isdir(pkg.source)
@@ -2728,6 +2732,7 @@ end
27282732
"44cfe95a-1eb2-52ea-b672-e2afdf69b78f", "f99d57aad0e5eb2434491b47bac92bb88d463001", "build.log"
27292733
)
27302734
@test isfile(log_file_add)
2735+
@test isfile(joinpath(DEPOT_PATH[1], "scratchspaces", "CACHEDIR.TAG"))
27312736
@test occursin("oops", read(log_file_add, String))
27322737
end
27332738
end

test/pkg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ temp_pkg_dir() do project_path
189189

190190
# Test that unused packages are reaped
191191
Pkg.gc()
192-
@test isempty(readdir(pkgdir))
192+
@test isempty(filter(x -> x != "CACHEDIR.TAG", readdir(pkgdir)))
193193

194194
clonedir = joinpath(Pkg.depots1(), "clones")
195195
Pkg.add(Pkg.PackageSpec(name = TEST_PKG.name, rev = "master"))
196196
@test !isempty(readdir(clonedir))
197197
Pkg.rm(TEST_PKG.name)
198198
# Test that unused repos are also reaped
199199
Pkg.gc()
200-
@test isempty(readdir(clonedir))
200+
@test isempty(filter(x -> x != "CACHEDIR.TAG", readdir(clonedir)))
201201
end
202202

203203
@testset "package with wrong UUID" begin

test/registry.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ if Pkg.Registry.registry_use_pkg_server()
443443
end
444444
Pkg.update()
445445
Pkg.Registry.rm(name = "General")
446-
@test isempty(readdir(joinpath(DEPOT_PATH[1], "registries")))
446+
@test isempty(filter(x -> x != "CACHEDIR.TAG", readdir(joinpath(DEPOT_PATH[1], "registries"))))
447447
end
448448
end
449449
end

0 commit comments

Comments
 (0)