diff --git a/bundle.json b/bundle.json index 95dbc537d0a3d2fcb80f4ae861bdfb655108e715..7275d54dee50454390f7946355bfe9c11c0ce3bb 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 0000000000000000000000000000000000000000..e2f41dcfd999ff89abc6200e901d83d0cfbe9127 --- /dev/null +++ b/interfaces1/kits/js/@ohos.fileAccess.d.ts @@ -0,0 +1,130 @@ +/* +* 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"; + +/** + * Provides fileAccess APIs + * + * @since 9 + * @syscap SystemCapability.FileManagement.UserFileService + */ + +declare namespace fileAccess { + +/** + * Open File + * + * @since 9 + * @systemapi + */ +function openFile(sourceFileUri: string, mode: string): Promise; +function openFile(sourceFileUri: string, mode: 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; + +/** + * Mkdir + * + * @since 9 + * @systemapi + */ +function mkdir(parentUri: string, displayName: string, newDirUri: string): Promise; +function mkdir(parentUri: string, displayName: string, newDirUri: string, callback: AsyncCallback): void; + +/** + * Delete + * + * @since 9 + * @systemapi + */ +function delete(selectFileUri: string): Promise; +function delete(selectFileUri: 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; + +/** + * 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; +} + +/** + * Query + * + * @since 9 + * @systemapi + */ +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; +} + +/** + * Query Child File + * + * @since 9 + * @systemapi + */ +function queryChildFile(sourceFileUri: string): Promise>; +function queryChildFile(sourceFileUri: string, callback: AsyncCallback>): void; + +export interface DeviceInfo { + deviceId : string; + uri : string; + title : string; + flags : number; +} + +/** + * Get Roots + * + * @since 9 + * @systemapi + */ +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 0000000000000000000000000000000000000000..32ae61f34d4d023bd32e3920fb7aed01e272ac3e --- /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_napi") { + 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_napi.cpp", + ] + + deps = [ + "//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", + ] + 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_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 new file mode 100644 index 0000000000000000000000000000000000000000..0caf316b1eeb9cfdec9a40d18392455386c73ea8 --- /dev/null +++ b/interfaces1/kits/js/src/file_access_napi.cpp @@ -0,0 +1,551 @@ +/* + * 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 "n_async_work_callback.h" +#include "n_async_work_promise.h" +#include "n_func_arg.h" +#include "uni_error.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_; +} + +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( { + 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 0000000000000000000000000000000000000000..ba2d280f8532fea799c85e5f733726de3a43b220 --- /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_ = "__properities__" ; + inline static FileAccessHelper* fileAccessHelper_ = nullptr; +}; +} +#endif \ No newline at end of file diff --git a/interfaces1/kits/js/src/module_napi.cpp b/interfaces1/kits/js/src/module_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24a1856eb9feb9a1c27ccf13ce811bcfedcb2a11 --- /dev/null +++ b/interfaces1/kits/js/src/module_napi.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 0000000000000000000000000000000000000000..90b5c87afc602686853312b647f904ea2c1bd6c9 --- /dev/null +++ b/services1/BUILD.gn @@ -0,0 +1,55 @@ +# 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", + ] +} + +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 0000000000000000000000000000000000000000..8060c57ef88589d53e3a744b3f0756e3e9ad5270 --- /dev/null +++ b/services1/src/client/file_access_helper.cpp @@ -0,0 +1,82 @@ +/* + * 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 5; +} + +std::unordered_map FileAccessHelper::Query(std::string sourceFileUri) +{ + std::unordered_map unordered_map= {{"str1","str2"},{"str11","str22"}};; + return unordered_map; +} + +std::vector FileAccessHelper::QueryChildFile(std::string sourceFileUri) +{ + FileInfo info; + info.name = "nameStr"; + std::vector vec; + vec.push_back(info); + return vec; +} + +std::vector FileAccessHelper::GetRoots() +{ + 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 new file mode 100644 index 0000000000000000000000000000000000000000..4e78456e52345b13815be754e6cb233694201b36 --- /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