Skip to content

Commit

Permalink
Fix ops::package leaving old path in created FileLockGuard
Browse files Browse the repository at this point in the history
commit-id:d57e36fd
  • Loading branch information
mkaput committed Oct 12, 2023
1 parent cb730f7 commit 90fcd2f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion scarb/src/flock.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fs::{File, OpenOptions};
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::sync::{Arc, Weak};
use std::{fmt, io};

use anyhow::{Context, Result};
use anyhow::{ensure, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use fs4::{lock_contended_error, FileExt};
use tokio::sync::Mutex;
Expand All @@ -12,6 +13,7 @@ use scarb_ui::components::Status;

use crate::core::Config;
use crate::internal::fsx;
use crate::internal::fsx::PathUtf8Ext;
use crate::internal::lazy_directory_creator::LazyDirectoryCreator;

const OK_FILE: &str = ".scarb-ok";
Expand All @@ -37,6 +39,18 @@ impl FileLockGuard {
pub fn lock_kind(&self) -> FileLockKind {
self.lock_kind
}

pub fn rename(&mut self, to: impl AsRef<Path>) -> Result<&mut Self> {
ensure!(
self.lock_kind == FileLockKind::Exclusive,
"cannot rename shared file: {}",
self.path,
);
let to = to.as_ref().try_to_utf8()?;
fsx::rename(&self.path, &to)?;
self.path = to;
Ok(self)
}
}

impl Deref for FileLockGuard {
Expand Down
4 changes: 2 additions & 2 deletions scarb/src/ops/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::core::publishing::manifest_normalization::prepare_manifest_for_publis
use crate::core::publishing::source::list_source_files;
use crate::core::{Package, PackageId, PackageName, Workspace};
use crate::flock::FileLockGuard;
use crate::internal::{fsx, restricted_names};
use crate::internal::restricted_names;
use crate::{ops, MANIFEST_FILE_NAME};

const VERSION: u8 = 1;
Expand Down Expand Up @@ -117,7 +117,7 @@ fn package_one_impl(

dst.seek(SeekFrom::Start(0))?;

fsx::rename(dst.path(), dst.path().with_file_name(filename))?;
dst.rename(dst.path().with_file_name(filename))?;

let dst_metadata = dst
.metadata()
Expand Down

0 comments on commit 90fcd2f

Please sign in to comment.