Skip to content

Commit fac3d9b

Browse files
feat: delete file on write failure in write_file function
1 parent f891e5c commit fac3d9b

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

pre-compute/src/compute/utils/file_utils.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ pub fn write_file(content: &[u8], file_path: &Path, context: &str) -> Result<(),
4545
"Failed to write file [{context}, path:{}]",
4646
file_path.display()
4747
);
48+
if file_path.exists() {
49+
match fs::remove_file(file_path) {
50+
Ok(_) => {
51+
info!("File deleted [path:{}]", file_path.display());
52+
}
53+
Err(e) => {
54+
error!(
55+
"Failed to delete file [path:{}, error:{e}]",
56+
file_path.display()
57+
);
58+
}
59+
}
60+
}
4861
Err(e)
4962
}
5063
}
@@ -118,19 +131,6 @@ pub fn download_file(url: &str, parent_dir: &str, filename: &str) -> Option<Path
118131
match write_file(&bytes, &file_path, &format!("url:{url}")) {
119132
Ok(_) => Some(file_path),
120133
Err(_) => {
121-
if file_path.exists() {
122-
match fs::remove_file(&file_path) {
123-
Ok(_) => {
124-
info!("File deleted [path:{}]", file_path.display());
125-
}
126-
Err(e) => {
127-
error!(
128-
"Failed to delete file [path:{}, error:{e}]",
129-
file_path.display()
130-
);
131-
}
132-
}
133-
}
134134
None
135135
}
136136
}

0 commit comments

Comments
 (0)