• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 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::io::{self, SeekFrom};
15 use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU32, AtomicU64, Ordering};
16 use std::sync::{Mutex, MutexGuard};
17 use std::time::Duration;
18 
19 use ylong_http_client::async_impl::{Body, Client, Request, RequestBuilder, Response};
20 use ylong_http_client::{ErrorKind, HttpClientError};
21 use ylong_runtime::io::{AsyncSeekExt, AsyncWriteExt};
22 
23 cfg_oh! {
24     use crate::manage::SystemConfig;
25     use crate::utils::{request_background_notify, RequestTaskMsg};
26 }
27 
28 use super::config::{Mode, Version};
29 use super::info::{CommonTaskInfo, State, TaskInfo, UpdateInfo};
30 use super::notify::{EachFileStatus, NotifyData, Progress};
31 use super::reason::Reason;
32 use crate::error::ErrorCode;
33 use crate::manage::database::RequestDb;
34 use crate::manage::network_manager::NetworkManager;
35 use crate::manage::notifier::Notifier;
36 use crate::service::client::ClientManagerEntry;
37 use crate::task::client::build_client;
38 use crate::task::config::{Action, TaskConfig};
39 use crate::task::files::{AttachedFiles, Files};
40 use crate::utils::form_item::FileSpec;
41 use crate::utils::get_current_timestamp;
42 
43 const RETRY_TIMES: u32 = 4;
44 const RETRY_INTERVAL: u64 = 400;
45 
46 pub(crate) struct RequestTask {
47     pub(crate) conf: TaskConfig,
48     pub(crate) client: Client,
49     pub(crate) files: Files,
50     pub(crate) body_files: Files,
51     pub(crate) ctime: u64,
52     pub(crate) mime_type: Mutex<String>,
53     pub(crate) progress: Mutex<Progress>,
54     pub(crate) status: Mutex<TaskStatus>,
55     pub(crate) code: Mutex<Vec<Reason>>,
56     pub(crate) tries: AtomicU32,
57     pub(crate) background_notify_time: AtomicU64,
58     pub(crate) file_total_size: AtomicI64,
59     pub(crate) rate_limiting: AtomicU64,
60     pub(crate) last_notify: AtomicU64,
61     pub(crate) client_manager: ClientManagerEntry,
62     pub(crate) running_result: Mutex<Option<Result<(), Reason>>>,
63     pub(crate) timeout_tries: AtomicU32,
64     pub(crate) upload_resume: AtomicBool,
65 }
66 
67 impl RequestTask {
task_id(&self) -> u3268     pub(crate) fn task_id(&self) -> u32 {
69         self.conf.common_data.task_id
70     }
71 
uid(&self) -> u6472     pub(crate) fn uid(&self) -> u64 {
73         self.conf.common_data.uid
74     }
75 
config(&self) -> &TaskConfig76     pub(crate) fn config(&self) -> &TaskConfig {
77         &self.conf
78     }
79 
80     // only use for download task
mime_type(&self) -> String81     pub(crate) fn mime_type(&self) -> String {
82         self.mime_type.lock().unwrap().clone()
83     }
84 
action(&self) -> Action85     pub(crate) fn action(&self) -> Action {
86         self.conf.common_data.action
87     }
88 
mode(&self) -> Mode89     pub(crate) fn mode(&self) -> Mode {
90         self.conf.common_data.mode
91     }
92 
bundle(&self) -> &str93     pub(crate) fn bundle(&self) -> &str {
94         self.conf.bundle.as_str()
95     }
96 
speed_limit(&self, limit: u64)97     pub(crate) fn speed_limit(&self, limit: u64) {
98         let old = self.rate_limiting.swap(limit, Ordering::SeqCst);
99         if old != limit {
100             info!("task {} speed_limit {}", self.task_id(), limit);
101         }
102     }
103 
network_retry(&self) -> Result<(), TaskError>104     pub(crate) async fn network_retry(&self) -> Result<(), TaskError> {
105         if self.tries.load(Ordering::SeqCst) < RETRY_TIMES {
106             self.tries.fetch_add(1, Ordering::SeqCst);
107             if !NetworkManager::is_online() {
108                 return Err(TaskError::Waiting(TaskPhase::NetworkOffline));
109             } else {
110                 ylong_runtime::time::sleep(Duration::from_millis(RETRY_INTERVAL)).await;
111                 return Err(TaskError::Waiting(TaskPhase::NeedRetry));
112             }
113         }
114         Ok(())
115     }
116 }
117 
change_upload_size(begins: u64, mut ends: i64, size: i64) -> i64118 pub(crate) fn change_upload_size(begins: u64, mut ends: i64, size: i64) -> i64 {
119     if ends < 0 || ends >= size {
120         ends = size - 1;
121     }
122     if begins as i64 > ends {
123         return size;
124     }
125     ends - begins as i64 + 1
126 }
127 
128 impl RequestTask {
new( config: TaskConfig, files: AttachedFiles, client: Client, client_manager: ClientManagerEntry, upload_resume: bool, ) -> RequestTask129     pub(crate) fn new(
130         config: TaskConfig,
131         files: AttachedFiles,
132         client: Client,
133         client_manager: ClientManagerEntry,
134         upload_resume: bool,
135     ) -> RequestTask {
136         let file_len = files.files.len();
137         let action = config.common_data.action;
138 
139         let file_total_size = match action {
140             Action::Upload => {
141                 let mut file_total_size = 0i64;
142                 // If the total size overflows, ignore it.
143                 for size in files.sizes.iter() {
144                     file_total_size += *size;
145                 }
146                 file_total_size
147             }
148             Action::Download => -1,
149             _ => unreachable!("Action::Any in RequestTask::new never reach"),
150         };
151 
152         let mut sizes = files.sizes.clone();
153 
154         if action == Action::Upload && config.common_data.index < sizes.len() as u32 {
155             sizes[config.common_data.index as usize] = change_upload_size(
156                 config.common_data.begins,
157                 config.common_data.ends,
158                 sizes[config.common_data.index as usize],
159             );
160         }
161 
162         let time = get_current_timestamp();
163         let status = TaskStatus::new(time);
164         let progress = Progress::new(sizes);
165 
166         RequestTask {
167             conf: config,
168             client,
169             files: files.files,
170             body_files: files.body_files,
171             ctime: time,
172             mime_type: Mutex::new(String::new()),
173             progress: Mutex::new(progress),
174             tries: AtomicU32::new(0),
175             status: Mutex::new(status),
176             code: Mutex::new(vec![Reason::Default; file_len]),
177             background_notify_time: AtomicU64::new(time),
178             file_total_size: AtomicI64::new(file_total_size),
179             rate_limiting: AtomicU64::new(0),
180             last_notify: AtomicU64::new(time),
181             client_manager,
182             running_result: Mutex::new(None),
183             timeout_tries: AtomicU32::new(0),
184             upload_resume: AtomicBool::new(upload_resume),
185         }
186     }
187 
new_by_info( config: TaskConfig, #[cfg(feature = "oh")] system: SystemConfig, info: TaskInfo, client_manager: ClientManagerEntry, upload_resume: bool, ) -> Result<RequestTask, ErrorCode>188     pub(crate) fn new_by_info(
189         config: TaskConfig,
190         #[cfg(feature = "oh")] system: SystemConfig,
191         info: TaskInfo,
192         client_manager: ClientManagerEntry,
193         upload_resume: bool,
194     ) -> Result<RequestTask, ErrorCode> {
195         #[cfg(feature = "oh")]
196         let (files, client) = check_config(&config, system)?;
197         #[cfg(not(feature = "oh"))]
198         let (files, client) = check_config(&config)?;
199 
200         let file_len = files.files.len();
201         let action = config.common_data.action;
202         let time = get_current_timestamp();
203 
204         let file_total_size = match action {
205             Action::Upload => {
206                 let mut file_total_size = 0i64;
207                 // If the total size overflows, ignore it.
208                 for size in files.sizes.iter() {
209                     file_total_size += *size;
210                 }
211                 file_total_size
212             }
213             Action::Download => *info.progress.sizes.first().unwrap_or(&-1),
214             _ => unreachable!("Action::Any in RequestTask::new never reach"),
215         };
216 
217         // If `TaskInfo` is provided, use data of it.
218         let ctime = info.common_data.ctime;
219         let mime_type = info.mime_type.clone();
220         let tries = info.common_data.tries;
221         let status = TaskStatus {
222             mtime: time,
223             state: State::from(info.progress.common_data.state),
224             reason: Reason::from(info.common_data.reason),
225         };
226         let progress = info.progress;
227 
228         Ok(RequestTask {
229             conf: config,
230             client,
231             files: files.files,
232             body_files: files.body_files,
233             ctime,
234             mime_type: Mutex::new(mime_type),
235             progress: Mutex::new(progress),
236             tries: AtomicU32::new(tries),
237             status: Mutex::new(status),
238             code: Mutex::new(vec![Reason::Default; file_len]),
239             background_notify_time: AtomicU64::new(time),
240             file_total_size: AtomicI64::new(file_total_size),
241             rate_limiting: AtomicU64::new(0),
242             last_notify: AtomicU64::new(time),
243             client_manager,
244             running_result: Mutex::new(None),
245             timeout_tries: AtomicU32::new(0),
246             upload_resume: AtomicBool::new(upload_resume),
247         })
248     }
249 
build_notify_data(&self) -> NotifyData250     pub(crate) fn build_notify_data(&self) -> NotifyData {
251         let vec = self.get_each_file_status();
252         NotifyData {
253             bundle: self.conf.bundle.clone(),
254             // `unwrap` for propagating panics among threads.
255             progress: self.progress.lock().unwrap().clone(),
256             action: self.conf.common_data.action,
257             version: self.conf.version,
258             each_file_status: vec,
259             task_id: self.conf.common_data.task_id,
260         }
261     }
262 
update_progress_in_database(&self)263     pub(crate) fn update_progress_in_database(&self) {
264         let mtime = self.status.lock().unwrap().mtime;
265         let reason = self.status.lock().unwrap().reason;
266         let progress = self.progress.lock().unwrap().clone();
267         let update_info = UpdateInfo {
268             mtime,
269             reason: reason.repr,
270             progress,
271             each_file_status: RequestTask::get_each_file_status_by_code(
272                 &self.code.lock().unwrap(),
273                 &self.conf.file_specs,
274             ),
275             tries: self.tries.load(Ordering::SeqCst),
276             mime_type: self.mime_type(),
277         };
278         RequestDb::get_instance().update_task(self.task_id(), update_info);
279     }
280 
build_request_builder(&self) -> Result<RequestBuilder, HttpClientError>281     pub(crate) fn build_request_builder(&self) -> Result<RequestBuilder, HttpClientError> {
282         use ylong_http_client::async_impl::PercentEncoder;
283 
284         let url = self.conf.url.clone();
285         let url = match PercentEncoder::encode(url.as_str()) {
286             Ok(value) => value,
287             Err(e) => {
288                 error!("url percent encoding error is {:?}", e);
289                 return Err(e);
290             }
291         };
292 
293         let method = match self.conf.method.to_uppercase().as_str() {
294             "PUT" => "PUT",
295             "POST" => "POST",
296             "GET" => "GET",
297             _ => match self.conf.common_data.action {
298                 Action::Upload => {
299                     if self.conf.version == Version::API10 {
300                         "PUT"
301                     } else {
302                         "POST"
303                     }
304                 }
305                 Action::Download => "GET",
306                 _ => "",
307             },
308         };
309         let mut request = RequestBuilder::new().method(method).url(url.as_str());
310         for (key, value) in self.conf.headers.iter() {
311             request = request.header(key.as_str(), value.as_str());
312         }
313         Ok(request)
314     }
315 
clear_downloaded_file(&self) -> Result<(), std::io::Error>316     pub(crate) async fn clear_downloaded_file(&self) -> Result<(), std::io::Error> {
317         info!("task {} clear downloaded file", self.task_id());
318         let file = self.files.get_mut(0).unwrap();
319         file.set_len(0).await?;
320         file.seek(SeekFrom::Start(0)).await?;
321 
322         let mut progress_guard = self.progress.lock().unwrap();
323         progress_guard.common_data.total_processed = 0;
324         progress_guard.processed[0] = 0;
325 
326         Ok(())
327     }
328 
build_download_request(&self) -> Result<Request, TaskError>329     pub(crate) async fn build_download_request(&self) -> Result<Request, TaskError> {
330         let mut request_builder = self.build_request_builder()?;
331 
332         let file = self.files.get_mut(0).unwrap();
333 
334         let has_downloaded = file.metadata().await?.len();
335         let resume_download = has_downloaded > 0;
336         let require_range = self.require_range();
337 
338         let begins = self.conf.common_data.begins;
339         let ends = self.conf.common_data.ends;
340 
341         debug!(
342             "task {} build download request, resume_download: {}, require_range: {}",
343             self.task_id(),
344             resume_download,
345             require_range
346         );
347         match (resume_download, require_range) {
348             (true, false) => {
349                 let (builder, support_range) = self.support_range(request_builder);
350                 request_builder = builder;
351                 if support_range {
352                     request_builder =
353                         self.range_request(request_builder, begins + has_downloaded, ends);
354                 } else {
355                     self.clear_downloaded_file().await?;
356                 }
357             }
358             (false, true) => {
359                 request_builder = self.range_request(request_builder, begins, ends);
360             }
361             (true, true) => {
362                 let (builder, support_range) = self.support_range(request_builder);
363                 request_builder = builder;
364                 if support_range {
365                     request_builder =
366                         self.range_request(request_builder, begins + has_downloaded, ends);
367                 } else {
368                     return Err(TaskError::Failed(Reason::UnsupportedRangeRequest));
369                 }
370             }
371             (false, false) => {}
372         };
373 
374         let request = request_builder.body(Body::slice(self.conf.data.clone()))?;
375         Ok(request)
376     }
377 
range_request( &self, request_builder: RequestBuilder, begins: u64, ends: i64, ) -> RequestBuilder378     fn range_request(
379         &self,
380         request_builder: RequestBuilder,
381         begins: u64,
382         ends: i64,
383     ) -> RequestBuilder {
384         let range = if ends < 0 {
385             format!("bytes={begins}-")
386         } else {
387             format!("bytes={begins}-{ends}")
388         };
389         request_builder.header("Range", range.as_str())
390     }
391 
support_range(&self, mut request_builder: RequestBuilder) -> (RequestBuilder, bool)392     fn support_range(&self, mut request_builder: RequestBuilder) -> (RequestBuilder, bool) {
393         let progress_guard = self.progress.lock().unwrap();
394         let mut support_range = false;
395         if let Some(etag) = progress_guard.extras.get("etag") {
396             request_builder = request_builder.header("If-Range", etag.as_str());
397             support_range = true;
398         } else if let Some(last_modified) = progress_guard.extras.get("last-modified") {
399             request_builder = request_builder.header("If-Range", last_modified.as_str());
400             support_range = true;
401         }
402         if !support_range {
403             info!("task {} not support range", self.task_id());
404         }
405         (request_builder, support_range)
406     }
407 
get_file_info(&self, response: &Response) -> Result<(), TaskError>408     pub(crate) fn get_file_info(&self, response: &Response) -> Result<(), TaskError> {
409         let content_type = response.headers().get("content-type");
410         if let Some(mime_type) = content_type {
411             if let Ok(value) = mime_type.to_string() {
412                 *self.mime_type.lock().unwrap() = value;
413             }
414         }
415 
416         let content_length = response.headers().get("content-length");
417         if let Some(Ok(len)) = content_length.map(|v| v.to_string()) {
418             match len.parse::<i64>() {
419                 Ok(v) => {
420                     let mut progress = self.progress.lock().unwrap();
421                     progress.sizes = vec![v + progress.processed[0] as i64];
422                     self.file_total_size.store(v, Ordering::SeqCst);
423                     debug!("the download task content-length is {}", v);
424                 }
425                 Err(e) => {
426                     error!("convert string to i64 error: {:?}", e);
427                 }
428             }
429         } else {
430             error!("cannot get content-length of the task");
431             if self.conf.common_data.precise {
432                 return Err(TaskError::Failed(Reason::GetFileSizeFailed));
433             }
434         }
435         Ok(())
436     }
437 
handle_download_error( &self, err: HttpClientError, ) -> Result<(), TaskError>438     pub(crate) async fn handle_download_error(
439         &self,
440         err: HttpClientError,
441     ) -> Result<(), TaskError> {
442         if err.error_kind() != ErrorKind::UserAborted {
443             error!("Task {} {:?}", self.task_id(), err);
444         }
445         match err.error_kind() {
446             ErrorKind::Timeout => Err(TaskError::Failed(Reason::ContinuousTaskTimeout)),
447             // user triggered
448             ErrorKind::UserAborted => Err(TaskError::Waiting(TaskPhase::UserAbort)),
449             ErrorKind::BodyTransfer | ErrorKind::BodyDecode => {
450                 self.network_retry().await?;
451                 Err(TaskError::Failed(Reason::OthersError))
452             }
453             _ => {
454                 if format!("{}", err).contains("No space left on device") {
455                     Err(TaskError::Failed(Reason::InsufficientSpace))
456                 } else {
457                     Err(TaskError::Failed(Reason::OthersError))
458                 }
459             }
460         }
461     }
462 
463     #[cfg(feature = "oh")]
notify_response(&self, response: &Response)464     pub(crate) fn notify_response(&self, response: &Response) {
465         let tid = self.conf.common_data.task_id;
466         let version: String = response.version().as_str().into();
467         let status_code: u32 = response.status().as_u16() as u32;
468         let status_message: String;
469         if let Some(reason) = response.status().reason() {
470             status_message = reason.into();
471         } else {
472             error!("bad status_message {:?}", status_code);
473             return;
474         }
475         let headers = response.headers().clone();
476         debug!("notify_response");
477         self.client_manager
478             .send_response(tid, version, status_code, status_message, headers)
479     }
480 
require_range(&self) -> bool481     pub(crate) fn require_range(&self) -> bool {
482         self.conf.common_data.begins > 0 || self.conf.common_data.ends >= 0
483     }
484 
record_upload_response( &self, index: usize, response: Result<Response, HttpClientError>, )485     pub(crate) async fn record_upload_response(
486         &self,
487         index: usize,
488         response: Result<Response, HttpClientError>,
489     ) {
490         if let Ok(mut r) = response {
491             {
492                 let mut guard = self.progress.lock().unwrap();
493                 guard.extras.clear();
494                 for (k, v) in r.headers() {
495                     if let Ok(value) = v.to_string() {
496                         guard.extras.insert(k.to_string().to_lowercase(), value);
497                     }
498                 }
499             }
500 
501             let file = match self.body_files.get_mut(index) {
502                 Some(file) => file,
503                 None => return,
504             };
505             let _ = file.set_len(0).await;
506             loop {
507                 let mut buf = [0u8; 1024];
508                 let size = r.data(&mut buf).await;
509                 let size = match size {
510                     Ok(size) => size,
511                     Err(_e) => break,
512                 };
513 
514                 if size == 0 {
515                     break;
516                 }
517                 let _ = file.write_all(&buf[..size]).await;
518             }
519             // Makes sure all the data has been written to the target file.
520             let _ = file.sync_all().await;
521         }
522     }
523 
get_each_file_status(&self) -> Vec<EachFileStatus>524     pub(crate) fn get_each_file_status(&self) -> Vec<EachFileStatus> {
525         let mut vec = Vec::new();
526         // `unwrap` for propagating panics among threads.
527         let codes_guard = self.code.lock().unwrap();
528         for (i, file_spec) in self.conf.file_specs.iter().enumerate() {
529             let reason = *codes_guard.get(i).unwrap_or(&Reason::Default);
530             vec.push(EachFileStatus {
531                 path: file_spec.path.clone(),
532                 reason,
533                 message: reason.to_str().into(),
534             });
535         }
536         vec
537     }
538 
get_each_file_status_by_code( codes_guard: &MutexGuard<Vec<Reason>>, file_specs: &[FileSpec], ) -> Vec<EachFileStatus>539     pub(crate) fn get_each_file_status_by_code(
540         codes_guard: &MutexGuard<Vec<Reason>>,
541         file_specs: &[FileSpec],
542     ) -> Vec<EachFileStatus> {
543         let mut vec = Vec::new();
544         for (i, file_spec) in file_specs.iter().enumerate() {
545             let reason = *codes_guard.get(i).unwrap_or(&Reason::Default);
546             vec.push(EachFileStatus {
547                 path: file_spec.path.clone(),
548                 reason,
549                 message: reason.to_str().into(),
550             });
551         }
552         vec
553     }
554 
info(&self) -> TaskInfo555     pub(crate) fn info(&self) -> TaskInfo {
556         let status = self.status.lock().unwrap();
557         let progress = self.progress.lock().unwrap();
558         TaskInfo {
559             bundle: self.conf.bundle.clone(),
560             url: self.conf.url.clone(),
561             data: self.conf.data.clone(),
562             token: self.conf.token.clone(),
563             form_items: self.conf.form_items.clone(),
564             file_specs: self.conf.file_specs.clone(),
565             title: self.conf.title.clone(),
566             description: self.conf.description.clone(),
567             mime_type: {
568                 match self.conf.version {
569                     Version::API10 => match self.conf.common_data.action {
570                         Action::Download => match self.conf.headers.get("Content-Type") {
571                             None => "".into(),
572                             Some(v) => v.clone(),
573                         },
574                         Action::Upload => "multipart/form-data".into(),
575                         _ => "".into(),
576                     },
577                     Version::API9 => self.mime_type.lock().unwrap().clone(),
578                 }
579             },
580             progress: progress.clone(),
581             extras: progress.extras.clone(),
582             each_file_status: self.get_each_file_status(),
583             common_data: CommonTaskInfo {
584                 task_id: self.conf.common_data.task_id,
585                 uid: self.conf.common_data.uid,
586                 action: self.conf.common_data.action.repr,
587                 mode: self.conf.common_data.mode.repr,
588                 ctime: self.ctime,
589                 mtime: status.mtime,
590                 reason: status.reason.repr,
591                 gauge: self.conf.common_data.gauge,
592                 retry: self.conf.common_data.retry,
593                 tries: self.tries.load(Ordering::SeqCst),
594                 version: self.conf.version as u8,
595                 priority: self.conf.common_data.priority,
596             },
597         }
598     }
599 
background_notify(&self)600     pub(crate) fn background_notify(&self) {
601         if self.conf.version == Version::API9 && !self.conf.common_data.background {
602             return;
603         }
604         if self.conf.version == Version::API10 && self.conf.common_data.mode == Mode::FrontEnd {
605             return;
606         }
607         let mut file_total_size = self.file_total_size.load(Ordering::SeqCst);
608         let total_processed = self.progress.lock().unwrap().common_data.total_processed as u64;
609         if file_total_size <= 0 || total_processed == 0 {
610             return;
611         }
612         if self.conf.common_data.action == Action::Download {
613             if self.conf.common_data.ends < 0 {
614                 file_total_size -= self.conf.common_data.begins as i64;
615             } else {
616                 file_total_size =
617                     self.conf.common_data.ends - self.conf.common_data.begins as i64 + 1;
618             }
619         }
620         self.background_notify_time
621             .store(get_current_timestamp(), Ordering::SeqCst);
622         let index = self.progress.lock().unwrap().common_data.index;
623         if index >= self.conf.file_specs.len() {
624             return;
625         }
626 
627         #[cfg(feature = "oh")]
628         {
629             let percent = total_processed * 100 / (file_total_size as u64);
630             let task_msg = RequestTaskMsg {
631                 task_id: self.conf.common_data.task_id,
632                 uid: self.conf.common_data.uid as i32,
633                 action: self.conf.common_data.action.repr,
634             };
635 
636             let path = self.conf.file_specs[index].path.as_str();
637             let file_name = self.conf.file_specs[index].file_name.as_str();
638             let _ = request_background_notify(task_msg, path, file_name, percent as u32);
639         }
640     }
641 
notify_header_receive(&self)642     pub(crate) fn notify_header_receive(&self) {
643         if self.conf.version == Version::API9 && self.conf.common_data.action == Action::Upload {
644             let notify_data = self.build_notify_data();
645 
646             Notifier::header_receive(&self.client_manager, notify_data);
647         }
648     }
649 }
650 
651 #[derive(Clone, Debug)]
652 pub(crate) struct TaskStatus {
653     pub(crate) mtime: u64,
654     pub(crate) state: State,
655     pub(crate) reason: Reason,
656 }
657 
658 impl TaskStatus {
new(mtime: u64) -> Self659     pub(crate) fn new(mtime: u64) -> Self {
660         TaskStatus {
661             mtime,
662             state: State::Initialized,
663             reason: Reason::Default,
664         }
665     }
666 }
667 
check_file_specs(file_specs: &[FileSpec]) -> bool668 fn check_file_specs(file_specs: &[FileSpec]) -> bool {
669     const EL1: &str = "/data/storage/el1/base/";
670     const EL2: &str = "/data/storage/el2/base/";
671     const EL5: &str = "/data/storage/el5/base/";
672 
673     let mut result = true;
674     for (idx, spec) in file_specs.iter().enumerate() {
675         let path = &spec.path;
676         if !spec.is_user_file && !path.starts_with(EL1) && !path.starts_with(EL2) && !path.starts_with(EL5) {
677             error!("File path invalid - path: {}, idx: {}", path, idx);
678             result = false;
679             break;
680         }
681     }
682 
683     result
684 }
685 
check_config( config: &TaskConfig, #[cfg(feature = "oh")] system: SystemConfig, ) -> Result<(AttachedFiles, Client), ErrorCode>686 pub(crate) fn check_config(
687     config: &TaskConfig,
688     #[cfg(feature = "oh")] system: SystemConfig,
689 ) -> Result<(AttachedFiles, Client), ErrorCode> {
690     if !check_file_specs(&config.file_specs) {
691         return Err(ErrorCode::Other);
692     }
693     let files = AttachedFiles::open(config).map_err(|_| ErrorCode::FileOperationErr)?;
694     #[cfg(feature = "oh")]
695     let client = build_client(config, system).map_err(|_| ErrorCode::Other)?;
696 
697     #[cfg(not(feature = "oh"))]
698     let client = build_client(config).map_err(|_| ErrorCode::Other)?;
699     Ok((files, client))
700 }
701 
702 impl From<HttpClientError> for TaskError {
from(_value: HttpClientError) -> Self703     fn from(_value: HttpClientError) -> Self {
704         TaskError::Failed(Reason::BuildRequestFailed)
705     }
706 }
707 
708 impl From<io::Error> for TaskError {
from(_value: io::Error) -> Self709     fn from(_value: io::Error) -> Self {
710         TaskError::Failed(Reason::IoError)
711     }
712 }
713 
714 #[derive(Debug, PartialEq, Eq)]
715 pub enum TaskPhase {
716     NeedRetry,
717     UserAbort,
718     NetworkOffline,
719 }
720 
721 #[derive(Debug, PartialEq, Eq)]
722 pub enum TaskError {
723     Failed(Reason),
724     Waiting(TaskPhase),
725 }
726 
727 #[cfg(test)]
728 mod test {
729     use crate::task::request_task::change_upload_size;
730 
731     #[test]
ut_upload_size()732     fn ut_upload_size() {
733         assert_eq!(change_upload_size(0, -1, 30), 30);
734         assert_eq!(change_upload_size(10, -1, 30), 20);
735         assert_eq!(change_upload_size(0, 10, 30), 11);
736         assert_eq!(change_upload_size(10, 10, 100), 1);
737         assert_eq!(change_upload_size(0, 30, 30), 30);
738         assert_eq!(change_upload_size(0, 0, 0), 0);
739         assert_eq!(change_upload_size(10, 9, 100), 100);
740     }
741 }
742