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 #include "b_error/b_excep_utils.h"
17
18 #include <string_view>
19 #include <cstdlib>
20 #include <climits>
21
22 #include "b_resources/b_constants.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26
VerifyPath(const string_view & path,bool isExtension)27 void BExcepUltils::VerifyPath(const string_view &path, bool isExtension)
28 {
29 try {
30 string absPath = BExcepUltils::Canonicalize(path);
31 if (isExtension &&
32 absPath.find(string(BConstants::PATH_BUNDLE_BACKUP_HOME)
33 .append(BConstants::SA_BUNDLE_BACKUP_RESTORE)) == string::npos) {
34 throw BError(BError::Codes::EXT_INVAL_ARG, "Invalid path, not in backup restore path");
35 }
36 } catch (const BError &e) {
37 throw BError(BError::Codes::EXT_INVAL_ARG, e.what());
38 }
39 }
40
Canonicalize(const string_view & path)41 string BExcepUltils::Canonicalize(const string_view &path)
42 {
43 try {
44 char full_path[PATH_MAX] = {0};
45 //1.转换绝对路径到dir
46 #ifdef _WIN32
47 _fullpath(full_path, path.data(), PATH_MAX);
48 #else
49 realpath(path.data(), full_path);
50 #endif
51 return string(full_path);
52 } catch (const exception &e) {
53 throw BError(BError::Codes::EXT_INVAL_ARG, "Invalid path");
54 }
55 }
56 } // namespace OHOS::FileManagement::Backup
57