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_PROCESS_H
17 #define OHOS_FILEMGMT_BACKUP_B_PROCESS_H
18 
19 #include <functional>
20 #include <string_view>
21 #include <tuple>
22 #include <vector>
23 
24 #include "errors.h"
25 #include "nocopyable.h"
26 
27 namespace OHOS::FileManagement::Backup {
28 class BProcess final : protected NoCopyable {
29 public:
30     /**
31      * @brief 执行一个命令并同步等待执行结果
32      *
33      * @param argv 命令参数表
34      * Parameter one is the command name represented by the absolute path.
35      * After that, parameter indicates the corresponding command parameter.
36      * Finally,nullptr does not need to be appended.
37      *
38      * @param DetectFatalLog 回调函数,用来对命令的stderr输出错误信息做进一步处理,检测是否发生了严重错误等。
39      *
40      * @return 回调函数返回值,命令执行结果
41      *
42      * @throw BError(UTILS_INVAL_PROCESS_ARG) 系统调用异常(子进程启动失败、waitpid调用失败)
43      *
44      * @throw BError(UTILS_INTERRUPTED_PROCESS) 系统调用异常(pipe调用失败、dup2调用失败、子进程被信号终止)
45      */
46     static std::tuple<bool, ErrCode> ExecuteCmd(std::vector<std::string_view> argv,
47                                                 std::function<bool(std::string_view)> DetectFatalLog = nullptr);
48 
49 private:
50     BProcess() = delete;
51 };
52 } // namespace OHOS::FileManagement::Backup
53 
54 #endif // OHOS_FILEMGMT_BACKUP_B_PROCESS_H