Skip to content
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

Correctly set directory mtimes #7

Merged
merged 2 commits into from
Dec 18, 2021
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kache"
version = "0.1.1"
version = "0.1.2"
authors = ["Tably Team"]
description = """
Store and retrieve the Rust build cache.
Expand All @@ -17,6 +17,7 @@ base-x = "0.2"
bytes = "1"
dotenv = "0.15.0"
envy = "0.4"
filetime = "0.2"
futures = "0.3"
home = "0.5"
indicatif = { version = "0.17.0-beta.1", features = ["tokio"], git = "https://github.com/mitsuhiko/indicatif" }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ curl \
--silent \
--show-error \
--fail \
"https://github.com/cargo-quick/kache/releases/download/v0.1.1/kache-v0.1.1-${TARGET_ARCH}.tar.gz" \
"https://github.com/cargo-quick/kache/releases/download/v0.1.2/kache-v0.1.2-${TARGET_ARCH}.tar.gz" \
| tar -xzvvf - -C "$CARGO_BIN_DIR"
```

Expand Down
16 changes: 15 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use std::{
time::SystemTime,
};
use tokio::io::{AsyncBufRead, AsyncRead, AsyncReadExt, AsyncWriteExt, ReadBuf};
use tokio_tar::EntryType;
use walkdir::WalkDir;

const WEB_SAFE: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_~";
Expand Down Expand Up @@ -282,11 +283,13 @@ async fn untar(slugs: BTreeMap<PathBuf, PathBuf>, tar: impl AsyncBufRead) -> Res

let mut entries = tokio_tar::Archive::new(tar).entries().unwrap();

let mut mtimes = BTreeMap::new();

while let Some(entry) = entries.next().await {
let mut entry = entry.unwrap();
let path = entry.path().unwrap().into_owned();

if path.ends_with("kache") || path.ends_with("kache.exe") {
if path.file_stem().unwrap() == "kache" {
// Don't replace ourselves - the version in cache may be stale
continue;
}
Expand All @@ -295,6 +298,9 @@ async fn untar(slugs: BTreeMap<PathBuf, PathBuf>, tar: impl AsyncBufRead) -> Res

let parent = path.parent().unwrap();
let _ = fs::create_dir_all(&parent);
if entry.header().entry_type() == EntryType::Directory {
let _ = mtimes.insert(path.clone(), entry.header().mtime().unwrap());
}
entry
.unpack(&path)
.await
Expand All @@ -318,6 +324,14 @@ async fn untar(slugs: BTreeMap<PathBuf, PathBuf>, tar: impl AsyncBufRead) -> Res
});
}

for (path, mtime) in mtimes.into_iter().rev() {
filetime::set_file_mtime(
path,
filetime::FileTime::from_unix_time(mtime.try_into().unwrap(), 0),
)
.unwrap();
}

Ok(())
}

Expand Down