From ac2edd86106233e11983ce1b70e7d17d1d4d191b Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Wed, 27 Sep 2023 23:15:54 +0800 Subject: [PATCH 1/4] =?UTF-8?q?lseek=E6=8E=A5=E5=8F=A3=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuhongtao666 --- interfaces/kits/js/BUILD.gn | 3 + .../kits/js/src/mod_fs/properties/lseek.cpp | 75 +++++++++++++++++++ .../kits/js/src/mod_fs/properties/lseek.h | 33 ++++++++ .../src/mod_fs/properties/prop_n_exporter.cpp | 2 + 4 files changed, 113 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/properties/lseek.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/lseek.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 4f420fad0..449b7012a 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -114,6 +114,7 @@ ohos_shared_library("fs") { "${src_path}/mod_fs", "${utils_path}/common/include", "//third_party/libuv/include", + "${file_api_path}/interfaces/kits/rust/include", ] sources = [ @@ -125,6 +126,7 @@ ohos_shared_library("fs") { "src/mod_fs/properties/close.cpp", "src/mod_fs/properties/fdatasync.cpp", "src/mod_fs/properties/fsync.cpp", + "src/mod_fs/properties/lseek.cpp", "src/mod_fs/properties/lstat.cpp", "src/mod_fs/properties/mkdtemp.cpp", "src/mod_fs/properties/open.cpp", @@ -140,6 +142,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 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 000000000..2d05e573a --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek.cpp @@ -0,0 +1,75 @@ +/* + * 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; + } + + //1. fd - file descriptor. + auto [succ1, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ1 || fd < 0) { + HILOGE("Lseek, Invalid fd from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + //2. offset - file offset. + auto [succ2, offset] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(); + if (!succ2) { + HILOGE("Lseek, Invalid offset from JS second argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + //3. whence - directive whence. + auto [succ3, whence] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(SEEK_SET); + if (!succ3) { + HILOGE("Lseek, Invalid whence from JS third argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if (whence < SeekPos::START || whence > SeekPos::END) { + HILOGE("Lseek, Value too large for defined data type"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + HILOGD("Lseek, fd:%{public}d, offset:%{public}lld, whence:%{public}d", fd, offset, whence); + ::Lseek(fd, offset, static_cast(whence)); + if (errno != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).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 000000000..3ee920300 --- /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 4bcccb9e4..b48fd721b 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), -- Gitee From e4730c28b11ccd9dd0a939f6e3f4c812682977d7 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Thu, 28 Sep 2023 18:12:43 +0800 Subject: [PATCH 2/4] lseek update Signed-off-by: zhuhongtao666 --- .../kits/js/src/mod_fs/properties/lseek.cpp | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek.cpp b/interfaces/kits/js/src/mod_fs/properties/lseek.cpp index 2d05e573a..739ffb650 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lseek.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lseek.cpp @@ -33,36 +33,33 @@ napi_value Lseek::Sync(napi_env env, napi_callback_info info) return nullptr; } - //1. fd - file descriptor. - auto [succ1, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ1 || fd < 0) { + 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; } - //2. offset - file offset. - auto [succ2, offset] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(); - if (!succ2) { + 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; } - //3. whence - directive whence. - auto [succ3, whence] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(SEEK_SET); - if (!succ3) { - HILOGE("Lseek, Invalid whence from JS third argument"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - if (whence < SeekPos::START || whence > SeekPos::END) { - HILOGE("Lseek, Value too large for defined data type"); - 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; + } } - HILOGD("Lseek, fd:%{public}d, offset:%{public}lld, whence:%{public}d", fd, offset, whence); + ::Lseek(fd, offset, static_cast(whence)); if (errno != 0) { + HILOGE("Lseek failed"); NError(errno).ThrowErr(env); return nullptr; } -- Gitee From 6941c7a7a114a7055086f603b8c7c2cf21613360 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Sat, 7 Oct 2023 11:51:39 +0800 Subject: [PATCH 3/4] lseek revise Signed-off-by: zhuhongtao666 --- interfaces/kits/js/BUILD.gn | 7 +++++-- interfaces/kits/js/src/mod_fs/properties/lseek.cpp | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 449b7012a..8267df9f8 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -109,32 +109,34 @@ 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", "${utils_path}/common/include", "//third_party/libuv/include", - "${file_api_path}/interfaces/kits/rust/include", ] sources = [ "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", "src/mod_fs/properties/fdatasync.cpp", "src/mod_fs/properties/fsync.cpp", - "src/mod_fs/properties/lseek.cpp", "src/mod_fs/properties/lstat.cpp", "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" ] @@ -173,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 index 739ffb650..4c537059f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lseek.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lseek.cpp @@ -59,7 +59,7 @@ napi_value Lseek::Sync(napi_env env, napi_callback_info info) ::Lseek(fd, offset, static_cast(whence)); if (errno != 0) { - HILOGE("Lseek failed"); + HILOGE("Failed to Lseek"); NError(errno).ThrowErr(env); return nullptr; } -- Gitee From c2eba8465714c75807b294c355a0ab01836e606a Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Sun, 8 Oct 2023 20:47:50 +0800 Subject: [PATCH 4/4] =?UTF-8?q?lseek=E6=8E=A5=E5=8F=A3=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuhongtao666 --- interfaces/kits/js/src/mod_fs/properties/lseek.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek.cpp b/interfaces/kits/js/src/mod_fs/properties/lseek.cpp index 4c537059f..77ed76f8d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lseek.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lseek.cpp @@ -57,14 +57,14 @@ napi_value Lseek::Sync(napi_env env, napi_callback_info info) } } - ::Lseek(fd, offset, static_cast(whence)); - if (errno != 0) { + int64_t pos = ::Lseek(fd, offset, static_cast(whence)); + if (pos < 0) { HILOGE("Failed to Lseek"); NError(errno).ThrowErr(env); return nullptr; } - return NVal::CreateUndefined(env).val_; + return NVal::CreateInt64(env, pos).val_; } } // ModuleFileIO -- Gitee