From 529513bf027d594794de789d0484c090f294eea0 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 20 Nov 2022 10:53:31 -0500 Subject: [PATCH] Add CACHEDIR.TAG to .ruff_cache (#830) --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/cache.rs | 26 ++++++++++++++++++++------ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 276a8b6590..cd6b925c2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -397,6 +397,15 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c" +[[package]] +name = "cachedir" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e236bf5873ea57ec2877445297f4da008916bfae51567131acfc54a073d694f3" +dependencies = [ + "tempfile", +] + [[package]] name = "cast" version = "0.3.0" @@ -2257,6 +2266,7 @@ dependencies = [ "bincode", "bitflags", "cacache", + "cachedir", "chrono", "clap 4.0.22", "clearscreen", diff --git a/Cargo.toml b/Cargo.toml index 1692e06440..d81b24829c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ anyhow = { version = "1.0.66" } atty = { version = "0.2.14" } bincode = { version = "1.3.3" } bitflags = { version = "1.3.2" } +cachedir = { version = "0.3.0" } chrono = { version = "0.4.21", default-features = false, features = ["clock"] } clap = { version = "4.0.1", features = ["derive"] } colored = { version = "2.0.0" } diff --git a/src/cache.rs b/src/cache.rs index d3c7bfbe04..f85623c187 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -94,16 +94,29 @@ fn cache_key(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> String ) } +/// Initialize the cache directory. pub fn init() -> Result<()> { - let gitignore_path = Path::new(cache_dir()).join(".gitignore"); - if gitignore_path.exists() { - return Ok(()); + let path = Path::new(cache_dir()); + + // Create the directory. + create_dir_all(path)?; + + // Add the CACHEDIR.TAG. + if !cachedir::is_tagged(path)? { + cachedir::add_tag(path)?; } - create_dir_all(cache_dir())?; - let mut file = File::create(gitignore_path)?; - file.write_all(b"*").map_err(|e| e.into()) + + // Add the .gitignore. + let gitignore_path = path.join(".gitignore"); + if !gitignore_path.exists() { + let mut file = File::create(gitignore_path)?; + file.write_all(b"*")?; + } + + Ok(()) } +/// Get a value from the cache. pub fn get( path: &Path, metadata: &Metadata, @@ -134,6 +147,7 @@ pub fn get( None } +/// Set a value in the cache. pub fn set( path: &Path, metadata: &Metadata,