diff --git a/interfaces/kits/js/@ohos.remotefileshare.d.ts b/interfaces/kits/js/@ohos.remotefileshare.d.ts index 876b8379afbd8905d32e7331608740831396a640..565a869c590e1707f178149d8f6c30f990b4fde3 100644 --- a/interfaces/kits/js/@ohos.remotefileshare.d.ts +++ b/interfaces/kits/js/@ohos.remotefileshare.d.ts @@ -28,8 +28,8 @@ declare namespace Remotefileshare { * * @since 8 */ - function createSharePath(fd: number, cid: string, callback: AsyncCallback): void; - function createSharePath(fd: number, cid: string): Promise; + function createSharePath(fd: number, cid: string, callback: AsyncCallback): void; + function createSharePath(fd: number, cid: string): Promise; } export default Remotefileshare; diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index b1f295fe780a813162c9e6df3fa8e0c8c3dd6ca1..4c23f028216331c18fcc14041ff50a463b0fa8f9 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -20,11 +20,12 @@ ohos_shared_library("remotefileshare") { ] sources = [ - "remote_file_share/remote_file_share.cpp", + "remote_file_share/remotefileshare_n_exporter.cpp", + "remote_file_share/remotefileshare_napi.cpp", ] deps = [ - "//foundation/ace/napi:ace_napi", + "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn", "//utils/native/base:utilsecurec", ] diff --git a/interfaces/kits/js/remote_file_share/remote_file_share.cpp b/interfaces/kits/js/remote_file_share/remote_file_share.cpp deleted file mode 100644 index 1ff4d70b0252c164d59fcb2659d58d80f0920000..0000000000000000000000000000000000000000 --- a/interfaces/kits/js/remote_file_share/remote_file_share.cpp +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include - -#include -#include -#include - -#include "securec.h" -#include "napi/native_api.h" -#include "napi/native_node_api.h" - -#define HMDFS_IOC 0xf2 -#define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, \ - struct hmdfs_share_control) - -namespace OHOS { -namespace RemoteFileShare { -const int HMDFS_CID_SIZE = 64; -const int DEFAULT_PROMISE_ARGC = 2; -const int DEFAULT_ASYNC_ARGC = 3; -const int DEFAULT_AYSNC_CALLBACK_ARGC = 2; -const int ERR_BUF_SIZE = 256; - -enum NUM { - ZERO = 0, - ONE = 1, - TWO = 2, -}; - -struct hmdfs_share_control { - int32_t src_fd; - char cid[HMDFS_CID_SIZE]; -}; - -struct AddonData { - napi_async_work work; - napi_deferred deferred; - napi_ref callbackRef; - int err; - int status; - int32_t fd; - char *cid; -}; - -void ExecuteWork(napi_env env, void *data) -{ - struct AddonData *addonData = (struct AddonData *)data; - struct hmdfs_share_control sc; - int32_t err = 0; - int32_t dirFd; - std::string sharePath = "/data/storage/el2/distributedfiles/.share"; - - if (access(sharePath.c_str(), F_OK) != 0) { - err = mkdir(sharePath.c_str(), S_IRWXU | S_IRWXG | S_IXOTH); - if (err < 0) { - addonData->status = 0; - addonData->err = errno; - return; - } - } - - dirFd = open(sharePath.c_str(), O_RDONLY); - if (dirFd < 0) { - addonData->status = 0; - addonData->err = errno; - return; - } - - sc.src_fd = addonData->fd; - memcpy_s(sc.cid, HMDFS_CID_SIZE, addonData->cid, HMDFS_CID_SIZE); - - err = ioctl(dirFd, HMDFS_IOC_SET_SHARE_PATH, &sc); - if (err < 0) { - addonData->status = 0; - addonData->err = errno; - } - - close(dirFd); -} - -void WorkComplete(napi_env env, napi_status status, void *data) -{ - struct AddonData *addonData = (struct AddonData *)data; - napi_value path, callback, global; - napi_value argv[DEFAULT_AYSNC_CALLBACK_ARGC], result[DEFAULT_AYSNC_CALLBACK_ARGC]; - char errBuf[ERR_BUF_SIZE] = {0}; - - if (status != napi_ok) { - return; - } - - if (addonData->callbackRef != NULL) { - napi_get_reference_value(env, addonData->callbackRef, &callback); - napi_get_global(env, &global); - if (addonData->status) { - napi_get_null(env, &argv[NUM::ZERO]); - napi_create_string_utf8(env, "/data/storage/el2/distributedfiles/.share", - NAPI_AUTO_LENGTH, &argv[NUM::ONE]); - napi_call_function(env, global, callback, NUM::TWO, argv, result); - } else { - strerror_r(addonData->err, errBuf, ERR_BUF_SIZE); - napi_create_string_utf8(env, errBuf, NAPI_AUTO_LENGTH, &argv[NUM::ZERO]); - napi_get_null(env, &argv[NUM::ONE]); - napi_call_function(env, global, callback, NUM::TWO, argv, result); - } - napi_delete_reference(env, addonData->callbackRef); - } else { - if (addonData->status) { - napi_create_string_utf8(env, "/data/storage/el2/distributedfiles/.share", - NAPI_AUTO_LENGTH, &path); - napi_resolve_deferred(env, addonData->deferred, path); - } else { - strerror_r(addonData->err, errBuf, ERR_BUF_SIZE); - napi_create_string_utf8(env, errBuf, NAPI_AUTO_LENGTH, &path); - napi_reject_deferred(env, addonData->deferred, path); - } - } - - napi_delete_async_work(env, addonData->work); - free(addonData->cid); - delete addonData; -} - -static napi_value CreateSharePath(napi_env env, napi_callback_info info) -{ - napi_value result, workName; - struct AddonData *addonData = new AddonData(); - size_t argc = DEFAULT_ASYNC_ARGC; - napi_value args[DEFAULT_ASYNC_ARGC]; - size_t copysize; - napi_valuetype type = napi_undefined; - - addonData->status = 1; - addonData->err = 0; - addonData->callbackRef = NULL; - napi_get_cb_info(env, info, &argc, args, NULL, NULL); - - if (argc != DEFAULT_ASYNC_ARGC && argc != DEFAULT_PROMISE_ARGC) { - napi_throw_error(env, NULL, "number of arguments mismatch"); - delete addonData; - return NULL; - } - - napi_typeof(env, args[NUM::ZERO], &type); - if (type != napi_number) { - addonData->err = EINVAL; - addonData->status = 0; - } - napi_get_value_int32(env, args[NUM::ZERO], &(addonData->fd)); - - napi_typeof(env, args[NUM::ONE], &type); - if (type != napi_string) { - addonData->err = EINVAL; - addonData->status = 0; - } - napi_get_value_string_utf8(env, args[NUM::ONE], NULL, 0, ©size); - if (copysize != HMDFS_CID_SIZE) { - addonData->err = EINVAL; - addonData->status = 0; - } - addonData->cid = (char *)malloc(sizeof(char) * (copysize + 1)); - if (addonData->cid == nullptr) { - napi_throw_error(env, NULL, "malloc failed"); - delete addonData; - return NULL; - } - addonData->cid[copysize] = '\0'; - napi_get_value_string_utf8(env, args[NUM::ONE], addonData->cid, copysize + 1, ©size); - - if (argc == DEFAULT_ASYNC_ARGC) { - napi_typeof(env, args[NUM::TWO], &type); - if (type != napi_function) { - addonData->err = EINVAL; - addonData->status = 0; - } else { - napi_create_reference(env, args[NUM::TWO], NUM::ONE, &addonData->callbackRef); - } - } - - if (addonData->callbackRef == NULL) { - napi_create_promise(env, &addonData->deferred, &result); - } else { - napi_get_undefined(env, &result); - } - - napi_create_string_utf8(env, "Async Work", NAPI_AUTO_LENGTH, &workName); - napi_create_async_work(env, NULL, workName, ExecuteWork, WorkComplete, addonData, &(addonData->work)); - napi_queue_async_work(env, addonData->work); - - return result; -} - -#define DECLARE_NAPI_METHOD(name, func) \ - { \ - name, 0, func, 0, 0, 0, napi_default, 0 \ - } - -static napi_value Init(napi_env env, napi_value exports) -{ - napi_status status; - napi_property_descriptor desc = DECLARE_NAPI_METHOD("createSharePath", CreateSharePath); - status = napi_define_properties(env, exports, 1, &desc); - return exports; -} -} // namespace RemoteFileShare -} // namespace OHOS - -static napi_module demoModule = { - .nm_version = 1, - .nm_flags = 0, - .nm_filename = nullptr, - .nm_register_func = OHOS::RemoteFileShare::Init, - .nm_modname = "remotefileshare", - .nm_priv = ((void *)0), - .reserved = {0}, -}; - -extern "C" __attribute__((constructor)) void RegisterModule(void) -{ - napi_module_register(&demoModule); -} \ No newline at end of file diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp b/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d1dddd1507d76b3715c1be0e60fff1478e284dc --- /dev/null +++ b/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp @@ -0,0 +1,132 @@ +/* + * 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 +#include +#include +#include + +#include +#include +#include + +#include "securec.h" +#include "remotefileshare_n_exporter.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleRemoteFileShare { +using namespace FileManagement::LibN; +using namespace std; +static constexpr int HMDFS_CID_SIZE = 64; +static constexpr unsigned HMDFS_IOC = 0xf2; + +#define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct hmdfs_share_control) + +struct hmdfs_share_control { + int src_fd; + char cid[HMDFS_CID_SIZE]; +}; + +static NError CreateSharePath(const int src_fd, const std::string cid) +{ + struct hmdfs_share_control sc; + int32_t ret = 0; + int32_t dirFd; + std::string sharePath = "/data/storage/el2/distributedfiles/.share"; + + if (access(sharePath.c_str(), F_OK) != 0) { + ret = mkdir(sharePath.c_str(), S_IRWXU | S_IRWXG | S_IXOTH); + if (ret < 0) { + return NError(errno); + } + } + + dirFd = open(sharePath.c_str(), O_RDONLY); + if (dirFd < 0) { + return NError(errno); + } + + sc.src_fd = src_fd; + if (memcpy_s(sc.cid, HMDFS_CID_SIZE, cid.c_str(), HMDFS_CID_SIZE) != 0) { + close(dirFd); + return NError(ENOMEM); + } + + ret = ioctl(dirFd, HMDFS_IOC_SET_SHARE_PATH, &sc); + if (ret < 0) { + close(dirFd); + return NError(errno); + } + + close(dirFd); + return NError(ERRNO_NOERR); +} + +napi_value CreateSharePath(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(static_cast(NARG_CNT::TWO), static_cast(NARG_CNT::THREE))) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + int src_fd; + std::unique_ptr cid; + size_t cidLen; + tie(succ, src_fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid fd"); + return nullptr; + } + tie(succ, cid, cidLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + if (!succ || cidLen != HMDFS_CID_SIZE) { + NError(EINVAL).ThrowErr(env, "Invalid cid"); + return nullptr; + } + + std::string cidString(cid.get()); + auto cbExec = [src_fd, cidString]() -> NError { + return CreateSharePath(src_fd, cidString); + }; + auto cbComplete = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + std::string sharePath = "/data/storage/el2/distributedfiles/.share"; + return NVal::CreateUTF8String(env, sharePath); + } + }; + std::string procedureName = "CreateSharePath"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == static_cast(NARG_CNT::TWO)) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[static_cast(NARG_POS::THIRD)]); + if (cb.TypeIs(napi_function)) { + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NError(EINVAL).ThrowErr(env, "Callback funciton error"); + return nullptr; + } + } + return NVal::CreateUndefined(env).val_; +} +} // namespace ModuleRemoteFileShare +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.h b/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..34938bf8238573ae2ec8994c8b566c64713617d6 --- /dev/null +++ b/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.h @@ -0,0 +1,28 @@ +/* + * 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 REMOTEFILESHARE_N_EXPORTER_H +#define REMOTEFILESHARE_N_EXPORTER_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleRemoteFileShare { +napi_value CreateSharePath(napi_env env, napi_callback_info info); +} // namespace ModuleRemoteFileShare +} // namespace AppFileService +} // namespace OHOS +#endif // REMOTEFILESHARE_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp b/interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe4329d484ba194c553c4d523050d008a025ad60 --- /dev/null +++ b/interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp @@ -0,0 +1,37 @@ +/* + * 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 "remotefileshare_napi.h" +#include "remotefileshare_n_exporter.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleRemoteFileShare { +/*********************************************** + * Module export and register + ***********************************************/ +napi_value RemoteFileShareExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("createSharePath", CreateSharePath), + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} + +NAPI_MODULE(remotefileshare, RemoteFileShareExport) +} // namespace ModuleRemoteFileShare +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_napi.h b/interfaces/kits/js/remote_file_share/remotefileshare_napi.h new file mode 100644 index 0000000000000000000000000000000000000000..462f6c497523c233ac85740bfe0c19322c7d3cd8 --- /dev/null +++ b/interfaces/kits/js/remote_file_share/remotefileshare_napi.h @@ -0,0 +1,27 @@ +/* + * 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 REMOTEFILESHARE_NAPI_H +#define REMOTEFILESHARE_NAPI_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleRemoteFileShare { +} // namespace ModuleRemoteFileShare +} // namespace AppFileService +} // namespace OHOS +#endif // REMOTEFILESHARE_NAPI_H \ No newline at end of file