diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 4f420fad0ae5fb513c265717ce38f90ad7962c7a..8267df9f85c6bc08f9387fc7770ef0b9fc36fffd 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -109,6 +109,7 @@ ohos_shared_library("fs") { relative_install_dir = "module/file" include_dirs = [ + "${file_api_path}/interfaces/kits/rust/include", "${src_path}/common", "${src_path}/common/file_helper", "${src_path}/mod_fs", @@ -120,6 +121,7 @@ ohos_shared_library("fs") { "src/common/file_helper/fd_guard.cpp", "src/mod_fs/class_file/file_n_exporter.cpp", "src/mod_fs/class_stat/stat_n_exporter.cpp", + "src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp", "src/mod_fs/common_func.cpp", "src/mod_fs/module.cpp", "src/mod_fs/properties/close.cpp", @@ -129,10 +131,12 @@ ohos_shared_library("fs") { "src/mod_fs/properties/mkdtemp.cpp", "src/mod_fs/properties/open.cpp", "src/mod_fs/properties/prop_n_exporter.cpp", + "src/mod_fs/properties/read_lines.cpp", "src/mod_fs/properties/rename.cpp", "src/mod_fs/properties/rmdirent.cpp", "src/mod_fs/properties/stat.cpp", "src/mod_fs/properties/truncate.cpp", + "src/mod_fs/properties/utimes.cpp", ] cflags_cc = [ "-std=c++17" ] @@ -140,6 +144,7 @@ ohos_shared_library("fs") { deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libn:filemgmt_libn", + "${file_api_path}/interfaces/kits/rust:rust_file", ] use_exceptions = true @@ -170,6 +175,7 @@ ohos_shared_library("fs") { "src/mod_fs/properties/dup.cpp", "src/mod_fs/properties/fdopen_stream.cpp", "src/mod_fs/properties/listfile.cpp", + "src/mod_fs/properties/lseek.cpp", "src/mod_fs/properties/move.cpp", "src/mod_fs/properties/movedir.cpp", "src/mod_fs/properties/read_text.cpp", diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek.cpp b/interfaces/kits/js/src/mod_fs/properties/lseek.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77ed76f8d0801ebbdcad2a047427ef14dce51fef --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023 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.h" +#include "common_func.h" +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "rust_file.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +napi_value Lseek::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + HILOGE("Lseek, Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succGetFd, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succGetFd || fd < 0) { + HILOGE("Lseek, Invalid fd from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succGetOffset, offset] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(); + if (!succGetOffset) { + HILOGE("Lseek, Invalid offset from JS second argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + SeekPos whence = SeekPos::START; + if (funcArg.GetArgc() == NARG_CNT::THREE) { + auto [succGetWhence, whence] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(SeekPos::START); + if (!succGetWhence || whence < SeekPos::START || whence > SeekPos::END) { + HILOGE("Lseek, Invalid whence from JS third argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + } + + int64_t pos = ::Lseek(fd, offset, static_cast(whence)); + if (pos < 0) { + HILOGE("Failed to Lseek"); + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateInt64(env, pos).val_; +} + +} // ModuleFileIO +} // FileManagement +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek.h b/interfaces/kits/js/src/mod_fs/properties/lseek.h new file mode 100644 index 0000000000000000000000000000000000000000..3ee92030070078d0621200057e4f835c85c0380a --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 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_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { + +class Lseek final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); +}; + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 4bcccb9e494c0a9135ba33f412a162fdf5d9285d..b48fd721b438b2326bb9996ca485f1dc75a74a1d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -32,6 +32,7 @@ #include "fsync.h" #include "js_native_api.h" #include "js_native_api_types.h" +#include "lseek.h" #include "lstat.h" #include "mkdtemp.h" #include "open.h" @@ -567,6 +568,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("fdatasyncSync", Fdatasync::Sync), NVal::DeclareNapiFunction("fsync", Fsync::Async), NVal::DeclareNapiFunction("fsyncSync", Fsync::Sync), + NVal::DeclareNapiFunction("lseek", Lseek::Sync), NVal::DeclareNapiFunction("lstat", Lstat::Async), NVal::DeclareNapiFunction("lstatSync", Lstat::Sync), NVal::DeclareNapiFunction("mkdir", Mkdir),