1 /*
2  * Copyright (c) 2022-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 #ifndef OHOS_FILEMGMT_BACKUP_B_TARBALL_FACTORY_H
17 #define OHOS_FILEMGMT_BACKUP_B_TARBALL_FACTORY_H
18 
19 /**
20  * @file b_tarball.h
21  * @brief 使用指定实现打包/解包,本层在进行打包/解包前还负责防御路径穿越攻击
22  *
23  */
24 #include <functional>
25 #include <memory>
26 #include <string_view>
27 
28 #include "nocopyable.h"
29 
30 namespace OHOS::FileManagement::Backup {
31 class BTarballFactory final : protected NoCopyable {
32 public:
33     /**
34      * @brief 打包器仿函数集合
35      * 当前迫于时间所限,不得不采用命令行实现。后续必然要实现自研打包协议,从而满足在同一个进程进行打包的诉求。
36      * 在降低调用成本后,就可以实现逐个向tarball追加文件的append方法。append方法是优化方法,和tar二选一即可。
37      * 然而纯虚函数必须得在子类实现,这一性质使得基于继承的设计模式无法方便地选择合适的打包方法,为了解决这个问题,
38      * 这里采用组合的方式实现。现在在外层简单地判断相应仿函数是否为空,就知道如何进行选择了。
39      */
40     struct Impl {
41         /**
42          * @brief 打包
43          *
44          * @param _1 进入该参数指定路径打包文件。
45          * An absolute path is required.
46          * @param _2 _1中需要打包的路径。
47          * 要求输入相对路径,除禁止路径穿越外无其余要求,不填默认全部打包
48          * @param _3 The part that does not need to be packed in the path to be packed.
49          * 要求输入相对路径,路径穿越场景无实际意义因此予以禁止。可用于排除部分子目录
50          */
51         std::function<void(std::string_view, std::vector<std::string_view>, std::vector<std::string_view>)> tar;
52 
53         /**
54          * @brief 解包
55          *
56          * @param _1 用于存储解包文件的根目录
57          * An absolute path is required.
58          */
59         std::function<void(std::string_view)> untar;
60     };
61 
62 public:
63     /**
64      * @brief 打包器工厂方法
65      *
66      * @param implType 打包器实现方式,可选择'cmdline'
67      * @param tarballPath Absolute path of the file package。Cannot contain extra slashes, must be suffixed with .tar
68      * @return std::unique_ptr<Impl> 打包器仿函数集合
69      */
70     static std::unique_ptr<Impl> Create(std::string_view implType, std::string_view tarballPath);
71 
72 public:
73     BTarballFactory() = delete;
74 };
75 } // namespace OHOS::FileManagement::Backup
76 
77 #endif // OHOS_FILEMGMT_BACKUP_B_TARBALL_FACTORY_H