• 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::collections::HashMap;
15 
16 use super::config::{Action, Version};
17 use super::info::State;
18 use super::reason::Reason;
19 use crate::FileSpec;
20 
21 // NotifyData's callback arg
22 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
23 pub(crate) enum SubscribeType {
24     Complete = 0,
25     Fail,
26     HeaderReceive,
27     Pause,
28     Progress,
29     Remove,
30     Resume,
31 }
32 
33 #[derive(Debug, Clone)]
34 pub(crate) struct NotifyData {
35     pub(crate) bundle: String,
36     pub(crate) progress: Progress,
37     pub(crate) action: Action,
38     pub(crate) version: Version,
39     pub(crate) each_file_status: Vec<EachFileStatus>,
40     pub(crate) task_id: u32,
41     pub(crate) uid: u64,
42 }
43 
44 #[repr(C)]
45 #[derive(Clone, Debug)]
46 pub(crate) struct CommonProgress {
47     pub(crate) state: u8,
48     pub(crate) index: usize,
49     pub(crate) total_processed: usize,
50 }
51 
52 #[derive(Debug, Clone)]
53 pub(crate) struct Progress {
54     pub(crate) common_data: CommonProgress,
55     /// Total size of the files.
56     pub(crate) sizes: Vec<i64>,
57     /// Each progress size of the files.
58     pub(crate) processed: Vec<usize>,
59     pub(crate) extras: HashMap<String, String>,
60 }
61 
62 #[derive(Debug, Clone, PartialEq, Eq)]
63 #[repr(C)]
64 pub(crate) struct EachFileStatus {
65     pub(crate) path: String,
66     pub(crate) reason: Reason,
67     pub(crate) message: String,
68 }
69 
70 impl EachFileStatus {
create_each_file_status( file_specs: &[FileSpec], index: usize, reason: Reason, ) -> Vec<EachFileStatus>71     pub(crate) fn create_each_file_status(
72         file_specs: &[FileSpec],
73         index: usize,
74         reason: Reason,
75     ) -> Vec<EachFileStatus> {
76         let mut vec = Vec::new();
77         for (i, file_spec) in file_specs.iter().enumerate() {
78             let code = if i >= index { reason } else { Reason::Default };
79             let each_file_status = EachFileStatus {
80                 path: file_spec.path.clone(),
81                 reason: code,
82                 message: code.to_str().into(),
83             };
84             vec.push(each_file_status);
85         }
86         vec
87     }
88 }
89 
90 impl Progress {
new(sizes: Vec<i64>) -> Self91     pub(crate) fn new(sizes: Vec<i64>) -> Self {
92         let len = sizes.len();
93         Progress {
94             common_data: CommonProgress {
95                 state: State::Initialized.repr,
96                 index: 0,
97                 total_processed: 0,
98             },
99             sizes,
100             processed: vec![0; len],
101             extras: HashMap::<String, String>::new(),
102         }
103     }
104 
is_finish(&self) -> bool105     pub(crate) fn is_finish(&self) -> bool {
106         self.sizes.iter().all(|a| *a != -1)
107             && self.processed.iter().sum::<usize>() == self.sizes.iter().sum::<i64>() as usize
108     }
109 }
110