From fb804d8d1fb1902e6333ed3a104abd08560cf66d Mon Sep 17 00:00:00 2001 From: panqinxu Date: Fri, 9 Dec 2022 16:30:23 +0800 Subject: [PATCH] Add mod_hash for file_api new framework Signed-off-by: panqinxu --- .../js/src/mod_fs/class_file/file_entity.h | 34 +++++ .../src/mod_fs/class_file/file_n_exporter.cpp | 119 ++++++++++++++++ .../src/mod_fs/class_file/file_n_exporter.h | 40 ++++++ interfaces/kits/js/src/mod_hash/hash.cpp | 133 ++++++++++++++++++ interfaces/kits/js/src/mod_hash/hash.h | 56 ++++++++ interfaces/kits/js/src/mod_hash/module.cpp | 42 ++++++ 6 files changed, 424 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/class_file/file_entity.h create mode 100644 interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp create mode 100644 interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h create mode 100644 interfaces/kits/js/src/mod_hash/hash.cpp create mode 100644 interfaces/kits/js/src/mod_hash/hash.h create mode 100644 interfaces/kits/js/src/mod_hash/module.cpp diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_entity.h b/interfaces/kits/js/src/mod_fs/class_file/file_entity.h new file mode 100644 index 000000000..397f6df95 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_file/file_entity.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 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_CLASS_FILE_FILE_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_FILE_FILE_ENTITY_H + +#include + +#include "fd_guard.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +struct FileEntity { + std::unique_ptr fd_ = { nullptr }; + std::string path_; + std::string uri_; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_FILE_FILE_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp new file mode 100644 index 000000000..6d7ad806d --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2022 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 "file_entity.h" +#include "file_n_exporter.h" + +#include +#include +#include +#include + +#include "filemgmt_libhilog.h" +#include "filemgmt_libn.h" +#include "../common_func.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +static FileEntity *GetFileEntity(napi_env env, napi_value raf_entity) +{ + auto rafEntity = NClass::GetEntityOf(env, raf_entity); + if (!rafEntity) { + HILOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if (!rafEntity->fd_) { + HILOGE("rafEntity fd is not exist"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + return rafEntity; +} + +napi_value FileNExporter::GetFD(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto rafEntity = GetFileEntity(env, funcArg.GetThisVar()); + if (!rafEntity) { + HILOGE("Failed to get file entity"); + return nullptr; + } + return NVal::CreateInt32(env, rafEntity->fd_.get()->GetFD()).val_; +} + +napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto rafEntity = make_unique(); + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(rafEntity))) { + HILOGE("Failed to set file entity"); + NError(EIO).ThrowErr(env); + return nullptr; + } + return funcArg.GetThisVar(); +} + +bool FileNExporter::Export() +{ + vector props = { + NVal::DeclareNapiGetter("fd", GetFD), + }; + + string className = GetClassName(); + bool succ = false; + napi_value classValue = nullptr; + tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, + FileNExporter::Constructor, move(props)); + if (!succ) { + HILOGE("Define class exceptions"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + HILOGE("Save class exceptions"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + + return exports_.AddProp(className, classValue); +} + +string FileNExporter::GetClassName() +{ + return FileNExporter::className_; +} + +FileNExporter::FileNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} +FileNExporter::~FileNExporter() {} +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h new file mode 100644 index 000000000..f79ef539d --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 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_CLASS_FILE_FILE_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_FILE_FILE_N_EXPORTER_H + +#include "n_exporter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace OHOS::FileManagement::LibN; +class FileNExporter final : public NExporter { +public: + inline static const std::string className_ = "File"; + + bool Export() override; + std::string GetClassName() override; + + static napi_value Constructor(napi_env env, napi_callback_info cbinfo); + static napi_value GetFD(napi_env env, napi_callback_info cbinfo); + FileNExporter(napi_env env, napi_value exports); + ~FileNExporter() override; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_FILE_FILE_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/hash.cpp b/interfaces/kits/js/src/mod_hash/hash.cpp new file mode 100644 index 000000000..a8145961e --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/hash.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2022 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 "hash.h" + +#include +#include +#include + +#include "filemgmt_libhilog.h" +#include "hash_file.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +static HASH_ALGORITHM_TYPE GetHashAlgorithm(const string &alg) +{ + return (algorithmMaps.find(alg) != algorithmMaps.end()) ? algorithmMaps.at(alg) : HASH_ALGORITHM_TYPE_UNSUPPORTED; +} + +static tuple, HASH_ALGORITHM_TYPE, bool> GetHashArgs(napi_env env, const NFuncArg &funcArg) +{ + bool isPromise = false; + auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise }; + } + + auto [resGetSecondArg, alg, ignore] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + if (!resGetSecondArg) { + HILOGE("Invalid algorithm"); + NError(EINVAL).ThrowErr(env); + return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise }; + } + + HASH_ALGORITHM_TYPE algType = GetHashAlgorithm(alg.get()); + if (algType == HASH_ALGORITHM_TYPE_UNSUPPORTED) { + HILOGE("Invalid algorithm"); + NError(EINVAL).ThrowErr(env); + return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise }; + } + + if (funcArg.GetArgc() == NARG_CNT::THREE && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { + HILOGE("Invalid callback"); + NError(EINVAL).ThrowErr(env); + return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise }; + } + + isPromise = funcArg.GetArgc() == NARG_CNT::TWO; + return { true, move(path), algType, isPromise }; +} + +napi_value Hash::Async(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 [succ, fpath, algType, isPromise] = GetHashArgs(env, funcArg); + if (!succ) { + HILOGE("Failed to get hash args"); + return nullptr; + } + + auto arg = make_shared(); + auto cbExec = [fpath = string(fpath.release()), arg, algType = algType, env = env]() -> NError { + int ret = EIO; + string &res = *arg; + if (algType == HASH_ALGORITHM_TYPE_MD5) { + tie(ret, res) = HashFile::HashWithMD5(fpath); + } else if (algType == HASH_ALGORITHM_TYPE_SHA1) { + tie(ret, res) = HashFile::HashWithSHA1(fpath); + } else if (algType == HASH_ALGORITHM_TYPE_SHA256) { + tie(ret, res) = HashFile::HashWithSHA256(fpath); + } + return NError(ret); + }; + + auto cbComplete = [arg](napi_env env, NError err) -> NVal { + if (err) { + return { NVal(env, err.GetNapiErr(env)) }; + } + + return { NVal::CreateUTF8String(env, *arg) }; + }; + + const string procedureName = "FileIOHash"; + NVal thisVar(env, funcArg.GetThisVar()); + if (isPromise) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } +} + +bool HashNExporter::Export() +{ + return exports_.AddProp({ + NVal::DeclareNapiFunction("hash", Hash::Async), + }); +} + +string HashNExporter::GetClassName() +{ + return HashNExporter::className_; +} + +HashNExporter::HashNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/hash.h b/interfaces/kits/js/src/mod_hash/hash.h new file mode 100644 index 000000000..1a9f85505 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/hash.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022 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_HASH_PROPERTIES_HASH_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_PROPERTIES_HASH_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace OHOS::FileManagement::LibN; + +enum HASH_ALGORITHM_TYPE { + HASH_ALGORITHM_TYPE_MD5, + HASH_ALGORITHM_TYPE_SHA1, + HASH_ALGORITHM_TYPE_SHA256, + HASH_ALGORITHM_TYPE_UNSUPPORTED, +}; + +const std::map algorithmMaps = { + {"md5", HASH_ALGORITHM_TYPE_MD5}, + {"sha1", HASH_ALGORITHM_TYPE_SHA1}, + {"sha256", HASH_ALGORITHM_TYPE_SHA256}, +}; + +class Hash final { +public: + static napi_value Async(napi_env env, napi_callback_info info); +}; + +class HashNExporter final : public NExporter { +public: + inline static const std::string className_ = "Hash"; + bool Export() override; + std::string GetClassName() override; + + HashNExporter(napi_env env, napi_value exports); + ~HashNExporter() = default; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_PROPERTIES_HASH_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/module.cpp b/interfaces/kits/js/src/mod_hash/module.cpp new file mode 100644 index 000000000..5521fafe9 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/module.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 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 +#include + +#include "filemgmt_libhilog.h" +#include "hash.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +static napi_value Export(napi_env env, napi_value exports) +{ + std::unique_ptr products = make_unique(env, exports); + + if (!products->Export()) { + HILOGE("INNER BUG. Failed to export class %{public}s for module fileio", products->GetClassName().c_str()); + return nullptr; + } else { + HILOGI("Class %{public}s for module fileio has been exported", products->GetClassName().c_str()); + } + return exports; +} + +NAPI_MODULE(hash, Export) +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file -- Gitee