Skip to content

add CACHEDIR.TAG to directories that should not be backed up #4283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Artifacts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Tar: can_symlink
using FileWatching: FileWatching

import ..set_readonly, ..GitTools, ..TOML, ..pkg_server, ..can_fancyprint,
..stderr_f, ..printpkgstyle, ..mv_temp_dir_retries
..stderr_f, ..printpkgstyle, ..mv_temp_dir_retries, ..create_cachedir_tag

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

# Temporary directory where we'll do our creation business
temp_dir = mktempdir(artifacts_dir)
Expand Down Expand Up @@ -347,6 +348,7 @@ function download_artifact(
# Ensure the `artifacts` directory exists in our default depot
artifacts_dir = first(artifacts_dirs())
mkpath(artifacts_dir)
create_cachedir_tag(artifacts_dir)
# expected artifact path
dst = joinpath(artifacts_dir, bytes2hex(tree_hash.bytes))

Expand Down
6 changes: 5 additions & 1 deletion src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using Base.BinaryPlatforms
import ...Pkg
import ...Pkg: pkg_server, Registry, pathrepr, can_fancyprint, printpkgstyle, stderr_f, OFFLINE_MODE
import ...Pkg: UPDATED_REGISTRY_THIS_SESSION, RESPECT_SYSIMAGE_VERSIONS, should_autoprecompile
import ...Pkg: usable_io
import ...Pkg: usable_io, create_cachedir_tag

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

tmp_objects = String[]
url_success = false
Expand Down Expand Up @@ -794,6 +795,7 @@ function install_git(
try
clones_dir = joinpath(depots1(), "clones")
ispath(clones_dir) || mkpath(clones_dir)
create_cachedir_tag(clones_dir)
repo_path = joinpath(clones_dir, string(uuid))
repo = GitTools.ensure_clone(io, repo_path, first(urls); isbare=true,
header = "[$uuid] $name from $(first(urls))")
Expand All @@ -816,6 +818,8 @@ function install_git(
tree isa LibGit2.GitTree ||
error("$name: git object $(string(hash)) should be a tree, not $(typeof(tree))")
mkpath(version_path)
@show abspath(joinpath(dirname(dirname(dirname(version_path)))))
create_cachedir_tag(joinpath(dirname(dirname(dirname(version_path))), "packages"))
GitTools.checkout_tree_to_path(repo, tree, version_path)
return
finally
Expand Down
11 changes: 11 additions & 0 deletions src/Pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ end
logdir(depot = depots1()) = joinpath(depot, "logs")
devdir(depot = depots1()) = get(ENV, "JULIA_PKG_DEVDIR", joinpath(depot, "dev"))
envdir(depot = depots1()) = joinpath(depot, "environments")

function create_cachedir_tag(cache_dir::AbstractString)
return try
tag_file = joinpath(cache_dir, "CACHEDIR.TAG")
if !isfile(tag_file)
write(tag_file, "Signature: 8a477f597d28d172789f06886806bc55\n# This file is a cache directory tag created by Julia Pkg.\n# See https://bford.info/cachedir/\n")
end
catch
# Ignore errors to avoid failing operations on read-only filesystems
end
end
const UPDATED_REGISTRY_THIS_SESSION = Ref(false)
const OFFLINE_MODE = Ref(false)
const RESPECT_SYSIMAGE_VERSIONS = Ref(true)
Expand Down
4 changes: 3 additions & 1 deletion src/Registry/Registry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Registry

import ..Pkg
using ..Pkg: depots, depots1, printpkgstyle, stderr_f, isdir_nothrow, pathrepr, pkg_server,
GitTools
GitTools, create_cachedir_tag
using ..Pkg.PlatformEngines: download_verify_unpack, download, download_verify, exe7z, verify_archive_tree_hash
using UUIDs, LibGit2, TOML, Dates
import FileWatching
Expand Down Expand Up @@ -177,6 +177,7 @@ function download_registries(io::IO, regs::Vector{RegistrySpec}, depots::Union{S
populate_known_registries_with_urls!(regs)
regdir = joinpath(target_depot, "registries")
isdir(regdir) || mkpath(regdir)
create_cachedir_tag(regdir)
# only allow one julia process to download and install registries at a time
FileWatching.mkpidlock(joinpath(regdir, ".pid"), stale_age = 10) do
# once we're pidlocked check if another process has installed any of the registries
Expand Down Expand Up @@ -403,6 +404,7 @@ function update(regs::Vector{RegistrySpec}; io::IO=stderr_f(), force::Bool=true,
depot_regs = isempty(regs) ? reachable_registries(; depots=depot) : regs
regdir = joinpath(depot, "registries")
isdir(regdir) || mkpath(regdir)
create_cachedir_tag(regdir)
# only allow one julia process to update registries in this depot at a time
FileWatching.mkpidlock(joinpath(regdir, ".pid"), stale_age = 10) do
errors = Tuple{String, String}[]
Expand Down
5 changes: 3 additions & 2 deletions src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Base.string

using TOML
import ..Pkg, ..Registry
import ..Pkg: GitTools, depots, depots1, logdir, set_readonly, safe_realpath, pkg_server, stdlib_dir, stdlib_path, isurl, stderr_f, RESPECT_SYSIMAGE_VERSIONS
import ..Pkg: GitTools, depots, depots1, logdir, set_readonly, safe_realpath, pkg_server, stdlib_dir, stdlib_path, isurl, stderr_f, RESPECT_SYSIMAGE_VERSIONS, create_cachedir_tag
import Base.BinaryPlatforms: Platform
using ..Pkg.Versions
import FileWatching
Expand Down Expand Up @@ -898,7 +898,7 @@ function handle_repo_add!(ctx::Context, pkg::PackageSpec)
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
repo_source_typed = repo_source::Union{Nothing,String}
GitTools.check_valid_HEAD(repo)

create_cachedir_tag(dirname(add_repo_cache_path(repo_source)))
# If the user didn't specify rev, assume they want the default (master) branch if on a branch, otherwise the current commit
if pkg.repo.rev === nothing
pkg.repo.rev = LibGit2.isattached(repo) ? LibGit2.branch(repo) : string(LibGit2.GitHash(LibGit2.head(repo)))
Expand Down Expand Up @@ -956,6 +956,7 @@ function handle_repo_add!(ctx::Context, pkg::PackageSpec)
# Otherwise, move the temporary path into its correct place and set read only
mkpath(version_path)
mv(temp_path, version_path; force=true)
create_cachedir_tag(joinpath(dirname(dirname(version_path)), "packages"))
set_readonly(version_path)
return true
end
Expand Down
4 changes: 4 additions & 0 deletions test/new.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Pkg._auto_gc_enabled[] = false
reg = regs[1]
@test reg.name == "General"
@test reg.uuid == general_uuid
# - Check that CACHEDIR.TAG files exist in cache directories
@test isfile(joinpath(LOADED_DEPOT, "registries", "CACHEDIR.TAG"))
@test isfile(joinpath(LOADED_DEPOT, "packages", "CACHEDIR.TAG"))
# - The package should be installed correctly.
source053, source053_time = nothing, nothing
Pkg.dependencies(exuuid) do pkg
Expand Down Expand Up @@ -90,6 +93,7 @@ Pkg._auto_gc_enabled[] = false
end
# Now check packages which track repos instead of registered versions
Pkg.add(url="https://github.com/JuliaLang/Example.jl", rev="v0.5.3")
@test isfile(joinpath(LOADED_DEPOT, "clones", "CACHEDIR.TAG"))
Pkg.dependencies(exuuid) do pkg
@test !pkg.is_tracking_registry
@test isdir(pkg.source)
Expand Down
Loading