here is a fully demo source code, it works. But if I call write_bytes_into_file(file_path, data);
with the same params in my project, the dir will be created,but the file won't.
if I change the file extension,like .encr
/ .enc+
/ .en
/ .e
etc. just not .enc
, it will works.
It's too strange.
use std::{
fs::File,
io::Write,
path::Path,
io
};
pub fn get_parent_dir_in_path_string(path_str: &String) -> String {
let dir = std::path::Path::new(&path_str);
let parent_dir = dir.parent().unwrap();
return parent_dir
.as_os_str()
.to_os_string()
.to_str()
.unwrap()
.to_string();
}
pub fn check_dir_or_create(dir_str: String) {
if !Path::new(&dir_str).exists() {
std::fs::create_dir_all(&dir_str).unwrap();
}
}
pub fn write_bytes_into_file(file_path: String, file_content: Vec<u8>) -> Result<bool, io::Error> {
check_dir_or_create(get_parent_dir_in_path_string(&file_path));
print!(">>> file_path: {}\n", &file_path);
print!(">>> file_content: {:?}\n", &file_content);
let mut f = match File::create(&file_path) {
Ok(file) => file,
Err(e) => {
print!(
"write_bytes_into_file create error, path: {}, error: {}\n",
&file_path, e
);
return Err(e);
}
};
if let Err(e) = f.write_all(&file_content) {
print!(
"write_bytes_into_file write_all error, path: {}, error: {}\n",
&file_path, e
);
return Err(e);
}
Ok(true)
}
fn main() {
let data: Vec<u8> = vec![
123, 34, 100, 97, 116, 97, 86, 101, 114, 115, 105, 111, 110, 34, 58, 49, 44, 34, 110, 111,
116, 101, 66, 111, 111, 107, 115, 34, 58, 123, 34, 97, 116, 116, 114, 115, 65, 114, 114,
34, 58, 91, 34, 116, 105, 116, 108, 101, 34, 44, 34, 105, 99, 111, 110, 34, 44, 34, 99,
104, 105, 108, 100, 67, 111, 117, 110, 116, 34, 44, 34, 104, 97, 115, 104, 101, 100, 83,
105, 103, 110, 34, 93, 44, 34, 100, 97, 116, 97, 65, 114, 114, 34, 58, 91, 91, 34, 109,
109, 109, 109, 34, 44, 34, 34, 44, 34, 48, 34, 44, 34, 56, 98, 98, 54, 57, 54, 54, 57, 45,
53, 52, 56, 56, 45, 52, 101, 57, 102, 45, 97, 100, 57, 55, 45, 101, 56, 98, 50, 52, 55, 57,
57, 55, 102, 100, 98, 34, 93, 93, 125, 44, 34, 116, 97, 103, 115, 34, 58, 123, 34, 97, 116,
116, 114, 115, 65, 114, 114, 34, 58, 91, 34, 116, 105, 116, 108, 101, 34, 44, 34, 105, 99,
111, 110, 34, 44, 34, 104, 97, 115, 104, 101, 100, 83, 105, 103, 110, 34, 93, 44, 34, 100,
97, 116, 97, 65, 114, 114, 34, 58, 91, 93, 125, 44, 34, 97, 116, 116, 97, 99, 104, 109,
101, 110, 116, 115, 34, 58, 123, 34, 97, 116, 116, 114, 115, 65, 114, 114, 34, 58, 91, 34,
102, 105, 108, 101, 78, 97, 109, 101, 34, 44, 34, 104, 97, 115, 104, 101, 100, 83, 105,
103, 110, 34, 44, 34, 117, 112, 108, 111, 97, 100, 84, 105, 109, 101, 34, 93, 44, 34, 100,
97, 116, 97, 65, 114, 114, 34, 58, 91, 93, 125, 44, 34, 102, 105, 108, 101, 115, 34, 58,
123, 34, 97, 116, 116, 114, 115, 65, 114, 114, 34, 58, 91, 34, 102, 105, 108, 101, 78, 97,
109, 101, 34, 44, 34, 104, 97, 115, 104, 101, 100, 83, 105, 103, 110, 34, 44, 34, 117, 112,
108, 111, 97, 100, 84, 105, 109, 101, 34, 44, 34, 118, 105, 114, 116, 117, 97, 108, 80, 97,
116, 104, 34, 93, 44, 34, 100, 97, 116, 97, 65, 114, 114, 34, 58, 91, 93, 125, 125,
];
let file_path = "./user_data/sync_cached/manifest.enc".to_string();
write_bytes_into_file(file_path, data);
}