rocketmq_common/utils/
file_utils.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use 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(); //todo enhancement: not use global lock
62
63    let bak_file = format!("{file_name}.bak");
64
65    // Read previous content and create a backup
66    if let Ok(prev_content) = file_to_string(file_name) {
67        string_to_file_not_safe(&prev_content, &bak_file)?;
68    }
69
70    // Write new content to the file
71    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    // Create parent directories if they don't exist
78    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    // Read previous content and create a backup
126    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    // Write new content to the file
131    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    // Create parent directories if they don't exist
139    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        // Create a temporary file for testing
165        let temp_file = tempfile::NamedTempFile::new().unwrap();
166        let file_path = temp_file.path().to_str().unwrap();
167
168        // Write some content to the file
169        let content = "Hello, World!";
170        std::fs::write(file_path, content).unwrap();
171
172        // Call the file_to_string function
173        let result = file_to_string(file_path);
174
175        // Check if the result is Ok and contains the expected content
176        assert!(result.is_ok());
177        assert_eq!(result.unwrap(), content);
178    }
179
180    #[test]
181    fn test_string_to_file() {
182        // Create a temporary file for testing
183        let temp_file = tempfile::NamedTempFile::new().unwrap();
184        let file_path = temp_file.path().to_str().unwrap();
185
186        // Call the string_to_file function
187        let content = "Hello, World!";
188        let result = string_to_file(content, file_path);
189
190        // Check if the result is Ok and the file was created with the expected content
191        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        // Create a temporary file for testing
199        let temp_file = tempfile::NamedTempFile::new().unwrap();
200        let file_path = temp_file.path().to_str().unwrap();
201
202        // Write some content to the file
203        let content = "Hello, Async World!";
204        tokio::fs::write(file_path, content).await.unwrap();
205
206        // Call the file_to_string_async function
207        let result = file_to_string_async(file_path).await;
208
209        // Check if the result is Ok and contains the expected content
210        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        // Create a temporary file for testing
218        let temp_file = tempfile::NamedTempFile::new().unwrap();
219        let file_path = temp_file.path().to_str().unwrap();
220
221        // Call the string_to_file_async function
222        let content = "Hello, Async World!";
223        let result = string_to_file_async(content, file_path).await;
224
225        // Check if the result is Ok and the file was created with the expected content
226        assert!(result.is_ok());
227        assert_eq!(tokio::fs::read_to_string(file_path).await.unwrap(), content);
228    }
229}