diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 4f420fad0ae5fb513c265717ce38f90ad7962c7a..194ea2ece15dd5d9e74ab7fb548646aa7595d337 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -157,6 +157,7 @@ ohos_shared_library("fs") { ] if (!use_mingw_win && !use_mac) { + include_dirs += [ "${file_api_path}/interfaces/kits/rust/include" ] sources += [ "src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp", "src/mod_fs/class_stream/flush.cpp", @@ -170,6 +171,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", @@ -189,7 +191,10 @@ ohos_shared_library("fs") { "ipc:ipc_core", "samgr:samgr_proxy", ] - deps += [ "${file_api_path}/interfaces/kits/native:remote_uri_native" ] + deps += [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + ] } } diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 94f4cf5c5ac712bdf9bbfbd667e35169d5061d7b..32f3fa64b8e22b3302779edd0d732225bb153171 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -80,6 +80,32 @@ void InitOpenMode(napi_env env, napi_value exports) } } +void InitWhenceType(napi_env env, napi_value exports) +{ + char propertyName[] = "WhenceType"; + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("SEEK_SET", NVal::CreateInt32(env, SEEK_SET).val_), + DECLARE_NAPI_STATIC_PROPERTY("SEEK_CUR", NVal::CreateInt32(env, SEEK_CUR).val_), + DECLARE_NAPI_STATIC_PROPERTY("SEEK_END", NVal::CreateInt32(env, SEEK_END).val_), + }; + napi_value obj = nullptr; + napi_status status = napi_create_object(env, &obj); + if (status != napi_ok) { + HILOGE("Failed to create object at initializing whenceType"); + return; + } + status = napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc); + if (status != napi_ok) { + HILOGE("Failed to set properties of character at initializing whenceType"); + return; + } + status = napi_set_named_property(env, exports, propertyName, obj); + if (status != napi_ok) { + HILOGE("Failed to set direction property at initializing whenceType"); + return; + } +} + static tuple GetActualLen(napi_env env, size_t bufLen, size_t bufOff, NVal op) { bool succ = false; diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index f332321c1e00e891a3c9a02d3f15735f2aec8223..93de1716063b883a9e446a8d4edcc5a8f3e4c71c 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -53,6 +53,7 @@ struct FileInfo { }; void InitOpenMode(napi_env env, napi_value exports); +void InitWhenceType(napi_env env, napi_value exports); struct CommonFunc { static unsigned int ConvertJsFlags(unsigned int &flags); diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 7975920422e8b84624e2127539a618942486f2e3..a276ac5ac73db9af2fa9846bd7ce4bf7c51847ea 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -36,6 +36,7 @@ namespace ModuleFileIO { static napi_value Export(napi_env env, napi_value exports) { InitOpenMode(env, exports); + InitWhenceType(env, exports); std::vector> products; products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); 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..78bb8dabd7cd3bc248f504705f441e3f061b8c0b --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek.cpp @@ -0,0 +1,74 @@ +/* + * 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("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("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("Invalid offset from JS second argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + SeekPos whence = SeekPos::START; + if (funcArg.GetArgc() == NARG_CNT::THREE) { + auto [succGetWhence, pos] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(SeekPos::START); + if (!succGetWhence || pos < SeekPos::START || pos > SeekPos::END) { + HILOGE("Invalid whence from JS third argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + whence = static_cast(pos); + } + + int64_t ret = ::Lseek(fd, offset, whence); + if (ret < 0) { + HILOGE("Failed to lseek, error:%{public}d", errno); + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateInt64(env, ret).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..160ce2e6908c4b8a97ca09e9d6907959b3a42379 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 @@ -48,6 +48,7 @@ #include "dup.h" #include "fdopen_stream.h" #include "listfile.h" +#include "lseek.h" #include "move.h" #include "movedir.h" #include "read_text.h" @@ -603,6 +604,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync), NVal::DeclareNapiFunction("listFile", ListFile::Async), NVal::DeclareNapiFunction("listFileSync", ListFile::Sync), + NVal::DeclareNapiFunction("lseek", Lseek::Sync), NVal::DeclareNapiFunction("moveDir", MoveDir::Async), NVal::DeclareNapiFunction("moveDirSync", MoveDir::Sync), NVal::DeclareNapiFunction("moveFile", Move::Async),