diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 9334220be9d28e01972e60ed84bec52d044d4e8a..5f52730dba641a902c6d8d09e6d1d85162e89238 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -684,6 +684,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/ani/close_ani.cpp", "src/mod_fs/properties/ani/copy_file_ani.cpp", "src/mod_fs/properties/ani/listfile_ani.cpp", + "src/mod_fs/properties/ani/lseek_ani.cpp", "src/mod_fs/properties/ani/mkdir_ani.cpp", "src/mod_fs/properties/ani/move_ani.cpp", "src/mod_fs/properties/ani/open_ani.cpp", @@ -696,6 +697,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/close_core.cpp", "src/mod_fs/properties/copy_file_core.cpp", "src/mod_fs/properties/listfile_core.cpp", + "src/mod_fs/properties/lseek_core.cpp", "src/mod_fs/properties/mkdir_core.cpp", "src/mod_fs/properties/move_core.cpp", "src/mod_fs/properties/open_core.cpp", diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index 065495c9ade9c4c775ce494fa218d0c382077459..35a732ec8db6224a2f9612021bddf1071cb6d1a6 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -24,6 +24,7 @@ #include "file_ani.h" #include "filemgmt_libhilog.h" #include "listfile_ani.h" +#include "lseek_ani.h" #include "mkdir_ani.h" #include "move_ani.h" #include "open_ani.h" @@ -77,6 +78,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, + ani_native_function { "lseekSync", nullptr, reinterpret_cast(LseekAni::LseekSync) }, ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index 67eb95add30a0cd803f0542ae46c90f870189651..c925e518002e1fcbad06c3d8e6fa5a7c1229bf02 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -492,6 +492,10 @@ function stat(file: string | number, callback: AsyncCallback): void }); } +function lseek(fd: number, offset: number, whence?: WhenceType): number { + return FileIoImpl.lseekSync(fd, offset, whence); +} + export interface Filter { suffix?: Array; displayName?: Array; @@ -623,6 +627,12 @@ class StatInner implements Stat { native isSymbolicLink(): boolean; } +enum WhenceType { + SEEK_SET = 0, + SEEK_CUR = 1, + SEEK_END = 2 +} + class FileIoImpl { static { @@ -637,6 +647,8 @@ class FileIoImpl { static native listFileSync(path: string, options?: ListFileOptions): string[]; + static native lseekSync(fd: number, offset: number, whence?: WhenceType): number; + static native mkdirSync(path: string): void; static native mkdirSync(path: string, recursion: boolean): void; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d689c696ff61f52234c577d46c741d24b5753c0 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lseek_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "lseek_core.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace std; + +optional ParseSeekPos(const optional &whence) +{ + if (!whence.has_value()) { + return nullopt; + } + + return make_optional(static_cast(move(whence.value()))); +} + +ani_double LseekAni::LseekSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_double offset, ani_enum_item whence) +{ + auto [succWhence, whenceOp] = TypeConverter::EnumToInt32(env, whence); + if (!succWhence) { + HILOGE("Invalid whence"); + ErrorHandler::Throw(env, EINVAL); + return -1; + } + auto pos = ParseSeekPos(whenceOp); + + auto ret = LseekCore::DoLseek(static_cast(fd), static_cast(offset), pos); + if (!ret.IsSuccess()) { + HILOGE("DoLseek failed!"); + const FsError &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return -1; + } + + return ani_double(static_cast(ret.GetData().value())); +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h new file mode 100644 index 0000000000000000000000000000000000000000..5a7995d799ec2ab6d000304f4b4a1d9589a58177 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_LSEEK_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_LSEEK_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +class LseekAni final { +public: + static ani_double LseekSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_double offset, ani_enum_item whence); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_LSEEK_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp b/interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dfec873c9cf2bea50fb4fb2932408c95ca116a2c --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lseek_core.h" + +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +FsResult LseekCore::DoLseek(const int32_t &fd, const int64_t &offset, + const optional &pos) +{ + if (fd < 0) { + HILOGE("Invalid fd from JS first argument"); + return FsResult::Error(EINVAL); + } + + SeekPos whence = SeekPos::START; + if (pos.has_value()) { + if (pos.value() < SeekPos::START || pos.value() > SeekPos::END) { + HILOGE("Invalid whence from JS third argument"); + return FsResult::Error(EINVAL); + } + whence = pos.value(); + } + + int64_t ret = ::Lseek(fd, offset, whence); + if (ret < 0) { + HILOGE("Failed to lseek, error:%{public}d", errno); + return FsResult::Error(errno); + } + + return FsResult::Success(ret); +} + +} // ModuleFileIO +} // FileManagement +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek_core.h b/interfaces/kits/js/src/mod_fs/properties/lseek_core.h new file mode 100644 index 0000000000000000000000000000000000000000..dd399f652e37dbe23edc17b24be32b74d1f19536 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek_core.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_CORE_H + +#include "filemgmt_libfs.h" +#include "rust_file.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; + +class LseekCore final { +public: + static FsResult DoLseek(const int32_t &fd, const int64_t &offset, + const optional &pos = nullopt); +}; + +} // namespace OHOS::FileManagement::ModuleFileIO + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_CORE_H \ No newline at end of file