1 // Copyright (C) 2025 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 use std::fs::{File, Metadata};
15 use std::io::{self, Seek, SeekFrom, Write};
16 use std::sync::{Arc, Mutex};
17
18 use ylong_runtime::task::JoinHandle;
19
20 use crate::task::request_task::RequestTask;
21
runtime_spawn_blocking<F, T>(fut: F) -> JoinHandle<Result<T, io::Error>> where F: FnOnce() -> Result<T, io::Error> + Send + Sync + 'static, T: Send + 'static,22 pub(crate) fn runtime_spawn_blocking<F, T>(fut: F) -> JoinHandle<Result<T, io::Error>>
23 where
24 F: FnOnce() -> Result<T, io::Error> + Send + Sync + 'static,
25 T: Send + 'static,
26 {
27 ylong_runtime::spawn_blocking(
28 Box::new(fut) as Box<dyn FnOnce() -> Result<T, io::Error> + Send + Sync>
29 )
30 }
31
file_seek(file: Arc<Mutex<File>>, pos: SeekFrom) -> io::Result<u64>32 pub(crate) async fn file_seek(file: Arc<Mutex<File>>, pos: SeekFrom) -> io::Result<u64> {
33 runtime_spawn_blocking(move || {
34 let mut file = file.lock().unwrap();
35 file.flush()?;
36 file.seek(pos)
37 })
38 .await
39 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
40 }
41
file_rewind(file: Arc<Mutex<File>>) -> io::Result<()>42 pub(crate) async fn file_rewind(file: Arc<Mutex<File>>) -> io::Result<()> {
43 runtime_spawn_blocking(move || {
44 let mut file = file.lock().unwrap();
45 file.flush()?;
46 file.rewind()
47 })
48 .await
49 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
50 }
51
file_sync_all(file: Arc<Mutex<File>>) -> io::Result<()>52 pub(crate) async fn file_sync_all(file: Arc<Mutex<File>>) -> io::Result<()> {
53 runtime_spawn_blocking(move || {
54 let mut file = file.lock().unwrap();
55 file.flush()?;
56 file.sync_all()
57 })
58 .await
59 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
60 }
61
file_metadata(file: Arc<Mutex<File>>) -> io::Result<Metadata>62 pub(crate) async fn file_metadata(file: Arc<Mutex<File>>) -> io::Result<Metadata> {
63 runtime_spawn_blocking(move || {
64 let file = file.lock().unwrap();
65 file.metadata()
66 })
67 .await
68 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
69 }
70
file_set_len(file: Arc<Mutex<File>>, size: u64) -> io::Result<()>71 pub(crate) async fn file_set_len(file: Arc<Mutex<File>>, size: u64) -> io::Result<()> {
72 runtime_spawn_blocking(move || {
73 let mut file = file.lock().unwrap();
74 file.flush()?;
75 file.set_len(size)
76 })
77 .await
78 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
79 }
80
file_write_all<'a>(file: Arc<Mutex<File>>, buf: &[u8]) -> io::Result<()>81 pub(crate) async fn file_write_all<'a>(file: Arc<Mutex<File>>, buf: &[u8]) -> io::Result<()> {
82 let buf = buf.to_vec();
83 runtime_spawn_blocking(move || {
84 let mut file = file.lock().unwrap();
85 file.write_all(&buf)
86 })
87 .await
88 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
89 }
90
clear_downloaded_file(task: Arc<RequestTask>) -> Result<(), std::io::Error>91 pub(crate) async fn clear_downloaded_file(task: Arc<RequestTask>) -> Result<(), std::io::Error> {
92 info!("task {} clear downloaded file", task.task_id());
93 runtime_spawn_blocking(move || {
94 {
95 let file_mutex = task.files.get(0).unwrap();
96 let mut file = file_mutex.lock().unwrap();
97 file.set_len(0)?;
98 file.seek(SeekFrom::Start(0))?;
99 }
100 {
101 let mut progress_guard = task.progress.lock().unwrap();
102 progress_guard.common_data.total_processed = 0;
103 if let Some(elem) = progress_guard.processed.get_mut(0) {
104 *elem = 0;
105 } else {
106 info!("Failed to get a process size from an empty vector in Progress");
107 }
108 }
109 Ok(())
110 })
111 .await
112 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
113 }
114