diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 90795c6f80b8e4dd4e7ccf84ba9873a133fb3a53..cc1bcf303e4e44192ccbb0ae14f6d5fc3e71a514 100755 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -119,6 +119,8 @@ ohos_shared_library("fs") { "src/mod_fs/class_stat/stat_n_exporter.cpp", "src/mod_fs/class_stream/flush.cpp", "src/mod_fs/class_stream/stream_n_exporter.cpp", + "src/mod_fs/class_watcher/watcher_entity.cpp", + "src/mod_fs/class_watcher/watcher_n_exporter.cpp", "src/mod_fs/common_func.cpp", "src/mod_fs/module.cpp", "src/mod_fs/properties/close.cpp", @@ -137,6 +139,7 @@ ohos_shared_library("fs") { "src/mod_fs/properties/stat.cpp", "src/mod_fs/properties/symlink.cpp", "src/mod_fs/properties/truncate.cpp", + "src/mod_fs/properties/watcher.cpp", ] deps = [ @@ -147,6 +150,7 @@ ohos_shared_library("fs") { external_deps = [ "ability_base:zuri", "ability_runtime:abilitykit_native", + "access_token:libtokenid_sdk", "c_utils:utils", "data_share:datashare_common", "data_share:datashare_consumer", diff --git a/interfaces/kits/js/src/mod_fileio/module.cpp b/interfaces/kits/js/src/mod_fileio/module.cpp index b26592acb850f6dab3a0cae722cf4d1e66adcd5d..e0abeac25114fd9e3f8247c4243127695738fd5d 100644 --- a/interfaces/kits/js/src/mod_fileio/module.cpp +++ b/interfaces/kits/js/src/mod_fileio/module.cpp @@ -57,4 +57,4 @@ static napi_value Export(napi_env env, napi_value exports) NAPI_MODULE(fileio, Export) } // namespace ModuleFileIO } // namespace DistributedFS -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1078fb8219306c87178e9db24a4bbe2dc3a4925e --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp @@ -0,0 +1,95 @@ +/* + * 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 "watcher_entity.h" + +#include +#include + +#include "filemgmt_libhilog.h" +#include "uv.h" +namespace OHOS::FileManagement::ModuleFileIO { +using namespace OHOS::FileManagement::LibN; +FileWatcher::FileWatcher() {} + +FileWatcher::~FileWatcher() {} + +bool FileWatcher::InitNotify(int &fd) +{ + fd = inotify_init(); + if (fd == -1) { + HILOGE("Failed to init notify fail errCode:%{public}d", errno); + return false; + } + return true; +} + +bool FileWatcher::StartNotify(WatcherInfoArg &arg) +{ + int wd = inotify_add_watch(arg.fd, arg.filename.c_str(), arg.events); + if (wd == -1) { + HILOGE("Failed to start notify fail errCode:%{public}d", errno); + return false; + } + arg.wd = wd; + run_ = true; + return true; +} + +bool FileWatcher::StopNotify(const WatcherInfoArg &arg) +{ + run_ = false; + if (inotify_rm_watch(arg.fd, arg.wd) == -1) { + HILOGE("Failed to stop notify fail errCode:%{public}d", errno); + return false; + } + close(arg.fd); + return true; +} + +void FileWatcher::HandleEvent(WatcherInfoArg &arg, + const struct inotify_event *event, + WatcherCallback callback) +{ + if (event->wd != arg.wd) { + return; + } + std::string filename = arg.filename; + if ((event->name)[0] ! = '\0') { + filename += event->name; + } + callback(arg.env, arg.nRef, filename, event->mask, event->cookie); +} + +void FileWatcher::GetNotifyEvent(WatcherInfoArg &arg, WatcherCallback callback) +{ + char buf[BUF_SIZE] = {0}; + struct inotify_event *event = nullptr; + while (run_) { + int fd = arg.fd; + fd_set fds; + FD_ZERO(&fds); + FD_SET(fd, &fds); + if (select(fd + 1, &fds, nullptr, nullptr, nullptr) > 0) { + int len, index = 0; + while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR)) {}; + while (index < len) { + event = reinterpret_cast(buf + index); + HandleEvent(arg, event, callback); + index += sizeof(struct inotify_event) + event->len; + } + } + } +} +} // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h new file mode 100644 index 0000000000000000000000000000000000000000..fad6ec27adcd5a43fb5ed99f44b4ed9b391dde84 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -0,0 +1,61 @@ +/* + * 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_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H +#include +#include +#include +#include + +#include "filemgmt_libn.h" +namespace OHOS::FileManagement::ModuleFileIO { +using WatcherCallback = void (*)(napi_env env, + LibN::NRef &callback, + const std::string &filename, + const uint32_t &event, + const uint32_t &cookie); +constexpr int BUF_SIZE = 1024; +struct WatcherInfoArg { + std::string filename = ""; + uint32_t events = 0; + int fd = -1; + int wd = -1; + napi_env env = nullptr; + LibN::NRef nRef; + explicit WatcherInfoArg(LibN::NVal jsVal) : nRef(jsVal) {} + ~WatcherInfoArg() = default; +}; + +class FileWatcher { +public: + FileWatcher(); + ~FileWatcher(); + bool InitNotify(int &fd); + bool StartNotify(WatcherInfoArg &arg); + bool StopNotify(const WatcherInfoArg &arg); + void GetNotifyEvent(WatcherInfoArg &arg, WatcherCallback callback); + +private: + void HandleEvent(WatcherInfoArg &arg, const struct inotify_event *event, + WatcherCallback callback); + bool run_ = false; +}; + +struct WatcherEntity { + std::unique_ptr data_; + std::shared_ptr watcherPtr_; +}; +} // namespace OHOS::FileManagement::ModuleFileIO namespace OHOS +#endif diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38c5b82b234e214cb40663b035716d6c8bf5c093 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -0,0 +1,209 @@ +/* + * 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 "watcher_n_exporter.h" + +#include +#include +#include +#include + +#include "../common_func.h" +#include "filemgmt_libn.h" +#include "filemgmt_libhilog.h" +#include "securec.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + unique_ptr watcherEntity = make_unique(); + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(watcherEntity))) { + NError(EIO).ThrowErr(env); + return nullptr; + } + return funcArg.GetThisVar(); +} + +napi_value WatcherNExporter::Stop(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto watchEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!watchEntity) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if (!watchEntity->watcherPtr_->StopNotify(*(watchEntity->data_))) { + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto watchEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!watchEntity) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + if (!watchEntity->watcherPtr_->StartNotify(*(watchEntity->data_))) { + NError(errno).ThrowErr(env); + return nullptr; + } + + auto cbExec = [watchEntity]() -> NError { + watchEntity->watcherPtr_->GetNotifyEvent(*(watchEntity->data_), WatcherCallback); + return NError(ERRNO_NOERR); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return {NVal::CreateUndefined(env)}; + }; + + const string procedureName = "FileIOStartWatcher"; + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; +} + +static void WatcherCallbackComplete(uv_work_t *work, int stat) +{ + if (work == nullptr) { + HILOGE("Failed to get uv_queue_work pointer"); + return; + } + + WatcherNExporter::JSCallbackContext *callbackContext = + reinterpret_cast(work->data); + do { + if (callbackContext == nullptr) { + HILOGE("Failed to create context pointer"); + break; + } + if (!callbackContext->ref_) { + HILOGE("Failed to get nref reference"); + break; + } + napi_env env = callbackContext->env_; + napi_value jsCallback = callbackContext->ref_.Deref(env).val_; + NVal objn = NVal::CreateObject(env); + objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); + objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); + objn.AddProp("cookie", NVal::CreateUint32(env, callbackContext->cookie_).val_); + napi_value retVal = nullptr; + napi_status status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); + if (status != napi_ok) { + HILOGE("Failed to call napi_call_function, status: %{public}d", status); + break; + } + } while (0); + delete callbackContext; + delete work; +} + +void WatcherNExporter::WatcherCallback(napi_env env, NRef & callback, const std::string &fileName, + const uint32_t &event, const uint32_t &cookie) +{ + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env, &loop); + if (loop == nullptr) { + HILOGE("Failed to get uv event loop"); + return; + } + + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + HILOGE("Failed to create uv_work_t pointer"); + return; + } + + if (!callback) { + HILOGE("Failed to pass watcher callback"); + return; + } + + JSCallbackContext *callbackContext = new (std::nothrow) JSCallbackContext(callback); + callbackContext->env_ = env; + callbackContext->fileName_ = fileName; + callbackContext->event_ = event; + callbackContext->cookie_ = cookie; + work->data = reinterpret_cast(callbackContext); + + int ret = uv_queue_work( + loop, work, [](uv_work_t *work) {}, reinterpret_cast(WatcherCallbackComplete)); + if (ret != 0) { + HILOGE("Failed to execute libuv work queue, ret: %{public}d", ret); + delete callbackContext; + delete work; + } +} + +bool WatcherNExporter::Export() +{ + vector props = { + NVal::DeclareNapiFunction("start", Start), + NVal::DeclareNapiFunction("stop", Stop), + }; + + string className = GetClassName(); + auto [resDefineClass, classValue] = + NClass::DefineClass(exports_.env_, className, WatcherNExporter::Constructor, std::move(props)); + if (!resDefineClass) { + NError(EIO).ThrowErr(exports_.env_); + return false; + } + + bool succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + NError(EIO).ThrowErr(exports_.env_); + return false; + } + + return exports_.AddProp(className, classValue); +} + +string WatcherNExporter::GetClassName() +{ + return WatcherNExporter::className_; +} + +WatcherNExporter::WatcherNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +WatcherNExporter::~WatcherNExporter() {} +} // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..54dbd91b85f9a27c5a89abbcfae32cd7db06231d --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h @@ -0,0 +1,56 @@ +/* + * 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_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H +#include + +#include "filemgmt_libn.h" +#include "watcher_entity.h" +namespace OHOS::FileManagement::ModuleFileIO { +using namespace OHOS::FileManagement::LibN; +class WatcherNExporter final : public NExporter { +public: + class JSCallbackContext { + public: + explicit JSCallbackContext(NRef &ref) : ref_(ref) {} + ~JSCallbackContext() {} + + public: + napi_env env_; + NRef &ref_; + std::string fileName_; + uint32_t event_; + uint32_t cookie_; + }; + + inline static const std::string className_ = "Watcher"; + + bool Export() override; + std::string GetClassName() override; + + static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value Start(napi_env env, napi_callback_info info); + static napi_value Stop(napi_env env, napi_callback_info info); + static void WatcherCallback(napi_env env, NRef & callback, const std::string &fileName, const uint32_t &event, + const uint32_t &cookie); + + WatcherNExporter(napi_env env, napi_value exports); + ~WatcherNExporter() override; + +private: +}; +} // namespace OHOS::FileManagement::ModuleFileIO +#endif diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 6d2ae250b87e0184b0555476b3b5c244b55b8524..4b39bd12967e6c4bbbd86f20bfe2f7e2d0e62471 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -21,6 +21,7 @@ #include "class_file/file_n_exporter.h" #include "class_stat/stat_n_exporter.h" #include "class_stream/stream_n_exporter.h" +#include "class_watcher/watcher_n_exporter.h" #include "filemgmt_libhilog.h" #include "properties/prop_n_exporter.h" @@ -37,6 +38,7 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); for (auto &&product : products) { if (!product->Export()) { @@ -52,4 +54,4 @@ static napi_value Export(napi_env env, napi_value exports) NAPI_MODULE(fs, Export) } // namespace ModuleFileIO } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS 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 f20fe97f7b45d3c4e027e187bfff53cd4cd98552..c6da56bc781668e87bc8074e2fffafddedf4d181 100755 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -43,7 +43,7 @@ #include "stat.h" #include "symlink.h" #include "truncate.h" - +#include "watcher.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { @@ -609,6 +609,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("unlinkSync", UnlinkSync), NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("writeSync", WriteSync), + NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), }); } @@ -622,4 +623,4 @@ PropNExporter::PropNExporter(napi_env env, napi_value exports) : NExporter(env, PropNExporter::~PropNExporter() {} } // namespace ModuleFileIO } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37e2d83abe5d7218d6b1dab58f529f224db0fe4c --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -0,0 +1,96 @@ +/* + * 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 "watcher.h" + +#include +#include +#include +#include +#include +#include "ipc_skeleton.h" +#include "filemgmt_libhilog.h" +#include "tokenid_kit.h" +#include "../class_watcher/watcher_entity.h" +#include "../class_watcher/watcher_n_exporter.h" +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +namespace { + const std::string STORAGE_DATA_PATH = "/data"; + bool IsSystemApp() + { + uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); + } +} + + +napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) +{ + if (!IsSystemApp()) { + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succGetPath, filename, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succGetPath) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succGetEvent, events] = NVal(env, funcArg[NARG_POS::SECOND]).ToUint32(); + if (!succGetEvent) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + int fd = -1; + shared_ptr watcherPtr = make_shared(); + if (!watcherPtr->InitNotify(fd)) { + NError(errno).ThrowErr(env); + return nullptr; + } + + napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); + if (!objWatcher) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto watcherEntity = NClass::GetEntityOf(env, objWatcher); + if (!watcherEntity) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + watcherEntity->data_ = std::make_unique(NVal(env, funcArg[NARG_POS::THIRD])); + watcherEntity->data_->events = events; + watcherEntity->data_->env = env; + watcherEntity->data_->filename = string(filename.get()); + watcherEntity->data_->fd = fd; + + watcherEntity->watcherPtr_ = watcherPtr; + + return objWatcher; +} +} // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.h b/interfaces/kits/js/src/mod_fs/properties/watcher.h new file mode 100644 index 0000000000000000000000000000000000000000..4d2f0a4fc5b86364f2100efcebe0103fdfe9224d --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.h @@ -0,0 +1,26 @@ +/* + * 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_FILEIO_PROPERTIES_WATCHER_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_WATCHER_H +#include "filemgmt_libn.h" +namespace OHOS::FileManagement::ModuleFileIO { + +class Watcher final { +public: + static napi_value CreateWatcher(napi_env env, napi_callback_info info); +}; +} // namespace OHOS::FileManagement::ModuleFileIO +#endif \ No newline at end of file diff --git a/utils/filemgmt_libn/include/n_val.h b/utils/filemgmt_libn/include/n_val.h index c66b88a56b398ea86bbd5f6285baa0306e43545a..487a0d35c4e7ac5c9957737e623643ee5a6e58d1 100644 --- a/utils/filemgmt_libn/include/n_val.h +++ b/utils/filemgmt_libn/include/n_val.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -53,6 +53,7 @@ public: std::tuple ToTypedArray() const; std::tuple, uint32_t> ToStringArray(); std::tuple ToUint64() const; + std::tuple ToUint32() const; std::tuple ToDouble() const; /* Static helpers to create js objects */ @@ -60,6 +61,7 @@ public: static NVal CreateBigInt64(napi_env env, int64_t val); static NVal CreateInt64(napi_env env, int64_t val); static NVal CreateInt32(napi_env env, int32_t val); + static NVal CreateUint32(napi_env env, int32_t val); static NVal CreateObject(napi_env env); static NVal CreateBool(napi_env env, bool val); static NVal CreateUTF8String(napi_env env, std::string str); diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index a2d6a591272fc084f06551b6a52fb99350366bd5..11fb78aaba7a5066c603a8851b6c612fa31be265 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -131,6 +131,13 @@ tuple NVal::ToDouble() const return make_tuple(status == napi_ok, res); } +tuple NVal::ToUint32() const +{ + uint32_t res = 0; + napi_status status = napi_get_value_uint32(env_, val_, &res); + return make_tuple(status == napi_ok, res); +} + tuple NVal::ToUint64() const { uint64_t res = 0; @@ -258,6 +265,13 @@ NVal NVal::CreateInt32(napi_env env, int32_t val) return {env, res}; } +NVal NVal::CreateUint32(napi_env env, int32_t val) +{ + napi_value res = nullptr; + napi_create_uint32(env, val, &res); + return {env, res}; +} + NVal NVal::CreateObject(napi_env env) { napi_value res = nullptr;