• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "securitylabel_ani.h"
17 
18 #include "error_handler.h"
19 #include "filemgmt_libhilog.h"
20 #include "securitylabel_core.h"
21 #include "type_converter.h"
22 
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 namespace ANI {
27 
28 using namespace std;
29 using namespace OHOS::FileManagement::ModuleFileIO;
30 using namespace OHOS::FileManagement::ModuleSecurityLabel;
31 
SetSecurityLabelSync(ani_env * env,ani_class clazz,ani_string path,ani_string level)32 void SecurityLabelAni::SetSecurityLabelSync(
33     ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_string level)
34 {
35     auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path);
36     if (!succPath) {
37         HILOGE("Invalid path");
38         ErrorHandler::Throw(env, EINVAL);
39         return;
40     }
41 
42     auto [succLevel, dataLevel] = TypeConverter::ToUTF8String(env, level);
43     if (!succLevel) {
44         HILOGE("Invalid dataLevel");
45         ErrorHandler::Throw(env, EINVAL);
46         return;
47     }
48 
49     auto ret = DoSetSecurityLabel(srcPath, dataLevel);
50     if (!ret.IsSuccess()) {
51         HILOGE("Set securitylabel failed");
52         const auto &err = ret.GetError();
53         ErrorHandler::Throw(env, err);
54         return;
55     }
56 }
57 
GetSecurityLabelSync(ani_env * env,ani_class clazz,ani_string path)58 ani_string SecurityLabelAni::GetSecurityLabelSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path)
59 {
60     auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path);
61     if (!succPath) {
62         HILOGE("Invalid path");
63         ErrorHandler::Throw(env, EINVAL);
64         return nullptr;
65     }
66 
67     auto ret = DoGetSecurityLabel(srcPath);
68     if (!ret.IsSuccess()) {
69         HILOGE("Get securitylabel failed");
70         const auto &err = ret.GetError();
71         ErrorHandler::Throw(env, err);
72         return nullptr;
73     }
74 
75     string res = ret.GetData().value();
76     auto [succ, result] = TypeConverter::ToAniString(env, res);
77     if (!succ) {
78         HILOGE("Create ani_string error");
79         ErrorHandler::Throw(env, UNKNOWN_ERR);
80         return nullptr;
81     }
82 
83     return result;
84 }
85 
86 } // namespace ANI
87 } // namespace ModuleFileIO
88 } // namespace FileManagement
89 } // namespace OHOS