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
IsTarFatalErrorOccur(string_view output)31 static bool IsTarFatalErrorOccur(string_view output)
32 {
33 vector<string_view> fatalError {"EOF", "bad xform", "bad header", "sparse overflow",
34 "short header", "empty archive", "Not tar"};
35 for (auto &item : fatalError) {
36 if (output.find(item) != string_view::npos) {
37 return true;
38 }
39 }
40 return false;
41 }
42
Tar(string_view root,vector<string_view> includes,vector<string_view> excludes)43 void BTarballCmdline::Tar(string_view root, vector<string_view> includes, vector<string_view> excludes)
44 {
45 // 切换到根路径,从而在打包时使用文件或目录的相对路径
46 BGuardCwd guard(root);
47
48 vector<string_view> argv = {
49 "/system/bin/tar",
50 "-cf",
51 tarballPath_,
52 };
53
54 if (includes.empty()) {
55 throw BError(BError::Codes::UTILS_INVAL_TARBALL_ARG, "tar includes argument must be not empty");
56 }
57
58 vector<string> includesDirs = BDir::GetDirs(includes);
59 if (includesDirs.empty()) {
60 HILOGE("The package path does not exist, and an empty package is generated");
61 includesDirs.push_back("");
62 }
63 for (auto &&include : includesDirs) {
64 argv.push_back(include);
65 }
66 vector<string> excludesDirs = BDir::GetDirs(excludes);
67 for (auto &&exclude : excludesDirs) {
68 argv.push_back("--exclude");
69 argv.push_back(exclude);
70 }
71
72 // 如果打包后生成了打包文件,则默认打包器打包时生成的错误可以忽略(比如打包一个不存在的文件)
73 auto [bFatalError, errCode] = BProcess::ExecuteCmd(argv, IsTarFatalErrorOccur);
74 if (bFatalError || (errCode && access(tarballPath_.data(), F_OK) != 0)) {
75 stringstream ss;
76 ss << "Is a fatal error occurred: " << bFatalError << ", error code : " << errCode;
77 throw BError(BError::Codes::UTILS_INVAL_PROCESS_ARG, ss.str());
78 }
79 }
80
Untar(string_view root)81 void BTarballCmdline::Untar(string_view root)
82 {
83 vector<string_view> argv = {
84 "tar", "-xf", tarballPath_, "-C", root,
85 };
86 auto [bFatalError, errCode] = BProcess::ExecuteCmd(argv, IsTarFatalErrorOccur);
87 if (bFatalError) {
88 stringstream ss;
89 ss << "Is a fatal error occurred in untar process: " << bFatalError << ", error code : " << errCode;
90 throw BError(BError::Codes::UTILS_INVAL_PROCESS_ARG, ss.str());
91 }
92 }
93
BTarballCmdline(string_view tarballDir,string_view tarballName)94 BTarballCmdline::BTarballCmdline(string_view tarballDir, string_view tarballName)
95 : tarballDir_(tarballDir), tarballName_(tarballName)
96 {
97 tarballPath_ = tarballDir_ + "/" + tarballName_;
98 }
99 } // namespace OHOS::FileManagement::Backup
100