From 6eacbc3946cd3755c7dfabaf98ab77319fa0b751 Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Fri, 15 Apr 2022 15:11:54 +0800 Subject: [PATCH 1/5] test Signed-off-by: wangjianqiang --- bundle.json | 4 +- interfaces1/kits/js/@ohos.fileAccess.d.ts | 69 ++++ interfaces1/kits/js/BUILD.gn | 70 ++++ interfaces1/kits/js/src/file_access_napi.cpp | 347 +++++++++++++++++++ interfaces1/kits/js/src/file_access_napi.h | 44 +++ interfaces1/kits/js/src/module.cpp | 43 +++ services1/BUILD.gn | 56 +++ services1/src/client/file_access_helper.cpp | 76 ++++ services1/src/client/file_access_helper.h | 57 +++ 9 files changed, 765 insertions(+), 1 deletion(-) create mode 100644 interfaces1/kits/js/@ohos.fileAccess.d.ts create mode 100644 interfaces1/kits/js/BUILD.gn create mode 100644 interfaces1/kits/js/src/file_access_napi.cpp create mode 100644 interfaces1/kits/js/src/file_access_napi.h create mode 100644 interfaces1/kits/js/src/module.cpp create mode 100644 services1/BUILD.gn create mode 100644 services1/src/client/file_access_helper.cpp create mode 100644 services1/src/client/file_access_helper.h diff --git a/bundle.json b/bundle.json index 95dbc537..7275d54d 100644 --- a/bundle.json +++ b/bundle.json @@ -34,8 +34,10 @@ "build": { "sub_component": [ "//foundation/filemanagement/user_file_service/services:fms", + "//foundation/filemanagement/user_file_service/services1:fah", "//foundation/filemanagement/user_file_service/services/sa_profile:filemanager_service_sa_profile", - "//foundation/filemanagement/user_file_service/interfaces/kits/js:filemanager" + "//foundation/filemanagement/user_file_service/interfaces/kits/js:filemanager", + "//foundation/filemanagement/user_file_service/interfaces1/kits/js:fileaccesshelper_js" ], "test": [ "//foundation/filemanagement/user_file_service/services/test:user_file_manager_test" diff --git a/interfaces1/kits/js/@ohos.fileAccess.d.ts b/interfaces1/kits/js/@ohos.fileAccess.d.ts new file mode 100644 index 00000000..f0829d80 --- /dev/null +++ b/interfaces1/kits/js/@ohos.fileAccess.d.ts @@ -0,0 +1,69 @@ +/* +* 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. +*/ + +import {AsyncCallback, Callback} from "./basic"; + +declare namespace fileaccess { + +function OpenFile(sourceFileUri: string, mode: string): Promise; +function OpenFile(sourceFileUri: string, mode: string, callback: AsyncCallback): void; + +function CreateFile(parentUri: string, displayName: string, newFileUri: string): Promise; +function CreateFile(parentUri: string, displayName: string, newFileUri: string, callback: AsyncCallback): void; + +function Mkdir(parentUri: string, displayName: string, newDirUri: string): Promise; +function Mkdir(parentUri: string, displayName: string, newDirUri: string, callback: AsyncCallback): void; + +function Delete(selectFileUri: string): Promise; +function Delete(selectFileUri: string, callback: AsyncCallback): void; + +function Move(sourceFileUri: string, targetParentUri: string, newFileUri: string): Promise; +function Move(sourceFileUri: string, targetParentUri: string, newFileUri: string, callback: AsyncCallback): void; + +function Rename(sourceFileUri: string, displayName: string, newFileUri: string): Promise; +function Rename(sourceFileUri: string, displayName: string, newFileUri: string, callback: AsyncCallback): void; + +export interface unordered_map { + key : string; + value : string; +} + +function Query(sourceFileUri: string): Promise>; +function Query(sourceFileUri: string, callback: AsyncCallback>): void; + +export interface fileinfo { + uri : string; + name : string; + mode : string; + size : number; + mtime : number; + mimitype : string; +} + +function QueryChildFile(sourceFileUri: string): Promise>; +function QueryChildFile(sourceFileUri: string, callback: AsyncCallback>): void; + +export interface deviceinfo { + deviceId : string; + uri : string; + title : string; + flags : number; +} + +function GetRoots(): Promise>; +function GetRoots(callback: AsyncCallback>): void; +} + +export default fileaccess; \ No newline at end of file diff --git a/interfaces1/kits/js/BUILD.gn b/interfaces1/kits/js/BUILD.gn new file mode 100644 index 00000000..49b05bed --- /dev/null +++ b/interfaces1/kits/js/BUILD.gn @@ -0,0 +1,70 @@ +# 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") + +js_declaration("fileaccess_js") { + part_name = "user_file_service" + sources = [ "./@ohos.fileAccess.d.ts" ] +} + +ohos_shared_library("fileaccess") { + subsystem_name = "filemanagement" + part_name = "user_file_service" + + relative_install_dir = "module" + + include_dirs = [ + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//utils/native/base/include", + "//third_party/libuv/include", + "//foundation/filemanagement/user_file_service/services/include", + "//foundation/filemanagement/user_file_service/services1/src/client", + "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi/n_async", + "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common/napi", + "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/common", + "//foundation/multimedia/medialibrary_standard/interfaces/innerkits/native/include", + "//foundation/aafwk/standard/interfaces/innerkits/uri/include", + ] + + sources = [ + "src/file_access_napi.cpp", + "src/module.cpp", + ] + + deps = [ + "//foundation/filemanagement/user_file_service/services1:fms_access", + "//foundation/ace/napi:ace_napi", + "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js:fileio", + "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/medialibrary_data_ability:medialibrary_data_ability", + "//utils/native/base:utils", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] +} + +group("fileaccesshelper_js") { + deps = [ + ":fileaccess", + ":fileaccess_js", + ] +} \ No newline at end of file diff --git a/interfaces1/kits/js/src/file_access_napi.cpp b/interfaces1/kits/js/src/file_access_napi.cpp new file mode 100644 index 00000000..231fbb57 --- /dev/null +++ b/interfaces1/kits/js/src/file_access_napi.cpp @@ -0,0 +1,347 @@ +/* + * 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 "file_access_napi.h" + +#include +#include +#include +#include + +#include "file_access_helper.h" +#include "log.h" +#include "uri.h" + +#include "n_async_work_callback.h" +#include "n_async_work_promise.h" +#include "n_func_arg.h" +#include "uni_error.h" +#include "n_val.h" + +namespace OHOS { +using namespace std; +using namespace DistributedFS; + +tuple FileAccessNapi::GetFileAccessHelper() +{ + if (fileAccessHelper_ == nullptr) { + fileAccessHelper_ = FileAccessHelper::GetInstance(); + } + if (fileAccessHelper_ == nullptr) { + ERR_LOG("fms get instance fails"); + return make_tuple(false, nullptr); + } else { + return make_tuple(true, fileAccessHelper_); + } +} + +napi_value FileAccessNapi::OpenFile(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs((int)NARG_CNT::TWO, (int)NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + unique_ptr sourceFileUri; + tie(succ, sourceFileUri, ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid sourceFileUri"); + return nullptr; + } + unique_ptr mode; + tie(succ, mode, ignore) = NVal(env, funcArg[(int)NARG_POS::SECOND]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid mode"); + return nullptr; + } + auto resultSize = std::make_shared(); + string sourceFileUriString(sourceFileUri.get()); + string modeString(mode.get()); + auto cbExec = [sourceFileUriString, modeString, resultSize](napi_env env) -> UniError { + FileAccessHelper* fileAccessHelper = nullptr; + bool succ = false; + tie(succ, fileAccessHelper) = GetFileAccessHelper(); + if (!succ) { + return UniError(ESRCH); + } + *resultSize = FileAccessHelper::GetInstance()->OpenFile(sourceFileUriString, modeString); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt32(env, *resultSize) }; + }; + + std::string procedureName = "openFile"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == (int)NARG_CNT::TWO) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[(int)NARG_POS::THIRD]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value FileAccessNapi::CreateFile(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs((int)NARG_CNT::THREE, (int)NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + unique_ptr parentUri; + tie(succ, parentUri, ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid parentUri"); + return nullptr; + } + unique_ptr displayName; + tie(succ, displayName, ignore) = NVal(env, funcArg[(int)NARG_POS::SECOND]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid displayName"); + return nullptr; + } + unique_ptr newFileUri; + tie(succ, newFileUri, ignore) = NVal(env, funcArg[(int)NARG_POS::THIRD]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid newFileUri"); + return nullptr; + } + auto resultSize = std::make_shared(); + string parentUriString(parentUri.get()); + string displayNameString(displayName.get()); + string newFileUriString(newFileUri.get()); + auto cbExec = [parentUriString, displayNameString, newFileUriString, resultSize](napi_env env) -> UniError { + FileAccessHelper* fileAccessHelper = nullptr; + bool succ = false; + tie(succ, fileAccessHelper) = GetFileAccessHelper(); + if (!succ) { + return UniError(ESRCH); + } + *resultSize = FileAccessHelper::GetInstance()->CreateFile(parentUriString, displayNameString, newFileUriString); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt32(env, *resultSize) }; + }; + + std::string procedureName = "createFile"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == (int)NARG_CNT::THREE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[(int)NARG_POS::FOURTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value FileAccessNapi::Mkdir(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs((int)NARG_CNT::THREE, (int)NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + unique_ptr parentUri; + tie(succ, parentUri, ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid parentUri"); + return nullptr; + } + unique_ptr displayName; + tie(succ, displayName, ignore) = NVal(env, funcArg[(int)NARG_POS::SECOND]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid displayName"); + return nullptr; + } + unique_ptr newDirUri; + tie(succ, newDirUri, ignore) = NVal(env, funcArg[(int)NARG_POS::THIRD]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid newDirUri"); + return nullptr; + } + auto resultSize = std::make_shared(); + string parentUriString(parentUri.get()); + string displayNameString(displayName.get()); + string newDirUriString(newDirUri.get()); + auto cbExec = [parentUriString, displayNameString, newDirUriString, resultSize](napi_env env) -> UniError { + FileAccessHelper* fileAccessHelper = nullptr; + bool succ = false; + tie(succ, fileAccessHelper) = GetFileAccessHelper(); + if (!succ) { + return UniError(ESRCH); + } + *resultSize = FileAccessHelper::GetInstance()->Mkdir(parentUriString, displayNameString, newDirUriString); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt32(env, *resultSize) }; + }; + + std::string procedureName = "mkdir"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == (int)NARG_CNT::THREE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[(int)NARG_POS::FOURTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value FileAccessNapi::Delete(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs((int)NARG_CNT::ONE, (int)NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + unique_ptr selectFileUri; + tie(succ, selectFileUri, ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid selectFileUri"); + return nullptr; + } + auto resultSize = std::make_shared(); + string selectFileUriString(selectFileUri.get()); + auto cbExec = [selectFileUriString, resultSize](napi_env env) -> UniError { + FileAccessHelper* fileAccessHelper = nullptr; + bool succ = false; + tie(succ, fileAccessHelper) = GetFileAccessHelper(); + if (!succ) { + return UniError(ESRCH); + } + *resultSize = FileAccessHelper::GetInstance()->Delete(selectFileUriString); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt32(env, *resultSize) }; + }; + + std::string procedureName = "delete"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == (int)NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[(int)NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value FileAccessNapi::Move(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs((int)NARG_CNT::THREE, (int)NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + unique_ptr sourceFileUri; + tie(succ, sourceFileUri, ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid sourceFileUri"); + return nullptr; + } + unique_ptr targetParentUri; + tie(succ, targetParentUri, ignore) = NVal(env, funcArg[(int)NARG_POS::SECOND]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid targetParentUri"); + return nullptr; + } + unique_ptr newFileUri; + tie(succ, newFileUri, ignore) = NVal(env, funcArg[(int)NARG_POS::THIRD]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid newFileUri"); + return nullptr; + } + auto resultSize = std::make_shared(); + string sourceFileUriString(sourceFileUri.get()); + string targetParentUriString(targetParentUri.get()); + string newFileUriString(newFileUri.get()); + auto cbExec = [sourceFileUriString, targetParentUriString, newFileUriString, resultSize](napi_env env) -> UniError { + FileAccessHelper* fileAccessHelper = nullptr; + bool succ = false; + tie(succ, fileAccessHelper) = GetFileAccessHelper(); + if (!succ) { + return UniError(ESRCH); + } + *resultSize = FileAccessHelper::GetInstance()->Move(sourceFileUriString, targetParentUriString, newFileUriString); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt32(env, *resultSize) }; + }; + + std::string procedureName = "move"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == (int)NARG_CNT::THREE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[(int)NARG_POS::FOURTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + return NVal::CreateUndefined(env).val_; +} + +bool FileAccessNapi::Export() +{ + return exports_.AddProp( { + NVal::DeclareNapiFunction("openFile", OpenFile), + NVal::DeclareNapiFunction("createFile", CreateFile), + NVal::DeclareNapiFunction("mkdir", Mkdir), + NVal::DeclareNapiFunction("delete", Delete), + NVal::DeclareNapiFunction("move", Move), + // NVal::DeclareNapiFunction("rename", Rename), + // NVal::DeclareNapiFunction("query", Query), + // NVal::DeclareNapiFunction("queryChildFile", QueryChildFile), + // NVal::DeclareNapiFunction("getRoots", GetRoots), + }); +} + +string FileAccessNapi::GetClassName() +{ + return FileAccessNapi::className_; +} + +FileAccessNapi::FileAccessNapi(napi_env env, napi_value exports) : NExporter(env, exports) {} + +FileAccessNapi::~FileAccessNapi() {} +} \ No newline at end of file diff --git a/interfaces1/kits/js/src/file_access_napi.h b/interfaces1/kits/js/src/file_access_napi.h new file mode 100644 index 00000000..7195fc66 --- /dev/null +++ b/interfaces1/kits/js/src/file_access_napi.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILE_ACCESS_NAPI_H +#define FILE_ACCESS_NAPI_H + +#include "n_exporter.h" +#include "file_access_helper.h" + +namespace OHOS { +class FileAccessNapi : public DistributedFS::NExporter { +public: + static napi_value OpenFile(napi_env env, napi_callback_info info); + static napi_value CreateFile(napi_env env, napi_callback_info info); + static napi_value Mkdir(napi_env env, napi_callback_info info); + static napi_value Delete(napi_env env, napi_callback_info info); + static napi_value Move(napi_env env, napi_callback_info info); + // static napi_value Rename(napi_env env, napi_callback_info info); + // static napi_value Query(napi_env env, napi_callback_info info); + // static napi_value QueryChildFile(napi_env env, napi_callback_info info); + // static napi_value GetRoots(napi_env env, napi_callback_info info); + bool Export() override; + std::string GetClassName() override; + static std::tuple GetFileAccessHelper(); + FileAccessNapi(napi_env env, napi_value exports); + ~FileAccessNapi() override; +private: + inline static const std::string className_ ; + inline static FileAccessHelper* fileAccessHelper_ = nullptr; +}; +} +#endif \ No newline at end of file diff --git a/interfaces1/kits/js/src/module.cpp b/interfaces1/kits/js/src/module.cpp new file mode 100644 index 00000000..ecc0835f --- /dev/null +++ b/interfaces1/kits/js/src/module.cpp @@ -0,0 +1,43 @@ +/* + * 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 +#include + +#include "file_access_napi.h" +#include "log.h" + +using namespace std; + +namespace OHOS { +using namespace DistributedFS; +static napi_value Export(napi_env env, napi_value exports) +{ + // ERR_LOG("----------1---------------"); + std::vector> products; + products.emplace_back(make_unique(env, exports)); + + for (auto &&product : products) { + if (!product->Export()) { + ERR_LOG("INNER BUG. Failed to export class %{public}s for module fileaccess", + product->GetClassName().c_str()); + return nullptr; + } + } + return exports; +} + +NAPI_MODULE(fileaccess, Export) +} // namespace OHOS \ No newline at end of file diff --git a/services1/BUILD.gn b/services1/BUILD.gn new file mode 100644 index 00000000..f0104a5c --- /dev/null +++ b/services1/BUILD.gn @@ -0,0 +1,56 @@ +# 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. + +import("//build/ohos.gni") + +group("fah") { + deps = [ + ":fms_access", + # ":fms_access.cfg", + ] +} + +ohos_shared_library("fms_access") { + subsystem_name = "filemanagement" + part_name = "user_file_service" + + include_dirs = [ + "//foundation/filemanagement/user_file_service/services1/src/client", + "//foundation/aafwk/standard/interfaces/innerkits/uri/include", + ] + + sources = [ + "src/client/file_access_helper.cpp", + ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:wantagent_innerkits", + "access_token:libaccesstoken_sdk", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "native_appdatamgr:native_appdatafwk", + "native_appdatamgr:native_dataability", + "native_appdatamgr:native_rdb", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] +} diff --git a/services1/src/client/file_access_helper.cpp b/services1/src/client/file_access_helper.cpp new file mode 100644 index 00000000..40d3efc7 --- /dev/null +++ b/services1/src/client/file_access_helper.cpp @@ -0,0 +1,76 @@ +/* + * 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 "file_access_helper.h" + +namespace OHOS { +FileAccessHelper* FileAccessHelper::instance_ = nullptr; + +FileAccessHelper *FileAccessHelper::GetInstance() +{ + if (instance_ == nullptr) { + instance_ = new FileAccessHelper(); + } + return instance_; +} + +int FileAccessHelper::OpenFile(std::string sourceFileUri, std::string mode) +{ + return 0; +} + +int FileAccessHelper::CreateFile(std::string parentUri, std::string displayName, std::string newFileUri) +{ + return 1; +} + +int FileAccessHelper::Mkdir(std::string parentUri, std::string displayName, std::string newDirUri) +{ + return 2; +} + +int FileAccessHelper::Delete(std::string selectFileUri) +{ + return 3; +} + +int FileAccessHelper::Move(std::string sourceFileUri, std::string targetParentUri, std::string newFileUri) +{ + return 4; +} + +int FileAccessHelper::Rename(std::string sourceFileUri, std::string displayName, std::string newFileUri) +{ + return 0; +} + +std::unordered_map FileAccessHelper::Query(std::string sourceFileUri) +{ + std::unordered_map unordered_map; + return unordered_map; +} + +std::vector FileAccessHelper::QueryChildFile(std::string sourceFileUri) +{ + std::vector fileInfo; + return fileInfo; +} + +std::vector GetRoots() +{ + std::vector deviceinfo; + return deviceinfo; +} +} \ No newline at end of file diff --git a/services1/src/client/file_access_helper.h b/services1/src/client/file_access_helper.h new file mode 100644 index 00000000..9a88edc0 --- /dev/null +++ b/services1/src/client/file_access_helper.h @@ -0,0 +1,57 @@ +/* + * 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 FILE_ACCESS_HELPER_H +#define FILE_ACCESS_HELPER_H + +#include +#include +#include +#include +#include "uri.h" + +namespace OHOS { +struct fileinfo { + std::string uri; // Uri + std::string name; // char* + std::string mode; // mode_t + size_t size; + int64_t mtime; // size_t + std::string mimitype; // char* +}; + +struct deviceinfo { + std::string deviceId; + std::string uri; + std::string title; + int flags; +}; +class FileAccessHelper { +public: + static FileAccessHelper *GetInstance(); + int OpenFile(std::string sourceFileUri, std::string mode); + int CreateFile(std::string parentUri, std::string displayName, std::string newFileUri); + int Mkdir(std::string parentUri, std::string displayName, std::string newDirUri); + int Delete(std::string selectFileUri); + int Move(std::string sourceFileUri, std::string targetParentUri, std::string newFileUri); + int Rename(std::string sourceFileUri, std::string displayName, std::string newFileUri); + std::unordered_map Query(std::string sourceFileUri); + std::vector QueryChildFile(std::string sourceFileUri); + std::vector GetRoots(); +private: + static FileAccessHelper *instance_; +}; +} // namespace OHOS +#endif \ No newline at end of file -- Gitee From dd7d217e28f441b6f1583032560b4f10701b32f0 Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Fri, 15 Apr 2022 17:22:20 +0800 Subject: [PATCH 2/5] test Signed-off-by: wangjianqiang --- interfaces1/kits/js/BUILD.gn | 2 +- interfaces1/kits/js/src/module.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces1/kits/js/BUILD.gn b/interfaces1/kits/js/BUILD.gn index 49b05bed..0f46efb0 100644 --- a/interfaces1/kits/js/BUILD.gn +++ b/interfaces1/kits/js/BUILD.gn @@ -44,9 +44,9 @@ ohos_shared_library("fileaccess") { ] deps = [ - "//foundation/filemanagement/user_file_service/services1:fms_access", "//foundation/ace/napi:ace_napi", "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js:fileio", + "//foundation/filemanagement/user_file_service/services1:fms_access", "//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/medialibrary_data_ability:medialibrary_data_ability", "//utils/native/base:utils", ] diff --git a/interfaces1/kits/js/src/module.cpp b/interfaces1/kits/js/src/module.cpp index ecc0835f..ffe9ea78 100644 --- a/interfaces1/kits/js/src/module.cpp +++ b/interfaces1/kits/js/src/module.cpp @@ -25,7 +25,7 @@ namespace OHOS { using namespace DistributedFS; static napi_value Export(napi_env env, napi_value exports) { - // ERR_LOG("----------1---------------"); + ERR_LOG("----------1---------------"); std::vector> products; products.emplace_back(make_unique(env, exports)); -- Gitee From 2316ec804000fe2ebafb4e28c6b4eec87573616d Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Mon, 18 Apr 2022 10:23:12 +0800 Subject: [PATCH 3/5] test Signed-off-by: wangjianqiang --- interfaces1/kits/js/@ohos.fileAccess.d.ts | 18 +++++++++--------- interfaces1/kits/js/BUILD.gn | 4 ++-- interfaces1/kits/js/src/file_access_napi.cpp | 2 ++ interfaces1/kits/js/src/file_access_napi.h | 4 +++- interfaces1/kits/js/src/module.cpp | 4 +++- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/interfaces1/kits/js/@ohos.fileAccess.d.ts b/interfaces1/kits/js/@ohos.fileAccess.d.ts index f0829d80..427465f2 100644 --- a/interfaces1/kits/js/@ohos.fileAccess.d.ts +++ b/interfaces1/kits/js/@ohos.fileAccess.d.ts @@ -35,15 +35,15 @@ function Move(sourceFileUri: string, targetParentUri: string, newFileUri: string function Rename(sourceFileUri: string, displayName: string, newFileUri: string): Promise; function Rename(sourceFileUri: string, displayName: string, newFileUri: string, callback: AsyncCallback): void; -export interface unordered_map { +export interface uMap { key : string; value : string; } -function Query(sourceFileUri: string): Promise>; -function Query(sourceFileUri: string, callback: AsyncCallback>): void; +function Query(sourceFileUri: string): Promise>; +function Query(sourceFileUri: string, callback: AsyncCallback>): void; -export interface fileinfo { +export interface fileInfo { uri : string; name : string; mode : string; @@ -52,18 +52,18 @@ export interface fileinfo { mimitype : string; } -function QueryChildFile(sourceFileUri: string): Promise>; -function QueryChildFile(sourceFileUri: string, callback: AsyncCallback>): void; +function QueryChildFile(sourceFileUri: string): Promise>; +function QueryChildFile(sourceFileUri: string, callback: AsyncCallback>): void; -export interface deviceinfo { +export interface deviceInfo { deviceId : string; uri : string; title : string; flags : number; } -function GetRoots(): Promise>; -function GetRoots(callback: AsyncCallback>): void; +function GetRoots(): Promise>; +function GetRoots(callback: AsyncCallback>): void; } export default fileaccess; \ No newline at end of file diff --git a/interfaces1/kits/js/BUILD.gn b/interfaces1/kits/js/BUILD.gn index 0f46efb0..b0abddf6 100644 --- a/interfaces1/kits/js/BUILD.gn +++ b/interfaces1/kits/js/BUILD.gn @@ -18,7 +18,7 @@ js_declaration("fileaccess_js") { sources = [ "./@ohos.fileAccess.d.ts" ] } -ohos_shared_library("fileaccess") { +ohos_shared_library("fileaccess_napi") { subsystem_name = "filemanagement" part_name = "user_file_service" @@ -64,7 +64,7 @@ ohos_shared_library("fileaccess") { group("fileaccesshelper_js") { deps = [ - ":fileaccess", + ":fileaccess_napi", ":fileaccess_js", ] } \ No newline at end of file diff --git a/interfaces1/kits/js/src/file_access_napi.cpp b/interfaces1/kits/js/src/file_access_napi.cpp index 231fbb57..63973846 100644 --- a/interfaces1/kits/js/src/file_access_napi.cpp +++ b/interfaces1/kits/js/src/file_access_napi.cpp @@ -31,6 +31,7 @@ #include "n_val.h" namespace OHOS { +namespace FileManagerService { using namespace std; using namespace DistributedFS; @@ -344,4 +345,5 @@ string FileAccessNapi::GetClassName() FileAccessNapi::FileAccessNapi(napi_env env, napi_value exports) : NExporter(env, exports) {} FileAccessNapi::~FileAccessNapi() {} +} // namespace FileManagerService } \ No newline at end of file diff --git a/interfaces1/kits/js/src/file_access_napi.h b/interfaces1/kits/js/src/file_access_napi.h index 7195fc66..971ff3fb 100644 --- a/interfaces1/kits/js/src/file_access_napi.h +++ b/interfaces1/kits/js/src/file_access_napi.h @@ -20,6 +20,7 @@ #include "file_access_helper.h" namespace OHOS { +namespace FileManagerService { class FileAccessNapi : public DistributedFS::NExporter { public: static napi_value OpenFile(napi_env env, napi_callback_info info); @@ -37,8 +38,9 @@ public: FileAccessNapi(napi_env env, napi_value exports); ~FileAccessNapi() override; private: - inline static const std::string className_ ; + inline static const std::string className_ = "__properities__" ; inline static FileAccessHelper* fileAccessHelper_ = nullptr; }; } +} #endif \ No newline at end of file diff --git a/interfaces1/kits/js/src/module.cpp b/interfaces1/kits/js/src/module.cpp index ffe9ea78..535f0d01 100644 --- a/interfaces1/kits/js/src/module.cpp +++ b/interfaces1/kits/js/src/module.cpp @@ -22,6 +22,7 @@ using namespace std; namespace OHOS { +namespace FileManagerService { using namespace DistributedFS; static napi_value Export(napi_env env, napi_value exports) { @@ -39,5 +40,6 @@ static napi_value Export(napi_env env, napi_value exports) return exports; } -NAPI_MODULE(fileaccess, Export) +NAPI_MODULE(fileaccess_napi, Export) +} // namespace FileManagerService } // namespace OHOS \ No newline at end of file -- Gitee From deebd3b51d869777448c73f233c8275d4ef4d4ac Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Sun, 24 Apr 2022 15:29:40 +0800 Subject: [PATCH 4/5] test Signed-off-by: wangjianqiang --- interfaces1/kits/js/@ohos.fileAccess.d.ts | 109 +++++++-- interfaces1/kits/js/src/file_access_napi.cpp | 218 +++++++++++++++++- interfaces1/kits/js/src/file_access_napi.h | 10 +- .../js/src/{module.cpp => module_napi.cpp} | 4 +- services1/src/client/file_access_helper.cpp | 22 +- services1/src/client/file_access_helper.h | 8 +- 6 files changed, 318 insertions(+), 53 deletions(-) rename interfaces1/kits/js/src/{module.cpp => module_napi.cpp} (92%) diff --git a/interfaces1/kits/js/@ohos.fileAccess.d.ts b/interfaces1/kits/js/@ohos.fileAccess.d.ts index 427465f2..e2f41dcf 100644 --- a/interfaces1/kits/js/@ohos.fileAccess.d.ts +++ b/interfaces1/kits/js/@ohos.fileAccess.d.ts @@ -15,35 +15,84 @@ import {AsyncCallback, Callback} from "./basic"; -declare namespace fileaccess { +/** + * Provides fileAccess APIs + * + * @since 9 + * @syscap SystemCapability.FileManagement.UserFileService + */ -function OpenFile(sourceFileUri: string, mode: string): Promise; -function OpenFile(sourceFileUri: string, mode: string, callback: AsyncCallback): void; +declare namespace fileAccess { -function CreateFile(parentUri: string, displayName: string, newFileUri: string): Promise; -function CreateFile(parentUri: string, displayName: string, newFileUri: string, callback: AsyncCallback): void; +/** + * Open File + * + * @since 9 + * @systemapi + */ +function openFile(sourceFileUri: string, mode: string): Promise; +function openFile(sourceFileUri: string, mode: string, callback: AsyncCallback): void; -function Mkdir(parentUri: string, displayName: string, newDirUri: string): Promise; -function Mkdir(parentUri: string, displayName: string, newDirUri: string, callback: AsyncCallback): void; +/** + * Create File + * + * @since 9 + * @systemapi + */ +function createFile(parentUri: string, displayName: string, newFileUri: string): Promise; +function createFile(parentUri: string, displayName: string, newFileUri: string, callback: AsyncCallback): void; -function Delete(selectFileUri: string): Promise; -function Delete(selectFileUri: string, callback: AsyncCallback): void; +/** + * Mkdir + * + * @since 9 + * @systemapi + */ +function mkdir(parentUri: string, displayName: string, newDirUri: string): Promise; +function mkdir(parentUri: string, displayName: string, newDirUri: string, callback: AsyncCallback): void; -function Move(sourceFileUri: string, targetParentUri: string, newFileUri: string): Promise; -function Move(sourceFileUri: string, targetParentUri: string, newFileUri: string, callback: AsyncCallback): void; +/** + * Delete + * + * @since 9 + * @systemapi + */ +function delete(selectFileUri: string): Promise; +function delete(selectFileUri: string, callback: AsyncCallback): void; -function Rename(sourceFileUri: string, displayName: string, newFileUri: string): Promise; -function Rename(sourceFileUri: string, displayName: string, newFileUri: string, callback: AsyncCallback): void; +/** + * Move + * + * @since 9 + * @systemapi + */ +function move(sourceFileUri: string, targetParentUri: string, newFileUri: string): Promise; +function move(sourceFileUri: string, targetParentUri: string, newFileUri: string, callback: AsyncCallback): void; -export interface uMap { +/** + * Rename + * + * @since 9 + * @systemapi + */ +function rename(sourceFileUri: string, displayName: string, newFileUri: string): Promise; +function rename(sourceFileUri: string, displayName: string, newFileUri: string, callback: AsyncCallback): void; + +export interface UnorderedMap { key : string; value : string; } -function Query(sourceFileUri: string): Promise>; -function Query(sourceFileUri: string, callback: AsyncCallback>): void; +/** + * Query + * + * @since 9 + * @systemapi + */ +function query(sourceFileUri: string): Promise>; +function query(sourceFileUri: string, callback: AsyncCallback>): void; -export interface fileInfo { +export interface FileInfo { uri : string; name : string; mode : string; @@ -52,18 +101,30 @@ export interface fileInfo { mimitype : string; } -function QueryChildFile(sourceFileUri: string): Promise>; -function QueryChildFile(sourceFileUri: string, callback: AsyncCallback>): void; +/** + * Query Child File + * + * @since 9 + * @systemapi + */ +function queryChildFile(sourceFileUri: string): Promise>; +function queryChildFile(sourceFileUri: string, callback: AsyncCallback>): void; -export interface deviceInfo { +export interface DeviceInfo { deviceId : string; uri : string; title : string; flags : number; } - -function GetRoots(): Promise>; -function GetRoots(callback: AsyncCallback>): void; + +/** + * Get Roots + * + * @since 9 + * @systemapi + */ +function getRoots(): Promise>; +function getRoots(callback: AsyncCallback>): void; } -export default fileaccess; \ No newline at end of file +export default fileAccess; \ No newline at end of file diff --git a/interfaces1/kits/js/src/file_access_napi.cpp b/interfaces1/kits/js/src/file_access_napi.cpp index 63973846..0caf316b 100644 --- a/interfaces1/kits/js/src/file_access_napi.cpp +++ b/interfaces1/kits/js/src/file_access_napi.cpp @@ -22,16 +22,13 @@ #include "file_access_helper.h" #include "log.h" -#include "uri.h" #include "n_async_work_callback.h" #include "n_async_work_promise.h" #include "n_func_arg.h" #include "uni_error.h" -#include "n_val.h" namespace OHOS { -namespace FileManagerService { using namespace std; using namespace DistributedFS; @@ -322,6 +319,212 @@ napi_value FileAccessNapi::Move(napi_env env, napi_callback_info info) return NVal::CreateUndefined(env).val_; } +napi_value FileAccessNapi::Rename(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs((int)NARG_CNT::THREE, (int)NARG_CNT::FOUR)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + unique_ptr sourceFileUri; + tie(succ, sourceFileUri, ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid sourceFileUri"); + return nullptr; + } + unique_ptr displayName; + tie(succ, displayName, ignore) = NVal(env, funcArg[(int)NARG_POS::SECOND]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid displayName"); + return nullptr; + } + unique_ptr newFileUri; + tie(succ, newFileUri, ignore) = NVal(env, funcArg[(int)NARG_POS::THIRD]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid newFileUri"); + return nullptr; + } + auto resultSize = std::make_shared(); + string sourceFileUriString(sourceFileUri.get()); + string displayNameString(displayName.get()); + string newFileUriString(newFileUri.get()); + auto cbExec = [sourceFileUriString, displayNameString, newFileUriString, resultSize](napi_env env) -> UniError { + FileAccessHelper* fileAccessHelper = nullptr; + bool succ = false; + tie(succ, fileAccessHelper) = GetFileAccessHelper(); + if (!succ) { + return UniError(ESRCH); + } + *resultSize = FileAccessHelper::GetInstance()->Rename(sourceFileUriString, displayNameString, newFileUriString); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt32(env, *resultSize) }; + }; + + std::string procedureName = "rename"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == (int)NARG_CNT::THREE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[(int)NARG_POS::FOURTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value FileAccessNapi::Query(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs((int)NARG_CNT::ONE, (int)NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + unique_ptr sourceFileUri; + tie(succ, sourceFileUri, ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid sourceFileUri"); + return nullptr; + } + auto queryStats = std::make_shared>(); + string sourceFileUriString(sourceFileUri.get()); + auto cbExec = [queryStats, sourceFileUriString](napi_env env) -> UniError { + FileAccessHelper* fileAccessHelper = nullptr; + bool succ = false; + tie(succ, fileAccessHelper) = GetFileAccessHelper(); + if (!succ) { + return UniError(ESRCH); + } + *queryStats = FileAccessHelper::GetInstance()->Query(sourceFileUriString); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [queryStats](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + NVal queryObject = NVal::CreateObject(env); + if((*queryStats).size() !=2) { + UniError(EINVAL).ThrowErr(env, "vector size error"); + return queryObject; + } + // queryObject.AddProp("appSize", NVal::CreateUTF8String(env, (*queryStats)[0]).val_); + // queryObject.AddProp("cacheSize", NVal::CreateUTF8String(env, (*queryStats)[1]).val_); + return queryObject; + }; + std::string procedureName = "query"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == (int)NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[(int)NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value FileAccessNapi::QueryChildFile(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs((int)NARG_CNT::ONE, (int)NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + unique_ptr sourceFileUri; + tie(succ, sourceFileUri, ignore) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid sourceFileUri"); + return nullptr; + } + auto queryChildFileStats = std::make_shared>(); + string sourceFileUriString(sourceFileUri.get()); + auto cbExec = [queryChildFileStats, sourceFileUriString](napi_env env) -> UniError { + FileAccessHelper* fileAccessHelper = nullptr; + bool succ = false; + tie(succ, fileAccessHelper) = GetFileAccessHelper(); + if (!succ) { + return UniError(ESRCH); + } + *queryChildFileStats = FileAccessHelper::GetInstance()->QueryChildFile(sourceFileUriString); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [queryChildFileStats](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + NVal queryChildFileObject = NVal::CreateObject(env); + if((*queryChildFileStats).size() != 6) { + UniError(EINVAL).ThrowErr(env, "vector size error"); + return queryChildFileObject; + } + // queryChildFileObject.AddProp("uri", NVal::CreateUTF8String(env, (*queryChildFileStats)[0]).val_); + // queryChildFileObject.AddProp("name", NVal::CreateUTF8String(env, (*queryChildFileStats)[1]).val_); + // queryChildFileObject.AddProp("mode", NVal::CreateUTF8String(env, (*queryChildFileStats)[2]).val_); + // queryChildFileObject.AddProp("size", NVal::CreateUTF8String(env, (*queryChildFileStats)[3]).val_); + // queryChildFileObject.AddProp("mtime", NVal::CreateInt64(env, (*queryChildFileStats)[4]).val_); + // queryChildFileObject.AddProp("mimitype", NVal::CreateUTF8String(env, (*queryChildFileStats)[5]).val_); + return queryChildFileObject; + }; + std::string procedureName = "queryChildFile"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == (int)NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[(int)NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value FileAccessNapi::GetRoots(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs((int)NARG_CNT::ZERO, (int)NARG_CNT::ONE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto getRootsStats = std::make_shared>(); + auto cbExec = [getRootsStats](napi_env env) -> UniError { + FileAccessHelper* fileAccessHelper = nullptr; + bool succ = false; + tie(succ, fileAccessHelper) = GetFileAccessHelper(); + if (!succ) { + return UniError(ESRCH); + } + *getRootsStats = FileAccessHelper::GetInstance()->GetRoots(); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [getRootsStats](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + NVal getRootsObject = NVal::CreateObject(env); + if((*getRootsStats).size() != 4) { + UniError(EINVAL).ThrowErr(env, "vector size error"); + return getRootsObject; + } + return getRootsObject; + }; + std::string procedureName = "getRoots"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == (int)NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[(int)NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + return NVal::CreateUndefined(env).val_; +} + bool FileAccessNapi::Export() { return exports_.AddProp( { @@ -330,10 +533,10 @@ bool FileAccessNapi::Export() NVal::DeclareNapiFunction("mkdir", Mkdir), NVal::DeclareNapiFunction("delete", Delete), NVal::DeclareNapiFunction("move", Move), - // NVal::DeclareNapiFunction("rename", Rename), - // NVal::DeclareNapiFunction("query", Query), - // NVal::DeclareNapiFunction("queryChildFile", QueryChildFile), - // NVal::DeclareNapiFunction("getRoots", GetRoots), + NVal::DeclareNapiFunction("rename", Rename), + NVal::DeclareNapiFunction("query", Query), + NVal::DeclareNapiFunction("queryChildFile", QueryChildFile), + NVal::DeclareNapiFunction("getRoots", GetRoots), }); } @@ -345,5 +548,4 @@ string FileAccessNapi::GetClassName() FileAccessNapi::FileAccessNapi(napi_env env, napi_value exports) : NExporter(env, exports) {} FileAccessNapi::~FileAccessNapi() {} -} // namespace FileManagerService } \ No newline at end of file diff --git a/interfaces1/kits/js/src/file_access_napi.h b/interfaces1/kits/js/src/file_access_napi.h index 971ff3fb..ba2d280f 100644 --- a/interfaces1/kits/js/src/file_access_napi.h +++ b/interfaces1/kits/js/src/file_access_napi.h @@ -20,7 +20,6 @@ #include "file_access_helper.h" namespace OHOS { -namespace FileManagerService { class FileAccessNapi : public DistributedFS::NExporter { public: static napi_value OpenFile(napi_env env, napi_callback_info info); @@ -28,10 +27,10 @@ public: static napi_value Mkdir(napi_env env, napi_callback_info info); static napi_value Delete(napi_env env, napi_callback_info info); static napi_value Move(napi_env env, napi_callback_info info); - // static napi_value Rename(napi_env env, napi_callback_info info); - // static napi_value Query(napi_env env, napi_callback_info info); - // static napi_value QueryChildFile(napi_env env, napi_callback_info info); - // static napi_value GetRoots(napi_env env, napi_callback_info info); + static napi_value Rename(napi_env env, napi_callback_info info); + static napi_value Query(napi_env env, napi_callback_info info); + static napi_value QueryChildFile(napi_env env, napi_callback_info info); + static napi_value GetRoots(napi_env env, napi_callback_info info); bool Export() override; std::string GetClassName() override; static std::tuple GetFileAccessHelper(); @@ -42,5 +41,4 @@ private: inline static FileAccessHelper* fileAccessHelper_ = nullptr; }; } -} #endif \ No newline at end of file diff --git a/interfaces1/kits/js/src/module.cpp b/interfaces1/kits/js/src/module_napi.cpp similarity index 92% rename from interfaces1/kits/js/src/module.cpp rename to interfaces1/kits/js/src/module_napi.cpp index 535f0d01..24a1856e 100644 --- a/interfaces1/kits/js/src/module.cpp +++ b/interfaces1/kits/js/src/module_napi.cpp @@ -22,7 +22,6 @@ using namespace std; namespace OHOS { -namespace FileManagerService { using namespace DistributedFS; static napi_value Export(napi_env env, napi_value exports) { @@ -40,6 +39,5 @@ static napi_value Export(napi_env env, napi_value exports) return exports; } -NAPI_MODULE(fileaccess_napi, Export) -} // namespace FileManagerService +NAPI_MODULE(fileAccess, Export) } // namespace OHOS \ No newline at end of file diff --git a/services1/src/client/file_access_helper.cpp b/services1/src/client/file_access_helper.cpp index 40d3efc7..8060c57e 100644 --- a/services1/src/client/file_access_helper.cpp +++ b/services1/src/client/file_access_helper.cpp @@ -53,24 +53,30 @@ int FileAccessHelper::Move(std::string sourceFileUri, std::string targetParentUr int FileAccessHelper::Rename(std::string sourceFileUri, std::string displayName, std::string newFileUri) { - return 0; + return 5; } std::unordered_map FileAccessHelper::Query(std::string sourceFileUri) { - std::unordered_map unordered_map; + std::unordered_map unordered_map= {{"str1","str2"},{"str11","str22"}};; return unordered_map; } -std::vector FileAccessHelper::QueryChildFile(std::string sourceFileUri) +std::vector FileAccessHelper::QueryChildFile(std::string sourceFileUri) { - std::vector fileInfo; - return fileInfo; + FileInfo info; + info.name = "nameStr"; + std::vector vec; + vec.push_back(info); + return vec; } -std::vector GetRoots() +std::vector FileAccessHelper::GetRoots() { - std::vector deviceinfo; - return deviceinfo; + DeviceInfo deviceInfo; + deviceInfo.deviceId = "id"; + std::vector vec; + vec.push_back(deviceInfo); + return vec; } } \ No newline at end of file diff --git a/services1/src/client/file_access_helper.h b/services1/src/client/file_access_helper.h index 9a88edc0..4e78456e 100644 --- a/services1/src/client/file_access_helper.h +++ b/services1/src/client/file_access_helper.h @@ -23,7 +23,7 @@ #include "uri.h" namespace OHOS { -struct fileinfo { +struct FileInfo { std::string uri; // Uri std::string name; // char* std::string mode; // mode_t @@ -32,7 +32,7 @@ struct fileinfo { std::string mimitype; // char* }; -struct deviceinfo { +struct DeviceInfo { std::string deviceId; std::string uri; std::string title; @@ -48,8 +48,8 @@ public: int Move(std::string sourceFileUri, std::string targetParentUri, std::string newFileUri); int Rename(std::string sourceFileUri, std::string displayName, std::string newFileUri); std::unordered_map Query(std::string sourceFileUri); - std::vector QueryChildFile(std::string sourceFileUri); - std::vector GetRoots(); + std::vector QueryChildFile(std::string sourceFileUri); + std::vector GetRoots(); private: static FileAccessHelper *instance_; }; -- Gitee From 81290889313a312b9078f7ab933782b8b55ce472 Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Sun, 24 Apr 2022 15:45:54 +0800 Subject: [PATCH 5/5] test Signed-off-by: wangjianqiang --- interfaces1/kits/js/BUILD.gn | 2 +- services1/BUILD.gn | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/interfaces1/kits/js/BUILD.gn b/interfaces1/kits/js/BUILD.gn index b0abddf6..32ae61f3 100644 --- a/interfaces1/kits/js/BUILD.gn +++ b/interfaces1/kits/js/BUILD.gn @@ -40,7 +40,7 @@ ohos_shared_library("fileaccess_napi") { sources = [ "src/file_access_napi.cpp", - "src/module.cpp", + "src/module_napi.cpp", ] deps = [ diff --git a/services1/BUILD.gn b/services1/BUILD.gn index f0104a5c..90b5c87a 100644 --- a/services1/BUILD.gn +++ b/services1/BUILD.gn @@ -16,7 +16,6 @@ import("//build/ohos.gni") group("fah") { deps = [ ":fms_access", - # ":fms_access.cfg", ] } -- Gitee