From 20e3c57ddcd4212bb56fcf3603e783a60c1ff8ed Mon Sep 17 00:00:00 2001 From: bubble_mao Date: Sat, 16 Jul 2022 16:18:16 +0800 Subject: [PATCH] add hash.cpp-read_text.h Signed-off-by: bubble_mao --- .../js/src/mod_fileio/properties/hash.cpp | 125 ++++++++++ .../kits/js/src/mod_fileio/properties/hash.h | 34 +++ .../js/src/mod_fileio/properties/lchown.cpp | 113 +++++++++ .../js/src/mod_fileio/properties/lchown.h | 32 +++ .../js/src/mod_fileio/properties/link.cpp | 110 +++++++++ .../kits/js/src/mod_fileio/properties/link.h | 32 +++ .../js/src/mod_fileio/properties/lseek.cpp | 116 +++++++++ .../kits/js/src/mod_fileio/properties/lseek.h | 32 +++ .../js/src/mod_fileio/properties/lstat.cpp | 125 ++++++++++ .../kits/js/src/mod_fileio/properties/lstat.h | 32 +++ .../js/src/mod_fileio/properties/mkdtemp.cpp | 93 ++++++++ .../js/src/mod_fileio/properties/mkdtemp.h | 32 +++ .../js/src/mod_fileio/properties/open.cpp | 181 ++++++++++++++ .../kits/js/src/mod_fileio/properties/open.h | 32 +++ .../js/src/mod_fileio/properties/open_dir.cpp | 135 +++++++++++ .../js/src/mod_fileio/properties/open_dir.h | 32 +++ .../mod_fileio/properties/posix_fallocate.cpp | 117 +++++++++ .../mod_fileio/properties/posix_fallocate.h | 32 +++ .../src/mod_fileio/properties/read_text.cpp | 223 ++++++++++++++++++ .../js/src/mod_fileio/properties/read_text.h | 44 ++++ 20 files changed, 1672 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fileio/properties/hash.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/hash.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/lchown.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/lchown.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/link.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/link.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/lseek.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/lseek.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/lstat.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/lstat.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/mkdtemp.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/mkdtemp.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/open.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/open.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/open_dir.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/open_dir.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/posix_fallocate.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/posix_fallocate.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/read_text.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/read_text.h diff --git a/interfaces/kits/js/src/mod_fileio/properties/hash.cpp b/interfaces/kits/js/src/mod_fileio/properties/hash.cpp new file mode 100644 index 000000000..35d4c11d8 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/hash.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2021 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 "../../common/file_helper/hash_file.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +enum HASH_ALGORITHM_TYPE { + HASH_ALGORITHM_TYPE_MD5, + HASH_ALGORITHM_TYPE_SHA1, + HASH_ALGORITHM_TYPE_SHA256, + HASH_ALGORITHM_TYPE_UNSUPPORTED, +}; + +static HASH_ALGORITHM_TYPE GetHashAlgorithm(const unique_ptr &alg, const size_t algLen) +{ + if (algLen == string_view("md5").length() && string_view(alg.get()).compare("md5") == 0) { + return HASH_ALGORITHM_TYPE_MD5; + } else if (algLen == string_view("sha1").length() && string_view(alg.get()).compare("sha1") == 0) { + return HASH_ALGORITHM_TYPE_SHA1; + } else if (algLen == string_view("sha256").length() && string_view(alg.get()).compare("sha256") == 0) { + return HASH_ALGORITHM_TYPE_SHA256; + } else { + return 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) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise }; + } + + auto [resGetSecondArg, alg, algLen] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid algorithm"); + return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise }; + } + + HASH_ALGORITHM_TYPE algType = GetHashAlgorithm(alg, algLen); + if (algType == HASH_ALGORITHM_TYPE_UNSUPPORTED) { + UniError(EINVAL).ThrowErr(env, "Invalid algorithm"); + return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise }; + } + + if (funcArg.GetArgc() == NARG_CNT::THREE && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { + UniError(EINVAL).ThrowErr(env, "Invalid callback"); + 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)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [succ, fpath, algType, isPromise] = GetHashArgs(env, funcArg); + if (!succ) { + return nullptr; + } + + auto arg = make_shared(); + auto cbExec = [fpath = string(fpath.release()), arg, algType = algType](napi_env env) -> UniError { + 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 UniError(ret); + }; + + auto cbComplete = [arg](napi_env env, UniError 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_; + } +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/hash.h b/interfaces/kits/js/src/mod_fileio/properties/hash.h new file mode 100644 index 000000000..8aca34ce1 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/hash.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 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_HASH_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_HASH_H + +#include "../../common/log.h" +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_func_arg.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class Hash final { +public: + static napi_value Async(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/lchown.cpp b/interfaces/kits/js/src/mod_fileio/properties/lchown.cpp new file mode 100644 index 000000000..fe3511ee5 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/lchown.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2021 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 "lchown.h" + +#include +#include +#include + +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_func_arg.h" +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +static tuple GetLchownArg(napi_env env, const NFuncArg &funcArg) +{ + auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return { false, "", -1, -1 }; + } + + auto [resGetSecondArg, owner] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid owner"); + return { false, "", -1, -1 }; + } + + auto [resGetThirdArg, group] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + if (!resGetThirdArg) { + UniError(EINVAL).ThrowErr(env, "Invalid group"); + return { false, "", -1, -1 }; + } + + return { true, path.get(), owner, group }; +} + +napi_value Lchown::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetLchownArg, path, owner, group] = GetLchownArg(env, funcArg); + if (!resGetLchownArg) { + return nullptr; + } + + if (lchown(path.c_str(), owner, group) == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value Lchown::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE, NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetLchownArg, path, owner, group] = GetLchownArg(env, funcArg); + if (!resGetLchownArg) { + return nullptr; + } + + auto cbExec = [path = path, owner = owner, group = group](napi_env env) -> UniError { + if (lchown(path.c_str(), owner, group) == -1) { + return UniError(errno); + } else { + return UniError(ERRNO_NOERR); + } + }; + + auto cbCompl = [](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + const string procedureName = "FileIOLchown"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::THREE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FOURTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; + } +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/properties/lchown.h b/interfaces/kits/js/src/mod_fileio/properties/lchown.h new file mode 100644 index 000000000..c13ad0967 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/lchown.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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_LCHOWN_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_LCHOWN_H + +#include "../../common/napi/n_val.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class Lchown final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); + static napi_value Async(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/link.cpp b/interfaces/kits/js/src/mod_fileio/properties/link.cpp new file mode 100644 index 000000000..c631dec53 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/link.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2021 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 "link.h" + +#include +#include +#include +#include + +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_func_arg.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +static tuple GetLinkArg(napi_env env, const NFuncArg &funcArg) +{ + auto [resGetFirstArg, src, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid src"); + return { false, "", "" }; + } + + auto [resGetSecondArg, dest, useless] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid dest"); + return { false, "", "" }; + } + + return { true, src.get(), dest.get() }; +} + +napi_value Link::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetLinkArg, oldPath, newPath] = GetLinkArg(env, funcArg); + if (!resGetLinkArg) { + return nullptr; + } + + if (link(oldPath.c_str(), newPath.c_str()) == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value Link::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetLinkArg, oldPath, newPath] = GetLinkArg(env, funcArg); + if (!resGetLinkArg) { + return nullptr; + } + + auto cbExec = [oldPath = move(oldPath), newPath = move(newPath)](napi_env env) -> UniError { + int ret = link(oldPath.c_str(), newPath.c_str()); + if (ret == -1) { + return UniError(errno); + } else { + return UniError(ERRNO_NOERR); + } + }; + + auto cbComplCallback = [](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + const string procedureName = "FileIOLink"; + NVal thisVar(env, funcArg.GetThisVar()); + size_t argc = funcArg.GetArgc(); + if (argc == NARG_CNT::TWO) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_; + } else { + NVal cb(env, funcArg[NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_; + } +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/link.h b/interfaces/kits/js/src/mod_fileio/properties/link.h new file mode 100644 index 000000000..f287a24e7 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/link.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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_LINK_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_LINK_H + +#include "../../common/napi/n_val.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class Link final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); + static napi_value Async(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/lseek.cpp b/interfaces/kits/js/src/mod_fileio/properties/lseek.cpp new file mode 100644 index 000000000..11ecc64b4 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/lseek.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2021 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 +#include +#include + +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_func_arg.h" +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +static tuple GetLseekArg(napi_env env, const NFuncArg &funcArg) +{ + auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid fd"); + return { false, -1, -1, -1 }; + } + + auto [resGetSecondArg, offset] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid offset"); + return { false, -1, -1, -1 }; + } + + auto [resGetThirdArg, whence] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + if (!resGetThirdArg) { + UniError(EINVAL).ThrowErr(env, "Invalid whence"); + return { false, -1, -1, -1 }; + } + + return { true, fd, offset, whence }; +} + +napi_value Lseek::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetLseekArg, fd, offset, whence] = GetLseekArg(env, funcArg); + if (!resGetLseekArg) { + return nullptr; + } + + int ret = lseek(fd, offset, whence); + if (ret == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateInt64(env, ret).val_; +} + +napi_value Lseek::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE, NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetLseekArg, fd, offset, whence] = GetLseekArg(env, funcArg); + if (!resGetLseekArg) { + return nullptr; + } + + auto arg = make_shared(); + auto cbExec = [fd = fd, offset = offset, whence = whence, arg](napi_env env) -> UniError { + int ret = lseek(fd, offset, whence); + *arg = ret; + if (ret == -1) { + return UniError(errno); + } else { + return UniError(ERRNO_NOERR); + } + }; + + auto cbComplCallback = [arg](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt64(env, *arg) }; + }; + + size_t argc = funcArg.GetArgc(); + const string procedureName = "FileIOLseek"; + NVal thisVar(env, funcArg.GetThisVar()); + if (argc == NARG_CNT::THREE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FOURTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_; + } +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/lseek.h b/interfaces/kits/js/src/mod_fileio/properties/lseek.h new file mode 100644 index 000000000..b8ffb861c --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/lseek.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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_LSEEK_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_LSEEK_H + +#include "../../common/napi/n_val.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class Lseek final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); + static napi_value Async(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/lstat.cpp b/interfaces/kits/js/src/mod_fileio/properties/lstat.cpp new file mode 100644 index 000000000..9945fb8e8 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/lstat.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2021 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 "lstat.h" + +#include +#include + +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_class.h" +#include "../../common/napi/n_func_arg.h" +#include "../../common/napi/n_val.h" +#include "../../common/uni_error.h" + +#include "../class_stat/stat_entity.h" +#include "../class_stat/stat_n_exporter.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +napi_value Lstat::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, pathPtr, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "The first argument requires type string"); + return nullptr; + } + + struct stat buf; + int ret = lstat(pathPtr.get(), &buf); + if (ret == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {}); + if (!objStat) { + return nullptr; + } + + auto statEntity = NClass::GetEntityOf(env, objStat); + if (!statEntity) { + return nullptr; + } + + statEntity->stat_ = buf; + return objStat; +} + +struct AsyncStatArg { + struct stat stat_; +}; + +napi_value Lstat::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + string path = tmp.get(); + auto arg = make_shared(); + auto cbExec = [arg, path](napi_env env) -> UniError { + if (lstat(path.c_str(), &arg->stat_)) { + return UniError(errno); + } else { + return UniError(ERRNO_NOERR); + } + }; + + auto cbCompl = [arg](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {}); + if (!objStat) { + return { env, UniError(EIO).GetNapiErr(env) }; + } + auto statEntity = NClass::GetEntityOf(env, objStat); + if (!statEntity) { + return { env, UniError(EIO).GetNapiErr(env) }; + } + statEntity->stat_ = arg->stat_; + return { env, objStat }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + const string procedureName = "FileIOLstat"; + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; + } +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/lstat.h b/interfaces/kits/js/src/mod_fileio/properties/lstat.h new file mode 100644 index 000000000..6bd24beff --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/lstat.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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_LSTAT_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_LSTAT_H + +#include "../../common/napi/uni_header.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class Lstat final { +public: + static napi_value Async(napi_env env, napi_callback_info info); + static napi_value Sync(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fileio/properties/mkdtemp.cpp new file mode 100644 index 000000000..48dc15eeb --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/mkdtemp.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2021-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 "mkdtemp.h" + +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_func_arg.h" +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; +napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + string path = tmp.get(); + if (mkdtemp(const_cast(path.c_str())) == nullptr) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUTF8String(env, path).val_; +} + +napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + string path = tmp.get(); + auto arg = make_shared(); + auto cbExec = [path, arg](napi_env env) -> UniError { + if (mkdtemp(const_cast(path.c_str())) == nullptr) { + return UniError(errno); + } else { + *arg = path; + return UniError(ERRNO_NOERR); + } + }; + + auto cbComplete = [arg](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return NVal::CreateUTF8String(env, *arg); + } + }; + + const string procedureName = "FileIOmkdtemp"; + size_t argc = funcArg.GetArgc(); + NVal thisVar(env, funcArg.GetThisVar()); + if (argc == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/properties/mkdtemp.h b/interfaces/kits/js/src/mod_fileio/properties/mkdtemp.h new file mode 100644 index 000000000..de9a7aed1 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/mkdtemp.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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_MKDTEMP_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_MKDTEMP_H + +#include "../../common/napi/n_val.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class Mkdtemp final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); + static napi_value Async(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/open.cpp b/interfaces/kits/js/src/mod_fileio/properties/open.cpp new file mode 100644 index 000000000..1dfe4cbf4 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/open.cpp @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2021-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 "open.h" +#include +#include +#include +#include + +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_func_arg.h" +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +int AdaptToAbi(int &flags) +{ + static constexpr int USR_O_RDONLY = 00; + static constexpr int USR_O_WRONLY = 01; + static constexpr int USR_O_RDWR = 02; + static constexpr int USR_O_CREAT = 0100; + static constexpr int USR_O_EXCL = 0200; + static constexpr int USR_O_TRUNC = 01000; + static constexpr int USR_O_APPEND = 02000; + static constexpr int USR_O_NONBLOCK = 04000; + static constexpr int USR_O_DIRECTORY = 0200000; + static constexpr int USR_O_NOFOLLOW = 0400000; + static constexpr int USR_O_SYNC = 04010000; + + flags |= ((flags & USR_O_RDONLY) == USR_O_RDONLY) ? O_RDONLY : 0; + flags |= ((flags & USR_O_WRONLY) == USR_O_WRONLY) ? O_WRONLY : 0; + flags |= ((flags & USR_O_RDWR) == USR_O_RDWR) ? O_RDWR : 0; + flags |= ((flags & USR_O_CREAT) == USR_O_CREAT) ? O_CREAT : 0; + flags |= ((flags & USR_O_EXCL) == USR_O_EXCL) ? O_EXCL : 0; + flags |= ((flags & USR_O_TRUNC) == USR_O_TRUNC) ? O_TRUNC : 0; + flags |= ((flags & USR_O_APPEND) == USR_O_APPEND) ? O_APPEND : 0; + flags |= ((flags & USR_O_NONBLOCK) == USR_O_NONBLOCK) ? O_NONBLOCK : 0; + flags |= ((flags & USR_O_DIRECTORY) == USR_O_DIRECTORY) ? O_DIRECTORY : 0; + flags |= ((flags & USR_O_NOFOLLOW) == USR_O_NOFOLLOW) ? O_NOFOLLOW : 0; + flags |= ((flags & USR_O_SYNC) == USR_O_SYNC) ? O_SYNC : 0; + return flags; +} + +napi_value Open::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + int flags = O_RDONLY; + if (funcArg.GetArgc() >= NARG_CNT::TWO) { + auto [resGetSecondArg, flg] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid flags"); + return nullptr; + } + flags = flg; + } + + (void)AdaptToAbi(flags); + int fd = -1; + size_t argc = funcArg.GetArgc(); + if (argc != NARG_CNT::THREE) { + size_t flagsFirst { flags }; + if ((flagsFirst & O_CREAT) || (flagsFirst & O_TMPFILE)) { + UniError(EINVAL).ThrowErr(env, "called with O_CREAT/O_TMPFILE but no mode"); + return nullptr; + } + fd = open(path.get(), flags); + } else { + auto [resGetThirdArg, mode] = NVal(env, funcArg.GetArg(NARG_POS::THIRD)).ToInt32(); + if (!resGetThirdArg) { + UniError(EINVAL).ThrowErr(env, "Invalid mode"); + return nullptr; + } + fd = open(path.get(), flags, mode); + } + + if (fd == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateInt64(env, fd).val_; +} + +static UniError DoOpenExec(const std::string& path, const int flags, const int mode, shared_ptr arg) +{ + int ret = open(path.c_str(), flags, mode); + *arg = ret; + if (ret == -1) { + return UniError(errno); + } else { + return UniError(ERRNO_NOERR); + } +} + +napi_value Open::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + int flags = O_RDONLY; + size_t argc = funcArg.GetArgc(); + if (argc >= NARG_CNT::TWO && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { + auto [resGetSecondArg, flg] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid flags"); + return nullptr; + } + flags = flg; + } + + int mode = 0; + if (argc == NARG_CNT::FOUR || + (argc == NARG_CNT::THREE && NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number))) { + auto [resGetThirdArg, modes] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + if (!resGetThirdArg) { + UniError(EINVAL).ThrowErr(env, "Invalid mode"); + return nullptr; + } + mode = modes; + } + + (void)AdaptToAbi(flags); + auto arg = make_shared(); + auto cbExec = [path = string(path.get()), flags = flags, mode = mode, arg](napi_env env) -> UniError { + return DoOpenExec(path, flags, mode, arg); + }; + + auto cbComplCallback = [arg](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt64(env, *arg) }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) || + (argc == NARG_CNT::THREE && (NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number)))) { + return NAsyncWorkPromise(env, thisVar).Schedule("FileIOOpen", cbExec, cbComplCallback).val_; + } else { + size_t cbIdx = argc - 1; + NVal cb(env, funcArg[cbIdx]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOOpen", cbExec, cbComplCallback).val_; + } +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/open.h b/interfaces/kits/js/src/mod_fileio/properties/open.h new file mode 100644 index 000000000..f6b6d718c --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/open.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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_OPEN_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_OPEN_H + +#include "../../common/napi/n_val.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class Open final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); + static napi_value Async(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_dir.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_dir.cpp new file mode 100644 index 000000000..f038733c1 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/open_dir.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2021-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 "open_dir.h" +#include +#include +#include + +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_class.h" +#include "../../common/napi/n_func_arg.h" +#include "../../common/napi/n_val.h" +#include "../../common/uni_error.h" +#include "../class_dir/dir_entity.h" +#include "../class_dir/dir_n_exporter.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +napi_value OpenDir::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + return nullptr; + } + + auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + std::unique_ptr> dir = { opendir(path.get()), closedir }; + if (dir == nullptr) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + napi_value objDir = NClass::InstantiateClass(env, DirNExporter::className_, {}); + if (objDir == nullptr) { + UniError(EINVAL).ThrowErr(env, "Cannot instantiate class DirSync"); + return nullptr; + } + + auto dirEntity = NClass::GetEntityOf(env, objDir); + if (dirEntity == nullptr) { + UniError(EINVAL).ThrowErr(env, "Cannot get the entity of objDir"); + return nullptr; + } + + dirEntity->dir_.swap(dir); + return objDir; +} + +struct OpenDirArgs { + NRef dirPtr_; + DIR *dir = nullptr; + explicit OpenDirArgs(NVal dirPtr) : dirPtr_(dirPtr) {} + ~OpenDirArgs() = default; +}; + +napi_value OpenDir::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + string path = tmp.get(); + auto arg = make_shared(NVal(env, funcArg.GetThisVar())); + auto cbExec = [arg, path](napi_env env) -> UniError { + DIR *dir = nullptr; + dir = opendir(path.c_str()); + if (dir == nullptr) { + return UniError(errno); + } + arg->dir = dir; + return UniError(ERRNO_NOERR); + }; + + auto cbCompl = [arg](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + std::unique_ptr> dir = { arg->dir, closedir }; + if (!dir) { + return { env, UniError(errno).GetNapiErr(env) }; + } + + napi_value objDir = NClass::InstantiateClass(env, DirNExporter::className_, {}); + if (!objDir) { + return { env, UniError(EINVAL).GetNapiErr(env) }; + } + + auto dirEntity = NClass::GetEntityOf(env, objDir); + if (!dirEntity) { + return { env, UniError(EINVAL).GetNapiErr(env) }; + } + dirEntity->dir_.swap(dir); + return { env, objDir }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule("fileIOOpenDir", cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule("fileIOOpenDir", cbExec, cbCompl).val_; + } +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_dir.h b/interfaces/kits/js/src/mod_fileio/properties/open_dir.h new file mode 100644 index 000000000..e2e07d3b0 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/open_dir.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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_OPEN_DIR_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_OPEN_DIR_H + +#include "../../common/napi/uni_header.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class OpenDir final { +public: + static napi_value Async(napi_env env, napi_callback_info info); + static napi_value Sync(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/posix_fallocate.cpp b/interfaces/kits/js/src/mod_fileio/properties/posix_fallocate.cpp new file mode 100644 index 000000000..fe9694d36 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/posix_fallocate.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2021-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 "posix_fallocate.h" +#include +#include +#include +#include + +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_func_arg.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +static tuple GetPosixFallocateArg(napi_env env, const NFuncArg &funcArg) +{ + auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid fd"); + return { false, -1, -1, -1 }; + } + + auto [resGetSecondArg, offset] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid offset"); + return { false, -1, -1, -1 }; + } + + auto [resGetThirdArg, len] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + if (!resGetThirdArg) { + UniError(EINVAL).ThrowErr(env, "Invalid len"); + return { false, -1, -1, -1 }; + } + + return { true, fd, offset, len }; +} + +napi_value PosixFallocate::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetPosixFallocateArg, fd, offset, len] = GetPosixFallocateArg(env, funcArg); + if (!resGetPosixFallocateArg) { + return nullptr; + } + + int ret = posix_fallocate(fd, offset, len); + if (ret == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value PosixFallocate::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE, NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetPosixFallocateArg, fd, offset, len] = GetPosixFallocateArg(env, funcArg); + if (!resGetPosixFallocateArg) { + return nullptr; + } + + auto cbExec = [fd = fd, offset = offset, len = len](napi_env env) -> UniError { + if (posix_fallocate(fd, offset, len) == -1) { + return UniError(errno); + } else { + return UniError(ERRNO_NOERR); + } + }; + + auto cbCompl = [](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + const string procedureName = "fileioPosixFallocate"; + size_t argc = funcArg.GetArgc(); + if (argc == NARG_CNT::THREE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FOURTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; + } + + return NVal::CreateUndefined(env).val_; +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/posix_fallocate.h b/interfaces/kits/js/src/mod_fileio/properties/posix_fallocate.h new file mode 100644 index 000000000..c26c70465 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/posix_fallocate.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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_POSIX_FALLOCATE_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_POSIX_FALLOCATE_H + +#include "../../common/napi/n_val.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class PosixFallocate final { +public: + static napi_value Async(napi_env env, napi_callback_info info); + static napi_value Sync(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp new file mode 100644 index 000000000..331227632 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp @@ -0,0 +1,223 @@ +/* + * Copyright (c) 2021-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 "read_text.h" + +#include +#include +#include +#include +#include +#include "../../common/file_helper/fd_guard.h" +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_func_arg.h" +#include "../common_func.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +static tuple, bool> GetReadTextArg(napi_env env, napi_value argOption) +{ + NVal op(env, argOption); + ssize_t position = 0; + ssize_t len = 0; + bool succ = false; + bool hasOp = false; + bool hasLen = false; + unique_ptr encoding; + + if (op.HasProp("position")) { + tie(succ, position) = op.GetProp("position").ToInt32(); + if (!succ) { + return { false, position, hasLen, len, nullptr, hasOp }; + } + hasOp = true; + } + + if (op.HasProp("length")) { + tie(succ, len) = op.GetProp("length").ToInt32(); + if (!succ) { + return { false, position, hasLen, len, nullptr, hasOp }; + } + hasOp = true; + hasLen = true; + } + + if (op.HasProp("encoding")) { + auto [succ, encoding, unuse] = op.GetProp("encoding").ToUTF8String(); + if (!succ) { + return { false, position, hasLen, len, nullptr, hasOp }; + } + hasOp = true; + } + + return { true, position, hasLen, len, move(encoding), hasOp }; +} + +napi_value ReadText::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto [resGetReadTextArg, position, hasLen, len, encoding, useless] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); + if (!resGetReadTextArg) { + UniError(EINVAL).ThrowErr(env, "Invalid option"); + return nullptr; + } + + struct stat statbf; + int ret; + FDGuard sfd; + sfd.SetFD(open(path.get(), O_RDONLY)); + if ((!sfd) || (fstat(sfd.GetFD(), &statbf) == -1)) { + UniError(errno).ThrowErr(env); + return nullptr; + } + + if (position > statbf.st_size) { + UniError(EINVAL).ThrowErr(env, "Invalid position"); + return nullptr; + } + + len = !hasLen ? statbf.st_size : len; + len = ((len < statbf.st_size) ? len : statbf.st_size); + std::unique_ptr readbuf = std::make_unique(len + 1); + if (readbuf == nullptr) { + UniError(EINVAL).ThrowErr(env, "file is too large"); + return nullptr; + } + + if (memset_s(readbuf.get(), len + 1, 0, len + 1) != EOK) { + UniError(errno).ThrowErr(env, "dfs mem error"); + return nullptr; + } + + ret = position > 0 ? pread(sfd.GetFD(), readbuf.get(), len, position) : read(sfd.GetFD(), readbuf.get(), len); + if (ret == -1) { + UniError(EINVAL).ThrowErr(env, "Invalid read file"); + return nullptr; + } + + return NVal::CreateUTF8String(env, readbuf.get(), ret).val_; +} + +UniError ReadText::AsyncExec(const std::string &path, std::shared_ptr arg, ssize_t position, + bool hasLen, ssize_t len) +{ + if (arg == nullptr) { + return UniError(ENOMEM); + } + + FDGuard sfd; + struct stat statbf; + arg->len = len; + sfd.SetFD(open(path.c_str(), O_RDONLY)); + if (sfd.GetFD() == -1) { + return UniError(EINVAL); + } + + if (fstat(sfd.GetFD(), &statbf) == -1) { + return UniError(EINVAL); + } + + if (position > statbf.st_size) { + return UniError(EINVAL); + } + + if (!hasLen) { + arg->len = statbf.st_size; + } + + arg->len = ((arg->len < statbf.st_size) ? arg->len : statbf.st_size); + arg->buf = std::make_unique(arg->len); + if (arg->buf == nullptr) { + return UniError(ENOMEM); + } + + if (position > 0) { + arg->len = pread(sfd.GetFD(), arg->buf.get(), arg->len, position); + } else { + arg->len = read(sfd.GetFD(), arg->buf.get(), arg->len); + } + + if (arg->len == -1) { + return UniError(EINVAL); + } + + return UniError(ERRNO_NOERR); +} + +napi_value ReadText::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto [resGetSecondArg, position, hasLen, len, encoding, hasOp] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid option"); + return nullptr; + } + + auto arg = make_shared(NVal(env, funcArg.GetThisVar())); + if (arg == nullptr) { + return nullptr; + } + + auto cbExec = + [path = string(path.get()), arg, position = position, hasLen = hasLen, len = len](napi_env env) -> UniError { + return AsyncExec(path, arg, position, hasLen, len); + }; + + auto cbComplete = [arg](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return NVal::CreateUTF8String(env, arg->buf.get(), arg->len); + } + }; + + size_t argc = funcArg.GetArgc(); + NVal thisVar(env, funcArg.GetThisVar()); + if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && hasOp)) { + return NAsyncWorkPromise(env, thisVar).Schedule("FileIOReadText", cbExec, cbComplete).val_; + } else { + int cbIdx = !hasOp ? NARG_POS::SECOND : NARG_POS::THIRD; + NVal cb(env, funcArg[cbIdx]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOReadText", cbExec, cbComplete).val_; + } +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/read_text.h b/interfaces/kits/js/src/mod_fileio/properties/read_text.h new file mode 100644 index 000000000..882143773 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/read_text.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 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_READ_TEXT_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_READ_TEXT_H + +#include "../../common/napi/n_async/n_ref.h" +#include "../../common/napi/n_val.h" +#include "../../common/uni_error.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +struct AsyncReadTextArg { + NRef _refReadBuf; + std::unique_ptr buf; + ssize_t len = 0; + explicit AsyncReadTextArg(NVal refReadBuf) : _refReadBuf(refReadBuf) {}; + ~AsyncReadTextArg() = default; +}; + +class ReadText final { +public: +static UniError AsyncExec(const std::string &path, std::shared_ptr arg, ssize_t position, + bool hasLen, ssize_t len); + static napi_value Async(napi_env env, napi_callback_info info); + static napi_value Sync(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file -- Gitee