rocketmq_common/utils/
file_utils.rs1use std::fs::File;
19use std::io::Read;
20use std::io::Write;
21use std::io::{self};
22use std::path::Path;
23use std::path::PathBuf;
24
25use parking_lot::Mutex;
26#[cfg(feature = "async_fs")]
27use tokio::io::AsyncReadExt;
28#[cfg(feature = "async_fs")]
29use tokio::io::AsyncWriteExt;
30use tracing::warn;
31
32static LOCK: Mutex<()> = Mutex::new(());
33
34#[cfg(feature = "async_fs")]
35static ASYNC_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
36
37pub fn file_to_string(file_name: &str) -> Result<String, io::Error> {
38 if !PathBuf::from(file_name).exists() {
39 warn!("file not exist:{}", file_name);
40 return Ok("".to_string());
41 }
42 let file = File::open(file_name)?;
43 file_to_string_impl(&file)
44}
45
46fn file_to_string_impl(file: &File) -> Result<String, io::Error> {
47 let file_length = file.metadata()?.len() as usize;
48 let mut data = vec![0; file_length];
49 let result = file.take(file_length as u64).read_exact(&mut data);
50
51 match result {
52 Ok(_) => Ok(String::from_utf8_lossy(&data).to_string()),
53 Err(_) => Err(io::Error::new(
54 io::ErrorKind::InvalidData,
55 "Failed to read file",
56 )),
57 }
58}
59
60pub fn string_to_file(str_content: &str, file_name: &str) -> io::Result<()> {
61 let lock = LOCK.lock(); let bak_file = format!("{file_name}.bak");
64
65 if let Ok(prev_content) = file_to_string(file_name) {
67 string_to_file_not_safe(&prev_content, &bak_file)?;
68 }
69
70 string_to_file_not_safe(str_content, file_name)?;
72 drop(lock);
73 Ok(())
74}
75
76fn string_to_file_not_safe(str_content: &str, file_name: &str) -> io::Result<()> {
77 if let Some(parent) = Path::new(file_name).parent() {
79 std::fs::create_dir_all(parent)?;
80 }
81 let file = File::create(file_name)?;
82
83 write_string_to_file(&file, str_content, "UTF-8")
84}
85
86fn write_string_to_file(file: &File, data: &str, _encoding: &str) -> io::Result<()> {
87 let mut os = io::BufWriter::new(file);
88
89 os.write_all(data.as_bytes())?;
90
91 Ok(())
92}
93
94#[cfg(feature = "async_fs")]
95pub async fn file_to_string_async(file_name: &str) -> Result<String, io::Error> {
96 if !tokio::fs::try_exists(file_name).await.unwrap_or(false) {
97 warn!("file not exist:{}", file_name);
98 return Ok("".to_string());
99 }
100 let mut file = tokio::fs::File::open(file_name).await?;
101 file_to_string_impl_async(&mut file).await
102}
103
104#[cfg(feature = "async_fs")]
105async fn file_to_string_impl_async(file: &mut tokio::fs::File) -> Result<String, io::Error> {
106 let file_length = file.metadata().await?.len() as usize;
107 let mut data = vec![0; file_length];
108 let result = file.read_exact(&mut data).await;
109
110 match result {
111 Ok(_) => Ok(String::from_utf8_lossy(&data).to_string()),
112 Err(_) => Err(io::Error::new(
113 io::ErrorKind::InvalidData,
114 "Failed to read file",
115 )),
116 }
117}
118
119#[cfg(feature = "async_fs")]
120pub async fn string_to_file_async(str_content: &str, file_name: &str) -> io::Result<()> {
121 let lock = ASYNC_LOCK.lock().await;
122
123 let bak_file = format!("{file_name}.bak");
124
125 if let Ok(prev_content) = file_to_string_async(file_name).await {
127 string_to_file_not_safe_async(&prev_content, &bak_file).await?;
128 }
129
130 string_to_file_not_safe_async(str_content, file_name).await?;
132 drop(lock);
133 Ok(())
134}
135
136#[cfg(feature = "async_fs")]
137async fn string_to_file_not_safe_async(str_content: &str, file_name: &str) -> io::Result<()> {
138 if let Some(parent) = Path::new(file_name).parent() {
140 tokio::fs::create_dir_all(parent).await?;
141 }
142 let mut file = tokio::fs::File::create(file_name).await?;
143
144 write_string_to_file_async(&mut file, str_content, "UTF-8").await
145}
146
147#[cfg(feature = "async_fs")]
148async fn write_string_to_file_async(
149 file: &mut tokio::fs::File,
150 data: &str,
151 _encoding: &str,
152) -> io::Result<()> {
153 file.write_all(data.as_bytes()).await?;
154 file.flush().await?;
155 Ok(())
156}
157
158#[cfg(test)]
159mod tests {
160 use super::*;
161
162 #[test]
163 fn test_file_to_string() {
164 let temp_file = tempfile::NamedTempFile::new().unwrap();
166 let file_path = temp_file.path().to_str().unwrap();
167
168 let content = "Hello, World!";
170 std::fs::write(file_path, content).unwrap();
171
172 let result = file_to_string(file_path);
174
175 assert!(result.is_ok());
177 assert_eq!(result.unwrap(), content);
178 }
179
180 #[test]
181 fn test_string_to_file() {
182 let temp_file = tempfile::NamedTempFile::new().unwrap();
184 let file_path = temp_file.path().to_str().unwrap();
185
186 let content = "Hello, World!";
188 let result = string_to_file(content, file_path);
189
190 assert!(result.is_ok());
192 assert_eq!(std::fs::read_to_string(file_path).unwrap(), content);
193 }
194
195 #[cfg(feature = "async_fs")]
196 #[tokio::test]
197 async fn test_file_to_string_async() {
198 let temp_file = tempfile::NamedTempFile::new().unwrap();
200 let file_path = temp_file.path().to_str().unwrap();
201
202 let content = "Hello, Async World!";
204 tokio::fs::write(file_path, content).await.unwrap();
205
206 let result = file_to_string_async(file_path).await;
208
209 assert!(result.is_ok());
211 assert_eq!(result.unwrap(), content);
212 }
213
214 #[cfg(feature = "async_fs")]
215 #[tokio::test]
216 async fn test_string_to_file_async() {
217 let temp_file = tempfile::NamedTempFile::new().unwrap();
219 let file_path = temp_file.path().to_str().unwrap();
220
221 let content = "Hello, Async World!";
223 let result = string_to_file_async(content, file_path).await;
224
225 assert!(result.is_ok());
227 assert_eq!(tokio::fs::read_to_string(file_path).await.unwrap(), content);
228 }
229}