• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 use clap::Parser;
17 use std::path::PathBuf;
18 use ylong_runtime::{block_on, sync::mpsc::bounded::bounded_channel};
19 
20 mod backup;
21 
22 #[derive(Parser, Debug)]
23 #[command(author, version, about, long_about = None)]
24 struct Args {
25     /// Tar file saving path
26     #[clap(short, long)]
27     stash_dir: String,
28 
29     /// Files and directories that you want to back up
30     #[clap(short, long)]
31     includes: Vec<String>,
32 
33     /// includes exceptions that do not need to be backed up
34     #[clap(short, long)]
35     excludes: Vec<String>,
36 }
37 
backup_main()38 fn backup_main() {
39     let mut args = Args::parse();
40     args.excludes.push(args.stash_dir.clone());
41     println!("{:#?}", args);
42 
43     let (paths_to_backed_tx, paths_to_backed_rx) = bounded_channel(100);
44     let (outputs_tx, mut outputs_rx) = bounded_channel(100);
45 
46     let handles = vec![
47         ylong_runtime::spawn(async move {
48             let _ = backup::scan_files(args.includes, args.excludes, paths_to_backed_tx)
49                 .await
50                 .unwrap();
51         }),
52         ylong_runtime::spawn(async move {
53             let option = backup::Options {
54                 stash_dir: PathBuf::from(args.stash_dir),
55                 ..Default::default()
56             };
57             let _ = backup::backup_files(option, paths_to_backed_rx, outputs_tx)
58                 .await
59                 .unwrap();
60         }),
61         ylong_runtime::spawn(async move {
62             while let Ok(archive) = outputs_rx.recv().await {
63                 println!("output: {:?}", archive);
64             }
65         }),
66     ];
67     for handle in handles {
68         block_on(handle).unwrap();
69     }
70 }
71 
main()72 fn main() {
73     backup_main();
74 }
75