mirror of
https://github.com/openharmony/distributeddatamgr_file.git
synced 2026-06-30 22:07:58 -04:00
@@ -1,15 +0,0 @@
|
||||
# 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.
|
||||
|
||||
ability_runtime_path = "//foundation/ability/ability_runtime"
|
||||
ability_runtime_kits_path = "${ability_runtime_path}/frameworks/kits"
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 34 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 32 KiB |
@@ -1,243 +0,0 @@
|
||||
# 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.
|
||||
|
||||
import("//build/ohos.gni")
|
||||
import(
|
||||
"//foundation/distributeddatamgr/distributedfile/distributedfile_aafwk.gni")
|
||||
|
||||
file_common_src = [
|
||||
"src/common/file_helper/fd_guard.cpp",
|
||||
"src/common/napi/n_async/n_async_work_callback.cpp",
|
||||
"src/common/napi/n_async/n_async_work_promise.cpp",
|
||||
"src/common/napi/n_async/n_ref.cpp",
|
||||
"src/common/napi/n_class.cpp",
|
||||
"src/common/napi/n_func_arg.cpp",
|
||||
"src/common/napi/n_val.cpp",
|
||||
"src/common/uni_error.cpp",
|
||||
]
|
||||
|
||||
ohos_shared_library("fileio") {
|
||||
subsystem_name = "distributeddatamgr"
|
||||
part_name = "distributedfilejs"
|
||||
|
||||
relative_install_dir = "module"
|
||||
|
||||
include_dirs = [
|
||||
"//third_party/node/src",
|
||||
"//foundation/arkui/napi/interfaces/kits",
|
||||
"//third_party/bounds_checking_function/include",
|
||||
"//third_party/libuv/include",
|
||||
"//third_party/openssl/include",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi/n_async",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/file_helper",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/native/remote_uri",
|
||||
]
|
||||
|
||||
sources = file_common_src
|
||||
sources += [
|
||||
"src/common/file_helper/hash_file.cpp",
|
||||
"src/mod_fileio/class_constants/constants.cpp",
|
||||
"src/mod_fileio/class_dir/dir_n_exporter.cpp",
|
||||
"src/mod_fileio/class_dirent/dirent_n_exporter.cpp",
|
||||
"src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp",
|
||||
"src/mod_fileio/class_stat/stat_n_exporter.cpp",
|
||||
"src/mod_fileio/class_stream/flush.cpp",
|
||||
"src/mod_fileio/class_stream/stream_n_exporter.cpp",
|
||||
"src/mod_fileio/class_watcher/watcher_n_exporter.cpp",
|
||||
"src/mod_fileio/common_func.cpp",
|
||||
"src/mod_fileio/module.cpp",
|
||||
"src/mod_fileio/properties/chmod.cpp",
|
||||
"src/mod_fileio/properties/chown.cpp",
|
||||
"src/mod_fileio/properties/close.cpp",
|
||||
"src/mod_fileio/properties/copy_file.cpp",
|
||||
"src/mod_fileio/properties/create_randomaccessfile.cpp",
|
||||
"src/mod_fileio/properties/create_stream.cpp",
|
||||
"src/mod_fileio/properties/fchmod.cpp",
|
||||
"src/mod_fileio/properties/fchown.cpp",
|
||||
"src/mod_fileio/properties/fdatasync.cpp",
|
||||
"src/mod_fileio/properties/fdopen_stream.cpp",
|
||||
"src/mod_fileio/properties/fstat.cpp",
|
||||
"src/mod_fileio/properties/fsync.cpp",
|
||||
"src/mod_fileio/properties/ftruncate.cpp",
|
||||
"src/mod_fileio/properties/hash.cpp",
|
||||
"src/mod_fileio/properties/lchown.cpp",
|
||||
"src/mod_fileio/properties/link.cpp",
|
||||
"src/mod_fileio/properties/lseek.cpp",
|
||||
"src/mod_fileio/properties/lstat.cpp",
|
||||
"src/mod_fileio/properties/mkdtemp.cpp",
|
||||
"src/mod_fileio/properties/open.cpp",
|
||||
"src/mod_fileio/properties/open_dir.cpp",
|
||||
"src/mod_fileio/properties/posix_fallocate.cpp",
|
||||
"src/mod_fileio/properties/prop_n_exporter.cpp",
|
||||
"src/mod_fileio/properties/read_dir.cpp",
|
||||
"src/mod_fileio/properties/read_text.cpp",
|
||||
"src/mod_fileio/properties/rename.cpp",
|
||||
"src/mod_fileio/properties/rmdir.cpp",
|
||||
"src/mod_fileio/properties/rmdirent.cpp",
|
||||
"src/mod_fileio/properties/stat.cpp",
|
||||
"src/mod_fileio/properties/symlink.cpp",
|
||||
"src/mod_fileio/properties/truncate.cpp",
|
||||
"src/mod_fileio/properties/watcher.cpp",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//foundation/arkui/napi:ace_napi",
|
||||
"//third_party/bounds_checking_function:libsec_static",
|
||||
"//third_party/openssl:libcrypto_static",
|
||||
]
|
||||
|
||||
external_deps = [ "hilog:libhilog" ]
|
||||
}
|
||||
|
||||
ohos_shared_library("file") {
|
||||
subsystem_name = "distributeddatamgr"
|
||||
part_name = "distributedfilejs"
|
||||
|
||||
relative_install_dir = "module"
|
||||
|
||||
include_dirs = [
|
||||
"//third_party/node/src",
|
||||
"//foundation/arkui/napi/interfaces/kits",
|
||||
"//third_party/bounds_checking_function/include",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi/n_async",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/file_helper",
|
||||
]
|
||||
|
||||
sources = file_common_src
|
||||
sources += [
|
||||
"src/common/ability_helper.cpp",
|
||||
"src/mod_file/class_file/file_n_exporter.cpp",
|
||||
"src/mod_file/common_func.cpp",
|
||||
"src/mod_file/module.cpp",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"${ability_runtime_path}/frameworks/native/ability/native:abilitykit_native",
|
||||
"//foundation/arkui/napi:ace_napi",
|
||||
"//third_party/bounds_checking_function:libsec_static",
|
||||
]
|
||||
|
||||
external_deps = [ "hilog:libhilog" ]
|
||||
}
|
||||
|
||||
ohos_shared_library("statfs") {
|
||||
subsystem_name = "distributeddatamgr"
|
||||
part_name = "distributedfilejs"
|
||||
|
||||
relative_install_dir = "module"
|
||||
|
||||
sources = [
|
||||
"src/mod_statfs/statfs_n_exporter.cpp",
|
||||
"src/mod_statfs/statfs_napi.cpp",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libhilog",
|
||||
"//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn",
|
||||
]
|
||||
}
|
||||
|
||||
ohos_shared_library("environment") {
|
||||
subsystem_name = "distributeddatamgr"
|
||||
part_name = "distributedfilejs"
|
||||
|
||||
relative_install_dir = "module"
|
||||
|
||||
include_dirs = [
|
||||
"//third_party/node/src",
|
||||
"//foundation/arkui/napi/interfaces/kits",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi/n_async",
|
||||
]
|
||||
|
||||
sources = [
|
||||
"src/common/napi/n_async/n_async_work_callback.cpp",
|
||||
"src/common/napi/n_async/n_async_work_promise.cpp",
|
||||
"src/common/napi/n_async/n_ref.cpp",
|
||||
"src/common/napi/n_func_arg.cpp",
|
||||
"src/common/napi/n_val.cpp",
|
||||
"src/common/uni_error.cpp",
|
||||
"src/mod_environment/environment_n_exporter.cpp",
|
||||
"src/mod_environment/environment_napi.cpp",
|
||||
]
|
||||
|
||||
deps = [ "//foundation/arkui/napi:ace_napi" ]
|
||||
|
||||
external_deps = [ "hilog:libhilog" ]
|
||||
}
|
||||
|
||||
ohos_shared_library("securitylabel") {
|
||||
subsystem_name = "distributeddatamgr"
|
||||
part_name = "distributedfilejs"
|
||||
|
||||
relative_install_dir = "module"
|
||||
|
||||
cflags = [ "-Wno-format" ]
|
||||
|
||||
include_dirs = [
|
||||
"//foundation/arkui/napi/interfaces/kits",
|
||||
"//foundation/arkui/ace_engine/frameworks/base/utils",
|
||||
"//foundation/arkui/ace_engine/frameworks",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi/n_async",
|
||||
]
|
||||
|
||||
sources = file_common_src
|
||||
sources += [
|
||||
"src/mod_securitylabel/securitylabel_n_exporter.cpp",
|
||||
"src/mod_securitylabel/securitylabel_napi.cpp",
|
||||
]
|
||||
|
||||
deps = [ "//foundation/arkui/napi:ace_napi" ]
|
||||
|
||||
external_deps = [ "hilog:libhilog" ]
|
||||
}
|
||||
|
||||
ohos_shared_library("document") {
|
||||
subsystem_name = "distributeddatamgr"
|
||||
part_name = "distributedfilejs"
|
||||
|
||||
relative_install_dir = "module"
|
||||
|
||||
include_dirs = [
|
||||
"//third_party/node/src",
|
||||
"//foundation/arkui/napi/interfaces/kits",
|
||||
"//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi/n_async",
|
||||
]
|
||||
|
||||
sources = [
|
||||
"src/common/napi/n_async/n_async_work_callback.cpp",
|
||||
"src/common/napi/n_async/n_async_work_promise.cpp",
|
||||
"src/common/napi/n_async/n_ref.cpp",
|
||||
"src/common/napi/n_func_arg.cpp",
|
||||
"src/common/napi/n_val.cpp",
|
||||
"src/common/uni_error.cpp",
|
||||
"src/mod_document/document_n_exporter.cpp",
|
||||
"src/mod_document/document_napi.cpp",
|
||||
]
|
||||
|
||||
deps = [ "//foundation/arkui/napi:ace_napi" ]
|
||||
|
||||
external_deps = [ "hilog:libhilog" ]
|
||||
}
|
||||
|
||||
group("build_kits_js") {
|
||||
deps = [
|
||||
":document",
|
||||
":environment",
|
||||
":file",
|
||||
":fileio",
|
||||
":securitylabel",
|
||||
":statfs",
|
||||
]
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* 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 "ability_helper.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "napi/n_func_arg.h"
|
||||
#include "napi/uni_header.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
using namespace std;
|
||||
using OHOS::AppExecFwk::Ability;
|
||||
using OHOS::AppExecFwk::AbilityContext;
|
||||
|
||||
Ability* AbilityHelper::GetJsAbility(napi_env env)
|
||||
{
|
||||
napi_value global = nullptr;
|
||||
napi_value abilityContext = nullptr;
|
||||
|
||||
napi_status status = napi_get_global(env, &global);
|
||||
if (status != napi_ok || global == nullptr) {
|
||||
HILOGE("Cannot get global instance for %{public}d", status);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
status = napi_get_named_property(env, global, "ability", &abilityContext);
|
||||
if (status != napi_ok || abilityContext == nullptr) {
|
||||
HILOGE("Cannot get ability context for %{public}d", status);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Ability *ability = nullptr;
|
||||
status = napi_get_value_external(env, abilityContext, (void **)&ability);
|
||||
if (status != napi_ok || ability == nullptr) {
|
||||
HILOGE("Get ability form property failed for %{public}d", status);
|
||||
}
|
||||
|
||||
return ability;
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* 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_NAPI_COMMON_ABILITY_HELPER_H
|
||||
#define INTERFACES_KITS_NAPI_COMMON_ABILITY_HELPER_H
|
||||
|
||||
#include "ability.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
struct AbilityHelper {
|
||||
static AppExecFwk::Ability *GetJsAbility(napi_env env);
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,174 +0,0 @@
|
||||
/*
|
||||
* 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 FILE_FILTER_H
|
||||
#define FILE_FILTER_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "parcel.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class FileFilter : public Parcelable {
|
||||
public:
|
||||
FileFilter() = default;
|
||||
~FileFilter() = default;
|
||||
|
||||
FileFilter(std::vector<std::string> suffix, std::vector<std::string> displayName,
|
||||
std::vector<std::string> mimeType, int64_t fileSizeOver,
|
||||
double lastModifiedAfter, bool excludeMedia, bool hasFilter)
|
||||
: suffix_(suffix), displayName_(displayName), mimeType_(mimeType), fileSizeOver_(fileSizeOver),
|
||||
lastModifiedAfter_(lastModifiedAfter), excludeMedia_(excludeMedia), hasFilter_(hasFilter) {}
|
||||
explicit FileFilter(std::vector<std::string> suffix): suffix_(suffix) {}
|
||||
FileFilter(const FileFilter &filter) = default;
|
||||
FileFilter &operator=(const FileFilter& filter) = default;
|
||||
|
||||
void SetSuffix(const std::vector<std::string> &suffix)
|
||||
{
|
||||
suffix_ = suffix;
|
||||
}
|
||||
|
||||
std::vector<std::string> GetSuffix() const
|
||||
{
|
||||
return suffix_;
|
||||
}
|
||||
|
||||
void SetDisplayName(const std::vector<std::string> &displayName)
|
||||
{
|
||||
displayName_ = displayName;
|
||||
}
|
||||
|
||||
std::vector<std::string> GetDisplayName() const
|
||||
{
|
||||
return displayName_;
|
||||
}
|
||||
|
||||
void SetMimeType(const std::vector<std::string> &mimeType)
|
||||
{
|
||||
mimeType_ = mimeType;
|
||||
}
|
||||
|
||||
std::vector<std::string> GetMimeType() const
|
||||
{
|
||||
return mimeType_;
|
||||
}
|
||||
|
||||
void SetFileSizeOver(const int64_t &fileSizeOver)
|
||||
{
|
||||
fileSizeOver_ = fileSizeOver;
|
||||
}
|
||||
|
||||
int64_t GetFileSizeOver() const
|
||||
{
|
||||
return fileSizeOver_;
|
||||
}
|
||||
|
||||
void SetLastModifiedAfter(const double &lastModifiedAfter)
|
||||
{
|
||||
lastModifiedAfter_ = lastModifiedAfter;
|
||||
}
|
||||
|
||||
double GetLastModifiedAfter() const
|
||||
{
|
||||
return lastModifiedAfter_;
|
||||
}
|
||||
|
||||
void SetExcludeMedia(const bool &excludeMedia)
|
||||
{
|
||||
excludeMedia_ = excludeMedia;
|
||||
}
|
||||
|
||||
bool GetExcludeMedia() const
|
||||
{
|
||||
return excludeMedia_;
|
||||
}
|
||||
|
||||
void SetHasFilter(const bool &hasFilter)
|
||||
{
|
||||
hasFilter_ = hasFilter;
|
||||
}
|
||||
|
||||
bool GetHasFilter() const
|
||||
{
|
||||
return hasFilter_;
|
||||
}
|
||||
|
||||
bool Marshalling(Parcel &parcel) const
|
||||
{
|
||||
if (!parcel.WriteStringVector(suffix_)) {
|
||||
return false;
|
||||
}
|
||||
if (!parcel.WriteStringVector(displayName_)) {
|
||||
return false;
|
||||
}
|
||||
if (!parcel.WriteStringVector(mimeType_)) {
|
||||
return false;
|
||||
}
|
||||
if (!parcel.WriteInt64(fileSizeOver_)) {
|
||||
return false;
|
||||
}
|
||||
if (!parcel.WriteDouble(lastModifiedAfter_)) {
|
||||
return false;
|
||||
}
|
||||
if (!parcel.WriteBool(excludeMedia_)) {
|
||||
return false;
|
||||
}
|
||||
if (!parcel.WriteBool(hasFilter_)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static FileFilter* Unmarshalling(Parcel &parcel)
|
||||
{
|
||||
auto obj = new (std::nothrow) FileFilter();
|
||||
if (obj != nullptr && !obj->ReadFromParcel(parcel)) {
|
||||
delete obj;
|
||||
obj = nullptr;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
bool ReadFromParcel(Parcel &parcel)
|
||||
{
|
||||
if (!parcel.ReadStringVector(&suffix_)) {
|
||||
return false;
|
||||
}
|
||||
if (!parcel.ReadStringVector(&displayName_)) {
|
||||
return false;
|
||||
}
|
||||
if (!parcel.ReadStringVector(&mimeType_)) {
|
||||
return false;
|
||||
}
|
||||
fileSizeOver_ = parcel.ReadInt64();
|
||||
lastModifiedAfter_ = parcel.ReadDouble();
|
||||
excludeMedia_ = parcel.ReadBool();
|
||||
hasFilter_ = parcel.ReadBool();
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::string> suffix_ = std::vector<std::string>();
|
||||
std::vector<std::string> displayName_ = std::vector<std::string>();
|
||||
std::vector<std::string> mimeType_ = std::vector<std::string>();
|
||||
int64_t fileSizeOver_ = 0;
|
||||
double lastModifiedAfter_ = 0;
|
||||
bool excludeMedia_ = false;
|
||||
bool hasFilter_ = false;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // FILE_FILTER_H
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* 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 "fd_guard.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../log.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
FDGuard::FDGuard(int fd) : fd_(fd) {}
|
||||
|
||||
FDGuard::FDGuard(int fd, bool autoClose) : fd_(fd), autoClose_(autoClose) {}
|
||||
|
||||
FDGuard::FDGuard(FDGuard &&fdg) : fd_(fdg.fd_), autoClose_(fdg.autoClose_)
|
||||
{
|
||||
fdg.fd_ = -1;
|
||||
}
|
||||
|
||||
FDGuard &FDGuard::operator=(FDGuard &&fdg)
|
||||
{
|
||||
if (this == &fdg) {
|
||||
return *this;
|
||||
}
|
||||
this->fd_ = fdg.fd_;
|
||||
this->autoClose_ = fdg.autoClose_;
|
||||
fdg.fd_ = -1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
FDGuard::~FDGuard()
|
||||
{
|
||||
if (fd_ >= 0 && fd_ <= STDERR_FILENO) {
|
||||
HILOGI("~FDGuard, fd_ = %{public}d", fd_);
|
||||
}
|
||||
if (fd_ >= 0 && autoClose_) {
|
||||
close(fd_);
|
||||
}
|
||||
}
|
||||
|
||||
FDGuard::operator bool() const
|
||||
{
|
||||
return fd_ >= 0;
|
||||
}
|
||||
|
||||
int FDGuard::GetFD() const
|
||||
{
|
||||
return fd_;
|
||||
}
|
||||
|
||||
void FDGuard::SetFD(int fd, bool autoClose)
|
||||
{
|
||||
fd_ = fd;
|
||||
autoClose_ = autoClose;
|
||||
}
|
||||
|
||||
void FDGuard::ClearFD()
|
||||
{
|
||||
fd_ = -1;
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* 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 FD_GUARD_H
|
||||
#define FD_GUARD_H
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class FDGuard final {
|
||||
public:
|
||||
FDGuard() = default;
|
||||
explicit FDGuard(int fd);
|
||||
FDGuard(int fd, bool autoClose);
|
||||
|
||||
FDGuard(const FDGuard &fdg) = delete;
|
||||
FDGuard &operator=(const FDGuard &fdg) = delete;
|
||||
|
||||
FDGuard(FDGuard &&fdg);
|
||||
FDGuard &operator=(FDGuard &&fdg);
|
||||
|
||||
operator bool() const;
|
||||
|
||||
~FDGuard();
|
||||
|
||||
int GetFD() const;
|
||||
void SetFD(int fd, bool autoClose = true);
|
||||
void ClearFD();
|
||||
|
||||
private:
|
||||
int fd_ = -1;
|
||||
bool autoClose_ = true;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,106 +0,0 @@
|
||||
/*
|
||||
* 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_file.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
#include <openssl/md5.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
using namespace std;
|
||||
|
||||
static tuple<int, string> HashFinal(int err, const unique_ptr<unsigned char[]> &hashBuf, size_t hashLen)
|
||||
{
|
||||
if (err) {
|
||||
return { err, "" };
|
||||
}
|
||||
|
||||
stringstream ss;
|
||||
for (size_t i = 0; i < hashLen; ++i) {
|
||||
const int hexPerByte = 2;
|
||||
ss << std::uppercase << std::setfill('0') << std::setw(hexPerByte) << std::hex <<
|
||||
static_cast<uint32_t>(hashBuf[i]);
|
||||
}
|
||||
|
||||
return { err, ss.str() };
|
||||
}
|
||||
|
||||
static int ForEachFileSegment(const string &fpath, function<void(char *, size_t)> executor)
|
||||
{
|
||||
unique_ptr<FILE, decltype(&fclose)> filp = { fopen(fpath.c_str(), "r"), fclose };
|
||||
if (!filp) {
|
||||
return errno;
|
||||
}
|
||||
|
||||
const size_t pageSize { getpagesize() };
|
||||
auto buf = make_unique<char[]>(pageSize);
|
||||
size_t actLen;
|
||||
do {
|
||||
actLen = fread(buf.get(), 1, pageSize, filp.get());
|
||||
if (actLen > 0) {
|
||||
executor(buf.get(), actLen);
|
||||
}
|
||||
} while (actLen == pageSize);
|
||||
|
||||
return ferror(filp.get()) ? errno : 0;
|
||||
}
|
||||
|
||||
tuple<int, string> HashFile::HashWithMD5(const string &fpath)
|
||||
{
|
||||
auto res = make_unique<unsigned char[]>(MD5_DIGEST_LENGTH);
|
||||
MD5_CTX ctx;
|
||||
MD5_Init(&ctx);
|
||||
auto md5Update = [ctx = &ctx](char *buf, size_t len) {
|
||||
MD5_Update(ctx, buf, len);
|
||||
};
|
||||
int err = ForEachFileSegment(fpath, md5Update);
|
||||
MD5_Final(res.get(), &ctx);
|
||||
return HashFinal(err, res, MD5_DIGEST_LENGTH);
|
||||
}
|
||||
|
||||
tuple<int, string> HashFile::HashWithSHA1(const string &fpath)
|
||||
{
|
||||
auto res = make_unique<unsigned char[]>(SHA_DIGEST_LENGTH);
|
||||
SHA_CTX ctx;
|
||||
SHA1_Init(&ctx);
|
||||
auto sha1Update = [ctx = &ctx](char *buf, size_t len) {
|
||||
SHA1_Update(ctx, buf, len);
|
||||
};
|
||||
int err = ForEachFileSegment(fpath, sha1Update);
|
||||
SHA1_Final(res.get(), &ctx);
|
||||
return HashFinal(err, res, SHA_DIGEST_LENGTH);
|
||||
}
|
||||
|
||||
tuple<int, string> HashFile::HashWithSHA256(const string &fpath)
|
||||
{
|
||||
auto res = make_unique<unsigned char[]>(SHA256_DIGEST_LENGTH);
|
||||
SHA256_CTX ctx;
|
||||
SHA256_Init(&ctx);
|
||||
auto sha256Update = [ctx = &ctx](char *buf, size_t len) {
|
||||
SHA256_Update(ctx, buf, len);
|
||||
};
|
||||
int err = ForEachFileSegment(fpath, sha256Update);
|
||||
SHA256_Final(res.get(), &ctx);
|
||||
return HashFinal(err, res, SHA256_DIGEST_LENGTH);
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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 HASH_FILE_H
|
||||
#define HASH_FILE_H
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class HashFile {
|
||||
public:
|
||||
static std::tuple<int, std::string> HashWithMD5(const std::string &fpath);
|
||||
static std::tuple<int, std::string> HashWithSHA1(const std::string &fpath);
|
||||
static std::tuple<int, std::string> HashWithSHA256(const std::string &fpath);
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* 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_NAPI_COMMON_LOG_H
|
||||
#define INTERFACES_KITS_NAPI_COMMON_LOG_H
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef FILE_SUBSYSTEM_DEBUG_LOCAL
|
||||
#include "hilog/log.h"
|
||||
#endif
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
#ifndef FILE_SUBSYSTEM_DEBUG_LOCAL
|
||||
static constexpr int FILEIO_DOMAIN_ID = 0;
|
||||
static constexpr OHOS::HiviewDFX::HiLogLabel FILEIO_LABEL = { LOG_CORE, FILEIO_DOMAIN_ID, "distributedfilejs" };
|
||||
|
||||
#ifdef HILOGD
|
||||
#undef HILOGD
|
||||
#endif
|
||||
|
||||
#ifdef HILOGF
|
||||
#undef HILOGF
|
||||
#endif
|
||||
|
||||
#ifdef HILOGE
|
||||
#undef HILOGE
|
||||
#endif
|
||||
|
||||
#ifdef HILOGW
|
||||
#undef HILOGW
|
||||
#endif
|
||||
|
||||
#ifdef HILOGI
|
||||
#undef HILOGI
|
||||
#endif
|
||||
|
||||
#define HILOGD(fmt, ...) \
|
||||
(void)OHOS::HiviewDFX::HiLog::Debug(OHOS::DistributedFS::FILEIO_LABEL, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
#define HILOGI(fmt, ...) \
|
||||
(void)OHOS::HiviewDFX::HiLog::Info(OHOS::DistributedFS::FILEIO_LABEL, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
#define HILOGW(fmt, ...) \
|
||||
(void)OHOS::HiviewDFX::HiLog::Warn(OHOS::DistributedFS::FILEIO_LABEL, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
#define HILOGE(fmt, ...) \
|
||||
(void)OHOS::HiviewDFX::HiLog::Error(OHOS::DistributedFS::FILEIO_LABEL, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
#define HILOGF(fmt, ...) \
|
||||
(void)OHOS::HiviewDFX::HiLog::Fatal(OHOS::DistributedFS::FILEIO_LABEL, "%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
|
||||
#else
|
||||
|
||||
#define PCLOG(fmt, ...) \
|
||||
do { \
|
||||
const std::vector<std::string> filter = { \
|
||||
"{public}", \
|
||||
"{private}", \
|
||||
}; \
|
||||
std::string str____(fmt); \
|
||||
for (auto &&pattern : filter) { \
|
||||
size_t pos = 0; \
|
||||
while (std::string::npos != (pos = str____.find(pattern))) { \
|
||||
str____.erase(pos, pattern.length()); \
|
||||
} \
|
||||
} \
|
||||
str____ += "\n"; \
|
||||
printf(str____.c_str(), ##__VA_ARGS__); \
|
||||
} while (0) \
|
||||
|
||||
#define HILOGD(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
#define HILOGI(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
#define HILOGW(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
#define HILOGE(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
#define HILOGF(fmt, ...) PCLOG("%{public}s: " fmt, __func__, ##__VA_ARGS__)
|
||||
|
||||
#endif
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* 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 N_ASYNC_CONTEXT_H
|
||||
#define N_ASYNC_CONTEXT_H
|
||||
|
||||
#include "../../uni_error.h"
|
||||
#include "../n_val.h"
|
||||
#include "n_ref.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
using NContextCBExec = std::function<UniError(napi_env)>;
|
||||
using NContextCBComplete = std::function<NVal(napi_env, UniError)>;
|
||||
|
||||
class NAsyncContext {
|
||||
public:
|
||||
UniError err_;
|
||||
NVal res_;
|
||||
NContextCBExec cbExec_;
|
||||
NContextCBComplete cbComplete_;
|
||||
napi_async_work awork_;
|
||||
NRef thisPtr_;
|
||||
|
||||
explicit NAsyncContext(NVal thisPtr) : err_(0), res_(NVal()), cbExec_(nullptr),
|
||||
cbComplete_(nullptr), awork_(nullptr), thisPtr_(thisPtr) {}
|
||||
virtual ~NAsyncContext() = default;
|
||||
};
|
||||
|
||||
class NAsyncContextPromise : public NAsyncContext {
|
||||
public:
|
||||
napi_deferred deferred_ = nullptr;
|
||||
explicit NAsyncContextPromise(NVal thisPtr) : NAsyncContext(thisPtr) {}
|
||||
~NAsyncContextPromise() = default;
|
||||
};
|
||||
|
||||
class NAsyncContextCallback : public NAsyncContext {
|
||||
public:
|
||||
NRef cb_;
|
||||
NAsyncContextCallback(NVal thisPtr, NVal cb) : NAsyncContext(thisPtr), cb_(cb) {}
|
||||
~NAsyncContextCallback() = default;
|
||||
};
|
||||
|
||||
class NAsyncContextLegacy : public NAsyncContext {
|
||||
public:
|
||||
NRef cbSucc_;
|
||||
NRef cbFail_;
|
||||
NRef cbFinal_;
|
||||
NAsyncContextLegacy(NVal thisPtr, NVal cbSucc, NVal cbFail, NVal cbFinal)
|
||||
: NAsyncContext(thisPtr), cbSucc_(cbSucc), cbFail_(cbFail), cbFinal_(cbFinal)
|
||||
{}
|
||||
~NAsyncContextLegacy() = default;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // N_ASYNC_CONTEXT_H
|
||||
@@ -1,111 +0,0 @@
|
||||
/*
|
||||
* 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 "n_async_work_callback.h"
|
||||
|
||||
#include <cerrno>
|
||||
|
||||
#include <functional>
|
||||
#include <new>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../../log.h"
|
||||
#include "../../uni_error.h"
|
||||
#include "js_native_api.h"
|
||||
#include "n_ref.h"
|
||||
#include "node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
using namespace std;
|
||||
|
||||
NAsyncWorkCallback::NAsyncWorkCallback(napi_env env, NVal thisPtr, NVal cb) : NAsyncWorkFactory(env)
|
||||
{
|
||||
ctx_ = new NAsyncContextCallback(thisPtr, cb);
|
||||
}
|
||||
|
||||
static void CallbackExecute(napi_env env, void *data)
|
||||
{
|
||||
auto ctx = static_cast<NAsyncContextCallback *>(data);
|
||||
if (ctx != nullptr && ctx->cbExec_ != nullptr) {
|
||||
ctx->err_ = ctx->cbExec_(env);
|
||||
}
|
||||
}
|
||||
|
||||
static void CallbackComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
napi_handle_scope scope = nullptr;
|
||||
napi_open_handle_scope(env, &scope);
|
||||
auto ctx = static_cast<NAsyncContextCallback *>(data);
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (ctx->cbComplete_ != nullptr) {
|
||||
ctx->res_ = ctx->cbComplete_(env, ctx->err_);
|
||||
ctx->cbComplete_ = nullptr;
|
||||
}
|
||||
|
||||
vector<napi_value> argv;
|
||||
if (!ctx->res_.TypeIsError(true)) {
|
||||
argv = { UniError(ERRNO_NOERR).GetNapiErr(env), ctx->res_.val_ };
|
||||
} else {
|
||||
argv = { ctx->res_.val_ };
|
||||
}
|
||||
|
||||
napi_value global = nullptr;
|
||||
napi_value callback = ctx->cb_.Deref(env).val_;
|
||||
napi_value tmp = nullptr;
|
||||
napi_get_global(env, &global);
|
||||
napi_status stat = napi_call_function(env, global, callback, argv.size(), argv.data(), &tmp);
|
||||
if (stat != napi_ok) {
|
||||
HILOGE("Failed to call function for %{public}d", stat);
|
||||
}
|
||||
napi_close_handle_scope(env, scope);
|
||||
napi_delete_async_work(env, ctx->awork_);
|
||||
delete ctx;
|
||||
}
|
||||
|
||||
NVal NAsyncWorkCallback::Schedule(string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete)
|
||||
{
|
||||
if (!ctx_->cb_ || !ctx_->cb_.Deref(env_).TypeIs(napi_function)) {
|
||||
UniError(EINVAL).ThrowErr(env_, "The callback shall be a function");
|
||||
return NVal();
|
||||
}
|
||||
|
||||
ctx_->cbExec_ = move(cbExec);
|
||||
ctx_->cbComplete_ = move(cbComplete);
|
||||
|
||||
napi_value resource = NVal::CreateUTF8String(env_, procedureName).val_;
|
||||
|
||||
napi_status status =
|
||||
napi_create_async_work(env_, nullptr, resource, CallbackExecute, CallbackComplete, ctx_, &ctx_->awork_);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("INNER BUG. Failed to create async work for %{public}d", status);
|
||||
return NVal();
|
||||
}
|
||||
|
||||
status = napi_queue_async_work(env_, ctx_->awork_);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("INNER BUG. Failed to queue async work for %{public}d", status);
|
||||
return NVal();
|
||||
}
|
||||
|
||||
ctx_ = nullptr; // The ownership of ctx_ has been transferred
|
||||
return NVal::CreateUndefined(env_);
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* 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 N_ASYNC_WORK_CALLBACK_H
|
||||
#define N_ASYNC_WORK_CALLBACK_H
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
#include "../n_val.h"
|
||||
#include "js_native_api_types.h"
|
||||
#include "n_async_context.h"
|
||||
#include "n_async_work_factory.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class NAsyncWorkCallback : public NAsyncWorkFactory {
|
||||
public:
|
||||
NAsyncWorkCallback(napi_env env, NVal thisPtr, NVal cb);
|
||||
~NAsyncWorkCallback() = default;
|
||||
|
||||
NVal Schedule(std::string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete) final;
|
||||
|
||||
private:
|
||||
NAsyncContextCallback *ctx_ = nullptr;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // N_ASYNC_WORK_CALLBACK_H
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* 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 N_ASYNC_WORK_FACTORY_H
|
||||
#define N_ASYNC_WORK_FACTORY_H
|
||||
|
||||
#include "n_async_context.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class NAsyncWorkFactory {
|
||||
public:
|
||||
explicit NAsyncWorkFactory(napi_env env) : env_(env) {}
|
||||
virtual ~NAsyncWorkFactory() = default;
|
||||
virtual NVal Schedule(std::string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete) = 0;
|
||||
|
||||
napi_env env_ = nullptr;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // N_ASYNC_WORK_FACTORY_H
|
||||
@@ -1,100 +0,0 @@
|
||||
/*
|
||||
* 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 "n_async_work_promise.h"
|
||||
|
||||
#include <functional>
|
||||
#include <new>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../../log.h"
|
||||
#include "../../uni_error.h"
|
||||
#include "js_native_api.h"
|
||||
#include "node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
using namespace std;
|
||||
|
||||
NAsyncWorkPromise::NAsyncWorkPromise(napi_env env, NVal thisPtr) : NAsyncWorkFactory(env)
|
||||
{
|
||||
ctx_ = new NAsyncContextPromise(thisPtr);
|
||||
}
|
||||
|
||||
static void PromiseOnExec(napi_env env, void *data)
|
||||
{
|
||||
auto ctx = static_cast<NAsyncContextPromise *>(data);
|
||||
if (ctx != nullptr && ctx->cbExec_ != nullptr) {
|
||||
ctx->err_ = ctx->cbExec_(env);
|
||||
}
|
||||
}
|
||||
|
||||
static void PromiseOnComplete(napi_env env, napi_status status, void *data)
|
||||
{
|
||||
auto ctx = static_cast<NAsyncContextPromise *>(data);
|
||||
if (ctx == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (ctx->cbComplete_ != nullptr) {
|
||||
ctx->res_ = ctx->cbComplete_(env, ctx->err_);
|
||||
}
|
||||
if (!ctx->res_.TypeIsError(true)) {
|
||||
napi_status status = napi_resolve_deferred(env, ctx->deferred_, ctx->res_.val_);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("Internal BUG, cannot resolve promise for %{public}d", status);
|
||||
}
|
||||
} else {
|
||||
napi_status status = napi_reject_deferred(env, ctx->deferred_, ctx->res_.val_);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("Internal BUG, cannot reject promise for %{public}d", status);
|
||||
}
|
||||
}
|
||||
ctx->deferred_ = nullptr;
|
||||
napi_delete_async_work(env, ctx->awork_);
|
||||
delete ctx;
|
||||
}
|
||||
|
||||
NVal NAsyncWorkPromise::Schedule(string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete)
|
||||
{
|
||||
ctx_->cbExec_ = move(cbExec);
|
||||
ctx_->cbComplete_ = move(cbComplete);
|
||||
|
||||
napi_status status;
|
||||
napi_value result = nullptr;
|
||||
status = napi_create_promise(env_, &ctx_->deferred_, &result);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("INNER BUG. Cannot create promise for %{public}d", status);
|
||||
return NVal();
|
||||
}
|
||||
|
||||
napi_value resource = NVal::CreateUTF8String(env_, procedureName).val_;
|
||||
status = napi_create_async_work(env_, nullptr, resource, PromiseOnExec, PromiseOnComplete, ctx_, &ctx_->awork_);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("INNER BUG. Failed to create async work for %{public}d", status);
|
||||
return NVal();
|
||||
}
|
||||
|
||||
status = napi_queue_async_work(env_, ctx_->awork_);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("INNER BUG. Failed to queue async work for %{public}d", status);
|
||||
return NVal();
|
||||
}
|
||||
|
||||
ctx_ = nullptr; // The ownership of ctx_ has been transferred
|
||||
return { env_, result };
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* 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 N_ASYNC_WORK_PROMISE_H
|
||||
#define N_ASYNC_WORK_PROMISE_H
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
#include "../n_val.h"
|
||||
#include "js_native_api_types.h"
|
||||
#include "n_async_context.h"
|
||||
#include "n_async_work_factory.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class NAsyncWorkPromise : public NAsyncWorkFactory {
|
||||
public:
|
||||
NAsyncWorkPromise(napi_env env, NVal thisPtr);
|
||||
~NAsyncWorkPromise() = default;
|
||||
|
||||
NVal Schedule(std::string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete) final;
|
||||
|
||||
private:
|
||||
NAsyncContextPromise *ctx_;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // N_ASYNC_WORK_PROMISE_H
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* 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 "n_ref.h"
|
||||
|
||||
#include "../n_val.h"
|
||||
#include "js_native_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
NRef::NRef() {}
|
||||
|
||||
NRef::NRef(NVal val)
|
||||
{
|
||||
if (val) {
|
||||
env_ = val.env_;
|
||||
napi_create_reference(val.env_, val.val_, 1, &ref_);
|
||||
}
|
||||
}
|
||||
|
||||
NRef::~NRef()
|
||||
{
|
||||
if (ref_) {
|
||||
napi_delete_reference(env_, ref_);
|
||||
}
|
||||
}
|
||||
|
||||
NRef::operator bool() const
|
||||
{
|
||||
return ref_ != nullptr;
|
||||
}
|
||||
|
||||
NVal NRef::Deref(napi_env env)
|
||||
{
|
||||
if (!ref_) {
|
||||
return NVal();
|
||||
}
|
||||
|
||||
napi_value val = nullptr;
|
||||
napi_get_reference_value(env, ref_, &val);
|
||||
return { env, val };
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* 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 N_REF_H
|
||||
#define N_REF_H
|
||||
|
||||
#include "../n_val.h"
|
||||
#include "js_native_api_types.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class NRef {
|
||||
public:
|
||||
NRef();
|
||||
explicit NRef(NVal val);
|
||||
~NRef();
|
||||
|
||||
explicit operator bool() const;
|
||||
NVal Deref(napi_env env);
|
||||
|
||||
private:
|
||||
napi_env env_ = nullptr;
|
||||
napi_ref ref_ = nullptr;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // N_REF_H
|
||||
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
* 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 "n_class.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "../log.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
using namespace std;
|
||||
NClass &NClass::GetInstance()
|
||||
{
|
||||
static thread_local NClass nClass;
|
||||
return nClass;
|
||||
}
|
||||
|
||||
tuple<bool, napi_value> NClass::DefineClass(napi_env env,
|
||||
string className,
|
||||
napi_callback constructor,
|
||||
vector<napi_property_descriptor> &&properties)
|
||||
{
|
||||
napi_value classVal = nullptr;
|
||||
napi_status stat = napi_define_class(env,
|
||||
className.c_str(),
|
||||
className.length(),
|
||||
constructor,
|
||||
nullptr,
|
||||
properties.size(),
|
||||
properties.data(),
|
||||
&classVal);
|
||||
if (stat != napi_ok) {
|
||||
HILOGE("INNER BUG. Cannot define class %{public}s because of %{public}d", className.c_str(), stat);
|
||||
}
|
||||
return { stat == napi_ok, classVal };
|
||||
}
|
||||
|
||||
bool NClass::SaveClass(napi_env env, string className, napi_value exClass)
|
||||
{
|
||||
NClass &nClass = NClass::GetInstance();
|
||||
lock_guard(nClass.exClassMapLock);
|
||||
|
||||
if (nClass.exClassMap.find(className) != nClass.exClassMap.end()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
napi_ref constructor;
|
||||
napi_status res = napi_create_reference(env, exClass, 1, &constructor);
|
||||
if (res == napi_ok) {
|
||||
nClass.exClassMap.insert({ className, constructor });
|
||||
HILOGI("Class %{public}s has been saved", className.c_str());
|
||||
} else {
|
||||
HILOGE("INNER BUG. Cannot ref class constructor %{public}s because of %{public}d", className.c_str(), res);
|
||||
}
|
||||
return res == napi_ok;
|
||||
}
|
||||
|
||||
napi_value NClass::InstantiateClass(napi_env env, const string& className, const vector<napi_value>& args)
|
||||
{
|
||||
NClass &nClass = NClass::GetInstance();
|
||||
lock_guard(nClass.exClassMapLock);
|
||||
|
||||
auto it = nClass.exClassMap.find(className);
|
||||
if (it == nClass.exClassMap.end()) {
|
||||
HILOGE("Class %{public}s hasn't been saved yet", className.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value cons = nullptr;
|
||||
napi_status status = napi_get_reference_value(env, it->second, &cons);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("INNER BUG. Cannot deref class %{public}s because of %{public}d", className.c_str(), status);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value instance = nullptr;
|
||||
status = napi_new_instance(env, cons, args.size(), args.data(), &instance);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("INNER BUG. Cannot instantiate the class %{public}s because of %{public}d", className.c_str(), status);
|
||||
return nullptr;
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* 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_NAPI_COMMON_NAPI_N_CLASS_H
|
||||
#define INTERFACES_KITS_NAPI_COMMON_NAPI_N_CLASS_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "../log.h"
|
||||
#include "uni_header.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class NClass final {
|
||||
public:
|
||||
NClass(const NClass &) = delete;
|
||||
NClass &operator = (const NClass &) = delete;
|
||||
static NClass &GetInstance();
|
||||
|
||||
static std::tuple<bool, napi_value> DefineClass(napi_env env,
|
||||
std::string className,
|
||||
napi_callback constructor,
|
||||
std::vector<napi_property_descriptor> &&properties);
|
||||
static bool SaveClass(napi_env env, std::string className, napi_value exClass);
|
||||
static napi_value InstantiateClass(napi_env env, const std::string& className, const std::vector<napi_value>& args);
|
||||
|
||||
template <class T> static T *GetEntityOf(napi_env env, napi_value objStat)
|
||||
{
|
||||
if (!env || !objStat) {
|
||||
HILOGE("Empty input: env %d, obj %d", env == nullptr, objStat == nullptr);
|
||||
return nullptr;
|
||||
}
|
||||
T *t = nullptr;
|
||||
napi_status status = napi_unwrap(env, objStat, (void **)&t);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("Cannot umwarp for pointer: %d", status);
|
||||
return nullptr;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
template <class T> static bool SetEntityFor(napi_env env, napi_value obj, std::unique_ptr<T> entity)
|
||||
{
|
||||
napi_status status = napi_wrap(
|
||||
env,
|
||||
obj,
|
||||
entity.get(),
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
auto entity = static_cast<T *>(data);
|
||||
delete entity;
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
entity.release();
|
||||
return status == napi_ok;
|
||||
}
|
||||
|
||||
private:
|
||||
NClass() = default;
|
||||
~NClass() = default;
|
||||
std::map<std::string, napi_ref> exClassMap;
|
||||
std::mutex exClassMapLock;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
* 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_NAPI_COMMON_NAPI_N_EXPORTER_H
|
||||
#define INTERFACES_KITS_NAPI_COMMON_NAPI_N_EXPORTER_H
|
||||
|
||||
#include "n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class NExporter {
|
||||
public:
|
||||
NExporter(napi_env env, napi_value exports) : exports_(env, exports) {};
|
||||
virtual ~NExporter() = default;
|
||||
|
||||
virtual bool Export() = 0;
|
||||
virtual std::string GetClassName() = 0;
|
||||
|
||||
protected:
|
||||
NVal exports_;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,109 +0,0 @@
|
||||
/*
|
||||
* 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 "n_func_arg.h"
|
||||
|
||||
#include "js_native_api.h"
|
||||
#include "../log.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
using namespace std;
|
||||
|
||||
NFuncArg::NFuncArg(napi_env env, napi_callback_info info) : env_(env), info_(info) {}
|
||||
|
||||
NFuncArg::~NFuncArg() {}
|
||||
|
||||
void NFuncArg::SetArgc(size_t argc)
|
||||
{
|
||||
argc_ = argc;
|
||||
}
|
||||
|
||||
void NFuncArg::SetThisVar(napi_value thisVar)
|
||||
{
|
||||
thisVar_ = thisVar;
|
||||
}
|
||||
|
||||
size_t NFuncArg::GetArgc(void) const
|
||||
{
|
||||
return argc_;
|
||||
}
|
||||
|
||||
napi_value NFuncArg::GetThisVar(void) const
|
||||
{
|
||||
return thisVar_;
|
||||
}
|
||||
|
||||
napi_value NFuncArg::GetArg(size_t argPos) const
|
||||
{
|
||||
return (argPos < GetArgc()) ? argv_[argPos] : nullptr;
|
||||
}
|
||||
|
||||
napi_value NFuncArg::operator[](size_t argPos) const
|
||||
{
|
||||
return GetArg(argPos);
|
||||
}
|
||||
|
||||
bool NFuncArg::InitArgs(std::function<bool()> argcChecker)
|
||||
{
|
||||
SetArgc(0);
|
||||
argv_.reset();
|
||||
|
||||
size_t argc;
|
||||
napi_value thisVar;
|
||||
napi_status status = napi_get_cb_info(env_, info_, &argc, nullptr, &thisVar, nullptr);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("Cannot get num of func args for %{public}d", status);
|
||||
return false;
|
||||
}
|
||||
if (argc) {
|
||||
argv_ = make_unique<napi_value[]>(argc);
|
||||
status = napi_get_cb_info(env_, info_, &argc, argv_.get(), &thisVar, nullptr);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("Cannot get func args for %{public}d", status);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
SetArgc(argc);
|
||||
SetThisVar(thisVar);
|
||||
|
||||
return argcChecker();
|
||||
}
|
||||
|
||||
bool NFuncArg::InitArgs(size_t argc)
|
||||
{
|
||||
return InitArgs([argc, this]() {
|
||||
size_t realArgc = GetArgc();
|
||||
if (argc != realArgc) {
|
||||
HILOGE("Num of args recved eq %zu while expecting %{public}zu", realArgc, argc);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool NFuncArg::InitArgs(size_t minArgc, size_t maxArgc)
|
||||
{
|
||||
return InitArgs([minArgc, maxArgc, this]() {
|
||||
size_t realArgc = GetArgc();
|
||||
if (minArgc > realArgc || maxArgc < realArgc) {
|
||||
HILOGE("Num of args recved eq %zu while expecting %{public}zu ~ %{public}zu", realArgc, minArgc, maxArgc);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* 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_NAPI_COMMON_NAPI_N_FUNC_ARG_H
|
||||
#define INTERFACES_KITS_NAPI_COMMON_NAPI_N_FUNC_ARG_H
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "node_api.h"
|
||||
#include "js_native_api_types.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
enum NARG_CNT {
|
||||
ZERO = 0,
|
||||
ONE = 1,
|
||||
TWO = 2,
|
||||
THREE = 3,
|
||||
FOUR = 4,
|
||||
};
|
||||
|
||||
enum NARG_POS {
|
||||
FIRST = 0,
|
||||
SECOND = 1,
|
||||
THIRD = 2,
|
||||
FOURTH = 3,
|
||||
};
|
||||
|
||||
class NFuncArg final {
|
||||
public:
|
||||
NFuncArg(napi_env env, napi_callback_info info);
|
||||
virtual ~NFuncArg();
|
||||
|
||||
bool InitArgs(size_t argc);
|
||||
bool InitArgs(size_t minArgc, size_t maxArgc);
|
||||
|
||||
size_t GetArgc() const;
|
||||
napi_value GetThisVar() const;
|
||||
|
||||
napi_value operator[](size_t idx) const;
|
||||
napi_value GetArg(size_t argPos) const;
|
||||
|
||||
private:
|
||||
napi_env env_ = nullptr;
|
||||
napi_callback_info info_ = nullptr;
|
||||
|
||||
size_t argc_ = 0;
|
||||
std::unique_ptr<napi_value[]> argv_ = { nullptr };
|
||||
napi_value thisVar_ = nullptr;
|
||||
|
||||
bool InitArgs(std::function<bool()> argcChecker);
|
||||
|
||||
void SetArgc(size_t argc);
|
||||
void SetThisVar(napi_value thisVar);
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,348 +0,0 @@
|
||||
/*
|
||||
* 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 "n_val.h"
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../log.h"
|
||||
#include "js_native_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
using namespace std;
|
||||
|
||||
NVal::NVal(napi_env nEnv, napi_value nVal = nullptr) : env_(nEnv), val_(nVal) {}
|
||||
|
||||
NVal::operator bool() const
|
||||
{
|
||||
return env_ && val_;
|
||||
}
|
||||
|
||||
bool NVal::TypeIs(napi_valuetype expType) const
|
||||
{
|
||||
if (!*this) {
|
||||
return false;
|
||||
}
|
||||
napi_valuetype valueType;
|
||||
napi_typeof(env_, val_, &valueType);
|
||||
|
||||
if (expType != valueType) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NVal::TypeIsError(bool checkErrno) const
|
||||
{
|
||||
if (!*this) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool res = false;
|
||||
napi_is_error(env_, val_, &res);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
tuple<bool, unique_ptr<char[]>, size_t> NVal::ToUTF8String() const
|
||||
{
|
||||
size_t strLen = 0;
|
||||
napi_status status = napi_get_value_string_utf8(env_, val_, nullptr, -1, &strLen);
|
||||
if (status != napi_ok) {
|
||||
return { false, nullptr, 0 };
|
||||
}
|
||||
|
||||
size_t bufLen = strLen + 1;
|
||||
unique_ptr<char[]> str = make_unique<char[]>(bufLen);
|
||||
status = napi_get_value_string_utf8(env_, val_, str.get(), bufLen, &strLen);
|
||||
return make_tuple(status == napi_ok, move(str), strLen);
|
||||
}
|
||||
|
||||
tuple<bool, unique_ptr<char[]>, size_t> NVal::ToUTF16String() const
|
||||
{
|
||||
#ifdef FILE_SUBSYSTEM_DEBUG_LOCAL
|
||||
size_t strLen = 0;
|
||||
napi_status status = napi_get_value_string_utf16(env_, val_, nullptr, -1, &strLen);
|
||||
if (status != napi_ok) {
|
||||
return { false, nullptr, 0 };
|
||||
}
|
||||
|
||||
auto str = make_unique<char16_t[]>(++strLen);
|
||||
status = napi_get_value_string_utf16(env_, val_, str.get(), strLen, nullptr);
|
||||
if (status != napi_ok) {
|
||||
return { false, nullptr, 0 };
|
||||
}
|
||||
|
||||
strLen = reinterpret_cast<char *>(str.get() + strLen) - reinterpret_cast<char *>(str.get());
|
||||
auto strRet = unique_ptr<char[]>(reinterpret_cast<char *>(str.release()));
|
||||
return { true, move(strRet), strLen };
|
||||
#else
|
||||
// Note that quickjs doesn't support utf16
|
||||
return ToUTF8String();
|
||||
#endif
|
||||
}
|
||||
|
||||
tuple<bool, void *> NVal::ToPointer() const
|
||||
{
|
||||
void *res = nullptr;
|
||||
napi_status status = napi_get_value_external(env_, val_, &res);
|
||||
return make_tuple(status == napi_ok, res);
|
||||
}
|
||||
|
||||
tuple<bool, bool> NVal::ToBool() const
|
||||
{
|
||||
bool flag = false;
|
||||
napi_status status = napi_get_value_bool(env_, val_, &flag);
|
||||
return make_tuple(status == napi_ok, flag);
|
||||
}
|
||||
|
||||
tuple<bool, int32_t> NVal::ToInt32() const
|
||||
{
|
||||
int32_t res = 0;
|
||||
napi_status status = napi_get_value_int32(env_, val_, &res);
|
||||
return make_tuple(status == napi_ok, res);
|
||||
}
|
||||
|
||||
tuple<bool, int64_t> NVal::ToInt64() const
|
||||
{
|
||||
int64_t res = 0;
|
||||
napi_status status = napi_get_value_int64(env_, val_, &res);
|
||||
return make_tuple(status == napi_ok, res);
|
||||
}
|
||||
|
||||
tuple<bool, double> NVal::ToDouble() const
|
||||
{
|
||||
double res = 0;
|
||||
napi_status status = napi_get_value_double(env_, val_, &res);
|
||||
return make_tuple(status == napi_ok, res);
|
||||
}
|
||||
|
||||
tuple<bool, uint64_t, bool> NVal::ToUint64() const
|
||||
{
|
||||
uint64_t res = 0;
|
||||
bool lossless = false;
|
||||
napi_status status = napi_get_value_bigint_uint64(env_, val_, &res, &lossless);
|
||||
return make_tuple(status == napi_ok, res, lossless);
|
||||
}
|
||||
|
||||
tuple<bool, vector<string>, uint32_t> NVal::ToStringArray()
|
||||
{
|
||||
napi_status status;
|
||||
uint32_t size;
|
||||
status = napi_get_array_length(env_, val_, &size);
|
||||
vector<string> stringArray;
|
||||
napi_value result;
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
status = napi_get_element(env_, val_, i, &result);
|
||||
auto [succ, str, ignore] = NVal(env_, result).ToUTF8String();
|
||||
stringArray.push_back(string(str.get()));
|
||||
}
|
||||
return make_tuple(status == napi_ok, stringArray, size);
|
||||
}
|
||||
|
||||
tuple<bool, void *, size_t> NVal::ToArraybuffer() const
|
||||
{
|
||||
void *buf = nullptr;
|
||||
size_t bufLen = 0;
|
||||
bool status = napi_get_arraybuffer_info(env_, val_, &buf, &bufLen);
|
||||
return make_tuple(status == napi_ok, buf, bufLen);
|
||||
}
|
||||
|
||||
tuple<bool, void *, size_t> NVal::ToTypedArray() const
|
||||
{
|
||||
napi_typedarray_type type;
|
||||
napi_value in_array_buffer = nullptr;
|
||||
size_t byte_offset;
|
||||
size_t length;
|
||||
void *data = nullptr;
|
||||
napi_status status =
|
||||
napi_get_typedarray_info(env_, val_, &type, &length, (void **)&data, &in_array_buffer, &byte_offset);
|
||||
return make_tuple(status == napi_ok, data, length);
|
||||
}
|
||||
|
||||
bool NVal::HasProp(string propName) const
|
||||
{
|
||||
bool res = false;
|
||||
|
||||
if (!env_ || !val_ || !TypeIs(napi_object))
|
||||
return false;
|
||||
napi_status status = napi_has_named_property(env_, val_, propName.c_str(), &res);
|
||||
return (status == napi_ok) && res;
|
||||
}
|
||||
|
||||
NVal NVal::GetProp(string propName) const
|
||||
{
|
||||
if (!HasProp(propName)) {
|
||||
return { env_, nullptr };
|
||||
}
|
||||
napi_value prop = nullptr;
|
||||
napi_status status = napi_get_named_property(env_, val_, propName.c_str(), &prop);
|
||||
if (status != napi_ok) {
|
||||
return { env_, nullptr };
|
||||
}
|
||||
return NVal(env_, prop);
|
||||
}
|
||||
|
||||
bool NVal::AddProp(vector<napi_property_descriptor> &&propVec) const
|
||||
{
|
||||
if (!TypeIs(napi_valuetype::napi_object)) {
|
||||
HILOGE("INNER BUG. Prop should only be added to objects");
|
||||
return false;
|
||||
}
|
||||
napi_status status = napi_define_properties(env_, val_, propVec.size(), propVec.data());
|
||||
if (status != napi_ok) {
|
||||
HILOGE("INNER BUG. Cannot define properties because of %{public}d", status);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NVal::AddProp(string propName, napi_value val) const
|
||||
{
|
||||
if (!TypeIs(napi_valuetype::napi_object) || HasProp(propName)) {
|
||||
HILOGE("INNER BUG. Prop should only be added to objects");
|
||||
return false;
|
||||
}
|
||||
|
||||
napi_status status = napi_set_named_property(env_, val_, propName.c_str(), val);
|
||||
if (status != napi_ok) {
|
||||
HILOGE("INNER BUG. Cannot set named property because of %{public}d", status);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
NVal NVal::CreateUndefined(napi_env env)
|
||||
{
|
||||
napi_value res = nullptr;
|
||||
napi_get_undefined(env, &res);
|
||||
return { env, res };
|
||||
}
|
||||
|
||||
NVal NVal::CreateInt64(napi_env env, int64_t val)
|
||||
{
|
||||
napi_value res = nullptr;
|
||||
napi_create_int64(env, val, &res);
|
||||
return { env, res };
|
||||
}
|
||||
|
||||
NVal NVal::CreateInt32(napi_env env, int32_t val)
|
||||
{
|
||||
napi_value res = nullptr;
|
||||
napi_create_int32(env, val, &res);
|
||||
return { env, res };
|
||||
}
|
||||
|
||||
NVal NVal::CreateObject(napi_env env)
|
||||
{
|
||||
napi_value res = nullptr;
|
||||
napi_create_object(env, &res);
|
||||
return { env, res };
|
||||
}
|
||||
|
||||
NVal NVal::CreateBool(napi_env env, bool val)
|
||||
{
|
||||
napi_value res = nullptr;
|
||||
napi_get_boolean(env, val, &res);
|
||||
return { env, res };
|
||||
}
|
||||
|
||||
NVal NVal::CreateUTF8String(napi_env env, std::string str)
|
||||
{
|
||||
napi_value res = nullptr;
|
||||
napi_create_string_utf8(env, str.c_str(), str.length(), &res);
|
||||
return { env, res };
|
||||
}
|
||||
|
||||
NVal NVal::CreateUTF8String(napi_env env, const char* str, ssize_t len)
|
||||
{
|
||||
napi_value res = nullptr;
|
||||
napi_create_string_utf8(env, str, len, &res);
|
||||
return { env, res };
|
||||
}
|
||||
|
||||
NVal NVal::CreateUint8Array(napi_env env, void *buf, size_t bufLen)
|
||||
{
|
||||
napi_value output_buffer = nullptr;
|
||||
napi_create_external_arraybuffer(
|
||||
env,
|
||||
buf,
|
||||
bufLen,
|
||||
[](napi_env env, void *finalize_data, void *finalize_hint) { free(finalize_data); },
|
||||
NULL,
|
||||
&output_buffer);
|
||||
napi_value output_array = nullptr;
|
||||
napi_create_typedarray(env, napi_uint8_array, bufLen, output_buffer, 0, &output_array);
|
||||
return { env, output_array };
|
||||
}
|
||||
|
||||
NVal NVal::CreateArrayString(napi_env env, vector<string> strs)
|
||||
{
|
||||
napi_value res = nullptr;
|
||||
napi_create_array(env, &res);
|
||||
for (size_t i = 0; i < strs.size(); i++) {
|
||||
napi_value filename;
|
||||
napi_create_string_utf8(env, strs[i].c_str(), strs[i].length(), &filename);
|
||||
napi_set_element(env, res, i, filename);
|
||||
}
|
||||
return {env, res};
|
||||
}
|
||||
|
||||
tuple<NVal, void *> NVal::CreateArrayBuffer(napi_env env, size_t len)
|
||||
{
|
||||
napi_value val;
|
||||
void *buf = nullptr;
|
||||
napi_create_arraybuffer(env, len, &buf, &val);
|
||||
return { { env, val }, { buf } };
|
||||
}
|
||||
|
||||
napi_property_descriptor NVal::DeclareNapiProperty(const char *name, napi_value val)
|
||||
{
|
||||
return { (name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr };
|
||||
}
|
||||
|
||||
napi_property_descriptor NVal::DeclareNapiStaticProperty(const char *name, napi_value val)
|
||||
{
|
||||
return { (name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr };
|
||||
}
|
||||
|
||||
napi_property_descriptor NVal::DeclareNapiFunction(const char *name, napi_callback func)
|
||||
{
|
||||
return { (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr };
|
||||
}
|
||||
|
||||
napi_property_descriptor NVal::DeclareNapiStaticFunction(const char *name, napi_callback func)
|
||||
{
|
||||
return { (name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr };
|
||||
}
|
||||
|
||||
napi_property_descriptor NVal::DeclareNapiGetter(const char *name, napi_callback getter)
|
||||
{
|
||||
return { (name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr };
|
||||
}
|
||||
|
||||
napi_property_descriptor NVal::DeclareNapiSetter(const char *name, napi_callback setter)
|
||||
{
|
||||
return { (name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr };
|
||||
}
|
||||
|
||||
napi_property_descriptor NVal::DeclareNapiGetterSetter(const char *name, napi_callback getter, napi_callback setter)
|
||||
{
|
||||
return { (name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr };
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef N_VAL_H
|
||||
#define N_VAL_H
|
||||
|
||||
#include <iosfwd>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include "node_api.h"
|
||||
#include "js_native_api_types.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
class NVal final {
|
||||
public:
|
||||
NVal() = default;
|
||||
NVal(napi_env nEnv, napi_value nVal);
|
||||
NVal(const NVal &) = default;
|
||||
NVal &operator = (const NVal &) = default;
|
||||
virtual ~NVal() = default;
|
||||
|
||||
// NOTE! env_ and val_ is LIKELY to be null
|
||||
napi_env env_ = nullptr;
|
||||
napi_value val_ = nullptr;
|
||||
|
||||
explicit operator bool() const;
|
||||
bool TypeIs(napi_valuetype expType) const;
|
||||
bool TypeIsError(bool checkErrno = false) const;
|
||||
|
||||
/* SHOULD ONLY BE USED FOR EXPECTED TYPE */
|
||||
std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF8String() const;
|
||||
std::tuple<bool, std::unique_ptr<char[]>, size_t> ToUTF16String() const;
|
||||
std::tuple<bool, void *> ToPointer() const;
|
||||
std::tuple<bool, bool> ToBool() const;
|
||||
std::tuple<bool, int32_t> ToInt32() const;
|
||||
std::tuple<bool, int64_t> ToInt64() const;
|
||||
std::tuple<bool, void *, size_t> ToArraybuffer() const;
|
||||
std::tuple<bool, void *, size_t> ToTypedArray() const;
|
||||
std::tuple<bool, std::vector<std::string>, uint32_t> ToStringArray();
|
||||
std::tuple<bool, uint64_t, bool> ToUint64() const;
|
||||
std::tuple<bool, double> ToDouble() const;
|
||||
|
||||
/* Static helpers to create js objects */
|
||||
static NVal CreateUndefined(napi_env env);
|
||||
static NVal CreateInt64(napi_env env, int64_t val);
|
||||
static NVal CreateInt32(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);
|
||||
static NVal CreateUTF8String(napi_env env, const char* str, ssize_t len);
|
||||
static NVal CreateUint8Array(napi_env env, void *buf, size_t bufLen);
|
||||
static NVal CreateArrayString(napi_env env, std::vector<std::string> strs);
|
||||
static std::tuple<NVal, void *> CreateArrayBuffer(napi_env env, size_t len);
|
||||
/* SHOULD ONLY BE USED FOR OBJECT */
|
||||
bool HasProp(std::string propName) const;
|
||||
NVal GetProp(std::string propName) const;
|
||||
bool AddProp(std::vector<napi_property_descriptor> &&propVec) const;
|
||||
bool AddProp(std::string propName, napi_value nVal) const;
|
||||
|
||||
/* Static helpers to create prop of js objects */
|
||||
static napi_property_descriptor DeclareNapiProperty(const char *name, napi_value val);
|
||||
static napi_property_descriptor DeclareNapiStaticProperty(const char *name, napi_value val);
|
||||
static napi_property_descriptor DeclareNapiFunction(const char *name, napi_callback func);
|
||||
static napi_property_descriptor DeclareNapiStaticFunction(const char *name, napi_callback func);
|
||||
static napi_property_descriptor DeclareNapiGetter(const char *name, napi_callback getter);
|
||||
static napi_property_descriptor DeclareNapiSetter(const char *name, napi_callback setter);
|
||||
static inline napi_property_descriptor DeclareNapiGetterSetter(const char *name,
|
||||
napi_callback getter,
|
||||
napi_callback setter);
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // N_VAL_H
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* 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 N_UNI_HEADER_H
|
||||
#define N_UNI_HEADER_H
|
||||
|
||||
#ifdef FILE_SUBSYSTEM_DEBUG_LOCAL
|
||||
#include <node_api.h>
|
||||
#else
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,123 +0,0 @@
|
||||
/*
|
||||
* 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 "uni_error.h"
|
||||
|
||||
#include <cerrno>
|
||||
#include <string>
|
||||
|
||||
#include "js_native_api.h"
|
||||
#include "log.h"
|
||||
#include "napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
using namespace std;
|
||||
|
||||
UniError::UniError() {}
|
||||
|
||||
UniError::UniError(ELegacy eLegacy) : errno_(eLegacy), codingSystem_(ERR_CODE_SYSTEM_LEGACY) {}
|
||||
|
||||
UniError::UniError(int ePosix) : errno_(ePosix), codingSystem_(ERR_CODE_SYSTEM_POSIX) {}
|
||||
|
||||
UniError::operator bool() const
|
||||
{
|
||||
return errno_ != ERRNO_NOERR;
|
||||
}
|
||||
|
||||
int UniError::GetErrno(ErrCodeSystem cs)
|
||||
{
|
||||
if (errno_ == ERRNO_NOERR) {
|
||||
return ERRNO_NOERR;
|
||||
}
|
||||
if (cs == codingSystem_) {
|
||||
return errno_;
|
||||
}
|
||||
|
||||
if (cs == ERR_CODE_SYSTEM_POSIX) {
|
||||
// Note that we should support more codes here
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
// Note that this shall be done properly
|
||||
return ELEGACY_INVAL;
|
||||
}
|
||||
|
||||
void UniError::SetErrno(ELegacy eLegacy)
|
||||
{
|
||||
errno_ = eLegacy;
|
||||
codingSystem_ = ERR_CODE_SYSTEM_LEGACY;
|
||||
}
|
||||
|
||||
void UniError::SetErrno(int ePosix)
|
||||
{
|
||||
errno_ = ePosix;
|
||||
codingSystem_ = ERR_CODE_SYSTEM_POSIX;
|
||||
}
|
||||
|
||||
std::string UniError::GetDefaultErrstr()
|
||||
{
|
||||
if (codingSystem_ != ERR_CODE_SYSTEM_POSIX && codingSystem_ != ERR_CODE_SYSTEM_LEGACY) {
|
||||
return "BUG: Curious coding system";
|
||||
}
|
||||
return strerror(GetErrno(ERR_CODE_SYSTEM_POSIX));
|
||||
}
|
||||
|
||||
napi_value UniError::GetNapiErr(napi_env env)
|
||||
{
|
||||
return GetNapiErr(env, GetDefaultErrstr());
|
||||
}
|
||||
|
||||
napi_value UniError::GetNapiErr(napi_env env, string errMsg)
|
||||
{
|
||||
int errCode = GetErrno(codingSystem_);
|
||||
if (errCode == ERRNO_NOERR) {
|
||||
return nullptr;
|
||||
}
|
||||
napi_value code = NVal::CreateUTF8String(env, to_string(errCode)).val_;
|
||||
napi_value msg = NVal::CreateUTF8String(env, errMsg).val_;
|
||||
|
||||
napi_value res = nullptr;
|
||||
napi_status createRes = napi_create_error(env, code, msg, &res);
|
||||
if (createRes) {
|
||||
HILOGE("Failed to create an exception, msg = %{public}s", errMsg.c_str());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void UniError::ThrowErr(napi_env env)
|
||||
{
|
||||
string msg = GetDefaultErrstr();
|
||||
napi_value tmp = nullptr;
|
||||
napi_get_and_clear_last_exception(env, &tmp);
|
||||
// Note that ace engine cannot throw errors created by napi_create_error so far
|
||||
napi_status throwStatus = napi_throw_error(env, nullptr, msg.c_str());
|
||||
if (throwStatus != napi_ok) {
|
||||
HILOGE("Failed to throw an exception, %{public}d, code = %{public}s", throwStatus, msg.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void UniError::ThrowErr(napi_env env, string errMsg)
|
||||
{
|
||||
napi_value tmp = nullptr;
|
||||
napi_get_and_clear_last_exception(env, &tmp);
|
||||
// Note that ace engine cannot throw errors created by napi_create_error so far
|
||||
napi_status throwStatus = napi_throw_error(env, nullptr, errMsg.c_str());
|
||||
if (throwStatus != napi_ok) {
|
||||
HILOGE("Failed to throw an exception, %{public}d, code = %{public}s", throwStatus, errMsg.c_str());
|
||||
}
|
||||
}
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* 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_NAPI_COMMON_UNI_ERROR_H
|
||||
#define INTERFACES_KITS_NAPI_COMMON_UNI_ERROR_H
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
#include "node_api.h"
|
||||
#include "js_native_api_types.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
constexpr int ERRNO_NOERR = 0;
|
||||
|
||||
enum ELegacy {
|
||||
ELEGACY_INVAL = 202,
|
||||
ELEGACY_IO = 300,
|
||||
ELEGACY_NOENT = 301,
|
||||
};
|
||||
|
||||
enum ErrCodeSystem {
|
||||
ERR_CODE_SYSTEM_LEGACY,
|
||||
ERR_CODE_SYSTEM_POSIX,
|
||||
};
|
||||
|
||||
class UniError {
|
||||
public:
|
||||
UniError();
|
||||
explicit UniError(ELegacy eLegacy);
|
||||
explicit UniError(int ePosix);
|
||||
UniError(const UniError &) = default;
|
||||
~UniError() = default;
|
||||
|
||||
UniError &operator = (const UniError &) = default;
|
||||
|
||||
explicit operator bool() const;
|
||||
|
||||
void SetErrno(ELegacy eLegacy);
|
||||
void SetErrno(int ePosix);
|
||||
int GetErrno(ErrCodeSystem cs);
|
||||
|
||||
std::string GetDefaultErrstr();
|
||||
napi_value GetNapiErr(napi_env env);
|
||||
napi_value GetNapiErr(napi_env env, std::string errMsg);
|
||||
void ThrowErr(napi_env env);
|
||||
void ThrowErr(napi_env env, std::string errMsg);
|
||||
|
||||
private:
|
||||
int errno_ = ERRNO_NOERR;
|
||||
ErrCodeSystem codingSystem_ = ERR_CODE_SYSTEM_POSIX;
|
||||
};
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* 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 "document_n_exporter.h"
|
||||
|
||||
#include <cerrno>
|
||||
|
||||
#include "../common/uni_error.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleDocument {
|
||||
napi_value Choose(napi_env env, napi_callback_info info)
|
||||
{
|
||||
UniError(EINVAL).ThrowErr(env, "error");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value Show(napi_env env, napi_callback_info info)
|
||||
{
|
||||
UniError(EINVAL).ThrowErr(env, "error");
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace ModuleDocument
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* 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 DOCUMENT_N_EXPORTER_H
|
||||
#define DOCUMENT_N_EXPORTER_H
|
||||
|
||||
#include "node_api.h"
|
||||
#include "js_native_api_types.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleDocument {
|
||||
napi_value Choose(napi_env env, napi_callback_info info);
|
||||
napi_value Show(napi_env env, napi_callback_info info);
|
||||
} // namespace ModuleDocument
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // DOCUMENT_N_EXPORTER_H
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* 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 "document_napi.h"
|
||||
|
||||
#include "document_n_exporter.h"
|
||||
#include "js_native_api.h"
|
||||
#include "js_native_api_types.h"
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleDocument {
|
||||
/***********************************************
|
||||
* Module export and register
|
||||
***********************************************/
|
||||
napi_value DocumentExport(napi_env env, napi_value exports)
|
||||
{
|
||||
static napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("choose", Choose),
|
||||
DECLARE_NAPI_FUNCTION("show", Show),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
return exports;
|
||||
}
|
||||
|
||||
NAPI_MODULE(document, DocumentExport)
|
||||
} // namespace ModuleDocument
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* 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 DOCUMENT_NAPI_H
|
||||
#define DOCUMENT_NAPI_H
|
||||
|
||||
#include "napi/native_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleDocument {
|
||||
} // namespace ModuleDocument
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // DOCUMENT_NAPI_H
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* 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 "environment_n_exporter.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#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 "../common/napi/n_async/n_async_work_callback.h"
|
||||
#include "../common/napi/n_async/n_async_work_promise.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleEnvironment {
|
||||
namespace {
|
||||
const std::string STORAGE_DATA_PATH = "/data";
|
||||
const std::string USER_DATA_PATH = "/data/storage/0";
|
||||
}
|
||||
napi_value GetStorageDataDir(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [](napi_env env) -> UniError {
|
||||
return UniError(ERRNO_NOERR);
|
||||
};
|
||||
auto cbComplete = [](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
return NVal::CreateUTF8String(env, STORAGE_DATA_PATH);
|
||||
};
|
||||
|
||||
std::string procedureName = "GetStorageDataDir";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::ZERO) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::FIRST]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
|
||||
}
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
int GetUserId()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
napi_value GetUserDataDir(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto userDataPath = std::make_shared<std::string>();
|
||||
auto cbExec = [userDataPath](napi_env env) -> UniError {
|
||||
(*userDataPath).append("/storage/media/").append(std::to_string(GetUserId())).append("/local");
|
||||
return UniError(ERRNO_NOERR);
|
||||
};
|
||||
auto cbComplete = [userDataPath](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
return NVal::CreateUTF8String(env, *userDataPath);
|
||||
};
|
||||
|
||||
std::string procedureName = "GetUserDataDir";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::ZERO) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::FIRST]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
|
||||
}
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
} // namespace ModuleEnvironment
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* 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 ENVIRONMENT_N_EXPORTER_H
|
||||
#define ENVIRONMENT_N_EXPORTER_H
|
||||
|
||||
#include "../common/napi/n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleEnvironment {
|
||||
napi_value GetStorageDataDir(napi_env env, napi_callback_info info);
|
||||
napi_value GetUserDataDir(napi_env env, napi_callback_info info);
|
||||
} // namespace ModuleEnvironment
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // ENVIRONMENT_N_EXPORTER_H
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* 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 "environment_napi.h"
|
||||
|
||||
#include "environment_n_exporter.h"
|
||||
#include "js_native_api.h"
|
||||
#include "js_native_api_types.h"
|
||||
#include "napi/native_common.h"
|
||||
#include "node_api.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleEnvironment {
|
||||
/***********************************************
|
||||
* Module export and register
|
||||
***********************************************/
|
||||
napi_value EnvironmentExport(napi_env env, napi_value exports)
|
||||
{
|
||||
static napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("getStorageDataDir", GetStorageDataDir),
|
||||
DECLARE_NAPI_FUNCTION("getUserDataDir", GetUserDataDir),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
return exports;
|
||||
}
|
||||
|
||||
NAPI_MODULE(environment, EnvironmentExport)
|
||||
} // namespace ModuleEnvironment
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* 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 ENVIRONMENT_NAPI_H
|
||||
#define ENVIRONMENT_NAPI_H
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleEnvironment {
|
||||
} // namespace ModuleEnvironment
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // ENVIRONMENT_NAPI_H
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,198 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../../common/napi/n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFile {
|
||||
enum COMMON_NUM {
|
||||
ZERO = 0,
|
||||
ONE = 1,
|
||||
TWO = 2,
|
||||
THOUSAND = 1000,
|
||||
MILLION = 1000000,
|
||||
};
|
||||
|
||||
struct FileInfo {
|
||||
int32_t length = 0;
|
||||
int64_t lastModifiedTime = 0;
|
||||
std::string type = "";
|
||||
std::string uri = "";
|
||||
};
|
||||
|
||||
struct AsyncAccessCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
std::string url = "";
|
||||
int errorType = -1;
|
||||
int result = -100;
|
||||
};
|
||||
|
||||
struct AsyncMkdirCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
bool recursive = false;
|
||||
std::string url = "";
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
};
|
||||
|
||||
struct AsyncRmdirCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
bool recursive = false;
|
||||
std::string url = "";
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
};
|
||||
|
||||
struct AsyncGetCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
bool recursive = false;
|
||||
std::string url = "";
|
||||
std::string originUri = "";
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
int32_t length = 0;
|
||||
int64_t lastMT = 0;
|
||||
std::string type = "";
|
||||
std::vector<std::string> subFiles;
|
||||
};
|
||||
|
||||
struct AsyncListCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
bool recursive = false;
|
||||
std::string url = "";
|
||||
std::string originUri = "";
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
std::vector<FileInfo> fileList;
|
||||
};
|
||||
|
||||
struct AsyncCopyCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
std::string url = "";
|
||||
std::string urlDst = "";
|
||||
std::string originDst = "";
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
};
|
||||
|
||||
struct AsyncMoveCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
std::string url = "";
|
||||
std::string urlDst = "";
|
||||
std::string originDst = "";
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
};
|
||||
|
||||
struct AsyncDeleteCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
std::string url = "";
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
};
|
||||
|
||||
struct AsyncWriteCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
std::string url = "";
|
||||
std::string text = "";
|
||||
bool append = false;
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
};
|
||||
|
||||
struct AsyncWriteBufferCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
std::string url = "";
|
||||
bool append = false;
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
int32_t length = 0;
|
||||
int32_t position = 0;
|
||||
void* buf = nullptr;
|
||||
napi_ref bufferAddress = nullptr;
|
||||
};
|
||||
|
||||
struct AsyncReadCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
std::string url = "";
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
std::string contents = "";
|
||||
};
|
||||
|
||||
struct AsyncReadBufferCallbackInfo {
|
||||
napi_env env = nullptr;
|
||||
napi_async_work asyncWork = nullptr;
|
||||
napi_ref callback[3] = { 0 };
|
||||
std::string url = "";
|
||||
int length = 0;
|
||||
int position = 0;
|
||||
int result = -100;
|
||||
int errorType = -1;
|
||||
int32_t len = 0;
|
||||
std::string contents = "";
|
||||
};
|
||||
|
||||
class FileNExporter final : public NExporter {
|
||||
public:
|
||||
inline static const std::string className_ = "File";
|
||||
static napi_value Mkdir(napi_env env, napi_callback_info info);
|
||||
static napi_value Rmdir(napi_env env, napi_callback_info info);
|
||||
static napi_value Get(napi_env env, napi_callback_info info);
|
||||
static napi_value List(napi_env env, napi_callback_info info);
|
||||
static napi_value Copy(napi_env env, napi_callback_info info);
|
||||
static napi_value Move(napi_env env, napi_callback_info info);
|
||||
static napi_value Delete(napi_env env, napi_callback_info info);
|
||||
static napi_value Access(napi_env env, napi_callback_info info);
|
||||
static napi_value WriteText(napi_env env, napi_callback_info info);
|
||||
static napi_value WriteArrayBuffer(napi_env env, napi_callback_info info);
|
||||
static napi_value ReadText(napi_env env, napi_callback_info info);
|
||||
static napi_value ReadArrayBuffer(napi_env env, napi_callback_info info);
|
||||
|
||||
bool Export() override;
|
||||
|
||||
std::string GetClassName() override;
|
||||
|
||||
FileNExporter(napi_env env, napi_value exports);
|
||||
~FileNExporter() override;
|
||||
};
|
||||
} // namespace ModuleFile
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* 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 "common_func.h"
|
||||
|
||||
#include "../common/napi/n_func_arg.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFile {
|
||||
using namespace std;
|
||||
|
||||
tuple<bool, napi_ref, napi_ref, napi_ref> CommonFunc::GetCallbackHandles(napi_env env, napi_value object)
|
||||
{
|
||||
bool succ = false;
|
||||
NVal prop = NVal(env, object);
|
||||
napi_value successProp, failProp, completeProp;
|
||||
napi_ref successHandle = nullptr;
|
||||
napi_ref failHandle = nullptr;
|
||||
napi_ref completeHandle = nullptr;
|
||||
string success = "success";
|
||||
string fail = "fail";
|
||||
string complete = "complete";
|
||||
|
||||
successProp = prop.GetProp(success).val_;
|
||||
if (successProp != nullptr) {
|
||||
napi_create_reference(env, successProp, 1, &successHandle);
|
||||
succ = true;
|
||||
}
|
||||
|
||||
failProp = prop.GetProp(fail).val_;
|
||||
if (succ && failProp != nullptr) {
|
||||
napi_create_reference(env, failProp, 1, &failHandle);
|
||||
succ = true;
|
||||
}
|
||||
|
||||
completeProp = prop.GetProp(complete).val_;
|
||||
if (succ && completeProp != nullptr) {
|
||||
napi_create_reference(env, completeProp, 1, &completeHandle);
|
||||
succ = true;
|
||||
}
|
||||
|
||||
return { succ, successHandle, failHandle, completeHandle };
|
||||
}
|
||||
} // namespace ModuleFile
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFile {
|
||||
const std::string FUNC_PROP_SUCCESS = "success";
|
||||
const std::string FUNC_PROP_FAIL = "fail";
|
||||
const std::string FUNC_PROP_COMPLETE = "complete";
|
||||
|
||||
struct CommonFunc {
|
||||
static std::tuple<bool, napi_ref, napi_ref, napi_ref> GetCallbackHandles(napi_env env,
|
||||
napi_value object);
|
||||
};
|
||||
} // namespace ModuleFile
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* 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 <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "../common/log.h"
|
||||
#include "class_file/file_n_exporter.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFile {
|
||||
static napi_value Export(napi_env env, napi_value exports)
|
||||
{
|
||||
std::vector<unique_ptr<NExporter> > products;
|
||||
products.emplace_back(make_unique<FileNExporter>(env, exports));
|
||||
|
||||
for (auto &&product : products) {
|
||||
if (!product->Export()) {
|
||||
HILOGE("INNER BUG. Failed to export class %{public}s for module file", product->GetClassName().c_str());
|
||||
return nullptr;
|
||||
} else {
|
||||
HILOGE("Class %{public}s for module file has been exported", product->GetClassName().c_str());
|
||||
}
|
||||
}
|
||||
return exports;
|
||||
}
|
||||
|
||||
NAPI_MODULE(file, Export)
|
||||
} // namespace ModuleFile
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,189 +0,0 @@
|
||||
/*
|
||||
* 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 "constants.h"
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include "securec.h"
|
||||
|
||||
#include "../../common/log.h"
|
||||
#include "../../common/napi/n_class.h"
|
||||
#include "../../common/napi/n_func_arg.h"
|
||||
#include "../../common/uni_error.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
napi_value Constants::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
return funcArg.GetThisVar();
|
||||
}
|
||||
|
||||
bool Constants::Export()
|
||||
{
|
||||
// access
|
||||
napi_value F_OK_ = nullptr;
|
||||
napi_create_int32(exports_.env_, F_OK, &F_OK_); // 0 F_OK
|
||||
napi_value R_OK_ = nullptr;
|
||||
napi_create_int32(exports_.env_, R_OK, &R_OK_); // 4 R_OK
|
||||
napi_value W_OK_ = nullptr;
|
||||
napi_create_int32(exports_.env_, W_OK, &W_OK_); // 2 W_OK
|
||||
napi_value X_OK_ = nullptr;
|
||||
napi_create_int32(exports_.env_, X_OK, &X_OK_); // 1 X_OK
|
||||
// open
|
||||
napi_value O_RDONLY_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_RDONLY, &O_RDONLY_); // 0 O_RDONLY
|
||||
napi_value O_WRONLY_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_WRONLY, &O_WRONLY_); // 1 O_WRONLY
|
||||
napi_value O_RDWR_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_RDWR, &O_RDWR_); // 2 O_RDWR
|
||||
napi_value O_CREAT_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_CREAT, &O_CREAT_); // 0o100 O_CREAT
|
||||
napi_value O_EXCL_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_EXCL, &O_EXCL_); // 0o200 O_EXCL
|
||||
napi_value O_TRUNC_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_TRUNC, &O_TRUNC_); // 0o1000 O_TRUNC
|
||||
napi_value O_APPEND_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_APPEND, &O_APPEND_); // 0o2000 O_APPEND
|
||||
napi_value O_NONBLOCK_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_NONBLOCK, &O_NONBLOCK_); // 0o4000 O_NONBLOCK
|
||||
napi_value O_DIRECTORY_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_DIRECTORY, &O_DIRECTORY_); // 0o200000 O_DIRECTORY
|
||||
napi_value O_NOFOLLOW_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_NOFOLLOW, &O_NOFOLLOW_); // 0o400000 O_NOFOLLOW
|
||||
napi_value O_SYNC_ = nullptr;
|
||||
napi_create_int32(exports_.env_, O_SYNC, &O_SYNC_); // 0o4010000 O_SYNC
|
||||
|
||||
// stat
|
||||
napi_value S_IFMT_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IFMT, &S_IFMT_); // 0o170000 S_IFMT
|
||||
napi_value S_IFSOCK_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IFSOCK, &S_IFSOCK_); // 0o140000 S_IFSOCK
|
||||
napi_value S_IFLNK_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IFLNK, &S_IFLNK_); // 0o120000 S_IFLNK
|
||||
napi_value S_IFREG_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IFREG, &S_IFREG_); // 0o100000 S_IFREG
|
||||
napi_value S_IFBLK_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IFBLK, &S_IFBLK_); // 0o060000 S_IFBLK
|
||||
napi_value S_IFDIR_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IFDIR, &S_IFDIR_); // 0o040000 S_IFDIR
|
||||
napi_value S_IFCHR_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IFCHR, &S_IFCHR_); // 0o020000 S_IFCHR
|
||||
napi_value S_IFIFO_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IFIFO, &S_IFIFO_); // 0o010000 S_IFIFO
|
||||
napi_value S_IRWXU_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IRWXU, &S_IRWXU_); // 0o0700 S_IRWXU
|
||||
napi_value S_IRUSR_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IRUSR, &S_IRUSR_); // 0o0400 S_IRUSR
|
||||
napi_value S_IWUSR_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IWUSR, &S_IWUSR_); // 0o0200 S_IWUSR
|
||||
napi_value S_IXUSR_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IXUSR, &S_IXUSR_); // 0o0100 S_IXUSR
|
||||
napi_value S_IRWXG_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IRWXG, &S_IRWXG_); // 0o0070 S_IRWXG
|
||||
napi_value S_IRGRP_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IRGRP, &S_IRGRP_); // 0o0040 S_IRGRP
|
||||
napi_value S_IWGRP_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IWGRP, &S_IWGRP_); // 0o0020 S_IWGRP
|
||||
napi_value S_IXGRP_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IXGRP, &S_IXGRP_); // 0o0010 S_IXGRP
|
||||
napi_value S_IRWXO_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IRWXO, &S_IRWXO_); // 0o0007 S_IRWXO
|
||||
napi_value S_IROTH_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IROTH, &S_IROTH_); // 0o0004 S_IROTH
|
||||
napi_value S_IWOTH_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IWOTH, &S_IWOTH_); // 0o0002 S_IWOTH
|
||||
napi_value S_IXOTH_ = nullptr;
|
||||
napi_create_int32(exports_.env_, S_IXOTH, &S_IXOTH_); // 0o0001 S_IXOTH
|
||||
|
||||
vector<napi_property_descriptor> props = {
|
||||
NVal::DeclareNapiStaticProperty("F_OK", F_OK_),
|
||||
NVal::DeclareNapiStaticProperty("R_OK", R_OK_),
|
||||
NVal::DeclareNapiStaticProperty("W_OK", W_OK_),
|
||||
NVal::DeclareNapiStaticProperty("X_OK", X_OK_),
|
||||
NVal::DeclareNapiStaticProperty("O_RDONLY", O_RDONLY_),
|
||||
NVal::DeclareNapiStaticProperty("O_WRONLY", O_WRONLY_),
|
||||
NVal::DeclareNapiStaticProperty("O_RDWR", O_RDWR_),
|
||||
NVal::DeclareNapiStaticProperty("O_CREAT", O_CREAT_),
|
||||
NVal::DeclareNapiStaticProperty("O_EXCL", O_EXCL_),
|
||||
NVal::DeclareNapiStaticProperty("O_TRUNC", O_TRUNC_),
|
||||
NVal::DeclareNapiStaticProperty("O_APPEND", O_APPEND_),
|
||||
NVal::DeclareNapiStaticProperty("O_NONBLOCK", O_NONBLOCK_),
|
||||
NVal::DeclareNapiStaticProperty("O_DIRECTORY", O_DIRECTORY_),
|
||||
NVal::DeclareNapiStaticProperty("O_NOFOLLOW", O_NOFOLLOW_),
|
||||
NVal::DeclareNapiStaticProperty("O_SYNC", O_SYNC_),
|
||||
NVal::DeclareNapiStaticProperty("S_IFMT", S_IFMT_),
|
||||
NVal::DeclareNapiStaticProperty("S_IFSOCK", S_IFSOCK_),
|
||||
NVal::DeclareNapiStaticProperty("S_IFLNK", S_IFLNK_),
|
||||
NVal::DeclareNapiStaticProperty("S_IFREG", S_IFREG_),
|
||||
NVal::DeclareNapiStaticProperty("S_IFBLK", S_IFBLK_),
|
||||
NVal::DeclareNapiStaticProperty("S_IFDIR", S_IFDIR_),
|
||||
NVal::DeclareNapiStaticProperty("S_IFCHR", S_IFCHR_),
|
||||
NVal::DeclareNapiStaticProperty("S_IFIFO", S_IFIFO_),
|
||||
NVal::DeclareNapiStaticProperty("S_IRWXU", S_IRWXU_),
|
||||
NVal::DeclareNapiStaticProperty("S_IRUSR", S_IRUSR_),
|
||||
NVal::DeclareNapiStaticProperty("S_IWUSR", S_IWUSR_),
|
||||
NVal::DeclareNapiStaticProperty("S_IXUSR", S_IXUSR_),
|
||||
NVal::DeclareNapiStaticProperty("S_IRWXG", S_IRWXG_),
|
||||
NVal::DeclareNapiStaticProperty("S_IRGRP", S_IRGRP_),
|
||||
NVal::DeclareNapiStaticProperty("S_IWGRP", S_IWGRP_),
|
||||
NVal::DeclareNapiStaticProperty("S_IXGRP", S_IXGRP_),
|
||||
NVal::DeclareNapiStaticProperty("S_IRWXO", S_IRWXO_),
|
||||
NVal::DeclareNapiStaticProperty("S_IROTH", S_IROTH_),
|
||||
NVal::DeclareNapiStaticProperty("S_IWOTH", S_IWOTH_),
|
||||
NVal::DeclareNapiStaticProperty("S_IXOTH", S_IXOTH_),
|
||||
};
|
||||
|
||||
string className = GetClassName();
|
||||
bool succ = false;
|
||||
napi_value classValue = nullptr;
|
||||
tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, Constants::Constructor, std::move(props));
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class");
|
||||
return false;
|
||||
}
|
||||
succ = NClass::SaveClass(exports_.env_, className, classValue);
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class");
|
||||
return false;
|
||||
}
|
||||
|
||||
return exports_.AddProp(className, classValue);
|
||||
}
|
||||
|
||||
string Constants::GetClassName()
|
||||
{
|
||||
return Constants::className_;
|
||||
}
|
||||
|
||||
Constants::Constants(napi_env env, napi_value exports) : NExporter(env, exports) {}
|
||||
Constants::~Constants() {}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* 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_KITS1_BAD_JS_SRC_MOD_FILEIO_CLASS_CONSTANTS_CONSTANTS_H
|
||||
#define INTERFACES_KITS1_BAD_JS_SRC_MOD_FILEIO_CLASS_CONSTANTS_CONSTANTS_H
|
||||
|
||||
#include "../../common/napi/n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Constants final : public NExporter {
|
||||
public:
|
||||
inline static const std::string className_ = "constants";
|
||||
bool Export() override;
|
||||
std::string GetClassName() override;
|
||||
|
||||
static napi_value Constructor(napi_env env, napi_callback_info info);
|
||||
|
||||
Constants(napi_env env, napi_value exports);
|
||||
~Constants() override;
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_DIR_DIR_ENTITY_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_DIR_DIR_ENTITY_H
|
||||
|
||||
#include <dirent.h>
|
||||
#include <mutex>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
struct DirEntity {
|
||||
std::mutex lock_;
|
||||
std::unique_ptr<DIR, std::function<void(DIR *)> > dir_ = { nullptr, closedir };
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_DIR_DIR_ENTITY_H
|
||||
@@ -1,463 +0,0 @@
|
||||
/*
|
||||
* 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 "dir_n_exporter.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include "securec.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_class.h"
|
||||
#include "../../common/napi/n_func_arg.h"
|
||||
#include "../class_dirent/dirent_entity.h"
|
||||
#include "../class_dirent/dirent_n_exporter.h"
|
||||
#include "../common_func.h"
|
||||
#include "dir_entity.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
static DirEntity *GetDirEntity(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto dirEntity = NClass::GetEntityOf<DirEntity>(env, funcArg.GetThisVar());
|
||||
if (!dirEntity) {
|
||||
UniError(EIO).ThrowErr(env, "Cannot get entity of Dir");
|
||||
return nullptr;
|
||||
}
|
||||
return dirEntity;
|
||||
}
|
||||
|
||||
napi_value DirNExporter::CloseSync(napi_env env, napi_callback_info info)
|
||||
{
|
||||
DirEntity *dirEntity = GetDirEntity(env, info);
|
||||
if (!dirEntity || !dirEntity->dir_) {
|
||||
UniError(EBADF).ThrowErr(env, "Dir has been closed yet");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
dirEntity->dir_.reset();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value DirNExporter::Close(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto dirEntity = NClass::GetEntityOf<DirEntity>(env, funcArg.GetThisVar());
|
||||
if (!dirEntity) {
|
||||
UniError(EIO).ThrowErr(env, "Cannot get entity of Dir");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!dirEntity || !dirEntity->dir_) {
|
||||
UniError(EBADF).ThrowErr(env, "Dir has been closed yet");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [dirEntity](napi_env env) -> UniError {
|
||||
DIR *dir = dirEntity->dir_.release();
|
||||
int ret = closedir(dir);
|
||||
if (ret == -1) {
|
||||
return UniError(errno);
|
||||
} else {
|
||||
return UniError(ERRNO_NOERR);
|
||||
}
|
||||
};
|
||||
auto cbCompl = [](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
} else {
|
||||
return NVal::CreateUndefined(env);
|
||||
}
|
||||
};
|
||||
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
string procedureName = "fileioDirClose";
|
||||
if (funcArg.GetArgc() == NARG_CNT::ZERO) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::FIRST]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
|
||||
struct DirReadArgs {
|
||||
NRef thisptrRef_;
|
||||
struct dirent dirRes = {
|
||||
.d_ino = 0,
|
||||
.d_off = 0,
|
||||
.d_reclen = 0,
|
||||
.d_type = 0,
|
||||
.d_name = { '\0' },
|
||||
};
|
||||
explicit DirReadArgs(NVal obj) : thisptrRef_(obj) {}
|
||||
};
|
||||
|
||||
static NVal DoReadCompile(napi_env env, UniError err, shared_ptr<DirReadArgs> arg)
|
||||
{
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
} else {
|
||||
napi_value objDirent = NClass::InstantiateClass(env, DirentNExporter::className_, {});
|
||||
if (!objDirent) {
|
||||
return { env, UniError(EINVAL).GetNapiErr(env) };
|
||||
}
|
||||
auto direntEntity = NClass::GetEntityOf<DirentEntity>(env, objDirent);
|
||||
if (!direntEntity) {
|
||||
return { env, UniError(EINVAL).GetNapiErr(env) };
|
||||
}
|
||||
|
||||
if (strlen(arg->dirRes.d_name) == 0) {
|
||||
return { env, NVal::CreateUndefined(env).val_ };
|
||||
} else {
|
||||
direntEntity->dirent_ = arg->dirRes;
|
||||
return { env, objDirent };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
napi_value DirNExporter::Read(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto dirEntity = NClass::GetEntityOf<DirEntity>(env, funcArg.GetThisVar());
|
||||
if (!dirEntity) {
|
||||
UniError(EIO).ThrowErr(env, "Cannot get entity of Dir");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!dirEntity || !dirEntity->dir_) {
|
||||
UniError(EBADF).ThrowErr(env, "Dir has been closed yet");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
DIR *dir = dirEntity->dir_.get();
|
||||
auto arg = make_shared<DirReadArgs>(NVal(env, funcArg.GetThisVar()));
|
||||
auto cbExec = [arg, dir, dirEntity](napi_env env) -> UniError {
|
||||
struct dirent tmpDirent;
|
||||
lock_guard(dirEntity->lock_);
|
||||
errno = 0;
|
||||
dirent *res = nullptr;
|
||||
do {
|
||||
res = readdir(dir);
|
||||
if (res == nullptr && errno) {
|
||||
return UniError(errno);
|
||||
} else if (res == nullptr) {
|
||||
return UniError(ERRNO_NOERR);
|
||||
} else if (string(res->d_name) == "." || string(res->d_name) == "..") {
|
||||
continue;
|
||||
} else {
|
||||
tmpDirent = *res;
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
arg->dirRes = tmpDirent;
|
||||
return UniError(ERRNO_NOERR);
|
||||
};
|
||||
auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
|
||||
return DoReadCompile(env, err, arg);
|
||||
};
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::ZERO) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule("fileioDirRead", cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::FIRST]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule("fileioDirRead", cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value DirNExporter::ReadSync(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
DirEntity *dirEntity = GetDirEntity(env, info);
|
||||
if (!dirEntity || !dirEntity->dir_) {
|
||||
UniError(EBADF).ThrowErr(env, "Dir has been closed yet");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct dirent tmpDirent;
|
||||
{
|
||||
lock_guard(dirEntity->lock_);
|
||||
errno = 0;
|
||||
dirent *res = nullptr;
|
||||
do {
|
||||
res = readdir(dirEntity->dir_.get());
|
||||
if (res == nullptr && errno) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
} else if (res == nullptr) {
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
} else if (string(res->d_name) == "." || string(res->d_name) == "..") {
|
||||
continue;
|
||||
} else {
|
||||
tmpDirent = *res;
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
|
||||
napi_value objDirent = NClass::InstantiateClass(env, DirentNExporter::className_, {});
|
||||
if (!objDirent) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto direntEntity = NClass::GetEntityOf<DirentEntity>(env, objDirent);
|
||||
if (!direntEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
direntEntity->dirent_ = tmpDirent;
|
||||
|
||||
return objDirent;
|
||||
}
|
||||
|
||||
struct DirListFileArgs {
|
||||
vector<dirent> dirents;
|
||||
explicit DirListFileArgs()
|
||||
{
|
||||
dirents = vector<dirent>();
|
||||
}
|
||||
~DirListFileArgs() = default;
|
||||
};
|
||||
|
||||
static DirEntity *CheckDirEntity(napi_env env, napi_value dir_entity)
|
||||
{
|
||||
auto dirEntity = NClass::GetEntityOf<DirEntity>(env, dir_entity);
|
||||
if (!dirEntity) {
|
||||
UniError(EIO).ThrowErr(env, "Cannot get entity of Dir");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!dirEntity || !dirEntity->dir_) {
|
||||
UniError(EBADF).ThrowErr(env, "Dir has been closed yet");
|
||||
return nullptr;
|
||||
}
|
||||
return dirEntity;
|
||||
}
|
||||
|
||||
static tuple<bool, int> ParseJsListNum(napi_env env, napi_value listNumFromJs)
|
||||
{
|
||||
auto [succ, listNum] = NVal(env, listNumFromJs).ToInt32();
|
||||
return {succ, listNum};
|
||||
}
|
||||
|
||||
static napi_value DoListFileVector2NV(napi_env env, vector<dirent> dirents)
|
||||
{
|
||||
napi_value res = nullptr;
|
||||
napi_create_array(env, &res);
|
||||
for (size_t i = 0; i < dirents.size(); i++) {
|
||||
napi_value objDirent = NClass::InstantiateClass(env, DirentNExporter::className_, {});
|
||||
if (!objDirent) {
|
||||
UniError(EINVAL).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
auto direntEntity = NClass::GetEntityOf<DirentEntity>(env, objDirent);
|
||||
if (!direntEntity) {
|
||||
UniError(EINVAL).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
direntEntity->dirent_ = dirents[i];
|
||||
napi_set_element(env, res, i, objDirent);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static NVal DoListFileCompile(napi_env env, UniError err, shared_ptr<DirListFileArgs> arg)
|
||||
{
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
} else {
|
||||
return { env, DoListFileVector2NV(env, arg->dirents) };
|
||||
}
|
||||
}
|
||||
|
||||
napi_value DirNExporter::ListFile(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 dirEntity = CheckDirEntity(env, funcArg.GetThisVar());
|
||||
if (!dirEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
auto [succ, num] = ParseJsListNum(env, funcArg[NARG_POS::FIRST]);
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid listNum");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
DIR *dir = dirEntity->dir_.get();
|
||||
auto arg = make_shared<DirListFileArgs>();
|
||||
int listNum = num;
|
||||
auto cbExec = [arg, dir, dirEntity, listNum](napi_env env) -> UniError {
|
||||
lock_guard(dirEntity->lock_);
|
||||
errno = 0;
|
||||
dirent *res = nullptr;
|
||||
int listCount = 0;
|
||||
do {
|
||||
res = readdir(dir);
|
||||
if (res == nullptr && errno) {
|
||||
return UniError(errno);
|
||||
} else if (res == nullptr) {
|
||||
return UniError(ERRNO_NOERR);
|
||||
} else if (string(res->d_name) == "." || string(res->d_name) == "..") {
|
||||
continue;
|
||||
} else {
|
||||
arg->dirents.push_back(*res);
|
||||
listCount++;
|
||||
}
|
||||
} while (listCount < listNum || listNum == 0);
|
||||
return UniError(ERRNO_NOERR);
|
||||
};
|
||||
auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
|
||||
return DoListFileCompile(env, err, arg);
|
||||
};
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
|
||||
if (funcArg.GetArgc() == NARG_CNT::ONE) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(listfileProcedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::SECOND]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(listfileProcedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value DirNExporter::ListFileSync(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 dirEntity = CheckDirEntity(env, funcArg.GetThisVar());
|
||||
if (!dirEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
auto [succ, listNum] = ParseJsListNum(env, funcArg[NARG_POS::FIRST]);
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid listNum");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
vector<dirent> dirents;
|
||||
{
|
||||
lock_guard(dirEntity->lock_);
|
||||
errno = 0;
|
||||
dirent *res = nullptr;
|
||||
int listCount = 0;
|
||||
auto dir = dirEntity->dir_.get();
|
||||
do {
|
||||
res = readdir(dir);
|
||||
if (res == nullptr && errno) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
} else if (res == nullptr) {
|
||||
break;
|
||||
} else if (string(res->d_name) == "." || string(res->d_name) == "..") {
|
||||
continue;
|
||||
} else {
|
||||
dirents.push_back(*res);
|
||||
listCount++;
|
||||
}
|
||||
} while (listCount < listNum || listNum == 0);
|
||||
}
|
||||
return DoListFileVector2NV(env, dirents);
|
||||
}
|
||||
|
||||
napi_value DirNExporter::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto dirEntity = make_unique<DirEntity>();
|
||||
if (!NClass::SetEntityFor<DirEntity>(env, funcArg.GetThisVar(), move(dirEntity))) {
|
||||
UniError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj dir");
|
||||
return nullptr;
|
||||
}
|
||||
return funcArg.GetThisVar();
|
||||
}
|
||||
|
||||
bool DirNExporter::Export()
|
||||
{
|
||||
vector<napi_property_descriptor> props = {
|
||||
NVal::DeclareNapiFunction("readSync", ReadSync),
|
||||
NVal::DeclareNapiFunction("closeSync", CloseSync),
|
||||
NVal::DeclareNapiFunction("listfileSync", ListFileSync),
|
||||
NVal::DeclareNapiFunction("read", Read),
|
||||
NVal::DeclareNapiFunction("close", Close),
|
||||
NVal::DeclareNapiFunction("listfile", ListFile),
|
||||
};
|
||||
|
||||
string className = GetClassName();
|
||||
|
||||
bool succ = false;
|
||||
napi_value classValue = nullptr;
|
||||
tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, DirNExporter::Constructor, std::move(props));
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class Dirent");
|
||||
return false;
|
||||
}
|
||||
|
||||
succ = NClass::SaveClass(exports_.env_, className, classValue);
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class Dirent");
|
||||
return false;
|
||||
}
|
||||
|
||||
return exports_.AddProp(className, classValue);
|
||||
}
|
||||
|
||||
string DirNExporter::GetClassName()
|
||||
{
|
||||
return DirNExporter::className_;
|
||||
}
|
||||
|
||||
DirNExporter::DirNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
|
||||
|
||||
DirNExporter::~DirNExporter() {}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef INTERFACES_KITS1_BAD_JS_SRC_MOD_FILEIO_CLASS_DIR_DIR_N_EXPORTER_H
|
||||
#define INTERFACES_KITS1_BAD_JS_SRC_MOD_FILEIO_CLASS_DIR_DIR_N_EXPORTER_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../../common/napi/n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class DirNExporter final : public NExporter {
|
||||
public:
|
||||
inline static const std::string className_ = "Dir";
|
||||
|
||||
bool Export() override;
|
||||
std::string GetClassName() override;
|
||||
|
||||
static napi_value Constructor(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value CloseSync(napi_env env, napi_callback_info info);
|
||||
static napi_value ReadSync(napi_env env, napi_callback_info info);
|
||||
static napi_value ListFileSync(napi_env env, napi_callback_info info);
|
||||
static napi_value Read(napi_env env, napi_callback_info info);
|
||||
static napi_value Close(napi_env env, napi_callback_info info);
|
||||
static napi_value ListFile(napi_env env, napi_callback_info info);
|
||||
DirNExporter(napi_env env, napi_value exports);
|
||||
~DirNExporter() override;
|
||||
};
|
||||
const std::string listfileProcedureName = "fileioDirListFile";
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_DIRENT_DIRENT_ENTITY_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_DIRENT_DIRENT_ENTITY_H
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
struct DirentEntity {
|
||||
struct dirent dirent_;
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_DIRENT_DIRENT_ENTITY_H
|
||||
@@ -1,170 +0,0 @@
|
||||
/*
|
||||
* 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 "dirent_n_exporter.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
|
||||
#include "securec.h"
|
||||
|
||||
#include "../../common/log.h"
|
||||
#include "../../common/napi/n_class.h"
|
||||
#include "../../common/napi/n_func_arg.h"
|
||||
#include "../../common/uni_error.h"
|
||||
#include "../common_func.h"
|
||||
#include "dirent_entity.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
static DirentEntity *GetDirentEntity(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto direntEntity = NClass::GetEntityOf<DirentEntity>(env, funcArg.GetThisVar());
|
||||
if (!direntEntity) {
|
||||
UniError(EIO).ThrowErr(env, "Cannot get entity of Dirent");
|
||||
return nullptr;
|
||||
}
|
||||
return direntEntity;
|
||||
}
|
||||
|
||||
static napi_value CheckDirentDType(napi_env env, napi_callback_info info, unsigned char dType)
|
||||
{
|
||||
DirentEntity *direntEntity = GetDirentEntity(env, info);
|
||||
if (!direntEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateBool(env, direntEntity->dirent_.d_type == dType).val_;
|
||||
}
|
||||
|
||||
napi_value DirentNExporter::isBlockDevice(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckDirentDType(env, info, DT_BLK);
|
||||
}
|
||||
|
||||
napi_value DirentNExporter::isCharacterDevice(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckDirentDType(env, info, DT_CHR);
|
||||
}
|
||||
|
||||
napi_value DirentNExporter::isDirectory(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckDirentDType(env, info, DT_DIR);
|
||||
}
|
||||
|
||||
napi_value DirentNExporter::isFIFO(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckDirentDType(env, info, DT_FIFO);
|
||||
}
|
||||
|
||||
napi_value DirentNExporter::isFile(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckDirentDType(env, info, DT_REG);
|
||||
}
|
||||
|
||||
napi_value DirentNExporter::isSocket(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckDirentDType(env, info, DT_SOCK);
|
||||
}
|
||||
|
||||
napi_value DirentNExporter::isSymbolicLink(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckDirentDType(env, info, DT_LNK);
|
||||
}
|
||||
|
||||
napi_value DirentNExporter::GetName(napi_env env, napi_callback_info info)
|
||||
{
|
||||
DirentEntity *direntEntity = GetDirentEntity(env, info);
|
||||
if (!direntEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
return NVal::CreateUTF8String(env, direntEntity->dirent_.d_name).val_;
|
||||
}
|
||||
|
||||
napi_value DirentNExporter::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto direntEntity = make_unique<DirentEntity>();
|
||||
if (!NClass::SetEntityFor<DirentEntity>(env, funcArg.GetThisVar(), move(direntEntity))) {
|
||||
UniError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj dirent");
|
||||
return nullptr;
|
||||
}
|
||||
return funcArg.GetThisVar();
|
||||
}
|
||||
|
||||
bool DirentNExporter::Export()
|
||||
{
|
||||
vector<napi_property_descriptor> props = {
|
||||
NVal::DeclareNapiFunction("isBlockDevice", isBlockDevice),
|
||||
NVal::DeclareNapiFunction("isCharacterDevice", isCharacterDevice),
|
||||
NVal::DeclareNapiFunction("isDirectory", isDirectory),
|
||||
NVal::DeclareNapiFunction("isFIFO", isFIFO),
|
||||
NVal::DeclareNapiFunction("isFile", isFile),
|
||||
NVal::DeclareNapiFunction("isSocket", isSocket),
|
||||
NVal::DeclareNapiFunction("isSymbolicLink", isSymbolicLink),
|
||||
|
||||
NVal::DeclareNapiGetter("name", GetName),
|
||||
};
|
||||
|
||||
string className = GetClassName();
|
||||
|
||||
bool succ = false;
|
||||
napi_value classValue = nullptr;
|
||||
tie(succ, classValue) = NClass::DefineClass(exports_.env_,
|
||||
className,
|
||||
DirentNExporter::Constructor,
|
||||
std::move(props));
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class Dirent");
|
||||
return false;
|
||||
}
|
||||
|
||||
succ = NClass::SaveClass(exports_.env_, className, classValue);
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class Dirent");
|
||||
return false;
|
||||
}
|
||||
|
||||
return exports_.AddProp(className, classValue);
|
||||
}
|
||||
|
||||
string DirentNExporter::GetClassName()
|
||||
{
|
||||
return DirentNExporter::className_;
|
||||
}
|
||||
|
||||
DirentNExporter::DirentNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
|
||||
|
||||
DirentNExporter::~DirentNExporter() {}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_DIRENT_DIRENT_N_EXPORTER_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_DIRENT_DIRENT_N_EXPORTER_H
|
||||
|
||||
#include "../../common/napi/n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class DirentNExporter final : public NExporter {
|
||||
public:
|
||||
inline static const std::string className_ = "Dirent";
|
||||
|
||||
bool Export() override;
|
||||
std::string GetClassName() override;
|
||||
|
||||
static napi_value Constructor(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
static napi_value isBlockDevice(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value isCharacterDevice(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value isDirectory(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value isFIFO(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value isFile(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value isSocket(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value isSymbolicLink(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
static napi_value GetName(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
DirentNExporter(napi_env env, napi_value exports);
|
||||
~DirentNExporter() override;
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_DIRENT_DIRENT_N_EXPORTER_H
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* 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_FILEIO_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_ENTITY_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_ENTITY_H
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../../common/file_helper/fd_guard.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
struct RandomAccessFileEntity {
|
||||
std::unique_ptr<FDGuard> fd_ = { nullptr };
|
||||
size_t fpointer;
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
-416
@@ -1,416 +0,0 @@
|
||||
/*
|
||||
* 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 "randomaccessfile_n_exporter.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <uv.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_class.h"
|
||||
#include "../../common/napi/n_func_arg.h"
|
||||
#include "../../common/uni_error.h"
|
||||
#include "../common_func.h"
|
||||
#include "randomaccessfile_entity.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
static RandomAccessFileEntity *GetRAFEntity(napi_env env, napi_value raf_entity)
|
||||
{
|
||||
auto rafEntity = NClass::GetEntityOf<RandomAccessFileEntity>(env, raf_entity);
|
||||
if (!rafEntity) {
|
||||
UniError(EINVAL).ThrowErr(env, "Cannot get entity of RandomAccessFile");
|
||||
return nullptr;
|
||||
}
|
||||
if (!rafEntity->fd_) {
|
||||
UniError(EINVAL).ThrowErr(env, "RandomAccessFile has been closed yet");
|
||||
return nullptr;
|
||||
}
|
||||
return rafEntity;
|
||||
}
|
||||
|
||||
static tuple<bool, int> ParseJsFP(napi_env env, napi_value FPFromJs)
|
||||
{
|
||||
auto [succ, fp] = NVal(env, FPFromJs).ToInt32();
|
||||
return { succ, fp };
|
||||
}
|
||||
|
||||
static tuple<bool, void*, size_t, bool, size_t, size_t> GetRAFReadArg(napi_env env,
|
||||
napi_value ReadBuf, napi_value option)
|
||||
{
|
||||
bool succ = false, hasPos = false;
|
||||
void *buf = nullptr;
|
||||
size_t len, pos, offset;
|
||||
tie(succ, buf, len, hasPos, pos, offset) = CommonFunc::GetReadArg(env, ReadBuf, option);
|
||||
if (!succ) {
|
||||
return { false, nullptr, 0, false, 0, 0 };
|
||||
}
|
||||
return { succ, buf, len, hasPos, pos, offset };
|
||||
}
|
||||
|
||||
static tuple<bool, void *, size_t, bool, size_t> GetRAFWriteArg(napi_env env,
|
||||
napi_value WriteBuf, napi_value option)
|
||||
{
|
||||
bool succ = false, hasPos = false;
|
||||
void *buf = nullptr;
|
||||
size_t len, pos;
|
||||
tie(succ, ignore, buf, len, hasPos, pos) = CommonFunc::GetWriteArg(env, WriteBuf, option);
|
||||
if (!succ) {
|
||||
return { false, nullptr, 0, false, 0 };
|
||||
}
|
||||
return { succ, buf, len, hasPos, pos };
|
||||
}
|
||||
|
||||
static size_t DoReadRAF(napi_env env, void* buf, size_t len, int fd, size_t pos)
|
||||
{
|
||||
uv_loop_s *loop = nullptr;
|
||||
uv_fs_t read_req;
|
||||
napi_get_uv_event_loop(env, &loop);
|
||||
uv_buf_t iov = uv_buf_init((char *)buf, len);
|
||||
size_t ret = uv_fs_read(loop, &read_req, fd, &iov, 1, pos, NULL);
|
||||
uv_fs_req_cleanup(&read_req);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static size_t DoWriteRAF(napi_env env, void* buf, size_t len, int fd, size_t pos)
|
||||
{
|
||||
uv_loop_s *loop = nullptr;
|
||||
uv_fs_t write_req;
|
||||
napi_get_uv_event_loop(env, &loop);
|
||||
uv_buf_t iov = uv_buf_init((char *)buf, len);
|
||||
size_t ret = uv_fs_write(loop, &write_req, fd, &iov, 1, pos, NULL);
|
||||
uv_fs_req_cleanup(&write_req);
|
||||
return ret;
|
||||
}
|
||||
|
||||
napi_value RandomAccessFileNExporter::GetFD(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar());
|
||||
if (!rafEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
return NVal::CreateInt32(env, rafEntity->fd_.get()->GetFD()).val_;
|
||||
}
|
||||
|
||||
napi_value RandomAccessFileNExporter::GetFPointer(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar());
|
||||
if (!rafEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
return NVal::CreateInt64(env, rafEntity->fpointer).val_;
|
||||
}
|
||||
|
||||
napi_value RandomAccessFileNExporter::SetFilePointerSync(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 rafEntity = GetRAFEntity(env, funcArg.GetThisVar());
|
||||
if (!rafEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
auto [succ, fp] = ParseJsFP(env, funcArg[NARG_POS::FIRST]);
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fpointer");
|
||||
}
|
||||
rafEntity->fpointer = fp;
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value RandomAccessFileNExporter::ReadSync(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 rafEntity = GetRAFEntity(env, funcArg.GetThisVar());
|
||||
if (!rafEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
auto [succ, buf, len, hasPos, pos, ignore] =
|
||||
GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid buffer/options");
|
||||
}
|
||||
ssize_t actLen = DoReadRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), rafEntity->fpointer + pos);
|
||||
if (actLen < 0) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
rafEntity->fpointer += actLen;
|
||||
return NVal::CreateInt64(env, actLen).val_;
|
||||
}
|
||||
|
||||
struct AsyncIOReadArg {
|
||||
ssize_t lenRead { 0 };
|
||||
int offset { 0 };
|
||||
NRef refReadBuf;
|
||||
|
||||
explicit AsyncIOReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {}
|
||||
~AsyncIOReadArg() = default;
|
||||
};
|
||||
|
||||
napi_value RandomAccessFileNExporter::Read(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 rafEntity = GetRAFEntity(env, funcArg.GetThisVar());
|
||||
if (!rafEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
bool succ = false, hasPos = false;
|
||||
size_t len, pos, offset;
|
||||
void* buf = nullptr;
|
||||
tie(succ, buf, len, hasPos, pos, offset) = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid buffer/options");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto arg = make_shared<AsyncIOReadArg>(NVal(env, funcArg[NARG_POS::FIRST]));
|
||||
auto cbExec = [arg, buf, len, hasPos, pos, offset, rafEntity](napi_env env) -> UniError {
|
||||
ssize_t actLen = DoReadRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), rafEntity->fpointer + pos);
|
||||
if (actLen < 0) {
|
||||
return UniError(errno);
|
||||
}
|
||||
arg->lenRead = actLen;
|
||||
arg->offset = offset;
|
||||
rafEntity->fpointer += actLen;
|
||||
return UniError(ERRNO_NOERR);
|
||||
};
|
||||
|
||||
auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
NVal obj = NVal::CreateObject(env);
|
||||
obj.AddProp(
|
||||
{ NVal::DeclareNapiProperty("bytesRead", NVal::CreateInt64(env, arg->lenRead).val_),
|
||||
NVal::DeclareNapiProperty("buffer", arg->refReadBuf.Deref(env).val_),
|
||||
NVal::DeclareNapiProperty("offset", NVal::CreateInt64(env, arg->offset).val_)
|
||||
});
|
||||
return { obj };
|
||||
};
|
||||
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO &&
|
||||
!NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(readProcedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD);
|
||||
NVal cb(env, funcArg[cbIdx]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(readProcedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value RandomAccessFileNExporter::WriteSync(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 rafEntity = GetRAFEntity(env, funcArg.GetThisVar());
|
||||
if (!rafEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool succ = false, hasPos = false;
|
||||
void *buf = nullptr;
|
||||
size_t len, pos;
|
||||
tie(succ, buf, len, hasPos, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid buffer/options");
|
||||
return nullptr;
|
||||
}
|
||||
if (hasPos) {
|
||||
pos = rafEntity->fpointer + pos;
|
||||
} else {
|
||||
pos = rafEntity->fpointer;
|
||||
}
|
||||
ssize_t writeLen = DoWriteRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), pos);
|
||||
if (writeLen < 0) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
rafEntity->fpointer += writeLen;
|
||||
return NVal::CreateInt64(env, writeLen).val_;
|
||||
}
|
||||
|
||||
struct AsyncIOWriteArg {
|
||||
NRef refWriteArrayBuf_;
|
||||
size_t actLen = 0;
|
||||
explicit AsyncIOWriteArg(NVal refWriteArrayBuf) : refWriteArrayBuf_(refWriteArrayBuf) {}
|
||||
~AsyncIOWriteArg() = default;
|
||||
};
|
||||
|
||||
napi_value RandomAccessFileNExporter::Write(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 rafEntity = GetRAFEntity(env, funcArg.GetThisVar());
|
||||
if (!rafEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
bool succ = false, hasPos = false;
|
||||
void *buf = nullptr;
|
||||
size_t len, pos;
|
||||
tie(succ, buf, len, hasPos, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid buffer/options");
|
||||
return nullptr;
|
||||
}
|
||||
if (hasPos) {
|
||||
pos = rafEntity->fpointer + pos;
|
||||
} else {
|
||||
pos = rafEntity->fpointer;
|
||||
}
|
||||
|
||||
auto arg = make_shared<AsyncIOWriteArg>(NVal(env, funcArg[NARG_POS::FIRST]));
|
||||
auto cbExec = [arg, buf, len, fd = rafEntity->fd_.get()->GetFD(), pos, rafEntity](napi_env env) -> UniError {
|
||||
size_t writeLen = DoWriteRAF(env, buf, len, fd, pos);
|
||||
if (writeLen < 0) {
|
||||
return UniError(errno);
|
||||
}
|
||||
arg->actLen = writeLen;
|
||||
rafEntity->fpointer += writeLen;
|
||||
return UniError(ERRNO_NOERR);
|
||||
};
|
||||
|
||||
auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
} else {
|
||||
return { NVal::CreateInt64(env, arg->actLen) };
|
||||
}
|
||||
};
|
||||
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO &&
|
||||
!NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(writeProcedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD);
|
||||
NVal cb(env, funcArg[cbIdx]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(writeProcedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value RandomAccessFileNExporter::CloseSync(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar());
|
||||
if (!rafEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
rafEntity->fd_.reset();
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value RandomAccessFileNExporter::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto rafEntity = make_unique<RandomAccessFileEntity>();
|
||||
if (!NClass::SetEntityFor<RandomAccessFileEntity>(env, funcArg.GetThisVar(), move(rafEntity))) {
|
||||
UniError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj RandomAccessFile");
|
||||
return nullptr;
|
||||
}
|
||||
return funcArg.GetThisVar();
|
||||
}
|
||||
|
||||
bool RandomAccessFileNExporter::Export()
|
||||
{
|
||||
vector<napi_property_descriptor> props = {
|
||||
NVal::DeclareNapiFunction("read", Read),
|
||||
NVal::DeclareNapiFunction("readSync", ReadSync),
|
||||
NVal::DeclareNapiFunction("write", Write),
|
||||
NVal::DeclareNapiFunction("writeSync", WriteSync),
|
||||
NVal::DeclareNapiFunction("setFilePointerSync", SetFilePointerSync),
|
||||
NVal::DeclareNapiFunction("closeSync", CloseSync),
|
||||
NVal::DeclareNapiGetter("fd", GetFD),
|
||||
NVal::DeclareNapiGetter("fpointer", GetFPointer),
|
||||
};
|
||||
|
||||
string className = GetClassName();
|
||||
bool succ = false;
|
||||
napi_value classValue = nullptr;
|
||||
tie(succ, classValue) = NClass::DefineClass(exports_.env_, className,
|
||||
RandomAccessFileNExporter::Constructor, move(props));
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class");
|
||||
return false;
|
||||
}
|
||||
succ = NClass::SaveClass(exports_.env_, className, classValue);
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class");
|
||||
return false;
|
||||
}
|
||||
|
||||
return exports_.AddProp(className, classValue);
|
||||
}
|
||||
|
||||
string RandomAccessFileNExporter::GetClassName()
|
||||
{
|
||||
return RandomAccessFileNExporter::className_;
|
||||
}
|
||||
|
||||
RandomAccessFileNExporter::RandomAccessFileNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
|
||||
|
||||
RandomAccessFileNExporter::~RandomAccessFileNExporter() {}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
-52
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* 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_FILEIO_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_N_EXPORTER_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_N_EXPORTER_H
|
||||
|
||||
#include "../../common/napi/n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class RandomAccessFileNExporter final : public NExporter {
|
||||
public:
|
||||
inline static const std::string className_ = "RandomAccessFile";
|
||||
|
||||
bool Export() override;
|
||||
std::string GetClassName() override;
|
||||
|
||||
static napi_value Constructor(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
static napi_value SetFilePointerSync(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value WriteSync(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value ReadSync(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value CloseSync(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
static napi_value Write(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value Read(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
static napi_value GetFD(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetFPointer(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
RandomAccessFileNExporter(napi_env env, napi_value exports);
|
||||
~RandomAccessFileNExporter() override;
|
||||
};
|
||||
const std::string readProcedureName = "FileIORandomAccessFileRead";
|
||||
const std::string writeProcedureName = "FileIORandomAccessFileWrite";
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_STAT_STAT_ENTITY_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_STAT_STAT_ENTITY_H
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
struct StatEntity {
|
||||
struct stat stat_;
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,368 +0,0 @@
|
||||
/*
|
||||
* 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 "stat_n_exporter.h"
|
||||
#include "../../common/napi/n_async/n_async_work_callback.h"
|
||||
#include "../../common/napi/n_async/n_async_work_promise.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
|
||||
#include "securec.h"
|
||||
|
||||
#include "../../common/log.h"
|
||||
#include "../../common/napi/n_class.h"
|
||||
#include "../../common/napi/n_func_arg.h"
|
||||
#include "../../common/uni_error.h"
|
||||
#include "stat_entity.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
static napi_value CheckStatMode(napi_env env, napi_callback_info info, mode_t mode)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool check = (statEntity->stat_.st_mode & S_IFMT) == mode;
|
||||
return NVal::CreateBool(env, check).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::IsBlockDevice(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckStatMode(env, info, S_IFBLK);
|
||||
}
|
||||
|
||||
napi_value StatNExporter::IsCharacterDevice(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckStatMode(env, info, S_IFCHR);
|
||||
}
|
||||
|
||||
napi_value StatNExporter::IsDirectory(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckStatMode(env, info, S_IFDIR);
|
||||
}
|
||||
|
||||
napi_value StatNExporter::IsFIFO(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckStatMode(env, info, S_IFIFO);
|
||||
}
|
||||
|
||||
napi_value StatNExporter::IsFile(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckStatMode(env, info, S_IFREG);
|
||||
}
|
||||
|
||||
napi_value StatNExporter::IsSocket(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckStatMode(env, info, S_IFSOCK);
|
||||
}
|
||||
|
||||
napi_value StatNExporter::IsSymbolicLink(napi_env env, napi_callback_info info)
|
||||
{
|
||||
return CheckStatMode(env, info, S_IFLNK);
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetDev(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_dev).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetIno(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_ino).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetMode(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_mode).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetNlink(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_nlink).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetUid(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_uid).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetGid(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_gid).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetRdev(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_rdev).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetSize(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_size).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetBlksize(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_blksize).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetBlocks(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_blocks).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetAtime(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_atim.tv_sec).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetMtime(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_mtim.tv_sec).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::GetCtime(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, funcArg.GetThisVar());
|
||||
if (!statEntity) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, statEntity->stat_.st_ctim.tv_sec).val_;
|
||||
}
|
||||
|
||||
napi_value StatNExporter::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
unique_ptr<StatEntity> statEntity = make_unique<StatEntity>();
|
||||
if (!NClass::SetEntityFor<StatEntity>(env, funcArg.GetThisVar(), move(statEntity))) {
|
||||
UniError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj stat");
|
||||
return nullptr;
|
||||
}
|
||||
return funcArg.GetThisVar();
|
||||
}
|
||||
|
||||
bool StatNExporter::Export()
|
||||
{
|
||||
vector<napi_property_descriptor> props = {
|
||||
NVal::DeclareNapiFunction("isBlockDevice", IsBlockDevice),
|
||||
NVal::DeclareNapiFunction("isCharacterDevice", IsCharacterDevice),
|
||||
NVal::DeclareNapiFunction("isDirectory", IsDirectory),
|
||||
NVal::DeclareNapiFunction("isFIFO", IsFIFO),
|
||||
NVal::DeclareNapiFunction("isFile", IsFile),
|
||||
NVal::DeclareNapiFunction("isSocket", IsSocket),
|
||||
NVal::DeclareNapiFunction("isSymbolicLink", IsSymbolicLink),
|
||||
|
||||
NVal::DeclareNapiGetter("dev", GetDev),
|
||||
NVal::DeclareNapiGetter("ino", GetIno),
|
||||
NVal::DeclareNapiGetter("mode", GetMode),
|
||||
NVal::DeclareNapiGetter("nlink", GetNlink),
|
||||
NVal::DeclareNapiGetter("uid", GetUid),
|
||||
NVal::DeclareNapiGetter("gid", GetGid),
|
||||
NVal::DeclareNapiGetter("rdev", GetRdev),
|
||||
NVal::DeclareNapiGetter("size", GetSize),
|
||||
NVal::DeclareNapiGetter("blksize", GetBlksize),
|
||||
NVal::DeclareNapiGetter("blocks", GetBlocks),
|
||||
NVal::DeclareNapiGetter("atime", GetAtime),
|
||||
NVal::DeclareNapiGetter("mtime", GetMtime),
|
||||
NVal::DeclareNapiGetter("ctime", GetCtime),
|
||||
};
|
||||
|
||||
string className = GetClassName();
|
||||
bool succ = false;
|
||||
napi_value classValue = nullptr;
|
||||
tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, StatNExporter::Constructor, std::move(props));
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class");
|
||||
return false;
|
||||
}
|
||||
succ = NClass::SaveClass(exports_.env_, className, classValue);
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class");
|
||||
return false;
|
||||
}
|
||||
|
||||
return exports_.AddProp(className, classValue);
|
||||
}
|
||||
|
||||
string StatNExporter::GetClassName()
|
||||
{
|
||||
return StatNExporter::className_;
|
||||
}
|
||||
|
||||
StatNExporter::StatNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
|
||||
|
||||
StatNExporter::~StatNExporter() {}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_STAT_N_EXPORTER_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_STAT_N_EXPORTER_H
|
||||
|
||||
#include "../../common/napi/n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class StatNExporter final : public NExporter {
|
||||
public:
|
||||
inline static const std::string className_ = "Stat";
|
||||
|
||||
bool Export() override;
|
||||
std::string GetClassName() override;
|
||||
|
||||
static napi_value Constructor(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
static napi_value IsBlockDevice(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value IsCharacterDevice(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value IsDirectory(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value IsFIFO(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value IsFile(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value IsSocket(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value IsSymbolicLink(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
static napi_value GetDev(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetIno(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetMode(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetNlink(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetUid(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetGid(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetRdev(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetSize(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetBlksize(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetBlocks(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetAtime(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetMtime(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value GetCtime(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
StatNExporter(napi_env env, napi_value exports);
|
||||
~StatNExporter() override;
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_STAT_N_EXPORTER_H
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* 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 "flush.h"
|
||||
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
|
||||
#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;
|
||||
|
||||
struct StreamEntity {
|
||||
std::unique_ptr<FILE, decltype(&fclose)> fp = { nullptr, fclose };
|
||||
};
|
||||
|
||||
napi_value Flush::Sync(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
|
||||
if (!streamEntity || !streamEntity->fp) {
|
||||
UniError(EBADF).ThrowErr(env, "Stream may has been closed");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int ret = fflush(streamEntity->fp.get());
|
||||
if (ret == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value Flush::Async(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
|
||||
if (!streamEntity || !streamEntity->fp) {
|
||||
UniError(EBADF).ThrowErr(env, "Stream may has been closed");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [streamEntity](napi_env env) -> UniError {
|
||||
int ret = fflush(streamEntity->fp.get());
|
||||
if (ret == -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());
|
||||
string procedureName = "fileIOFlush";
|
||||
if (funcArg.GetArgc() == NARG_CNT::ZERO) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::FIRST]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_STREAM_FLUSH_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_STREAM_FLUSH_H
|
||||
|
||||
#include "../../common/napi/uni_header.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Flush 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
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_STREAM_STREAM_ENTITY_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_STREAM_STREAM_ENTITY_H
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
struct StreamEntity {
|
||||
std::unique_ptr<FILE, decltype(&fclose)> fp = { nullptr, fclose };
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif // INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_STREAM_STREAM_ENTITY_H
|
||||
@@ -1,399 +0,0 @@
|
||||
/*
|
||||
* 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 "stream_n_exporter.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "flush.h"
|
||||
#include "securec.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_class.h"
|
||||
#include "../../common/napi/n_func_arg.h"
|
||||
#include "../../common/uni_error.h"
|
||||
#include "../common_func.h"
|
||||
#include "stream_entity.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
napi_value StreamNExporter::ReadSync(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;
|
||||
}
|
||||
|
||||
/* To get entity */
|
||||
bool succ = false;
|
||||
FILE *filp = nullptr;
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
|
||||
if (!streamEntity || !streamEntity->fp) {
|
||||
UniError(EBADF).ThrowErr(env, "Stream may have been closed");
|
||||
return nullptr;
|
||||
} else {
|
||||
filp = streamEntity->fp.get();
|
||||
}
|
||||
|
||||
void *buf = nullptr;
|
||||
int64_t len;
|
||||
bool hasPos = false;
|
||||
int64_t pos;
|
||||
tie(succ, buf, len, hasPos, pos, ignore) =
|
||||
CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (hasPos && (fseek(filp, pos, SEEK_SET) == -1)) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t actLen = fread(buf, 1, len, filp);
|
||||
if (actLen != static_cast<size_t>(len) && ferror(filp)) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, actLen).val_;
|
||||
}
|
||||
|
||||
napi_value StreamNExporter::CloseSync(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
|
||||
if (!streamEntity || !streamEntity->fp) {
|
||||
UniError(EINVAL).ThrowErr(env, "Stream may have been closed yet");
|
||||
return nullptr;
|
||||
}
|
||||
streamEntity->fp.reset();
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value StreamNExporter::WriteSync(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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
FILE *filp = nullptr;
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
|
||||
if (!streamEntity || !streamEntity->fp) {
|
||||
UniError(EBADF).ThrowErr(env, "Stream may has been closed");
|
||||
return nullptr;
|
||||
} else {
|
||||
filp = streamEntity->fp.get();
|
||||
}
|
||||
|
||||
void *buf = nullptr;
|
||||
size_t len;
|
||||
size_t position;
|
||||
unique_ptr<char[]> bufGuard;
|
||||
bool hasPos = false;
|
||||
tie(succ, bufGuard, buf, len, hasPos, position) =
|
||||
CommonFunc::GetWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
if (hasPos && (fseek(filp, position, SEEK_SET) == -1)) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t writeLen = fwrite(buf, 1, len, filp);
|
||||
if (writeLen != len) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateInt64(env, writeLen).val_;
|
||||
}
|
||||
|
||||
struct AsyncWrtieArg {
|
||||
NRef refWriteArrayBuf;
|
||||
unique_ptr<char[]> guardWriteStr;
|
||||
size_t actLen { 0 };
|
||||
|
||||
explicit AsyncWrtieArg(NVal refWriteArrayBuf) : refWriteArrayBuf(refWriteArrayBuf) {}
|
||||
explicit AsyncWrtieArg(unique_ptr<char[]> &&guardWriteStr) : guardWriteStr(move(guardWriteStr)) {}
|
||||
~AsyncWrtieArg() = default;
|
||||
};
|
||||
|
||||
napi_value StreamNExporter::Write(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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
FILE *filp = nullptr;
|
||||
bool hasPosition = false;
|
||||
size_t position;
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
|
||||
if (!streamEntity || !streamEntity->fp) {
|
||||
UniError(EBADF).ThrowErr(env, "Stream may has been closed");
|
||||
return nullptr;
|
||||
}
|
||||
filp = streamEntity->fp.get();
|
||||
|
||||
unique_ptr<char[]> bufGuard;
|
||||
void *buf = nullptr;
|
||||
size_t len;
|
||||
tie(succ, bufGuard, buf, len, hasPosition, position) =
|
||||
CommonFunc::GetWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
shared_ptr<AsyncWrtieArg> arg;
|
||||
if (bufGuard) {
|
||||
arg = make_shared<AsyncWrtieArg>(move(bufGuard));
|
||||
} else {
|
||||
arg = make_shared<AsyncWrtieArg>(NVal(env, funcArg[NARG_POS::FIRST]));
|
||||
}
|
||||
|
||||
auto cbExec = [arg, buf, len, filp, hasPosition, position](napi_env env) -> UniError {
|
||||
if (hasPosition && (fseek(filp, position, SEEK_SET) == -1)) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return UniError(errno);
|
||||
}
|
||||
arg->actLen = fwrite(buf, 1, len, filp);
|
||||
if (arg->actLen != static_cast<size_t>(len) && ferror(filp)) {
|
||||
return UniError(errno);
|
||||
}
|
||||
return UniError(ERRNO_NOERR);
|
||||
};
|
||||
|
||||
auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
return { NVal::CreateInt64(env, arg->actLen) };
|
||||
};
|
||||
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() != NARG_CNT::THREE) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule("FileIOStreamWrite", cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::THIRD]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOStreamWrite", cbExec, cbCompl).val_;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
struct AsyncReadArg {
|
||||
size_t lenRead { 0 };
|
||||
NRef refReadBuf;
|
||||
int offset { 0 };
|
||||
|
||||
explicit AsyncReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {}
|
||||
~AsyncReadArg() = default;
|
||||
};
|
||||
|
||||
napi_value StreamNExporter::Read(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;
|
||||
}
|
||||
|
||||
/* To get entity */
|
||||
FILE *filp = nullptr;
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
|
||||
if (!streamEntity || !streamEntity->fp) {
|
||||
UniError(EBADF).ThrowErr(env, "Stream may have been closed");
|
||||
return nullptr;
|
||||
} else {
|
||||
filp = streamEntity->fp.get();
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
void *buf = nullptr;
|
||||
int64_t len;
|
||||
bool hasPosition = false;
|
||||
size_t position;
|
||||
int offset;
|
||||
tie(succ, buf, len, hasPosition, position, offset) =
|
||||
CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Failed GetReadArg");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto arg = make_shared<AsyncReadArg>(NVal(env, funcArg[NARG_POS::FIRST]));
|
||||
auto cbExec = [arg, buf, position, filp, len, hasPosition, offset](napi_env env) -> UniError {
|
||||
if (hasPosition && (fseek(filp, position, SEEK_SET) == -1)) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return UniError(errno);
|
||||
}
|
||||
size_t actLen = fread(buf, 1, len, filp);
|
||||
if (actLen != static_cast<size_t>(len) && ferror(filp)) {
|
||||
return UniError(errno);
|
||||
} else {
|
||||
arg->lenRead = actLen;
|
||||
arg->offset = offset;
|
||||
return UniError(ERRNO_NOERR);
|
||||
}
|
||||
};
|
||||
|
||||
auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
NVal obj = NVal::CreateObject(env);
|
||||
obj.AddProp({
|
||||
NVal::DeclareNapiProperty("bytesRead", NVal::CreateInt64(env, arg->lenRead).val_),
|
||||
NVal::DeclareNapiProperty("buffer", arg->refReadBuf.Deref(env).val_),
|
||||
NVal::DeclareNapiProperty("offset", NVal::CreateInt64(env, arg->offset).val_)
|
||||
});
|
||||
return { obj };
|
||||
};
|
||||
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() != NARG_CNT::THREE) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule("FileIOStreamRead", cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::THIRD]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOStreamRead", cbExec, cbCompl).val_;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value StreamNExporter::Close(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, funcArg.GetThisVar());
|
||||
if (!streamEntity || !streamEntity->fp) {
|
||||
UniError(EBADF).ThrowErr(env, "Stream may has been closed");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [streamEntity](napi_env env) -> UniError {
|
||||
auto filp = streamEntity->fp.release();
|
||||
if (!fclose(filp)) {
|
||||
return UniError(ERRNO_NOERR);
|
||||
} else {
|
||||
return UniError(errno);
|
||||
}
|
||||
};
|
||||
|
||||
auto cbCompl = [](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
} else {
|
||||
return NVal::CreateUndefined(env);
|
||||
}
|
||||
};
|
||||
|
||||
string procedureName = "FileIOStreamClose";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::ZERO) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::FIRST]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
unique_ptr<StreamEntity> streamEntity = make_unique<StreamEntity>();
|
||||
if (!NClass::SetEntityFor<StreamEntity>(env, funcArg.GetThisVar(), move(streamEntity))) {
|
||||
UniError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj stream");
|
||||
return nullptr;
|
||||
}
|
||||
return funcArg.GetThisVar();
|
||||
}
|
||||
|
||||
bool StreamNExporter::Export()
|
||||
{
|
||||
vector<napi_property_descriptor> props = {
|
||||
NVal::DeclareNapiFunction("writeSync", WriteSync),
|
||||
NVal::DeclareNapiFunction("flush", Flush::Async),
|
||||
NVal::DeclareNapiFunction("flushSync", Flush::Sync),
|
||||
NVal::DeclareNapiFunction("readSync", ReadSync),
|
||||
NVal::DeclareNapiFunction("closeSync", CloseSync),
|
||||
NVal::DeclareNapiFunction("write", Write),
|
||||
NVal::DeclareNapiFunction("read", Read),
|
||||
NVal::DeclareNapiFunction("close", Close),
|
||||
};
|
||||
|
||||
string className = GetClassName();
|
||||
bool succ = false;
|
||||
napi_value cls = nullptr;
|
||||
tie(succ, cls) = NClass::DefineClass(exports_.env_, className, StreamNExporter::Constructor, move(props));
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class");
|
||||
return false;
|
||||
}
|
||||
succ = NClass::SaveClass(exports_.env_, className, cls);
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class");
|
||||
return false;
|
||||
}
|
||||
|
||||
return exports_.AddProp(className, cls);
|
||||
}
|
||||
|
||||
string StreamNExporter::GetClassName()
|
||||
{
|
||||
return StreamNExporter::className_;
|
||||
}
|
||||
|
||||
StreamNExporter::StreamNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
|
||||
|
||||
StreamNExporter::~StreamNExporter() {}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_STREAM_STREAM_N_EXPORTER_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_STREAM_STREAM_N_EXPORTER_H
|
||||
|
||||
#include "../../common/napi/n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class StreamNExporter final : public NExporter {
|
||||
public:
|
||||
inline static const std::string className_ = "Stream";
|
||||
|
||||
bool Export() override;
|
||||
std::string GetClassName() override;
|
||||
|
||||
static napi_value Constructor(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
static napi_value WriteSync(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value FlushSync(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value ReadSync(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value CloseSync(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
static napi_value Write(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value Read(napi_env env, napi_callback_info cbinfo);
|
||||
static napi_value Close(napi_env env, napi_callback_info cbinfo);
|
||||
|
||||
StreamNExporter(napi_env env, napi_value exports);
|
||||
~StreamNExporter() override;
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_WATCHER_WATCHER_ENTITY_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H
|
||||
|
||||
#include "../../common/napi/uni_header.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class WatcherHandleDeleter {
|
||||
public:
|
||||
void operator()(uv_fs_event_t *ptr)
|
||||
{
|
||||
uv_fs_event_stop(ptr);
|
||||
uv_handle_t *handle = reinterpret_cast<uv_handle_t *>(ptr);
|
||||
uv_close(handle, [](uv_handle_t *handle) { delete handle; });
|
||||
}
|
||||
};
|
||||
|
||||
struct WatcherInforArg {
|
||||
int events = 0;
|
||||
napi_env env = nullptr;
|
||||
napi_ref ref = nullptr;
|
||||
};
|
||||
|
||||
struct WatcherEntity {
|
||||
std::unique_ptr<WatcherInforArg> data_;
|
||||
std::unique_ptr<uv_fs_event_t, WatcherHandleDeleter> fsEventReq_;
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,146 +0,0 @@
|
||||
/*
|
||||
* 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 "watcher_n_exporter.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
|
||||
#include "securec.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_class.h"
|
||||
#include "../../common/napi/n_func_arg.h"
|
||||
#include "../../common/uni_error.h"
|
||||
#include "watcher_entity.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
unique_ptr<WatcherEntity> watcherEntity = make_unique<WatcherEntity>();
|
||||
if (!NClass::SetEntityFor<WatcherEntity>(env, funcArg.GetThisVar(), move(watcherEntity))) {
|
||||
UniError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj watcher");
|
||||
return nullptr;
|
||||
}
|
||||
return funcArg.GetThisVar();
|
||||
}
|
||||
|
||||
napi_value WatcherNExporter::StopSync(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto watchEntity = NClass::GetEntityOf<WatcherEntity>(env, funcArg.GetThisVar());
|
||||
if (!watchEntity) {
|
||||
UniError(EINVAL).ThrowErr(env, "get watcherEntity fail");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
watchEntity->fsEventReq_.reset();
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value WatcherNExporter::Stop(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto watchEntity = NClass::GetEntityOf<WatcherEntity>(env, funcArg.GetThisVar());
|
||||
if (!watchEntity) {
|
||||
UniError(EINVAL).ThrowErr(env, "get watcherEntity fail");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [watchEntity](napi_env env) -> UniError {
|
||||
watchEntity->fsEventReq_.reset();
|
||||
return UniError(ERRNO_NOERR);
|
||||
};
|
||||
|
||||
auto cbCompl = [](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
return { NVal::CreateUndefined(env) };
|
||||
};
|
||||
|
||||
string procedureName = "FileIOCreaterWatcher";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::ZERO) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::FIRST]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
|
||||
bool WatcherNExporter::Export()
|
||||
{
|
||||
vector<napi_property_descriptor> props = {
|
||||
NVal::DeclareNapiFunction("stop", Stop),
|
||||
NVal::DeclareNapiFunction("stopSync", StopSync),
|
||||
};
|
||||
|
||||
string className = GetClassName();
|
||||
bool succ = false;
|
||||
napi_value classValue = nullptr;
|
||||
tie(succ, classValue) = NClass::DefineClass(exports_.env_,
|
||||
className,
|
||||
WatcherNExporter::Constructor,
|
||||
std::move(props));
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class");
|
||||
return false;
|
||||
}
|
||||
succ = NClass::SaveClass(exports_.env_, className, classValue);
|
||||
if (!succ) {
|
||||
UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class");
|
||||
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 ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* 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_CLASS_WATCHER_WATCHER_N_EXPORTER_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H
|
||||
|
||||
#include<uv.h>
|
||||
|
||||
#include "../../common/napi/n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class WatcherNExporter final : public NExporter {
|
||||
public:
|
||||
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 Stop(napi_env env, napi_callback_info info);
|
||||
static napi_value StopSync(napi_env env, napi_callback_info info);
|
||||
|
||||
WatcherNExporter(napi_env env, napi_value exports);
|
||||
~WatcherNExporter() override;
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,262 +0,0 @@
|
||||
/*
|
||||
* 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 "common_func.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../common/log.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"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
static tuple<bool, void *, int> GetActualBuf(napi_env env, void *rawBuf, int64_t bufLen, NVal op)
|
||||
{
|
||||
bool succ = false;
|
||||
void *realBuf = nullptr;
|
||||
int64_t opOffset = 0;
|
||||
if (op.HasProp("offset")) {
|
||||
tie(succ, opOffset) = op.GetProp("offset").ToInt64();
|
||||
if (!succ || opOffset < 0) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid option.offset, positive integer is desired");
|
||||
return { false, nullptr, opOffset };
|
||||
} else if (opOffset > bufLen) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid option.offset, buffer limit exceeded");
|
||||
return { false, nullptr, opOffset };
|
||||
} else {
|
||||
realBuf = static_cast<uint8_t *>(rawBuf) + opOffset;
|
||||
}
|
||||
} else {
|
||||
realBuf = rawBuf;
|
||||
}
|
||||
|
||||
return { true, realBuf, opOffset };
|
||||
}
|
||||
|
||||
static tuple<bool, size_t> GetActualLen(napi_env env, int64_t bufLen, int64_t bufOff, NVal op)
|
||||
{
|
||||
bool succ = false;
|
||||
int64_t retLen;
|
||||
|
||||
if (op.HasProp("length")) {
|
||||
int64_t opLength;
|
||||
tie(succ, opLength) = op.GetProp("length").ToInt64();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid option.length, expect integer");
|
||||
return { false, 0 };
|
||||
}
|
||||
if (opLength < 0) {
|
||||
retLen = bufLen - bufOff;
|
||||
} else if (opLength + bufOff > bufLen) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid option.length, buffer limit exceeded");
|
||||
return { false, 0 };
|
||||
} else {
|
||||
retLen = opLength;
|
||||
}
|
||||
} else {
|
||||
retLen = bufLen - bufOff;
|
||||
}
|
||||
|
||||
return { true, retLen };
|
||||
}
|
||||
|
||||
int CommonFunc::ConvertJsFlags(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;
|
||||
|
||||
int flagsABI = 0;
|
||||
flagsABI |= ((flags & USR_O_RDONLY) == USR_O_RDONLY) ? O_RDONLY : 0;
|
||||
flagsABI |= ((flags & USR_O_WRONLY) == USR_O_WRONLY) ? O_WRONLY : 0;
|
||||
flagsABI |= ((flags & USR_O_RDWR) == USR_O_RDWR) ? O_RDWR : 0;
|
||||
flagsABI |= ((flags & USR_O_CREAT) == USR_O_CREAT) ? O_CREAT : 0;
|
||||
flagsABI |= ((flags & USR_O_EXCL) == USR_O_EXCL) ? O_EXCL : 0;
|
||||
flagsABI |= ((flags & USR_O_TRUNC) == USR_O_TRUNC) ? O_TRUNC : 0;
|
||||
flagsABI |= ((flags & USR_O_APPEND) == USR_O_APPEND) ? O_APPEND : 0;
|
||||
flagsABI |= ((flags & USR_O_NONBLOCK) == USR_O_NONBLOCK) ? O_NONBLOCK : 0;
|
||||
flagsABI |= ((flags & USR_O_DIRECTORY) == USR_O_DIRECTORY) ? O_DIRECTORY : 0;
|
||||
flagsABI |= ((flags & USR_O_NOFOLLOW) == USR_O_NOFOLLOW) ? O_NOFOLLOW : 0;
|
||||
flagsABI |= ((flags & USR_O_SYNC) == USR_O_SYNC) ? O_SYNC : 0;
|
||||
flags = flagsABI;
|
||||
return flagsABI;
|
||||
}
|
||||
|
||||
tuple<bool, unique_ptr<char[]>, unique_ptr<char[]>> CommonFunc::GetCopyPathArg(napi_env env,
|
||||
napi_value srcPath,
|
||||
napi_value dstPath)
|
||||
{
|
||||
bool succ = false;
|
||||
unique_ptr<char[]> src;
|
||||
tie(succ, src, ignore) = NVal(env, srcPath).ToUTF8String();
|
||||
if (!succ) {
|
||||
return { false, nullptr, nullptr };
|
||||
}
|
||||
|
||||
unique_ptr<char[]> dest;
|
||||
tie(succ, dest, ignore) = NVal(env, dstPath).ToUTF8String();
|
||||
if (!succ) {
|
||||
return { false, nullptr, nullptr };
|
||||
}
|
||||
return make_tuple(succ, move(src), move(dest));
|
||||
}
|
||||
|
||||
tuple<bool, void *, int64_t, bool, int64_t, int> CommonFunc::GetReadArg(napi_env env,
|
||||
napi_value readBuf,
|
||||
napi_value option)
|
||||
{
|
||||
bool succ = false;
|
||||
void *retBuf = nullptr;
|
||||
int64_t retLen;
|
||||
bool posAssigned = false;
|
||||
int64_t position;
|
||||
|
||||
NVal txt(env, readBuf);
|
||||
void *buf = nullptr;
|
||||
int64_t bufLen;
|
||||
int offset = 0;
|
||||
tie(succ, buf, bufLen) = txt.ToArraybuffer();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid read buffer, expect arraybuffer");
|
||||
return { false, nullptr, 0, posAssigned, position, offset };
|
||||
}
|
||||
|
||||
NVal op = NVal(env, option);
|
||||
tie(succ, retBuf, offset) = GetActualBuf(env, buf, bufLen, op);
|
||||
if (!succ) {
|
||||
return { false, nullptr, 0, posAssigned, position, offset };
|
||||
}
|
||||
|
||||
int64_t bufOff = static_cast<uint8_t *>(retBuf) - static_cast<uint8_t *>(buf);
|
||||
tie(succ, retLen) = GetActualLen(env, bufLen, bufOff, op);
|
||||
if (!succ) {
|
||||
return { false, nullptr, 0, posAssigned, position, offset };
|
||||
}
|
||||
|
||||
if (op.HasProp("position")) {
|
||||
tie(succ, position) = op.GetProp("position").ToInt64();
|
||||
if (succ && position >= 0) {
|
||||
posAssigned = true;
|
||||
} else {
|
||||
UniError(EINVAL).ThrowErr(env, "option.position shall be positive number");
|
||||
return { false, nullptr, 0, posAssigned, position, offset };
|
||||
}
|
||||
}
|
||||
|
||||
return { true, retBuf, retLen, posAssigned, position, offset };
|
||||
}
|
||||
|
||||
static tuple<bool, unique_ptr<char[]>, int64_t> DecodeString(napi_env env, NVal jsStr, NVal encoding)
|
||||
{
|
||||
unique_ptr<char[]> buf;
|
||||
if (!jsStr.TypeIs(napi_string)) {
|
||||
return { false, nullptr, 0 };
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
if (!encoding) {
|
||||
return jsStr.ToUTF8String();
|
||||
}
|
||||
|
||||
unique_ptr<char[]> encodingBuf;
|
||||
tie(succ, encodingBuf, ignore) = encoding.ToUTF8String();
|
||||
if (!succ) {
|
||||
return { false, nullptr, 0 };
|
||||
}
|
||||
string encodingStr(encodingBuf.release());
|
||||
if (encodingStr == "utf-8") {
|
||||
return jsStr.ToUTF8String();
|
||||
} else if (encodingStr == "utf-16") {
|
||||
return jsStr.ToUTF16String();
|
||||
} else {
|
||||
return { false, nullptr, 0 };
|
||||
}
|
||||
}
|
||||
|
||||
// Is everything ok? Do we need to free memory? What's the three args required by fwrite? Where to start writing?
|
||||
tuple<bool, unique_ptr<char[]>, void *, int64_t, bool, int64_t> CommonFunc::GetWriteArg(napi_env env,
|
||||
napi_value argWBuf,
|
||||
napi_value argOption)
|
||||
{
|
||||
void *retBuf = nullptr;
|
||||
int64_t retLen;
|
||||
bool hasPos = false;
|
||||
int64_t retPos;
|
||||
|
||||
/* To get write buffer */
|
||||
bool succ = false;
|
||||
void *buf = nullptr;
|
||||
int64_t bufLen;
|
||||
NVal op(env, argOption);
|
||||
NVal jsBuffer(env, argWBuf);
|
||||
unique_ptr<char[]> bufferGuard;
|
||||
tie(succ, bufferGuard, bufLen) = DecodeString(env, jsBuffer, op.GetProp("encoding"));
|
||||
if (!succ) {
|
||||
tie(succ, buf, bufLen) = NVal(env, argWBuf).ToArraybuffer();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Illegal write buffer or encoding");
|
||||
return { false, nullptr, nullptr, 0, hasPos, retPos };
|
||||
}
|
||||
} else {
|
||||
buf = bufferGuard.get();
|
||||
}
|
||||
|
||||
tie(succ, retBuf, ignore) = GetActualBuf(env, buf, bufLen, op);
|
||||
if (!succ) {
|
||||
return { false, nullptr, nullptr, 0, hasPos, retPos };
|
||||
}
|
||||
|
||||
int64_t bufOff = static_cast<uint8_t *>(retBuf) - static_cast<uint8_t *>(buf);
|
||||
tie(succ, retLen) = GetActualLen(env, bufLen, bufOff, op);
|
||||
if (!succ) {
|
||||
return { false, nullptr, nullptr, 0, hasPos, retPos };
|
||||
}
|
||||
|
||||
/* To parse options - Where to begin writing */
|
||||
if (op.HasProp("position")) {
|
||||
int32_t position = 0;
|
||||
tie(succ, position) = op.GetProp("position").ToInt32();
|
||||
if (!succ || position < 0) {
|
||||
UniError(EINVAL).ThrowErr(env, "option.position shall be positive number");
|
||||
return { false, nullptr, nullptr, 0, hasPos, retPos };
|
||||
}
|
||||
hasPos = true;
|
||||
retPos = position;
|
||||
} else {
|
||||
retPos = INVALID_POSITION;
|
||||
}
|
||||
return { true, move(bufferGuard), retBuf, retLen, hasPos, retPos };
|
||||
}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* 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_COMMON_FUNC_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_COMMON_FUNC_H
|
||||
|
||||
#include "../common/napi/uni_header.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
constexpr int64_t INVALID_POSITION = std::numeric_limits<decltype(INVALID_POSITION)>::max();
|
||||
|
||||
struct CommonFunc {
|
||||
static int ConvertJsFlags(int &flags);
|
||||
static std::tuple<bool, void *, int64_t, bool, int64_t, int> GetReadArg(napi_env env,
|
||||
napi_value readBuf,
|
||||
napi_value option);
|
||||
static std::tuple<bool, std::unique_ptr<char[]>, void *, int64_t, bool, int64_t> GetWriteArg(napi_env env,
|
||||
napi_value argWBuf,
|
||||
napi_value argOption);
|
||||
static std::tuple<bool, std::unique_ptr<char[]>, std::unique_ptr<char[]>> GetCopyPathArg(napi_env env,
|
||||
napi_value srcPath,
|
||||
napi_value dstPath);
|
||||
};
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* 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 <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "../common/log.h"
|
||||
#include "class_constants/constants.h"
|
||||
#include "class_dir/dir_n_exporter.h"
|
||||
#include "class_dirent/dirent_n_exporter.h"
|
||||
#include "class_randomaccessfile/randomaccessfile_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 "properties/prop_n_exporter.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
static napi_value Export(napi_env env, napi_value exports)
|
||||
{
|
||||
std::vector<unique_ptr<NExporter>> products;
|
||||
products.emplace_back(make_unique<PropNExporter>(env, exports));
|
||||
products.emplace_back(make_unique<DirentNExporter>(env, exports));
|
||||
products.emplace_back(make_unique<DirNExporter>(env, exports));
|
||||
products.emplace_back(make_unique<RandomAccessFileNExporter>(env, exports));
|
||||
products.emplace_back(make_unique<StatNExporter>(env, exports));
|
||||
products.emplace_back(make_unique<StreamNExporter>(env, exports));
|
||||
products.emplace_back(make_unique<WatcherNExporter>(env, exports));
|
||||
products.emplace_back(make_unique<Constants>(env, exports));
|
||||
|
||||
for (auto &&product : products) {
|
||||
if (!product->Export()) {
|
||||
HILOGE("INNER BUG. Failed to export class %{public}s for module fileio", product->GetClassName().c_str());
|
||||
return nullptr;
|
||||
} else {
|
||||
HILOGE("Class %{public}s for module fileio has been exported", product->GetClassName().c_str());
|
||||
}
|
||||
}
|
||||
return exports;
|
||||
}
|
||||
|
||||
NAPI_MODULE(fileio, Export)
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,112 +0,0 @@
|
||||
/*
|
||||
* 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 "chmod.h"
|
||||
#include <cstring>
|
||||
#include <sys/stat.h>
|
||||
#include <tuple>
|
||||
#include <unistd.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 Chmod::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
unique_ptr<char[]> path;
|
||||
tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid path");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int mode;
|
||||
tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid mode");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (chmod(path.get(), mode) == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value Chmod::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;
|
||||
}
|
||||
|
||||
string path;
|
||||
unique_ptr<char[]> tmp;
|
||||
bool succ = false;
|
||||
tie(succ, tmp, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid path");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
path = tmp.get();
|
||||
int mode;
|
||||
size_t argc = funcArg.GetArgc();
|
||||
tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid mode");
|
||||
}
|
||||
|
||||
auto cbExec = [path, mode](napi_env env) -> UniError {
|
||||
if (chmod(path.c_str(), mode) == -1) {
|
||||
return UniError(errno);
|
||||
} else {
|
||||
return UniError(ERRNO_NOERR);
|
||||
}
|
||||
};
|
||||
auto cbComplete = [](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
return { NVal::CreateUndefined(env) };
|
||||
};
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
|
||||
string procedureName = "FileIOChmod";
|
||||
if (argc == NARG_CNT::TWO) {
|
||||
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_;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_CHMOD_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_CHMOD_H
|
||||
|
||||
#include "../../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Chmod 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
|
||||
@@ -1,126 +0,0 @@
|
||||
/*
|
||||
* 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 "chown.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <tuple>
|
||||
#include <unistd.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;
|
||||
|
||||
static tuple<bool, string, int, int> GetChownArg(napi_env env, const NFuncArg &funcArg)
|
||||
{
|
||||
bool succ = false;
|
||||
unique_ptr<char[]> path;
|
||||
tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid path");
|
||||
return { false, "", -1, -1 };
|
||||
}
|
||||
|
||||
int owner;
|
||||
tie(succ, owner) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid owner");
|
||||
return { false, "", -1, -1 };
|
||||
}
|
||||
|
||||
int group;
|
||||
tie(succ, group) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid group");
|
||||
return { false, "", -1, -1 };
|
||||
}
|
||||
return { succ, path.get(), owner, group };
|
||||
}
|
||||
|
||||
napi_value Chown::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int owner;
|
||||
int group;
|
||||
string path;
|
||||
tie(succ, path, owner, group) = GetChownArg(env, funcArg);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (chown(path.c_str(), owner, group) == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value Chown::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int owner;
|
||||
int group;
|
||||
string path;
|
||||
tie(succ, path, owner, group) = GetChownArg(env, funcArg);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [path, owner, group](napi_env env) -> UniError {
|
||||
if (chown(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) };
|
||||
};
|
||||
|
||||
string procedureName = "FileIOChown";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::THREE) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
int cbIdx = NARG_POS::FOURTH;
|
||||
NVal cb(env, funcArg[cbIdx]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_CHOWN_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_CHOWN_H
|
||||
|
||||
#include "../../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Chown 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
|
||||
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
* 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 "close.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <tuple>
|
||||
#include <unistd.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 Close::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (close(fd) == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value Close::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;
|
||||
}
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [fd](napi_env env) -> UniError {
|
||||
int ret = close(fd);
|
||||
if (ret == -1) {
|
||||
return UniError(errno);
|
||||
} else {
|
||||
return UniError(ERRNO_NOERR);
|
||||
}
|
||||
};
|
||||
auto cbComplete = [](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
} else {
|
||||
return NVal::CreateUndefined(env);
|
||||
}
|
||||
};
|
||||
string procedureName = "FileIOClose";
|
||||
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
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_CLOSE_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_CLOSE_H
|
||||
|
||||
#include "../../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Close 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
|
||||
@@ -1,211 +0,0 @@
|
||||
/*
|
||||
* 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 "copy_file.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <tuple>
|
||||
#include <unistd.h>
|
||||
|
||||
#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"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
struct FileInfo {
|
||||
bool isPath = false;
|
||||
unique_ptr<char[]> path;
|
||||
FDGuard fdg;
|
||||
};
|
||||
|
||||
static UniError CopyFileCore(FileInfo &srcFile, FileInfo &destFile)
|
||||
{
|
||||
int res = EINVAL;
|
||||
if (srcFile.isPath) {
|
||||
srcFile.fdg.SetFD(open(srcFile.path.get(), O_RDONLY), true);
|
||||
res = errno;
|
||||
}
|
||||
if (!srcFile.fdg) {
|
||||
return UniError(res);
|
||||
}
|
||||
struct stat statbf;
|
||||
if (fstat(srcFile.fdg.GetFD(), &statbf) == -1) {
|
||||
return UniError(errno);
|
||||
}
|
||||
|
||||
if (destFile.isPath) {
|
||||
destFile.fdg.SetFD(open(destFile.path.get(), O_WRONLY | O_CREAT, statbf.st_mode), true);
|
||||
res = errno;
|
||||
}
|
||||
if (!destFile.fdg) {
|
||||
return UniError(res);
|
||||
}
|
||||
|
||||
int block = 4096;
|
||||
auto copyBuf = make_unique<char[]>(block);
|
||||
do {
|
||||
ssize_t readSize = read(srcFile.fdg.GetFD(), copyBuf.get(), block);
|
||||
if (readSize == -1) {
|
||||
return UniError(errno);
|
||||
} else if (readSize == 0) {
|
||||
break;
|
||||
}
|
||||
ssize_t writeSize = write(destFile.fdg.GetFD(), copyBuf.get(), readSize);
|
||||
if (writeSize != readSize) {
|
||||
return UniError(errno);
|
||||
}
|
||||
if (readSize != block) {
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
return UniError(ERRNO_NOERR);
|
||||
}
|
||||
|
||||
static tuple<bool, int, bool> ParseJsModeAndProm(napi_env env, const NFuncArg &funcArg)
|
||||
{
|
||||
bool succ = false;
|
||||
bool promise = false;
|
||||
bool hasMode = false;
|
||||
int mode = 0;
|
||||
if (funcArg.GetArgc() == NARG_CNT::THREE && NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number)) {
|
||||
promise = true;
|
||||
hasMode = true;
|
||||
} else if (funcArg.GetArgc() == NARG_CNT::FOUR) {
|
||||
hasMode = true;
|
||||
}
|
||||
if (hasMode) {
|
||||
tie(succ, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
|
||||
if (!succ) {
|
||||
return {false, mode, promise};
|
||||
}
|
||||
}
|
||||
return {true, mode, promise};
|
||||
}
|
||||
|
||||
static tuple<bool, FileInfo> ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg)
|
||||
{
|
||||
auto [isPath, path, ignore] = pathOrFdFromJsArg.ToUTF8String();
|
||||
if (isPath) {
|
||||
return {true, FileInfo{true, move(path), {}}};
|
||||
}
|
||||
|
||||
auto [isFd, fd] = pathOrFdFromJsArg.ToInt32();
|
||||
if (isFd) {
|
||||
return {true, FileInfo{false, {}, {fd, false}}};
|
||||
}
|
||||
|
||||
return {false, FileInfo{false, {}, {}}};
|
||||
};
|
||||
|
||||
napi_value CopyFile::Sync(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 [succSrc, src] = ParseJsOperand(env, {env, funcArg[NARG_POS::FIRST]});
|
||||
auto [succDest, dest] = ParseJsOperand(env, {env, funcArg[NARG_POS::SECOND]});
|
||||
if (!succSrc || !succDest) {
|
||||
UniError(EINVAL).ThrowErr(env, "The first/second argument requires filepath/fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto [succMode, mode, ignore] = ParseJsModeAndProm(env, funcArg);
|
||||
if (!succMode) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid mode");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto err = CopyFileCore(src, dest);
|
||||
if (err) {
|
||||
if (err.GetErrno(ERR_CODE_SYSTEM_POSIX) == ENAMETOOLONG) {
|
||||
UniError(EINVAL).ThrowErr(env, "Filename too long");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
err.ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
class Para {
|
||||
public:
|
||||
FileInfo src_;
|
||||
FileInfo dest_;
|
||||
|
||||
Para(FileInfo src, FileInfo dest) : src_(move(src)), dest_(move(dest)) {};
|
||||
};
|
||||
|
||||
napi_value CopyFile::Async(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto [succSrc, src] = ParseJsOperand(env, {env, funcArg[NARG_POS::FIRST]});
|
||||
auto [succDest, dest] = ParseJsOperand(env, {env, funcArg[NARG_POS::SECOND]});
|
||||
if (!succSrc || !succDest) {
|
||||
UniError(EINVAL).ThrowErr(env, "The first/second argument requires filepath/fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto [succMode, mode, promise] = ParseJsModeAndProm(env, funcArg);
|
||||
if (!succMode) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid mode");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [para = make_shared<Para>(move(src), move(dest))](napi_env env) -> UniError {
|
||||
return CopyFileCore(para->src_, para->dest_);
|
||||
};
|
||||
|
||||
auto cbCompl = [](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
if (err.GetErrno(ERR_CODE_SYSTEM_POSIX) == ENAMETOOLONG) {
|
||||
return {env, err.GetNapiErr(env, "Filename too long")};
|
||||
}
|
||||
return {env, err.GetNapiErr(env)};
|
||||
}
|
||||
return {NVal::CreateUndefined(env)};
|
||||
};
|
||||
|
||||
string procedureName = "FileIOCopyFile";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::TWO || promise) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH)]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_COPY_FILE_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_COPY_FILE_H
|
||||
|
||||
#include "../../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class CopyFile 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
|
||||
@@ -1,213 +0,0 @@
|
||||
/*
|
||||
* 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 "create_randomaccessfile.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <uv.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_class.h"
|
||||
#include "../../common/napi/n_func_arg.h"
|
||||
#include "../../common/uni_error.h"
|
||||
#include "../common_func.h"
|
||||
|
||||
#include "../class_randomaccessfile/randomaccessfile_entity.h"
|
||||
#include "../class_randomaccessfile/randomaccessfile_n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
struct FileInfo {
|
||||
bool isPath = false;
|
||||
unique_ptr<char[]> path;
|
||||
FDGuard fdg;
|
||||
};
|
||||
|
||||
static bool CheckFilePath(napi_env env, string path)
|
||||
{
|
||||
if (access(path.c_str(), 0) == 0) {
|
||||
struct stat fileStat;
|
||||
int ret = stat(path.c_str(), &fileStat);
|
||||
if (ret != 0) {
|
||||
UniError(errno).ThrowErr(env, "Cannot stat filepath");
|
||||
return false;
|
||||
}
|
||||
if ((fileStat.st_mode & S_IFMT) != S_IFREG) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid filepath");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static tuple<bool, FileInfo, size_t> ParseJsFileAndFP(napi_env env, napi_value pathOrFdFromJsArg, napi_value FPFromJs)
|
||||
{
|
||||
auto [succ, fp] = NVal(env, FPFromJs).ToInt32();
|
||||
if (succ) {
|
||||
auto [isPath, path, ignore] = NVal(env, pathOrFdFromJsArg).ToUTF8String();
|
||||
if (isPath) {
|
||||
if (CheckFilePath(env, string(path.get()))) {
|
||||
return { true, FileInfo { true, move(path), {} }, fp };
|
||||
}
|
||||
return { false, FileInfo { false, {}, {} }, -1 };
|
||||
}
|
||||
auto [isFd, fd] = NVal(env, pathOrFdFromJsArg).ToInt32();
|
||||
if (isFd) {
|
||||
if (fd < 0) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return { false, FileInfo { false, {}, {} }, -1 };
|
||||
}
|
||||
return { true, FileInfo { false, {}, { fd, false } }, fp };
|
||||
}
|
||||
UniError(EINVAL).ThrowErr(env, "The first argument requires filepath/fd");
|
||||
}
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fp");
|
||||
return { false, FileInfo { false, {}, {} }, -1 };
|
||||
};
|
||||
|
||||
static tuple<bool, int> GetJsFlags(napi_env env, const NFuncArg &funcArg, FileInfo &fileInfo)
|
||||
{
|
||||
int flags = O_RDONLY;
|
||||
bool succ = false;
|
||||
if (fileInfo.isPath) {
|
||||
if (funcArg.GetArgc() >= NARG_CNT::THREE && !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) {
|
||||
tie(succ, flags) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid flags");
|
||||
return { false, flags };
|
||||
}
|
||||
(void)CommonFunc::ConvertJsFlags(flags);
|
||||
}
|
||||
}
|
||||
return { true, flags };
|
||||
}
|
||||
|
||||
static NVal InstantiateRandomAccessFile(napi_env env, int fd, size_t fp)
|
||||
{
|
||||
napi_value objRAF = NClass::InstantiateClass(env, RandomAccessFileNExporter::className_, {});
|
||||
if (!objRAF) {
|
||||
UniError(EIO).ThrowErr(env, "Cannot instantiate randomaccessfile");
|
||||
return NVal();
|
||||
}
|
||||
|
||||
auto rafEntity = NClass::GetEntityOf<RandomAccessFileEntity>(env, objRAF);
|
||||
if (!rafEntity) {
|
||||
UniError(EIO).ThrowErr(env, "Cannot instantiate randomaccessfile because of void entity");
|
||||
return NVal();
|
||||
}
|
||||
auto fdg = make_unique<FDGuard>(fd);
|
||||
rafEntity->fd_.swap(fdg);
|
||||
rafEntity->fpointer = fp;
|
||||
return { env, objRAF };
|
||||
}
|
||||
|
||||
napi_value CreateRandomAccessFile::Sync(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, fileInfo, fp] = ParseJsFileAndFP(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (fileInfo.isPath) {
|
||||
auto [succFlags, flags] = GetJsFlags(env, funcArg, fileInfo);
|
||||
if (!succFlags) {
|
||||
return nullptr;
|
||||
}
|
||||
uv_loop_s *loop = nullptr;
|
||||
napi_get_uv_event_loop(env, &loop);
|
||||
uv_fs_t open_req;
|
||||
int ret = uv_fs_open(loop, &open_req, fileInfo.path.get(), flags, S_IRUSR |
|
||||
S_IWUSR | S_IRGRP | S_IWGRP, NULL);
|
||||
if (ret < 0) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
fileInfo.fdg.SetFD(open_req.result, false);
|
||||
uv_fs_req_cleanup(&open_req);
|
||||
}
|
||||
return InstantiateRandomAccessFile(env, fileInfo.fdg.GetFD(), fp).val_;
|
||||
}
|
||||
|
||||
struct AsyncCreateRandomAccessFileArg {
|
||||
int fd;
|
||||
size_t fp;
|
||||
};
|
||||
|
||||
napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info)
|
||||
{
|
||||
NFuncArg funcArg(env, info);
|
||||
if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) {
|
||||
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
|
||||
return nullptr;
|
||||
}
|
||||
auto [succ, fileInfo, fp] = ParseJsFileAndFP(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
auto [succFlags, flags] = GetJsFlags(env, funcArg, fileInfo);
|
||||
if (!succFlags) {
|
||||
return nullptr;
|
||||
}
|
||||
auto arg = make_shared<AsyncCreateRandomAccessFileArg>();
|
||||
auto cbExec = [arg, fileInfo = make_shared<FileInfo>(move(fileInfo)), fp = fp, flags =
|
||||
flags](napi_env env) -> UniError {
|
||||
if (fileInfo->isPath) {
|
||||
uv_loop_s *loop = nullptr;
|
||||
napi_get_uv_event_loop(env, &loop);
|
||||
uv_fs_t open_req;
|
||||
int ret = uv_fs_open(loop, &open_req, fileInfo->path.get(), flags, S_IRUSR |
|
||||
S_IWUSR | S_IRGRP | S_IWGRP, NULL);
|
||||
if (ret < 0) {
|
||||
return UniError(errno);
|
||||
}
|
||||
fileInfo->fdg.SetFD(open_req.result, false);
|
||||
uv_fs_req_cleanup(&open_req);
|
||||
}
|
||||
arg->fd = fileInfo->fdg.GetFD();
|
||||
arg->fp = fp;
|
||||
return UniError(ERRNO_NOERR);
|
||||
};
|
||||
auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
return InstantiateRandomAccessFile(env, arg->fd, arg->fp);
|
||||
};
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE &&
|
||||
NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number))) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(createRAFProcedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
int cbIdx = ((funcArg.GetArgc() == NARG_CNT::FOUR) ? NARG_POS::FOURTH : NARG_POS::THIRD);
|
||||
NVal cb(env, funcArg[cbIdx]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(createRAFProcedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* 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_FILEIO_PROPERTIES_CREATE_RANDOMACCESSFILE_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_CREATE_RANDOMACCESSFILE_H
|
||||
|
||||
#include "../../common/napi/uni_header.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class CreateRandomAccessFile final {
|
||||
public:
|
||||
static napi_value Async(napi_env env, napi_callback_info info);
|
||||
static napi_value Sync(napi_env env, napi_callback_info info);
|
||||
};
|
||||
const std::string createRAFProcedureName = "FileIOCreateRandomAccessFile";
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -1,144 +0,0 @@
|
||||
/*
|
||||
* 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 "create_stream.h"
|
||||
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
|
||||
#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_stream/stream_entity.h"
|
||||
#include "../class_stream/stream_n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
static NVal InstantiateStream(napi_env env, unique_ptr<FILE, decltype(&fclose)> fp)
|
||||
{
|
||||
napi_value objStream = NClass::InstantiateClass(env, StreamNExporter::className_, {});
|
||||
if (!objStream) {
|
||||
UniError(EIO).ThrowErr(env, "INNER BUG. Cannot instantiate stream");
|
||||
return NVal();
|
||||
}
|
||||
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, objStream);
|
||||
if (!streamEntity) {
|
||||
UniError(EIO).ThrowErr(env, "Cannot instantiate stream because of void entity");
|
||||
return NVal();
|
||||
}
|
||||
|
||||
streamEntity->fp.swap(fp);
|
||||
return { env, objStream };
|
||||
}
|
||||
|
||||
static tuple<bool, string, string> GetCreateStreamArgs(napi_env env, const NFuncArg &funcArg)
|
||||
{
|
||||
bool succ = false;
|
||||
unique_ptr<char[]> path;
|
||||
tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid path");
|
||||
return { false, "", "" };
|
||||
}
|
||||
|
||||
unique_ptr<char[]> mode;
|
||||
tie(succ, mode, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid mode");
|
||||
return { false, "", "" };
|
||||
}
|
||||
|
||||
return { true, path.get(), mode.get() };
|
||||
}
|
||||
|
||||
napi_value CreateStream::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
string argPath;
|
||||
string argMode;
|
||||
tie(succ, argPath, argMode) = GetCreateStreamArgs(env, funcArg);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
unique_ptr<FILE, decltype(&fclose)> fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
|
||||
if (!fp) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
return InstantiateStream(env, move(fp)).val_;
|
||||
}
|
||||
|
||||
struct AsyncCreateStreamArg {
|
||||
unique_ptr<FILE, decltype(&fclose)> fp = { nullptr, fclose };
|
||||
};
|
||||
|
||||
napi_value CreateStream::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
string argPath;
|
||||
string argMode;
|
||||
tie(succ, argPath, argMode) = GetCreateStreamArgs(env, funcArg);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto arg = make_shared<AsyncCreateStreamArg>();
|
||||
auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)](napi_env env) -> UniError {
|
||||
arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose };
|
||||
if (!arg->fp) {
|
||||
return UniError(errno);
|
||||
} else {
|
||||
return UniError(ERRNO_NOERR);
|
||||
}
|
||||
};
|
||||
auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
return InstantiateStream(env, move(arg->fp));
|
||||
};
|
||||
|
||||
string procedureName = "FileIOCreateStream";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::TWO) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::THIRD]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_CREATE_STREAM_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_CREATE_STREAM_H
|
||||
|
||||
#include "../../common/napi/uni_header.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class CreateStream 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
|
||||
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* 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 "fchmod.h"
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <tuple>
|
||||
#include <unistd.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 Fchmod::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int mode;
|
||||
tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid mode");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int ret = fchmod(fd, mode);
|
||||
if (ret == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
|
||||
napi_value Fchmod::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;
|
||||
}
|
||||
|
||||
size_t argc = funcArg.GetArgc();
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int mode;
|
||||
tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid owner");
|
||||
}
|
||||
|
||||
auto cbExec = [fd, mode](napi_env env) -> UniError {
|
||||
int ret = fchmod(fd, mode);
|
||||
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) };
|
||||
};
|
||||
|
||||
string procedureName = "FileIOFchmod";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
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
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_FCHMOD_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_FCHMOD_H
|
||||
|
||||
#include "../../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Fchmod 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
|
||||
@@ -1,123 +0,0 @@
|
||||
/*
|
||||
* 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 "fchown.h"
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <tuple>
|
||||
#include <unistd.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 Fchown::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int owner;
|
||||
tie(succ, owner) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid owner");
|
||||
}
|
||||
|
||||
int group;
|
||||
tie(succ, group) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid group");
|
||||
}
|
||||
|
||||
int ret = fchown(fd, owner, group);
|
||||
if (ret == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value Fchown::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;
|
||||
}
|
||||
|
||||
size_t argc = funcArg.GetArgc();
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int owner;
|
||||
tie(succ, owner) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid owner");
|
||||
}
|
||||
|
||||
int group;
|
||||
tie(succ, group) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid group");
|
||||
}
|
||||
|
||||
auto cbExec = [fd, owner, group](napi_env env) -> UniError {
|
||||
int ret = fchown(fd, owner, group);
|
||||
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) };
|
||||
};
|
||||
|
||||
string procedureName = "FileIOFchown";
|
||||
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
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_FCHOWN_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_FCHOWN_H
|
||||
|
||||
#include "../../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Fchown 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
|
||||
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* 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 "fdatasync.h"
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <tuple>
|
||||
#include <unistd.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 Fdatasync::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int ret = fdatasync(fd);
|
||||
if (ret == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
|
||||
napi_value Fdatasync::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;
|
||||
}
|
||||
|
||||
size_t argc = funcArg.GetArgc();
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [fd](napi_env env) -> UniError {
|
||||
int ret = fdatasync(fd);
|
||||
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) };
|
||||
};
|
||||
|
||||
string procedureName = "FileIOFdatasync";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (argc == NARG_CNT::ONE) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::SECOND]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_;
|
||||
}
|
||||
}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_FDATASYNC_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_FDATASYNC_H
|
||||
|
||||
#include "../../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Fdatasync 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
|
||||
@@ -1,146 +0,0 @@
|
||||
/*
|
||||
* 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 "fdopen_stream.h"
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
|
||||
#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_stream/stream_entity.h"
|
||||
#include "../class_stream/stream_n_exporter.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
using namespace std;
|
||||
|
||||
static NVal InstantiateStream(napi_env env, unique_ptr<FILE, decltype(&fclose)> fp)
|
||||
{
|
||||
napi_value objStream = NClass::InstantiateClass(env, StreamNExporter::className_, {});
|
||||
if (!objStream) {
|
||||
UniError(EIO).ThrowErr(env, "INNER BUG. Cannot instantiate stream");
|
||||
return NVal();
|
||||
}
|
||||
|
||||
auto streamEntity = NClass::GetEntityOf<StreamEntity>(env, objStream);
|
||||
if (!streamEntity) {
|
||||
UniError(EIO).ThrowErr(env, "Cannot instantiate stream because of void entity");
|
||||
return NVal();
|
||||
}
|
||||
|
||||
streamEntity->fp.swap(fp);
|
||||
return { env, objStream };
|
||||
}
|
||||
|
||||
static tuple<bool, int, string> GetFdopenStreamArgs(napi_env env, const NFuncArg &funcArg)
|
||||
{
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Arg fd is required to be type integer");
|
||||
return { false, -1, "" };
|
||||
}
|
||||
|
||||
unique_ptr<char[]> mode;
|
||||
tie(succ, mode, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Arg mode is required to be type string");
|
||||
return { false, -1, "" };
|
||||
}
|
||||
return { true, fd, mode.get() };
|
||||
}
|
||||
|
||||
napi_value FdopenStream::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int fd;
|
||||
string mode;
|
||||
|
||||
tie(succ, fd, mode) = GetFdopenStreamArgs(env, funcArg);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
unique_ptr<FILE, decltype(&fclose)> fp = { fdopen(fd, mode.c_str()), fclose };
|
||||
if (!fp) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return InstantiateStream(env, move(fp)).val_;
|
||||
}
|
||||
|
||||
struct AsyncFdopenStreamArg {
|
||||
unique_ptr<FILE, decltype(&fclose)> fp = { nullptr, fclose };
|
||||
};
|
||||
|
||||
napi_value FdopenStream::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;
|
||||
}
|
||||
|
||||
bool success = false;
|
||||
int fd;
|
||||
string mode;
|
||||
tie(success, fd, mode) = GetFdopenStreamArgs(env, funcArg);
|
||||
if (!success) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto arg = make_shared<AsyncFdopenStreamArg>();
|
||||
auto cbExec = [arg, fd, mode = move(mode)](napi_env env) -> UniError {
|
||||
arg->fp = { fdopen(fd, mode.c_str()), fclose };
|
||||
if (!arg->fp) {
|
||||
return UniError(errno);
|
||||
} else {
|
||||
return UniError(ERRNO_NOERR);
|
||||
}
|
||||
};
|
||||
|
||||
auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
}
|
||||
return InstantiateStream(env, move(arg->fp));
|
||||
};
|
||||
|
||||
string procedureName = "FileIOFdopenStream";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (funcArg.GetArgc() == NARG_CNT::TWO) {
|
||||
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
} else {
|
||||
NVal cb(env, funcArg[NARG_POS::THIRD]);
|
||||
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
|
||||
}
|
||||
}
|
||||
} // namespace ModuleFileIO
|
||||
} // namespace DistributedFS
|
||||
} // namespace OHOS
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_FDOPEN_STREAM_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_FDOPEN_STREAM_H
|
||||
|
||||
#include "../../common/napi/uni_header.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class FdopenStream 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
|
||||
@@ -1,128 +0,0 @@
|
||||
/*
|
||||
* 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 "fstat.h"
|
||||
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
|
||||
#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 Fstat::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct stat buf;
|
||||
if (fstat(fd, &buf) == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
|
||||
if (!objStat) {
|
||||
UniError(EINVAL).ThrowErr(env, "Cannot instantiate class");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
|
||||
if (!statEntity) {
|
||||
UniError(EINVAL).ThrowErr(env, "Cannot get the entity of objStat");
|
||||
return nullptr;
|
||||
}
|
||||
statEntity->stat_ = buf;
|
||||
|
||||
return objStat;
|
||||
}
|
||||
|
||||
struct AsyncStatArg {
|
||||
struct stat stat_;
|
||||
};
|
||||
|
||||
napi_value Fstat::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto arg = make_shared<AsyncStatArg>();
|
||||
auto cbExec = [fd, arg](napi_env env) -> UniError {
|
||||
if (fstat(fd, &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<StatEntity>(env, objStat);
|
||||
if (!statEntity) {
|
||||
return { env, UniError(EIO).GetNapiErr(env) };
|
||||
}
|
||||
statEntity->stat_ = arg->stat_;
|
||||
return { env, objStat };
|
||||
};
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
string procedureName = "fileIOFstat";
|
||||
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
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_FSTAT_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_FSTAT_H
|
||||
|
||||
#include "../../common/napi/uni_header.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Fstat 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
|
||||
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* 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 "fsync.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <tuple>
|
||||
#include <unistd.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 Fsync::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (fsync(fd) == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
|
||||
napi_value Fsync::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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [fd](napi_env env) -> UniError {
|
||||
if (fsync(fd) == -1) {
|
||||
return UniError(errno);
|
||||
} else {
|
||||
return UniError(ERRNO_NOERR);
|
||||
}
|
||||
};
|
||||
auto cbComplete = [](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
} else {
|
||||
return NVal::CreateUndefined(env);
|
||||
}
|
||||
};
|
||||
string procedureName = "FileIOFsync";
|
||||
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
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_FSYNC_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_FSYNC_H
|
||||
|
||||
#include "../../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Fsync 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
|
||||
@@ -1,119 +0,0 @@
|
||||
/*
|
||||
* 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 "ftruncate.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <tuple>
|
||||
#include <unistd.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 Ftruncate::Sync(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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
|
||||
int fd;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t argc = funcArg.GetArgc();
|
||||
int ret = -1;
|
||||
if (argc == NARG_CNT::ONE) {
|
||||
ret = ftruncate(fd, 0);
|
||||
} else {
|
||||
int len;
|
||||
tie(succ, len) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid len");
|
||||
return nullptr;
|
||||
}
|
||||
ret = ftruncate(fd, len);
|
||||
}
|
||||
|
||||
if (ret == -1) {
|
||||
UniError(errno).ThrowErr(env);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return NVal::CreateUndefined(env).val_;
|
||||
}
|
||||
napi_value Ftruncate::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;
|
||||
}
|
||||
bool succ = false;
|
||||
int fd;
|
||||
int len = 0;
|
||||
tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid fd");
|
||||
return nullptr;
|
||||
}
|
||||
size_t argc = funcArg.GetArgc();
|
||||
if (argc > NARG_CNT::ONE) {
|
||||
tie(succ, len) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid len");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
auto cbExec = [fd, len](napi_env env) -> UniError {
|
||||
int ret = ftruncate(fd, len);
|
||||
if (ret == -1) {
|
||||
return UniError(errno);
|
||||
} else {
|
||||
return UniError(ERRNO_NOERR);
|
||||
}
|
||||
};
|
||||
auto cbComplete = [](napi_env env, UniError err) -> NVal {
|
||||
if (err) {
|
||||
return { env, err.GetNapiErr(env) };
|
||||
} else {
|
||||
return NVal::CreateUndefined(env);
|
||||
}
|
||||
};
|
||||
string procedureName = "fileIOFtruncate";
|
||||
NVal thisVar(env, funcArg.GetThisVar());
|
||||
if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) {
|
||||
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
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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_FTRUNCATE_H
|
||||
#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_FTRUNCATE_H
|
||||
|
||||
#include "../../common/napi/n_val.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedFS {
|
||||
namespace ModuleFileIO {
|
||||
class Ftruncate 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
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
* 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 <cstring>
|
||||
#include <tuple>
|
||||
|
||||
#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<char[]> &alg, const size_t algLen)
|
||||
{
|
||||
if (algLen == ((sizeof("md5") - 1)) && !strncmp(alg.get(), "md5", algLen)) {
|
||||
return HASH_ALGORITHM_TYPE_MD5;
|
||||
} else if (algLen == ((sizeof("sha1") - 1)) && !strncmp(alg.get(), "sha1", algLen)) {
|
||||
return HASH_ALGORITHM_TYPE_SHA1;
|
||||
} else if (algLen == ((sizeof("sha256") - 1)) && !strncmp(alg.get(), "sha256", algLen)) {
|
||||
return HASH_ALGORITHM_TYPE_SHA256;
|
||||
} else {
|
||||
return HASH_ALGORITHM_TYPE_UNSUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
static tuple<bool, unique_ptr<char[]>, HASH_ALGORITHM_TYPE, bool> GetHashArgs(napi_env env, const NFuncArg &funcArg)
|
||||
{
|
||||
bool isPromise = false;
|
||||
bool succ = false;
|
||||
unique_ptr<char[]> path;
|
||||
tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid path");
|
||||
return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise };
|
||||
}
|
||||
|
||||
unique_ptr<char[]> alg;
|
||||
size_t algLen;
|
||||
tie(succ, alg, algLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
|
||||
if (!succ) {
|
||||
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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
unique_ptr<char[]> fpath;
|
||||
HASH_ALGORITHM_TYPE algType;
|
||||
bool isPromise = false;
|
||||
tie(succ, fpath, algType, isPromise) = GetHashArgs(env, funcArg);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto arg = make_shared<string>();
|
||||
auto cbExec = [fpath = string(fpath.release()), arg, 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) };
|
||||
};
|
||||
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
|
||||
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
@@ -1,125 +0,0 @@
|
||||
/*
|
||||
* 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 <cstring>
|
||||
#include <tuple>
|
||||
#include <unistd.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;
|
||||
|
||||
static tuple<bool, string, int, int> GetLchownArg(napi_env env, const NFuncArg &funcArg)
|
||||
{
|
||||
bool succ = false;
|
||||
unique_ptr<char[]> path;
|
||||
tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid path");
|
||||
return { false, "", -1, -1 };
|
||||
}
|
||||
|
||||
int owner;
|
||||
tie(succ, owner) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid owner");
|
||||
return { false, "", -1, -1 };
|
||||
}
|
||||
|
||||
int group;
|
||||
tie(succ, group) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid group");
|
||||
return { false, "", -1, -1 };
|
||||
}
|
||||
return { succ, 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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int owner;
|
||||
int group;
|
||||
string path;
|
||||
tie(succ, path, owner, group) = GetLchownArg(env, funcArg);
|
||||
if (!succ) {
|
||||
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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
int owner;
|
||||
int group;
|
||||
string path;
|
||||
tie(succ, path, owner, group) = GetLchownArg(env, funcArg);
|
||||
if (!succ) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto cbExec = [path, owner, 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) };
|
||||
};
|
||||
|
||||
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
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
* 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 <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <tuple>
|
||||
#include <unistd.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;
|
||||
|
||||
static tuple<bool, string, string> GetLinkArg(napi_env env, const NFuncArg &funcArg)
|
||||
{
|
||||
bool succ = false;
|
||||
unique_ptr<char[]> src;
|
||||
tie(succ, src, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
|
||||
if (!succ) {
|
||||
UniError(EINVAL).ThrowErr(env, "Invalid src");
|
||||
return { false, "", "" };
|
||||
}
|
||||
|
||||
unique_ptr<char[]> dest;
|
||||
tie(succ, dest, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
|
||||
if (!succ) {
|
||||
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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
string oldPath;
|
||||
string newPath;
|
||||
tie(succ, oldPath, newPath) = GetLinkArg(env, funcArg);
|
||||
if (!succ) {
|
||||
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;
|
||||
}
|
||||
|
||||
bool succ = false;
|
||||
string oldPath;
|
||||
string newPath;
|
||||
tie(succ, oldPath, newPath) = GetLinkArg(env, funcArg);
|
||||
if (!succ) {
|
||||
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) };
|
||||
};
|
||||
|
||||
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
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user