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 #include "b_tarball/b_tarball_cmdline.h"
17
18 #include <sstream>
19 #include <string_view>
20 #include <unistd.h>
21
22 #include "b_error/b_error.h"
23 #include "b_filesystem/b_dir.h"
24 #include "b_process/b_guard_cwd.h"
25 #include "b_process/b_process.h"
26 #include "filemgmt_libhilog.h"
27
28 namespace OHOS::FileManagement::Backup {
29 using namespace std;
30
31 namespace {
32 const vector<string_view> COMMAND_INJECTION = {
33 "--to-command", "--xform", "-op", "--checkpoint", "--checkpoint-action",
34 };
35 } // namespace
36
VerifyArgv(const vector<string_view> & argv)37 static void VerifyArgv(const vector<string_view> &argv)
38 {
39 for (auto &arg : argv) {
40 if (std::any_of(COMMAND_INJECTION.begin(), COMMAND_INJECTION.end(),
41 [&arg](const string_view &cmd) { return arg == cmd; })) {
42 HILOGE("Invalid argv: %{public}s", arg.data());
43 throw BError(BError::Codes::EXT_INVAL_ARG, "Invalid argv");
44 }
45 }
46 }
47
IsTarFatalErrorOccur(string_view output)48 static bool IsTarFatalErrorOccur(string_view output)
49 {
50 vector<string_view> fatalError {"EOF", "bad xform", "bad header", "sparse overflow",
51 "short header", "empty archive", "Not tar"};
52 for (auto &item : fatalError) {
53 if (output.find(item) != string_view::npos) {
54 return true;
55 }
56 }
57 return false;
58 }
59
Tar(string_view root,vector<string_view> includes,vector<string_view> excludes)60 void BTarballCmdline::Tar(string_view root, vector<string_view> includes, vector<string_view> excludes)
61 {
62 // 切换到根路径,从而在打包时使用文件或目录的相对路径
63 BGuardCwd guard(root);
64
65 vector<string_view> argv = {
66 "/system/bin/tar",
67 "-cf",
68 tarballPath_,
69 };
70
71 if (includes.empty()) {
72 throw BError(BError::Codes::UTILS_INVAL_TARBALL_ARG, "tar includes argument must be not empty");
73 }
74
75 vector<string> includesDirs = BDir::GetDirs(includes);
76 if (includesDirs.empty()) {
77 HILOGE("The package path does not exist, and an empty package is generated");
78 includesDirs.push_back("");
79 }
80 for (auto &&include : includesDirs) {
81 argv.push_back(include);
82 }
83 vector<string> excludesDirs = BDir::GetDirs(excludes);
84 for (auto &&exclude : excludesDirs) {
85 argv.push_back("--exclude");
86 argv.push_back(exclude);
87 }
88
89 VerifyArgv(argv);
90
91 // 如果打包后生成了打包文件,则默认打包器打包时生成的错误可以忽略(比如打包一个不存在的文件)
92 auto [bFatalError, errCode] = BProcess::ExecuteCmd(argv, IsTarFatalErrorOccur);
93 if (bFatalError || (errCode && access(tarballPath_.data(), F_OK) != 0)) {
94 stringstream ss;
95 ss << "Is a fatal error occurred: " << bFatalError << ", error code : " << errCode;
96 throw BError(BError::Codes::UTILS_INVAL_PROCESS_ARG, ss.str());
97 }
98 }
99
Untar(string_view root)100 void BTarballCmdline::Untar(string_view root)
101 {
102 vector<string_view> argv = {
103 "tar", "-xf", tarballPath_, "-C", root,
104 };
105 auto [bFatalError, errCode] = BProcess::ExecuteCmd(argv, IsTarFatalErrorOccur);
106 if (bFatalError) {
107 stringstream ss;
108 ss << "Is a fatal error occurred in untar process: " << bFatalError << ", error code : " << errCode;
109 throw BError(BError::Codes::UTILS_INVAL_PROCESS_ARG, ss.str());
110 }
111 }
112
BTarballCmdline(string_view tarballDir,string_view tarballName)113 BTarballCmdline::BTarballCmdline(string_view tarballDir, string_view tarballName)
114 : tarballDir_(tarballDir), tarballName_(tarballName)
115 {
116 tarballPath_ = tarballDir_ + "/" + tarballName_;
117 }
118 } // namespace OHOS::FileManagement::Backup
119