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 "file_ani.h"
17
18 #include "error_handler.h"
19 #include "file_wrapper.h"
20 #include "filemgmt_libhilog.h"
21 #include "fs_file.h"
22 #include "type_converter.h"
23
24 namespace OHOS {
25 namespace FileManagement {
26 namespace ModuleFileIO {
27 namespace ANI {
28 using namespace OHOS::FileManagement::ModuleFileIO;
29 using namespace std;
30
GetParent(ani_env * env,ani_object object)31 ani_string FileAni::GetParent(ani_env *env, [[maybe_unused]] ani_object object)
32 {
33 auto fsFile = FileWrapper::Unwrap(env, object);
34 if (fsFile == nullptr) {
35 HILOGE("Cannot unwrap fsfile!");
36 ErrorHandler::Throw(env, UNKNOWN_ERR);
37 return {};
38 }
39 auto ret = fsFile->GetParent();
40 if (!ret.IsSuccess()) {
41 HILOGE("Cannot get file parent!");
42 const auto &err = ret.GetError();
43 ErrorHandler::Throw(env, err);
44 return {};
45 }
46 auto value = ret.GetData().value();
47 auto [succ, parent] = TypeConverter::ToAniString(env, value);
48 if (!succ) {
49 HILOGE("Cannot convert file parent to ani string!");
50 ErrorHandler::Throw(env, UNKNOWN_ERR);
51 return {};
52 }
53 return parent;
54 }
55
LockSync(ani_env * env,ani_object object,ani_object exclusive)56 void FileAni::LockSync(ani_env *env, [[maybe_unused]] ani_object object, ani_object exclusive)
57 {
58 ani_boolean isUndefined;
59 bool exc = false;
60 env->Reference_IsUndefined(exclusive, &isUndefined);
61 if (!isUndefined) {
62 exc = true;
63 }
64 auto fsFile = FileWrapper::Unwrap(env, object);
65 if (fsFile == nullptr) {
66 HILOGE("Cannot unwrap fsfile!");
67 ErrorHandler::Throw(env, UNKNOWN_ERR);
68 return;
69 }
70 auto ret = fsFile->Lock(exc);
71 if (!ret.IsSuccess()) {
72 HILOGE("Lock file failed!");
73 const auto &err = ret.GetError();
74 ErrorHandler::Throw(env, err);
75 return;
76 }
77 }
78
TryLock(ani_env * env,ani_object object,ani_object exclusive)79 void FileAni::TryLock(ani_env *env, [[maybe_unused]] ani_object object, ani_object exclusive)
80 {
81 ani_boolean isUndefined;
82 bool exc = false;
83 env->Reference_IsUndefined(exclusive, &isUndefined);
84 if (!isUndefined) {
85 exc = true;
86 }
87 auto fsFile = FileWrapper::Unwrap(env, object);
88 if (fsFile == nullptr) {
89 HILOGE("Cannot unwrap fsfile!");
90 ErrorHandler::Throw(env, UNKNOWN_ERR);
91 return;
92 }
93 auto ret = fsFile->TryLock(exc);
94 if (!ret.IsSuccess()) {
95 HILOGE("TryLock file failed!");
96 const auto &err = ret.GetError();
97 ErrorHandler::Throw(env, err);
98 return;
99 }
100 }
101
UnLock(ani_env * env,ani_object object)102 void FileAni::UnLock(ani_env *env, [[maybe_unused]] ani_object object)
103 {
104 auto fsFile = FileWrapper::Unwrap(env, object);
105 if (fsFile == nullptr) {
106 HILOGE("Cannot unwrap fsfile!");
107 ErrorHandler::Throw(env, UNKNOWN_ERR);
108 return;
109 }
110 auto ret = fsFile->UnLock();
111 if (!ret.IsSuccess()) {
112 HILOGE("UnLock file failed!");
113 const auto &err = ret.GetError();
114 ErrorHandler::Throw(env, err);
115 return;
116 }
117 }
118 } // namespace ANI
119 } // namespace ModuleFileIO
120 } // namespace FileManagement
121 } // namespace OHOS