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 "xattr_ani.h"
17
18 #include "error_handler.h"
19 #include "filemgmt_libhilog.h"
20 #include "type_converter.h"
21 #include "xattr_core.h"
22
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 namespace ANI {
27
SetXattrSync(ani_env * env,ani_class clazz,ani_string path,ani_string key,ani_string value)28 void XattrAni::SetXattrSync(
29 ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_string key, ani_string value)
30 {
31 auto [pathSucc, pathStr] = TypeConverter::ToUTF8String(env, path);
32 if (!pathSucc) {
33 HILOGE("Invalid path");
34 ErrorHandler::Throw(env, EINVAL);
35 return;
36 }
37
38 auto [keySucc, keyStr] = TypeConverter::ToUTF8String(env, key);
39 if (!keySucc) {
40 HILOGE("Invalid xattr key");
41 ErrorHandler::Throw(env, EINVAL);
42 return;
43 }
44
45 auto [valueSucc, valueStr] = TypeConverter::ToUTF8String(env, value);
46 if (!valueSucc) {
47 HILOGE("Invalid xattr value");
48 ErrorHandler::Throw(env, EINVAL);
49 return;
50 }
51
52 auto ret = XattrCore::DoSetXattr(pathStr, keyStr, valueStr);
53 if (!ret.IsSuccess()) {
54 HILOGE("DoSetXattr failed");
55 const auto &err = ret.GetError();
56 ErrorHandler::Throw(env, err);
57 return;
58 }
59 }
60
GetXattrSync(ani_env * env,ani_class clazz,ani_string path,ani_string key)61 ani_string XattrAni::GetXattrSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_string key)
62 {
63 auto [pathSucc, pathStr] = TypeConverter::ToUTF8String(env, path);
64 if (!pathSucc) {
65 HILOGE("Invalid path");
66 ErrorHandler::Throw(env, EINVAL);
67 return nullptr;
68 }
69
70 auto [keySucc, keyStr] = TypeConverter::ToUTF8String(env, key);
71 if (!keySucc) {
72 HILOGE("Invalid xattr key");
73 ErrorHandler::Throw(env, EINVAL);
74 return nullptr;
75 }
76
77 auto ret = XattrCore::DoGetXattr(pathStr, keyStr);
78 if (!ret.IsSuccess()) {
79 HILOGE("DoSetXattr failed");
80 const auto &err = ret.GetError();
81 ErrorHandler::Throw(env, err);
82 return nullptr;
83 }
84
85 const auto &res = ret.GetData().value();
86 auto [succ, result] = TypeConverter::ToAniString(env, res);
87 if (!succ) {
88 HILOGE("Create ani_string error");
89 ErrorHandler::Throw(env, UNKNOWN_ERR);
90 return nullptr;
91 }
92 return result;
93 }
94
95 } // namespace ANI
96 } // namespace ModuleFileIO
97 } // namespace FileManagement
98 } // namespace OHOS