Skip to content

Commit

Permalink
fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinLin666 committed Nov 19, 2023
1 parent 2fffc37 commit f25ace3
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions object_store/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,31 +338,39 @@ impl ObjectStore for LocalFileSystem {
maybe_spawn_blocking(move || {
let (mut file, suffix) = new_staged_upload(&path)?;
let staging_path = staged_upload_path(&path, &suffix);
let mut e_tag = None;

let err = match file.write_all(&bytes) {
Ok(_) => match opts.mode {
PutMode::Overwrite => {
std::mem::drop(file);
match std::fs::rename(&staging_path, &path) {
Ok(_) => None,
Err(source) => Some(Error::UnableToRenameFile { source }),
}
}
PutMode::Create => match std::fs::hard_link(&staging_path, &path) {
Ok(_) => {
let _ = std::fs::remove_file(&staging_path); // Attempt to cleanup
None
Ok(_) => {
let metadata = file.metadata().map_err(|e| Error::Metadata {
source: e.into(),
path: path.to_string_lossy().to_string(),
})?;
e_tag = Some(get_etag(&metadata));
match opts.mode {
PutMode::Overwrite => {
std::mem::drop(file);
match std::fs::rename(&staging_path, &path) {
Ok(_) => None,
Err(source) => Some(Error::UnableToRenameFile { source }),
}
}
Err(source) => match source.kind() {
ErrorKind::AlreadyExists => Some(Error::AlreadyExists {
path: path.to_str().unwrap().to_string(),
source,
}),
_ => Some(Error::UnableToRenameFile { source }),
PutMode::Create => match std::fs::hard_link(&staging_path, &path) {
Ok(_) => {
let _ = std::fs::remove_file(&staging_path); // Attempt to cleanup
None
}
Err(source) => match source.kind() {
ErrorKind::AlreadyExists => Some(Error::AlreadyExists {
path: path.to_str().unwrap().to_string(),
source,
}),
_ => Some(Error::UnableToRenameFile { source }),
},
},
},
PutMode::Update(_) => unreachable!(),
},
PutMode::Update(_) => unreachable!(),
}
}
Err(source) => Some(Error::UnableToCopyDataToFile { source }),
};

Expand All @@ -371,13 +379,8 @@ impl ObjectStore for LocalFileSystem {
return Err(err.into());
}

let metadata = metadata(&path).map_err(|e| Error::Metadata {
source: e.into(),
path: path.to_string_lossy().to_string(),
})?;

Ok(PutResult {
e_tag: Some(get_etag(&metadata)),
e_tag,
version: None,
})
})
Expand Down

0 comments on commit f25ace3

Please sign in to comment.