From 74959668c13bf4d7b28abab4738c5666900ab2d6 Mon Sep 17 00:00:00 2001 From: futurezhou Date: Fri, 6 Jan 2023 15:55:38 +0800 Subject: [PATCH] Add prop_n_exporter interface for mod_fs Signed-off-by: futurezhou --- interfaces/kits/js/BUILD.gn | 12 +- .../src/mod_fs/properties/prop_n_exporter.cpp | 398 +++++++++++++++--- .../src/mod_fs/properties/prop_n_exporter.h | 27 +- 3 files changed, 380 insertions(+), 57 deletions(-) mode change 100644 => 100755 interfaces/kits/js/BUILD.gn mode change 100644 => 100755 interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp mode change 100644 => 100755 interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn old mode 100644 new mode 100755 index 4a67419f1..44231e07f --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -119,9 +119,19 @@ ohos_shared_library("fs") { "src/mod_fs/class_stat/stat_n_exporter.cpp", "src/mod_fs/common_func.cpp", "src/mod_fs/module.cpp", + "src/mod_fs/properties/close.cpp", + "src/mod_fs/properties/copy_file.cpp", + "src/mod_fs/properties/create_stream.cpp", + "src/mod_fs/properties/fdatasync.cpp", + "src/mod_fs/properties/fdopen_stream.cpp", + "src/mod_fs/properties/fsync.cpp", "src/mod_fs/properties/lstat.cpp", + "src/mod_fs/properties/mkdtemp.cpp", "src/mod_fs/properties/open.cpp", "src/mod_fs/properties/prop_n_exporter.cpp", + "src/mod_fs/properties/read_text.cpp", + "src/mod_fs/properties/rename.cpp", + "src/mod_fs/properties/rmdirent.cpp", "src/mod_fs/properties/stat.cpp", "src/mod_fs/properties/symlink.cpp", "src/mod_fs/properties/truncate.cpp", diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp old mode 100644 new mode 100755 index 62baa5206..48f04124c --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,12 +23,24 @@ #include #include -#include "../common_func.h" +#include "common_func.h" #include "class_file/file_entity.h" #include "class_file/file_n_exporter.h" +#include "close.h" +#include "copy_file.h" +#include "create_stream.h" +#include "fdatasync.h" +#include "fdopen_stream.h" #include "filemgmt_libn.h" +#include "fsync.h" +#include "js_native_api.h" +#include "js_native_api_types.h" #include "lstat.h" +#include "mkdtemp.h" #include "open.h" +#include "read_text.h" +#include "rename.h" +#include "rmdirent.h" #include "stat.h" #include "symlink.h" #include "truncate.h" @@ -39,6 +51,248 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + bool isAccess = false; + std::unique_ptr access_req = { new uv_fs_t, uv_fs_req_cleanup }; + if (access_req == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + int ret = uv_fs_access(nullptr, access_req.get(), path.get(), 0, nullptr); + if (ret < 0 && errno != ENOENT) { + HILOGE("Failed to access file by path"); + NError(errno).ThrowErr(env); + return nullptr; + } + if (ret == 0) { + isAccess = true; + } + return NVal::CreateBool(env, isAccess).val_; +} + +napi_value PropNExporter::Access(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto result = make_shared(); + if (result == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + auto cbExec = [path = string(tmp.get()), result]() -> NError { + std::unique_ptr access_req = {new uv_fs_t, uv_fs_req_cleanup}; + if (access_req == nullptr) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_access(nullptr, access_req.get(), path.c_str(), 0, nullptr); + if (ret == 0) { + result->isAccess = true; + } + return (ret < 0 && errno != ENOENT) ? NError(errno) : NError(ERRNO_NOERR); + }; + + auto cbComplete = [result](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateBool(env, result->isAccess); + }; + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_ACCESS_NAME, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_ACCESS_NAME, cbExec, cbComplete).val_; + } +} + +napi_value PropNExporter::Unlink(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of Arguments Unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [path = string(tmp.get())]() -> NError { + std::unique_ptr unlink_req = { new uv_fs_t, uv_fs_req_cleanup }; + if (unlink_req == nullptr) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_unlink(nullptr, unlink_req.get(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to unlink with path"); + return NError(errno); + } + return NError(ERRNO_NOERR); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_UNLINK_NAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_UNLINK_NAME, cbExec, cbCompl).val_; + } +} + +napi_value PropNExporter::UnlinkSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + std::unique_ptr unlink_req = { new uv_fs_t, uv_fs_req_cleanup }; + if (unlink_req == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + int ret = uv_fs_unlink(nullptr, unlink_req.get(), path.get(), nullptr); + if (ret < 0) { + HILOGE("Failed to unlink with path"); + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [path = string(tmp.get())]() -> NError { + std::unique_ptr mkdir_req = { new uv_fs_t, uv_fs_req_cleanup }; + if (mkdir_req == nullptr) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.c_str(), DIR_DEFAULT_PERM, nullptr); + if (ret < 0) { + HILOGE("Failed to create directory"); + return NError(errno); + } + return NError(ERRNO_NOERR); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_MKDIR_NAME, cbExec, cbCompl).val_; + } +} + +napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + std::unique_ptr mkdir_req = { new uv_fs_t, uv_fs_req_cleanup }; + if (mkdir_req == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.get(), DIR_DEFAULT_PERM, nullptr); + if (ret < 0) { + HILOGE("Failed to create directory"); + NError(errno).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -51,12 +305,12 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - HILOGE("Invalid fd"); + HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, buf, len, hasPos, pos] = + auto [res, buf, len, hasOffset, offset] = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!res) { HILOGE("Failed to resolve buf and options"); @@ -66,34 +320,36 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) ssize_t actLen; uv_buf_t buffer = uv_buf_init(static_cast(buf), len); - uv_fs_t read_req; - int ret = uv_fs_read(nullptr, &read_req, fd, &buffer, 1, pos, nullptr); + std::unique_ptr read_req = { new uv_fs_t, uv_fs_req_cleanup }; + if (read_req == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + int ret = uv_fs_read(nullptr, read_req.get(), fd, &buffer, 1, offset, nullptr); if (ret < 0) { HILOGE("Failed to read file for %{public}d", ret); NError(errno).ThrowErr(env); return nullptr; } - actLen = read_req.result; - uv_fs_req_cleanup(&read_req); - + actLen = read_req.get()->result; return NVal::CreateInt64(env, actLen).val_; } -struct AsyncIOReadArg { - ssize_t lenRead { 0 }; -}; - -static NError ReadExec(shared_ptr arg, void *buf, size_t len, int fd, size_t position) +static NError ReadExec(shared_ptr arg, void *buf, size_t len, int fd, size_t offset) { uv_buf_t buffer = uv_buf_init(static_cast(buf), len); - uv_fs_t read_req; - int ret = uv_fs_read(nullptr, &read_req, fd, &buffer, 1, static_cast(position), nullptr); + std::unique_ptr read_req = { new uv_fs_t, uv_fs_req_cleanup }; + if (read_req == nullptr) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_read(nullptr, read_req.get(), fd, &buffer, 1, static_cast(offset), nullptr); if (ret < 0) { HILOGE("Failed to read file for %{public}d", ret); return NError(errno); } - arg->lenRead = read_req.result; - uv_fs_req_cleanup(&read_req); + arg->lenRead = read_req.get()->result; return NError(ERRNO_NOERR); } @@ -108,12 +364,12 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - HILOGE("Invalid fd"); + HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, buf, len, hasPos, pos] = + auto [res, buf, len, hasOffset, offset] = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!res) { HILOGE("Failed to resolve buf and options"); @@ -121,9 +377,14 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); - auto cbExec = [arg, buf = buf, len = len, fd = fd, pos = pos, env = env]() -> NError { - return ReadExec(arg, buf, len, fd, pos); + auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + auto cbExec = [arg, buf = buf, len = len, fd = fd, offset = offset]() -> NError { + return ReadExec(arg, buf, len, fd, offset); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { @@ -137,32 +398,33 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) bool hasOp = false; if (funcArg.GetArgc() == NARG_CNT::THREE) { NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("length")|| !op.TypeIs(napi_function)) { + if (op.HasProp("offset") || op.HasProp("length") || !op.TypeIs(napi_function)) { hasOp = true; } } if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && hasOp)) { - return NAsyncWorkPromise(env, thisVar).Schedule("FileIORead", cbExec, cbCompl).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_READ_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); NVal cb(env, funcArg[cbIdx]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIORead", cbExec, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_READ_NAME, cbExec, cbCompl).val_; } - - return NVal::CreateUndefined(env).val_; } -NError PropNExporter::WriteExec(shared_ptr arg, void *buf, size_t len, int fd, size_t position) +NError PropNExporter::WriteExec(shared_ptr arg, void *buf, size_t len, int fd, size_t offset) { uv_buf_t buffer = uv_buf_init(static_cast(buf), len); - uv_fs_t write_req; - int ret = uv_fs_write(nullptr, &write_req, fd, &buffer, 1, static_cast(position), nullptr); + std::unique_ptr write_req = { new uv_fs_t, uv_fs_req_cleanup }; + if (write_req == nullptr) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, static_cast(offset), nullptr); if (ret < 0) { HILOGE("Failed to write file for %{public}d", ret); return NError(errno); } - arg->actLen = write_req.result; - uv_fs_req_cleanup(&write_req); + arg->actLen = write_req.get()->result; return NError(ERRNO_NOERR); } @@ -177,12 +439,12 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - HILOGE("Invalid fd"); + HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, bufGuard, buf, len, hasPos, position] = + auto [res, bufGuard, buf, len, hasOffset, offset] = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!res) { HILOGE("Failed to resolve buf and options"); @@ -190,14 +452,14 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg; - if (bufGuard) { - arg = make_shared(move(bufGuard)); - } else { - arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); + auto arg = make_shared(move(bufGuard)); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; } - auto cbExec = [arg, buf = buf, len = len, fd = fd, position = position, env = env]() -> NError { - return WriteExec(arg, buf, len, fd, position); + auto cbExec = [arg, buf = buf, len = len, fd = fd, offset = offset]() -> NError { + return WriteExec(arg, buf, len, fd, offset); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { @@ -212,21 +474,18 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) bool hasOp = false; if (funcArg.GetArgc() == NARG_CNT::THREE) { NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length") || - op.HasProp("encoding") || !op.TypeIs(napi_function)) { + if (op.HasProp("offset") || op.HasProp("length") || op.HasProp("encoding") || !op.TypeIs(napi_function)) { hasOp = true; } } if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && hasOp)) { - return NAsyncWorkPromise(env, thisVar).Schedule("FileIOWrite", cbExec, cbCompl).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_WRITE_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); NVal cb(env, funcArg[cbIdx]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOWrite", cbExec, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_WRITE_NAME, cbExec, cbCompl).val_; } - - return NVal::CreateUndefined(env).val_; } napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) @@ -240,12 +499,12 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) auto [succ, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - HILOGE("Invalid fd"); + HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [res, bufGuard, buf, len, hasPos, position] = + auto [res, bufGuard, buf, len, hasOffset, offset] = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!res) { HILOGE("Failed to resolve buf and options"); @@ -254,34 +513,63 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) ssize_t writeLen; uv_buf_t buffer = uv_buf_init(static_cast(buf), len); - uv_fs_t write_req; - int ret = uv_fs_write(nullptr, &write_req, fd, &buffer, 1, static_cast(position), nullptr); + std::unique_ptr write_req = { new uv_fs_t, uv_fs_req_cleanup }; + if (write_req == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, static_cast(offset), nullptr); if (ret < 0) { HILOGE("Failed to write file for %{public}d", ret); NError(errno).ThrowErr(env); return nullptr; } - writeLen = write_req.result; - uv_fs_req_cleanup(&write_req); - + writeLen = write_req.get()->result; return NVal::CreateInt64(env, writeLen).val_; } bool PropNExporter::Export() { return exports_.AddProp({ + NVal::DeclareNapiFunction("access", Access), + NVal::DeclareNapiFunction("accessSync", AccessSync), + NVal::DeclareNapiFunction("close", Close::Async), + NVal::DeclareNapiFunction("closeSync", Close::Sync), + NVal::DeclareNapiFunction("copyFile", CopyFile::Async), + NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync), + NVal::DeclareNapiFunction("createStream", CreateStream::Async), + NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), + NVal::DeclareNapiFunction("fdatasync", Fdatasync::Async), + NVal::DeclareNapiFunction("fdatasyncSync", Fdatasync::Sync), + NVal::DeclareNapiFunction("fdopenStream", FdopenStream::Async), + NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync), + NVal::DeclareNapiFunction("fsync", Fsync::Async), + NVal::DeclareNapiFunction("fsyncSync", Fsync::Sync), NVal::DeclareNapiFunction("lstat", Lstat::Async), NVal::DeclareNapiFunction("lstatSync", Lstat::Sync), + NVal::DeclareNapiFunction("mkdir", Mkdir), + NVal::DeclareNapiFunction("mkdirSync", MkdirSync), + NVal::DeclareNapiFunction("mkdtemp", Mkdtemp::Async), + NVal::DeclareNapiFunction("mkdtempSync", Mkdtemp::Sync), NVal::DeclareNapiFunction("open", Open::Async), NVal::DeclareNapiFunction("openSync", Open::Sync), NVal::DeclareNapiFunction("read", Read), NVal::DeclareNapiFunction("readSync", ReadSync), + NVal::DeclareNapiFunction("readText", ReadText::Async), + NVal::DeclareNapiFunction("readTextSync", ReadText::Sync), + NVal::DeclareNapiFunction("rename", Rename::Async), + NVal::DeclareNapiFunction("renameSync", Rename::Sync), + NVal::DeclareNapiFunction("rmdir", Rmdirent::Async), + NVal::DeclareNapiFunction("rmdirSync", Rmdirent::Sync), NVal::DeclareNapiFunction("stat", Stat::Async), NVal::DeclareNapiFunction("statSync", Stat::Sync), NVal::DeclareNapiFunction("symlink", Symlink::Async), NVal::DeclareNapiFunction("symlinkSync", Symlink::Sync), NVal::DeclareNapiFunction("truncate", Truncate::Async), NVal::DeclareNapiFunction("truncateSync", Truncate::Sync), + NVal::DeclareNapiFunction("unlink", Unlink), + NVal::DeclareNapiFunction("unlinkSync", UnlinkSync), NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("writeSync", WriteSync), }); diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h old mode 100644 new mode 100755 index d4352746e..dce2a3ca4 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -33,12 +33,30 @@ struct AsyncIOWrtieArg { ~AsyncIOWrtieArg() = default; }; +struct AsyncAccessArg { + bool isAccess = false; +}; + +struct AsyncIOReadArg { + ssize_t lenRead { 0 }; + NRef refReadBuf; + + explicit AsyncIOReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {} + ~AsyncIOReadArg() = default; +}; + class PropNExporter final : public NExporter { public: inline static const std::string className_ = "__properities__"; + static napi_value AccessSync(napi_env env, napi_callback_info info); + static napi_value MkdirSync(napi_env env, napi_callback_info info); static napi_value ReadSync(napi_env env, napi_callback_info info); + static napi_value UnlinkSync(napi_env env, napi_callback_info info); static napi_value WriteSync(napi_env env, napi_callback_info info); + static napi_value Access(napi_env env, napi_callback_info info); + static napi_value Unlink(napi_env env, napi_callback_info info); + static napi_value Mkdir(napi_env env, napi_callback_info info); static napi_value Read(napi_env env, napi_callback_info info); static napi_value Write(napi_env env, napi_callback_info info); static NError WriteExec(std::shared_ptr arg, void *buf, size_t len, int fd, size_t position); @@ -48,6 +66,13 @@ public: PropNExporter(napi_env env, napi_value exports); ~PropNExporter() override; }; + +constexpr int DIR_DEFAULT_PERM = 0770; +const std::string PROCEDURE_ACCESS_NAME = "FileIOAccess"; +const std::string PROCEDURE_UNLINK_NAME = "FileIOUnlink"; +const std::string PROCEDURE_MKDIR_NAME = "FileIOMkdir"; +const std::string PROCEDURE_READ_NAME = "FileIORead"; +const std::string PROCEDURE_WRITE_NAME = "FileIOWrite"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS -- Gitee