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 "lseek_ani.h"
17
18 #include "error_handler.h"
19 #include "filemgmt_libhilog.h"
20 #include "lseek_core.h"
21 #include "type_converter.h"
22
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 namespace ANI {
27 using namespace std;
28
ParseSeekPos(const optional<int32_t> & whence)29 optional<SeekPos> ParseSeekPos(const optional<int32_t> &whence)
30 {
31 if (!whence.has_value()) {
32 return nullopt;
33 }
34
35 return make_optional(static_cast<SeekPos>(move(whence.value())));
36 }
37
LseekSync(ani_env * env,ani_class clazz,ani_double fd,ani_double offset,ani_enum_item whence)38 ani_double LseekAni::LseekSync(
39 ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_double offset, ani_enum_item whence)
40 {
41 auto [succWhence, whenceOp] = TypeConverter::EnumToInt32(env, whence);
42 if (!succWhence) {
43 HILOGE("Invalid whence");
44 ErrorHandler::Throw(env, EINVAL);
45 return -1;
46 }
47
48 auto pos = ParseSeekPos(whenceOp);
49 auto ret = LseekCore::DoLseek(static_cast<int32_t>(fd), static_cast<int64_t>(offset), pos);
50 if (!ret.IsSuccess()) {
51 HILOGE("DoLseek failed!");
52 const FsError &err = ret.GetError();
53 ErrorHandler::Throw(env, err);
54 return -1;
55 }
56
57 return ani_double(static_cast<double>(ret.GetData().value()));
58 }
59
60 } // namespace ANI
61 } // namespace ModuleFileIO
62 } // namespace FileManagement
63 } // namespace OHOS