Skip to content

Commit

Permalink
Add lock for dependencies directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Feb 26, 2024
1 parent e779f0c commit 514b6a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ edition = "2021"
directories = "5.0"
git-repository = {version = "0.35.0", optional = true, features = ["blocking-network-client", "blocking-http-transport-reqwest", "blocking-http-transport-reqwest-rust-tls"]}
log = {workspace = true}
fs4 = { version = "0.8.0", features = ["sync"] }
miette = {workspace = true}
regex = {workspace = true}
semver = {workspace = true}
Expand Down
22 changes: 21 additions & 1 deletion crates/metadata/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use crate::metadata::{Dependency, Metadata};
use crate::metadata_error::MetadataError;
use crate::pubfile::{Pubfile, Release};
use crate::{utils, PathPair};
use fs4::FileExt;
use log::info;
use semver::{Version, VersionReq};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::fs::{self, File};
use std::path::Path;
use std::str::FromStr;
use url::Url;
Expand Down Expand Up @@ -339,9 +340,11 @@ impl Lockfile {
let uuid = Self::gen_uuid(url, "")?;

let path = resolve_dir.join(uuid.simple().encode_lower(&mut Uuid::encode_buffer()));
let lock = Self::lock_dir("resolve")?;
let git = Git::clone(url, &path)?;
git.fetch()?;
git.checkout(None)?;
Self::unlock_dir(lock)?;

let toml = path.join("Veryl.pub");
let mut pubfile = Pubfile::load(toml)?;
Expand Down Expand Up @@ -373,25 +376,42 @@ impl Lockfile {
let toml = path.join("Veryl.toml");

if !path.exists() {
let lock = Self::lock_dir("dependencies")?;
let git = Git::clone(url, &path)?;
git.fetch()?;
git.checkout(Some(revision))?;
Self::unlock_dir(lock)?;
} else {
let git = Git::open(&path)?;
let ret = git.is_clean().map_or(false, |x| x);

// If the existing path is not git repository, cleanup and re-try
if !ret || !toml.exists() {
let lock = Self::lock_dir("dependencies")?;
fs::remove_dir_all(&path)?;
let git = Git::clone(url, &path)?;
git.fetch()?;
git.checkout(Some(revision))?;
Self::unlock_dir(lock)?;
}
}

let metadata = Metadata::load(toml)?;
Ok(metadata)
}

fn lock_dir(path: &str) -> Result<File, MetadataError> {
let base_dir = Metadata::cache_dir().join(path);
let lock = base_dir.join("lock");
let lock = File::create(lock)?;
lock.lock_exclusive()?;
Ok(lock)
}

fn unlock_dir(lock: File) -> Result<(), MetadataError> {
lock.unlock()?;
Ok(())
}
}

impl FromStr for Lockfile {
Expand Down

0 comments on commit 514b6a1

Please sign in to comment.