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
20 #include "b_resources/b_constants.h"
21 #include "cxx.h"
22 #include "lib.rs.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 auto ret = canonicalize(path.data());
31 string absPath = ret.c_str();
32 if (isExtension &&
33 absPath.find(string(BConstants::PATH_BUNDLE_BACKUP_HOME)
34 .append(BConstants::SA_BUNDLE_BACKUP_RESTORE)) == std::string::npos) {
35 throw BError(BError::Codes::EXT_INVAL_ARG, "Invalid path, not in backup restore path");
36 }
37 } catch (const rust::Error &e) {
38 throw BError(BError::Codes::EXT_INVAL_ARG, "Invalid path");
39 }
40 }
41
Canonicalize(const string_view & path)42 string BExcepUltils::Canonicalize(const string_view &path)
43 {
44 try {
45 auto ret = canonicalize(path.data());
46 return ret.c_str();
47 } catch (const rust::Error &e) {
48 throw BError(BError::Codes::EXT_INVAL_ARG, "Invalid path");
49 }
50 }
51 } // namespace OHOS::FileManagement::Backup
52