Skip to content
Draft
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: 2 additions & 2 deletions packages/zpm-config/src/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn check_tsconfig(context: &ConfigurationContext) -> bool {
if let Some(project_cwd) = &context.project_cwd {
let root_has_tsconfig = project_cwd
.with_join_str("tsconfig.json")
.fs_exists();
.fs_exists_blocking();

if root_has_tsconfig {
return true;
Expand All @@ -14,7 +14,7 @@ pub fn check_tsconfig(context: &ConfigurationContext) -> bool {
if let Some(package_cwd) = &context.package_cwd {
let package_has_tsconfig = package_cwd
.with_join_str("tsconfig.json")
.fs_exists();
.fs_exists_blocking();

if package_has_tsconfig {
return true;
Expand Down
10 changes: 5 additions & 5 deletions packages/zpm-formats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ impl<'a> Entry<'a> {
pub fn entries_to_disk<'a>(entries: &[Entry<'a>], base: &Path) -> Result<(), Error> {
for entry in entries {
base.with_join(&entry.name)
.fs_create_parent()?
.fs_change(&entry.data, entry.mode & 0o111 == 0o111)?;
.fs_create_parent_blocking()?
.fs_change_blocking(&entry.data, entry.mode & 0o111 == 0o111)?;
}

Ok(())
Expand All @@ -126,7 +126,7 @@ pub fn entries_from_folder<'a>(path: &Path) -> Result<Vec<Entry<'a>>, Error> {
let mut process_queue = vec![path.clone()];

while let Some(path) = process_queue.pop() {
let listing = path.fs_read_dir()?;
let listing = path.fs_read_dir_blocking()?;

for entry in listing {
let entry = entry?;
Expand All @@ -138,7 +138,7 @@ pub fn entries_from_folder<'a>(path: &Path) -> Result<Vec<Entry<'a>>, Error> {
}

let name = Path::try_from(entry.file_name().into_string()?)?;
let data = path.fs_read()?;
let data = path.fs_read_blocking()?;
let metadata = path.fs_metadata()?;

let is_exec = metadata.permissions().mode() & 0o111 != 0;
Expand All @@ -164,7 +164,7 @@ pub fn entries_from_files<'a>(base: &Path, files: &[Path]) -> Result<Vec<Entry<'
let abs_path = base
.with_join(rel_path);

let data = abs_path.fs_read()?;
let data = abs_path.fs_read_blocking()?;
let metadata = abs_path.fs_metadata()?;

let is_exec = metadata.permissions().mode() & 0o111 != 0;
Expand Down
6 changes: 3 additions & 3 deletions packages/zpm-formats/src/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,21 @@ impl ZipSupport for Path {

match parsed {
pnp::fs::VPath::Native(_) => {
Ok(self.fs_read_text_prealloc()?)
Ok(self.fs_read_text_prealloc_blocking()?)
},

pnp::fs::VPath::Virtual(info) => {
let file_data
= Path::try_from(info.physical_base_path()).unwrap()
.fs_read_text_prealloc()?;
.fs_read_text_prealloc_blocking()?;

Ok(file_data)
},

pnp::fs::VPath::Zip(info) => {
let zip_data
= Path::try_from(info.physical_base_path()).unwrap()
.fs_read_prealloc()?;
.fs_read_prealloc_blocking()?;

let file_data = Path::try_from(info.zip_path)?
.fs_read_text_from_zip_buffer(&zip_data)?;
Expand Down
41 changes: 28 additions & 13 deletions packages/zpm-switch/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn cache_dir() -> Result<Path, Error> {
pub fn cache_metadata(p: &Path) -> Result<CacheKey, Error> {
let key_string = p
.with_join_str("meta.json")
.fs_read_text()?;
.fs_read_text_blocking()?;

let key_data: CacheKey
= JsonDocument::hydrate_from_str(&key_string)?;
Expand Down Expand Up @@ -116,7 +116,7 @@ async fn pretty_download<F: Future<Output = Result<(), Error>>>(key_data: &Cache
result
}

fn access(key_data: &CacheKey) -> Result<(Path, bool), Error> {
async fn access(key_data: &CacheKey) -> Result<(Path, bool), Error> {
let key_string
= JsonDocument::to_string(key_data)?;
let key_hash
Expand All @@ -130,28 +130,42 @@ fn access(key_data: &CacheKey) -> Result<(Path, bool), Error> {
let ready_path = cache_path
.with_join_str(".ready");

Ok((cache_path, ready_path.fs_exists()))
Ok((cache_path, ready_path.fs_exists().await))
}

pub fn check(key_data: &CacheKey) -> Result<bool, Error> {
Ok(access(key_data)?.1)
pub async fn check(key_data: &CacheKey) -> Result<bool, Error> {
Ok(access(key_data).await?.1)
}

fn touch_ready_sync(ready_path: Path) -> Result<(), Error> {
ready_path
.fs_set_modified(std::time::SystemTime::now())?;
Ok(())
}

fn move_cache_sync(temp_dir: Path, cache_path: Path) -> Result<(), Error> {
temp_dir
.fs_concurrent_move(&cache_path)?;
Ok(())
}

pub async fn ensure<R: Future<Output = Result<(), Error>>, F: FnOnce(Path) -> R>(key_data: &CacheKey, f: F) -> Result<Path, Error> {
match access(key_data)? {
match access(key_data).await? {
(cache_path, true) => {
let ready_path = cache_path
.with_join_str(".ready");

// Not a big deal if this fails, which may happen on filesystems
// with limited permissions (read-only ones)
let _ = ready_path
.fs_set_modified(std::time::SystemTime::now());
let _ = tokio::task::spawn_blocking(move || touch_ready_sync(ready_path)).await;

Ok(cache_path)
},

(cache_path, false) => {
let cache_path_for_move
= cache_path.clone();

pretty_download(key_data, async {
let temp_dir
= Path::temp_dir()?;
Expand All @@ -163,17 +177,18 @@ pub async fn ensure<R: Future<Output = Result<(), Error>>, F: FnOnce(Path) -> R>

temp_dir
.with_join_str("meta.json")
.fs_write(&meta_content)?;
.fs_write(&meta_content).await?;

temp_dir
.with_join_str(".ready")
.fs_write([])?;
.fs_write([]).await?;

cache_path
.fs_create_parent()?;
.fs_create_parent().await?;

temp_dir
.fs_concurrent_move(&cache_path)?;
tokio::task::spawn_blocking(move || {
move_cache_sync(temp_dir, cache_path_for_move)
}).await??;

Ok(())
}).await?;
Expand Down
2 changes: 1 addition & 1 deletion packages/zpm-switch/src/commands/switch/cache_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl CacheCheckCommand {
platform: get_system_string().to_string(),
};

if !cache::check(&cache_key)? {
if !cache::check(&cache_key).await? {
return Err(Error::CacheNotFound(version.clone()));
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/zpm-switch/src/commands/switch/cache_clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ impl CacheClearCommand {
= cache::cache_dir()?;

if self.old {
let Some(cache_entries) = cache_dir.fs_read_dir().ok_missing()? else {
return Ok(());
let mut cache_entries = match cache_dir.fs_read_dir().await.ok_missing()? {
Some(cache_entries) => cache_entries,
None => return Ok(()),
};

for entry in cache_entries {
let entry
= entry?;
while let Some(entry) = cache_entries.next_entry().await? {

let entry_path
= Path::try_from(entry.path())?;
Expand All @@ -41,12 +40,13 @@ impl CacheClearCommand {
};

if entry_last_used.elapsed().unwrap() > std::time::Duration::from_secs(60 * 60 * 24 * 7) {
entry_path.fs_rm().ok_missing()?;
entry_path.fs_rm().await.ok_missing()?;
}
}
} else {
cache_dir
.fs_rm()
.await
.ok_missing()?;
}

Expand Down
9 changes: 4 additions & 5 deletions packages/zpm-switch/src/commands/switch/cache_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ impl CacheListCommand {
let cache_dir
= cache::cache_dir()?;

let Some(cache_entries) = cache_dir.fs_read_dir().ok_missing()? else {
return Ok(());
let mut cache_entries = match cache_dir.fs_read_dir().await.ok_missing()? {
Some(cache_entries) => cache_entries,
None => return Ok(()),
};

for entry in cache_entries {
let entry
= entry?;
while let Some(entry) = cache_entries.next_entry().await? {

let entry_path
= Path::try_from(entry.path())?;
Expand Down
2 changes: 1 addition & 1 deletion packages/zpm-switch/src/commands/switch/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl LinkCommand {

set_link(&Link {
project_cwd: detected_root_path.clone(),
link_target: LinkTarget::Local {bin_path: self.path.fs_canonicalize()?},
link_target: LinkTarget::Local {bin_path: self.path.fs_canonicalize().await?},
})?;

println!(
Expand Down
1 change: 1 addition & 0 deletions packages/zpm-switch/src/commands/switch/links_clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl LinksClearCommand {

links_dir
.fs_rm()
.await
.ok_missing()?;

Ok(())
Expand Down
16 changes: 8 additions & 8 deletions packages/zpm-switch/src/commands/switch/postinstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl PostinstallCommand {

fn write_profile(&self, bin_dir: &Path, profile: &ShellProfile) {
let profile_content = profile.rc_file
.fs_read_text_prealloc()
.fs_read_text_prealloc_blocking()
.ok_missing();

if let Ok(maybe_profile_content) = profile_content {
Expand Down Expand Up @@ -128,8 +128,8 @@ impl PostinstallCommand {

fn write_env(&self, env_file: &Path, env_line: &str) -> Result<(), Error> {
env_file
.fs_create_parent()?
.fs_write_text(env_line)?;
.fs_create_parent_blocking()?
.fs_write_text_blocking(env_line)?;

Ok(())
}
Expand All @@ -153,8 +153,8 @@ impl PostinstallCommand {
.push_str(&rc_line);

rc_file
.fs_create_parent()?
.fs_write_text(&rc_content)?;
.fs_create_parent_blocking()?
.fs_write_text_blocking(&rc_content)?;

Ok(())
}
Expand All @@ -168,7 +168,7 @@ impl PostinstallCommand {
= Path::from_str(&github_path).unwrap();

let github_path_file_write_result = github_path_file
.fs_append_text(format!("{}\n", bin_dir.to_file_string()));
.fs_append_text_blocking(format!("{}\n", bin_dir.to_file_string()));

if github_path_file_write_result.is_err() {
Note::Warning(format!("
Expand Down Expand Up @@ -239,7 +239,7 @@ impl PostinstallCommand {
.with_join_str("../../../../user/platform.json");

let mut volta_platform_content = volta_platform_path
.fs_read_prealloc()?;
.fs_read_prealloc_blocking()?;

if volta_platform_content.is_empty() {
volta_platform_content = "{}".as_bytes().to_vec();
Expand All @@ -254,7 +254,7 @@ impl PostinstallCommand {
)?;

volta_platform_path
.fs_write(&document.input)?;
.fs_write_blocking(&document.input)?;

Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion packages/zpm-switch/src/commands/switch/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ impl UpdateCommand {
.with_join_str("yarn-install-script.sh");

install_script_path
.fs_write(install_script)?;
.fs_write(install_script)
.await?;

Command::new("bash")
.arg(install_script_path.to_path_buf())
Expand Down
2 changes: 1 addition & 1 deletion packages/zpm-switch/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn install_native_from_zpm(source: &cache::CacheKey, binary_name: &Path) -

async fn install_node_js_from_url(source: &cache::CacheKey) -> Result<Command, Error> {
let cache_path = cache::ensure(source, |p| async move {
p.with_join_str("bin.js").fs_write(fetch(&source.to_url()).await?)?;
p.with_join_str("bin.js").fs_write(fetch(&source.to_url()).await?).await?;
Ok(())
}).await?;

Expand Down
12 changes: 6 additions & 6 deletions packages/zpm-switch/src/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub fn set_link(link: &Link) -> Result<(), Error> {
.with_join_str(format!("{}.json", hash.short()));

link_path
.fs_create_parent()?
.fs_write(JsonDocument::to_string(link)?)?;
.fs_create_parent_blocking()?
.fs_write_blocking(JsonDocument::to_string(link)?)?;

Ok(())
}
Expand All @@ -66,7 +66,7 @@ pub fn unset_link(project_cwd: &Path) -> Result<(), Error> {
.with_join_str(format!("{}.json", hash.short()));

link_path
.fs_rm()?;
.fs_rm_blocking()?;

Ok(())
}
Expand All @@ -75,15 +75,15 @@ pub fn list_links() -> Result<BTreeSet<Link>, Error> {
let links_dir
= links_dir()?;

let Some(dir_entries) = links_dir.fs_read_dir().ok_missing()? else {
let Some(dir_entries) = links_dir.fs_read_dir_blocking().ok_missing()? else {
return Ok(BTreeSet::new());
};

let links = dir_entries
.filter_map(|entry| entry.ok())
.filter(|entry| entry.file_type().map_or(false, |f| f.is_file()))
.filter_map(|link_path| Path::try_from(link_path.path()).ok())
.filter_map(|link_path| link_path.fs_read_text().ok())
.filter_map(|link_path| link_path.fs_read_text_blocking().ok())
.filter_map(|contents| JsonDocument::hydrate_from_str::<Link>(&contents).ok())
.collect::<BTreeSet<_>>();

Expand All @@ -98,7 +98,7 @@ pub fn get_link(path: &Path) -> Result<Option<Link>, Error> {
.with_join_str(format!("{}.json", hash.short()));

let link = link_path
.fs_read_text()
.fs_read_text_blocking()
.ok_missing()?
.and_then(|link| JsonDocument::hydrate_from_str::<Link>(&link).ok());

Expand Down
4 changes: 2 additions & 2 deletions packages/zpm-switch/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn find_closest_package_manager(path: &Path) -> Result<FindResult, Error> {
.with_join_str("package.json");

let manifest = manifest_path
.fs_read_text()
.fs_read_text_blocking()
.ok_missing()?;

if let Some(manifest) = &manifest {
Expand All @@ -157,7 +157,7 @@ pub fn find_closest_package_manager(path: &Path) -> Result<FindResult, Error> {
let root_file_path = parent
.with_join_str(root_file);

if root_file_path.fs_exists() {
if root_file_path.fs_exists_blocking() {
return Ok(FindResult {
detected_root_path: Some(parent),
detected_package_manager: None,
Expand Down
Loading
Loading