diff --git a/interfaces/kits/js/napi/@ohos.document.d.ts b/interfaces/kits/js/napi/@ohos.document.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..88df1e0cfde75ca6f7ae55ea9ea1645e0ba2b3be --- /dev/null +++ b/interfaces/kits/js/napi/@ohos.document.d.ts @@ -0,0 +1,63 @@ +/* + * 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 { AsyncCallback } from './basic' + +export default document; + +/** + * document + * @syscap SystemCapability.FileManagement.UserFileService + * @since 6 + * @import import document from '@ohos.document'; + * @permission N/A + */ +declare namespace document { + export { choose }; + export { show }; +} + +/** + * choose. + * + * @note N/A + * @syscap SystemCapability.FileManagement.UserFileService + * @since 6 + * @permission N/A + * @function choose + * @param {string} type - type. + * @param {AsyncCallback} [callback] - callback. + * @returns {void | Promise} no callback return Promise otherwise return void + * @throws {TypedError} Parameter check failed + */ +declare function choose(types?: string[]): Promise; +declare function choose(callback: AsyncCallback): void; +declare function choose(types: string[], callback: AsyncCallback): void; + +/** + * show. + * + * @note N/A + * @syscap SystemCapability.FileManagement.UserFileService + * @since 6 + * @permission N/A + * @function show + * @param {string} uri - uri. + * @param {string} type - type. + * @param {AsyncCallback} [callback] - callback. + * @returns {void | Promise} no callback return Promise otherwise return void + * @throws {TypedError} Parameter check failed + */ +declare function show(uri: string, type: string): Promise; +declare function show(uri: string, type: string, callback: AsyncCallback): void; \ No newline at end of file diff --git a/interfaces/kits/js/napi/@ohos.environment.d.ts b/interfaces/kits/js/napi/@ohos.environment.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..4ecb978046047db3a38db777c70b537bc8d6b874 --- /dev/null +++ b/interfaces/kits/js/napi/@ohos.environment.d.ts @@ -0,0 +1,41 @@ +/* +* 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 {AsyncCallback, Callback} from "./basic"; + +/** + * Provides Environment APIs. + * + * @since 8 + * @syscap SystemCapability.FileManagement.File.Environment + */ +declare namespace Environment { + /** + * Get the user data path. + * + * @since 8 + */ + function getStorageDataDir(callback: AsyncCallback): void; + function getStorageDataDir(): Promise; + /** + * Get the User storage path. + * + * @since 8 + */ + function getUserDataDir(callback: AsyncCallback): void; + function getUserDataDir(): Promise; +} + +export default Environment; diff --git a/interfaces/kits/js/napi/@ohos.securityLabel.d.ts b/interfaces/kits/js/napi/@ohos.securityLabel.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..8e3b8629f1ec97cf9089ef6eeb12f4d134b48739 --- /dev/null +++ b/interfaces/kits/js/napi/@ohos.securityLabel.d.ts @@ -0,0 +1,59 @@ +/* +* 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 {AsyncCallback, Callback} from "./basic"; + +/** + * Provides securityLabel APIs + * + * @since 9 + * @syscap SystemCapability.FileManagement.File.FileIO + */ + +declare namespace securityLabel { + + type dataLevel = 's0'|'s1'|'s2'|'s3'|'s4'; + /** + * set the SecurityLabel. + * + * @since 9 + */ + function setSecurityLabel(path:string, type:dataLevel, callback: AsyncCallback): void; + function setSecurityLabel(path:string, type:dataLevel): Promise; + + /** + * set the SecurityLabelSync. + * + * @since 9 + */ + function setSecurityLabelSync(path:string, type:dataLevel): void; + + /** + * get the SecurityLabel. + * + * @since 9 + */ + function getSecurityLabel(path:string, callback: AsyncCallback): void; + function getSecurityLabel(path:string): Promise; + + /** + * get the SecurityLabelSync. + * + * @since 9 + */ + function getSecurityLabelSync(path:string): string; +} + +export default securityLabel; \ No newline at end of file diff --git a/interfaces/kits/js/napi/@ohos.statfs.d.ts b/interfaces/kits/js/napi/@ohos.statfs.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a83f02b8e37d08348623e359707ad84a93daa99 --- /dev/null +++ b/interfaces/kits/js/napi/@ohos.statfs.d.ts @@ -0,0 +1,41 @@ +/* +* 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 {AsyncCallback, Callback} from "./basic"; + +/** + * Provides filesystem statistics APIs + * + * @since 8 + * @syscap SystemCapability.FileManagement.File.FileIO + */ +declare namespace Statfs { + /** + * Get the number of free bytes on the specified path. + * + * @since 8 + */ + function getFreeBytes(path: string, callback: AsyncCallback): void; + function getFreeBytes(path: string): Promise; + /** + * Get the total number of bytes of the specified path. + * + * @since 8 + */ + function getTotalBytes(path: string, callback: AsyncCallback): void; + function getTotalBytes(path: string): Promise; +} + +export default Statfs; diff --git a/interfaces/kits/js/src/mod_document/document_n_exporter.cpp b/interfaces/kits/js/src/mod_document/document_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a23741bc796a91bd97accd0fe451aa6ebe8ffed6 --- /dev/null +++ b/interfaces/kits/js/src/mod_document/document_n_exporter.cpp @@ -0,0 +1,44 @@ +/* + * 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 "document_n_exporter.h" + +#include +#include + +#include "../common/napi/n_async/n_async_work_callback.h" +#include "../common/napi/n_async/n_async_work_promise.h" +#include "../common/napi/n_class.h" +#include "../common/napi/n_func_arg.h" +#include "../common/napi/n_val.h" +#include "../common/uni_error.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleDocument { +napi_value Choose(napi_env env, napi_callback_info info) +{ + UniError(EINVAL).ThrowErr(env, "error"); + return nullptr; +} + +napi_value Show(napi_env env, napi_callback_info info) +{ + UniError(EINVAL).ThrowErr(env, "error"); + return nullptr; +} +} // namespace ModuleDocument +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_document/document_n_exporter.h b/interfaces/kits/js/src/mod_document/document_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..653e1e6c7b2017863358e6ed8ab7bd1bffa9afb0 --- /dev/null +++ b/interfaces/kits/js/src/mod_document/document_n_exporter.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DOCUMENT_N_EXPORTER_H +#define DOCUMENT_N_EXPORTER_H + +#include "../common/napi/n_exporter.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleDocument { +napi_value Choose(napi_env env, napi_callback_info info); +napi_value Show(napi_env env, napi_callback_info info); +} // namespace ModuleDocument +} // namespace DistributedFS +} // namespace OHOS +#endif // DOCUMENT_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_document/document_napi.cpp b/interfaces/kits/js/src/mod_document/document_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81d08a34ec625cca780533f254efb52fd42ffb82 --- /dev/null +++ b/interfaces/kits/js/src/mod_document/document_napi.cpp @@ -0,0 +1,41 @@ +/* + * 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 "document_napi.h" + +#include "document_n_exporter.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleDocument { +/*********************************************** + * Module export and register + ***********************************************/ +napi_value DocumentExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("choose", Choose), + DECLARE_NAPI_FUNCTION("show", Show), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +NAPI_MODULE(document, DocumentExport) +} // namespace ModuleDocument +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_document/document_napi.h b/interfaces/kits/js/src/mod_document/document_napi.h new file mode 100644 index 0000000000000000000000000000000000000000..80cdd7c8b85539fcef210b5a522c0abe79102028 --- /dev/null +++ b/interfaces/kits/js/src/mod_document/document_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 DOCUMENT_NAPI_H +#define DOCUMENT_NAPI_H + +#include "napi/native_api.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleDocument { +} // namespace ModuleDocument +} // namespace DistributedFS +} // namespace OHOS +#endif // DOCUMENT_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..780bdc2bc80e5fcc3c985fca78076eebb6357652 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp @@ -0,0 +1,97 @@ +/* + * 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 "environment_n_exporter.h" + +#include + +#include "../common/napi/n_async/n_async_work_callback.h" +#include "../common/napi/n_async/n_async_work_promise.h" +#include "../common/napi/n_class.h" +#include "../common/napi/n_func_arg.h" +#include "../common/napi/n_val.h" +#include "../common/uni_error.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleEnvironment { +namespace { + const std::string STORAGE_DATA_PATH = "/data"; +} +napi_value GetStorageDataDir(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto cbExec = [](napi_env env) -> UniError { + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateUTF8String(env, STORAGE_DATA_PATH); + }; + + static const std::string procedureName = "GetStorageDataDir"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} + +int GetUserId() +{ + return 0; +} + +napi_value GetUserDataDir(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto userDataPath = std::make_shared(); + auto cbExec = [userDataPath](napi_env env) -> UniError { + (*userDataPath).append("/storage/media/").append(std::to_string(GetUserId())).append("/local"); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [userDataPath](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateUTF8String(env, *userDataPath); + }; + + static const std::string procedureName = "GetUserDataDir"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} +} // namespace ModuleEnvironment +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_n_exporter.h b/interfaces/kits/js/src/mod_environment/environment_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..1b46a7b96924b89f17d889df4f1436be00dd2a53 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ENVIRONMENT_N_EXPORTER_H +#define ENVIRONMENT_N_EXPORTER_H + +#include "../common/napi/n_exporter.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleEnvironment { +napi_value GetStorageDataDir(napi_env env, napi_callback_info info); +napi_value GetUserDataDir(napi_env env, napi_callback_info info); +} // namespace ModuleEnvironment +} // namespace DistributedFS +} // namespace OHOS +#endif // ENVIRONMENT_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_napi.cpp b/interfaces/kits/js/src/mod_environment/environment_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6ec658a4fa60d99183e649250c0ee3a5446e5c0 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/environment_napi.cpp @@ -0,0 +1,39 @@ +/* + * 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 "environment_n_exporter.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleEnvironment { +/*********************************************** + * Module export and register + ***********************************************/ +napi_value EnvironmentExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getStorageDataDir", GetStorageDataDir), + DECLARE_NAPI_FUNCTION("getUserDataDir", GetUserDataDir), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +NAPI_MODULE(environment, EnvironmentExport) +} // namespace ModuleEnvironment +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/security_label.h b/interfaces/kits/js/src/mod_securitylabel/security_label.h new file mode 100644 index 0000000000000000000000000000000000000000..77b4c50f16f5c390cd62a2d45e1e68120f59211d --- /dev/null +++ b/interfaces/kits/js/src/mod_securitylabel/security_label.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SECURITY_LABEL_H +#define SECURITY_LABEL_H + +#include +#include +#include +#include +#include + +namespace OHOS { +namespace DistributedFS { +namespace ModuleSecurityLabel { +class SecurityLabel { +public: + static const char XATTR_KEY[] {"user.security"}; + static const std::string DEFAULT_DATA_LEVEL = "s3"; + static const std::set DATA_LEVEL = {"s0", "s1", "s2", "s3", "s4"}; + static bool SetSecurityLabel(const std::string &path, const std::string &dataLevel) + { + if (DATA_LEVEL.count(dataLevel) != 1) { + return false; + } + if (setxattr(path.c_str(), XATTR_KEY, dataLevel.c_str(), dataLevel.size(), 0) < 0) { + return false; + } + return true; + } + + static std::string GetSecurityLabel(const std::string &path) + { + auto xattrValueSize = getxattr(path.c_str(), XATTR_KEY, NULL, 0); + if (xattrValueSize == -1 || errno == ENOTSUP) { + return ""; + } + if (xattrValueSize <= 0) { + return DEFAULT_DATA_LEVEL; + } + std::unique_ptr xattrValue = std::make_unique((long)xattrValueSize + 1); + if (xattrValue == nullptr) { + return ""; + } + + xattrValueSize = getxattr(path.c_str(), XATTR_KEY, xattrValue.get(), xattrValueSize); + if (xattrValueSize == -1 || errno == ENOTSUP) { + return ""; + } + if (xattrValueSize <= 0) { + return DEFAULT_DATA_LEVEL; + } + return std::string(xattrValue.get()); + } +}; +} // namespace ModuleSecurityLabel +} // namespace FileIO +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e0ac78f6267c3b7d9df52da13d246b7b8acaa93 --- /dev/null +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp @@ -0,0 +1,187 @@ +/* + * 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 "securitylabel_n_exporter.h" + +#include +#include + +#include "../common/napi/n_class.h" +#include "../common/napi/n_func_arg.h" +#include "../common/napi/n_val.h" +#include "../common/uni_error.h" +#include "n_async_work_callback.h" +#include "n_async_work_promise.h" +#include "security_label.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleSecurityLabel { +using namespace std; + +napi_value SetSecurityLabel(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(static_cast(NARG_CNT::TWO), static_cast(NARG_CNT::THREE))) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + std::unique_ptr dataLevel; + tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + tie(succ, dataLevel, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::SECOND)]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid dataLevel"); + return nullptr; + } + std::string pathString(path.get()); + std::string dataLevelString(dataLevel.get()); + if (DATA_LEVEL.find(dataLevelString) == DATA_LEVEL.end()) { + UniError(EINVAL).ThrowErr(env, "Invalid Argument of dataLevelEnum"); + return nullptr; + } + auto cbExec = [pathString, dataLevelString](napi_env env) -> UniError { + bool ret = SecurityLabel::SetSecurityLabel(pathString, dataLevelString); + if (!ret) { + return UniError(-1); + } else { + return UniError(ERRNO_NOERR); + } + }; + auto cbComplete = [](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } else { + return NVal::CreateUndefined(env); + } + }; + static const std::string procedureName = "SetSecurityLabel"; + 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_; + } + } + return NVal::CreateUndefined(env).val_; +} + +napi_value SetSecurityLabelSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(static_cast(NARG_CNT::TWO), static_cast(NARG_CNT::THREE))) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + std::unique_ptr dataLevel; + tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + tie(succ, dataLevel, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::SECOND)]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid dataLevel"); + return nullptr; + } + + if (DATA_LEVEL.find(dataLevel.get()) == DATA_LEVEL.end()) { + UniError(EINVAL).ThrowErr(env, "Invalid Argument of dataLevelEnum"); + return nullptr; + } + + bool ret = SecurityLabel::SetSecurityLabel(path.get(), dataLevel.get()); + if (!ret) { + return UniError(-1).GetNapiErr(env); + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value GetSecurityLabel(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(static_cast(NARG_CNT::ONE), static_cast(NARG_CNT::TWO))) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + auto result = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString, result](napi_env env) -> UniError { + *result = SecurityLabel::GetSecurityLabel(pathString); + return UniError(ERRNO_NOERR); + }; + auto cbComplete = [result](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUTF8String(env, *result) }; + }; + + static const std::string procedureName = "GetSecurityLabel"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == static_cast(NARG_CNT::ONE)) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[static_cast(NARG_POS::SECOND)]); + if (cb.TypeIs(napi_function)) { + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + } + return NVal::CreateUndefined(env).val_; +} + +napi_value GetSecurityLabelSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(static_cast(NARG_CNT::ONE), static_cast(NARG_CNT::TWO))) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); + if (!succ) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + std::string result = SecurityLabel::GetSecurityLabel(path.get()); + return NVal::CreateUTF8String(env, result).val_; +} +} // namespace ModuleSecurityLabel +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.h b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..39748659bd7bed3029563304c48650e8228ae6b7 --- /dev/null +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SECURITYLABEL_N_EXPORTER_H +#define SECURITYLABEL_N_EXPORTER_H + +#include "../common/napi/n_exporter.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleSecurityLabel { +napi_value SetSecurityLabel(napi_env env, napi_callback_info info); +napi_value SetSecurityLabelSync(napi_env env, napi_callback_info info); +napi_value GetSecurityLabel(napi_env env, napi_callback_info info); +napi_value GetSecurityLabelSync(napi_env env, napi_callback_info info); +} // namespace ModuleSecurityLabel +} // namespace DistributedFS +} // namespace OHOS +#endif // SECURITYLABEL_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_napi.cpp b/interfaces/kits/js/src/mod_securitylabel/securitylabel_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48df4ce3801ca3338819189a99a58588d063525c --- /dev/null +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_napi.cpp @@ -0,0 +1,42 @@ +/* + * 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 "napi/native_api.h" +#include "napi/native_node_api.h" + +#include "securitylabel_n_exporter.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleSecurityLabel { +/*********************************************** + * Module export and register + ***********************************************/ +napi_value SecurityLabelExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("setSecurityLabel", SetSecurityLabel), + DECLARE_NAPI_FUNCTION("setSecurityLabelSync", SetSecurityLabelSync), + DECLARE_NAPI_FUNCTION("getSecurityLabel", GetSecurityLabel), + DECLARE_NAPI_FUNCTION("getSecurityLabelSync", GetSecurityLabelSync), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +NAPI_MODULE(securitylabel, SecurityLabelExport) +} // namespace ModuleSecurityLabel +} // namespace DistributedFS +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.cpp b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..066e1b601f83f10504cf5daa6d27b40a45b5601e --- /dev/null +++ b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.cpp @@ -0,0 +1,519 @@ +/* + * 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 "statfs_n_exporter.h" + +#include +#include + +namespace OHOS { +namespace DistributedFS { +namespace ModuleStatfs { +using namespace FileManagement::LibN; + +napi_value GetFrSizeSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + struct statvfs diskInfo; + int ret = statvfs(path.get(), &diskInfo); + if (ret != 0) { + NError(errno).ThrowErr(env, "Failed get info"); + return nullptr; + } + unsigned long long freeSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_bavail); + return NVal::CreateInt64(env, freeSize).val_; +} + +napi_value GetFrSize(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto resultSize = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString, resultSize]() -> NError { + struct statvfs diskInfo; + int ret = statvfs(pathString.c_str(), &diskInfo); + if (ret != 0) { + return NError(errno); + } + *resultSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_bavail); + return NError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateInt64(env, *resultSize); + }; + + static const std::string procedureName = "GetFrSize"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} + +napi_value GetBSizeSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + struct statvfs diskInfo; + int ret = statvfs(path.get(), &diskInfo); + if (ret != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + return NVal::CreateInt64(env, diskInfo.f_bsize).val_; +} + +napi_value GetBSize(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto resultSize = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString, resultSize]() -> NError { + struct statvfs diskInfo; + int ret = statvfs(pathString.c_str(), &diskInfo); + if (ret != 0) { + return NError(errno); + } + *resultSize = diskInfo.f_bsize; + return NError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateInt64(env, *resultSize); + }; + + static const std::string procedureName = "GetBSize"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} + +napi_value GetBAvailSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + struct statvfs diskInfo; + int ret = statvfs(path.get(), &diskInfo); + if (ret != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + return NVal::CreateInt64(env, diskInfo.f_bavail).val_; +} + +napi_value GetBAvail(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto resultSize = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString, resultSize]() -> NError { + struct statvfs diskInfo; + int ret = statvfs(pathString.c_str(), &diskInfo); + if (ret != 0) { + return NError(errno); + } + *resultSize = diskInfo.f_bavail; + return NError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateInt64(env, *resultSize); + }; + + static const std::string procedureName = "GetBAvail"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} + +napi_value GetBlocksSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + struct statvfs diskInfo; + int ret = statvfs(path.get(), &diskInfo); + if (ret != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + return NVal::CreateInt64(env, diskInfo.f_blocks).val_; +} + +napi_value GetBlocks(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto resultSize = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString, resultSize]() -> NError { + struct statvfs diskInfo; + int ret = statvfs(pathString.c_str(), &diskInfo); + if (ret != 0) { + return NError(errno); + } + *resultSize = diskInfo.f_blocks; + return NError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateInt64(env, *resultSize); + }; + + static const std::string procedureName = "GetBlocks"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} + +napi_value GetBFreeSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + struct statvfs diskInfo; + int ret = statvfs(path.get(), &diskInfo); + if (ret != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + return NVal::CreateInt64(env, diskInfo.f_bfree).val_; +} + +napi_value GetBFree(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto resultSize = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString, resultSize]() -> NError { + struct statvfs diskInfo; + int ret = statvfs(pathString.c_str(), &diskInfo); + if (ret != 0) { + return NError(errno); + } + *resultSize = diskInfo.f_bfree; + return NError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateInt64(env, *resultSize); + }; + + static const std::string procedureName = "GetBFree"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} + +napi_value GetFreeBytesSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + struct statvfs diskInfo; + int ret = statvfs(path.get(), &diskInfo); + if (ret != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + unsigned long long freeSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_bfree); + return NVal::CreateInt64(env, freeSize).val_; +} + +napi_value GetFreeBytes(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto resultSize = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString, resultSize]() -> NError { + struct statvfs diskInfo; + int ret = statvfs(pathString.c_str(), &diskInfo); + if (ret != 0) { + return NError(errno); + } + *resultSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_bfree); + return NError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateInt64(env, *resultSize); + }; + + static const std::string procedureName = "GetFreeBytes"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} + +napi_value GetTotalBytesSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + struct statvfs diskInfo; + int ret = statvfs(path.get(), &diskInfo); + if (ret != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + unsigned long long totalSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_blocks); + return NVal::CreateInt64(env, totalSize).val_; +} + +napi_value GetTotalBytes(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto resultSize = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString, resultSize]() -> NError { + struct statvfs diskInfo; + int ret = statvfs(pathString.c_str(), &diskInfo); + if (ret != 0) { + return NError(errno); + } + *resultSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_blocks); + return NError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt64(env, *resultSize) }; + }; + + static const std::string procedureName = "GetTotalBytes"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} +} // namespace ModuleStatfs +} // namespace DistributedFS +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.h b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..dd25b1724926781155cab9fd431acff1d11309bf --- /dev/null +++ b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.h @@ -0,0 +1,47 @@ +/* + * 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 STATFS_N_EXPORTER_H +#define STATFS_N_EXPORTER_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleStatfs { +napi_value GetFrSizeSync(napi_env env, napi_callback_info info); +napi_value GetFrSize(napi_env env, napi_callback_info info); + +napi_value GetBSizeSync(napi_env env, napi_callback_info info); +napi_value GetBSize(napi_env env, napi_callback_info info); + +napi_value GetBAvailSync(napi_env env, napi_callback_info info); +napi_value GetBAvail(napi_env env, napi_callback_info info); + +napi_value GetBlocksSync(napi_env env, napi_callback_info info); +napi_value GetBlocks(napi_env env, napi_callback_info info); + +napi_value GetBFreeSync(napi_env env, napi_callback_info info); +napi_value GetBFree(napi_env env, napi_callback_info info); + +napi_value GetFreeBytesSync(napi_env env, napi_callback_info info); +napi_value GetFreeBytes(napi_env env, napi_callback_info info); + +napi_value GetTotalBytesSync(napi_env env, napi_callback_info info); +napi_value GetTotalBytes(napi_env env, napi_callback_info info); +} // namespace ModuleStatfs +} // namespace DistributedFS +} // namespace OHOS +#endif // STATFS_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_statfs/statfs_napi.cpp b/interfaces/kits/js/src/mod_statfs/statfs_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c0f27dc2674bd5e3b381ae0dd3c5e7f87e14109 --- /dev/null +++ b/interfaces/kits/js/src/mod_statfs/statfs_napi.cpp @@ -0,0 +1,37 @@ +/* + * 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 "statfs_n_exporter.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleStatfs { +/*********************************************** + * Module export and register + ***********************************************/ +napi_value StatfsExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getFreeBytes", GetFreeBytes), + DECLARE_NAPI_FUNCTION("getTotalBytes", GetTotalBytes), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +NAPI_MODULE(statfs, StatfsExport) +} // namespace ModuleStatfs +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file