Skip to content
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
6 changes: 2 additions & 4 deletions src/cargo/ops/cargo_package/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::fs::{self, File};
use std::fs::File;
use std::io::SeekFrom;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -177,10 +177,8 @@ fn create_package(
.context("failed to prepare local package for uploading")?;

dst.seek(SeekFrom::Start(0))?;
let src_path = dst.path();
let dst_path = dst.parent().join(&filename);
fs::rename(&src_path, &dst_path)
.context("failed to move temporary tarball into final location")?;
dst.rename(&dst_path)?;

let dst_metadata = dst
.file()
Expand Down
23 changes: 23 additions & 0 deletions src/cargo/util/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ impl FileLock {
}
Ok(())
}

/// Renames the file and updates the internal path.
///
/// This method performs a filesystem rename operation using [`std::fs::rename`]
/// while keeping the FileLock's internal path synchronized with the actual
/// file location.
///
/// ## Difference from `std::fs::rename`
///
/// - `std::fs::rename(old, new)` only moves the file on the filesystem
/// - `FileLock::rename(new)` moves the file AND updates `self.path` to point to the new location
pub fn rename<P: AsRef<Path>>(&mut self, new_path: P) -> CargoResult<()> {
let new_path = new_path.as_ref();
std::fs::rename(&self.path, new_path).with_context(|| {
format!(
"failed to rename {} to {}",
self.path.display(),
new_path.display()
)
})?;
self.path = new_path.to_path_buf();
Ok(())
}
}

impl Read for FileLock {
Expand Down