From 2adf7474fb98e606b974160c5aebed9079b3ebd0 Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Fri, 16 Feb 2024 19:24:08 +0800 Subject: [PATCH] fileapi_add_environment_innerapi Signed-off-by: 18721213663 --- bundle.json | 37 ++++++ interfaces/kits/c/common/error_code.h | 57 ++++++++++ interfaces/kits/c/environment/BUILD.gn | 51 +++++++++ interfaces/kits/c/environment/environment.c | 57 ++++++++++ interfaces/kits/c/environment/environment.h | 31 +++++ .../c/environment/libenvironment.ndk.json | 14 +++ interfaces/kits/c/fileio/BUILD.gn | 50 +++++++++ interfaces/kits/c/fileio/fileio.c | 32 ++++++ interfaces/kits/c/fileio/fileio.h | 28 +++++ interfaces/kits/c/fileio/libfileio.ndk.json | 6 + interfaces/kits/native/BUILD.gn | 72 +++++++++++- .../native/environment/environment_native.cpp | 106 ++++++++++++++++++ .../native/environment/environment_native.h | 28 +++++ .../kits/native/fileio/fileio_native.cpp | 86 ++++++++++++++ interfaces/kits/native/fileio/fileio_native.h | 28 +++++ 15 files changed, 681 insertions(+), 2 deletions(-) create mode 100644 interfaces/kits/c/common/error_code.h create mode 100644 interfaces/kits/c/environment/BUILD.gn create mode 100644 interfaces/kits/c/environment/environment.c create mode 100644 interfaces/kits/c/environment/environment.h create mode 100644 interfaces/kits/c/environment/libenvironment.ndk.json create mode 100644 interfaces/kits/c/fileio/BUILD.gn create mode 100644 interfaces/kits/c/fileio/fileio.c create mode 100644 interfaces/kits/c/fileio/fileio.h create mode 100644 interfaces/kits/c/fileio/libfileio.ndk.json create mode 100644 interfaces/kits/native/environment/environment_native.cpp create mode 100644 interfaces/kits/native/environment/environment_native.h create mode 100644 interfaces/kits/native/fileio/fileio_native.cpp create mode 100644 interfaces/kits/native/fileio/fileio_native.h diff --git a/bundle.json b/bundle.json index c25599f4d..adc6662e7 100644 --- a/bundle.json +++ b/bundle.json @@ -28,6 +28,7 @@ "ability_base", "ability_runtime", "access_token", + "bounds_checking_function", "bundle_framework", "common_event_service", "c_utils", @@ -66,6 +67,24 @@ "header_base": "//foundation/filemanagement/file_api/interfaces/kits/native/remote_uri" } }, + { + "name": "//foundation/filemanagement/file_api/interfaces/kits/native:environment_native", + "header": { + "header_files": [ + "environment_native.h" + ], + "header_base": "//foundation/filemanagement/file_api/interfaces/kits/native/environment" + } + }, + { + "name": "//foundation/filemanagement/file_api/interfaces/kits/native:fileio_native", + "header": { + "header_files": [ + "fileio_native.h" + ], + "header_base": "//foundation/filemanagement/file_api/interfaces/kits/native/fileio" + } + }, { "name": "//foundation/filemanagement/file_api/interfaces/kits/rust:rust_file", "header": { @@ -92,6 +111,24 @@ ], "header_base": "//foundation/filemanagement/file_api/utils/filemgmt_libhilog" } + }, + { + "name": "//foundation/filemanagement/file_api/interfaces/kits/c/environment:ohenvironment", + "header": { + "header_files": [ + "environment.h" + ], + "header_base": "//foundation/filemanagement/file_api/interfaces/kits/c/environment" + } + }, + { + "name": "//foundation/filemanagement/file_api/interfaces/kits/c/fileio:ohfileio", + "header": { + "header_files": [ + "fileio.h" + ], + "header_base": "//foundation/filemanagement/file_api/interfaces/kits/c/fileio" + } } ], "test": [ diff --git a/interfaces/kits/c/common/error_code.h b/interfaces/kits/c/common/error_code.h new file mode 100644 index 000000000..65d39beaf --- /dev/null +++ b/interfaces/kits/c/common/error_code.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024 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 OHOS_FILEMANAGEMENT_FILE_API_C_COMMON_ERROR_CODE_H +#define OHOS_FILEMANAGEMENT_FILE_API_C_COMMON_ERROR_CODE_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif /* End of #ifdef __cplusplus */ +#define MAP_SIZE 6 +#define UNKNOWN_ERROR 13900042 +#define PERMISSION_ERROR 201 +#define DEVICE_NOT_SUPPORTED 801 +#define FILEIO_SYS_CAP_ID 13900000 + +typedef struct { + int errNo; + int errorCode; +} Map; + +static Map g_map[MAP_SIZE] = { + { UNKNOWN_ERROR, UNKNOWN_ERROR }, + { -PERMISSION_ERROR, PERMISSION_ERROR }, + { -DEVICE_NOT_SUPPORTED, DEVICE_NOT_SUPPORTED }, + { -EPERM, FILEIO_SYS_CAP_ID + EPERM }, + { -ENOENT, FILEIO_SYS_CAP_ID + ENOENT }, + { -ENOMEM, FILEIO_SYS_CAP_ID + ENOMEM - 1 }, +}; + +static int GetErrorCode(int errNum) +{ + for (int i = 1; i < MAP_SIZE; i++) { + if (g_map[i].errNo == errNum) { + return g_map[i].errorCode; + } + } + return g_map[0].errorCode; +} +#ifdef __cplusplus +} +#endif /* End of #ifdef __cplusplus */ +#endif /* OHOS_FILEMANAGEMENT_FILE_API_C_COMMON_ERROR_CODE_H */ diff --git a/interfaces/kits/c/environment/BUILD.gn b/interfaces/kits/c/environment/BUILD.gn new file mode 100644 index 000000000..0dcc11367 --- /dev/null +++ b/interfaces/kits/c/environment/BUILD.gn @@ -0,0 +1,51 @@ +# Copyright (C) 2024 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") +import("//build/ohos/ndk/ndk.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_shared_library("ohenvironment") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + include_dirs = [ "./" ] + + sources = [ "./environment.c" ] + deps = [ "${file_api_path}/interfaces/kits/native:environment_native" ] + external_deps = [] + + output_name = "ohenvironment" + output_extension = "so" + relative_install_dir = "ndk" + part_name = "file_api" + subsystem_name = "filemanagement" +} + +ohos_ndk_headers("oh_environment_header") { + dest_dir = "$ndk_headers_out_dir/filemanagement/environment/" + sources = [ "./environment.h" ] +} + +ohos_ndk_library("libohenvironment") { + output_name = "ohenvironment" + output_extension = "so" + ndk_description_file = "./libenvironment.ndk.json" + system_capability = + "SystemCapability.FileManagement.File.Environment.FolderObtain" +} diff --git a/interfaces/kits/c/environment/environment.c b/interfaces/kits/c/environment/environment.c new file mode 100644 index 000000000..6864467e3 --- /dev/null +++ b/interfaces/kits/c/environment/environment.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024 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.h" + +#include + +#include "environment_native.h" +#include "../common/error_code.h" + +int OH_Environment_GetUserDownloadDir(char **result) +{ + if (result == NULL) { + return GetErrorCode(-ENOMEM); + } + int ret = GetUserDir("ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY", result); + if (ret < 0) { + return GetErrorCode(ret); + } + return ret; +} + +int OH_Environment_GetUserDocumentDir(char **result) +{ + if (result == NULL) { + return GetErrorCode(-ENOMEM); + } + int ret = GetUserDir("ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY", result); + if (ret < 0) { + return GetErrorCode(ret); + } + return ret; +} + +int OH_Environment_GetUserDesktopDir(char **result) +{ + if (result == NULL) { + return GetErrorCode(-ENOMEM); + } + int ret = GetUserDir("ohos.permission.READ_WRITE_DESKTOP_DIRECTORY", result); + if (ret < 0) { + return GetErrorCode(ret); + } + return ret; +} diff --git a/interfaces/kits/c/environment/environment.h b/interfaces/kits/c/environment/environment.h new file mode 100644 index 000000000..c0571a7cd --- /dev/null +++ b/interfaces/kits/c/environment/environment.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 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 OHOS_FILEMANAGEMENT_FILE_API_C_ENVIRONMENT_ENVIRONMENT_H +#define OHOS_FILEMANAGEMENT_FILE_API_C_ENVIRONMENT_ENVIRONMENT_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif /* End of #ifdef __cplusplus */ +int OH_Environment_GetUserDownloadDir(char **result); +int OH_Environment_GetUserDesktopDir(char **result); +int OH_Environment_GetUserDocumentDir(char **result); +#ifdef __cplusplus +} +#endif /* End of #ifdef __cplusplus */ +#endif /* OHOS_FILEMANAGEMENT_FILE_API_C_ENVIRONMENT_ENVIRONMENT_H */ \ No newline at end of file diff --git a/interfaces/kits/c/environment/libenvironment.ndk.json b/interfaces/kits/c/environment/libenvironment.ndk.json new file mode 100644 index 000000000..267a28cbe --- /dev/null +++ b/interfaces/kits/c/environment/libenvironment.ndk.json @@ -0,0 +1,14 @@ +[ + { + "first_introduced": "12", + "name":"OH_Environment_GetUserDownloadDir" + }, + { + "first_introduced": "12", + "name":"OH_Environment_GetUserDesktopDir" + }, + { + "first_introduced": "12", + "name":"OH_Environment_GetUserDocumentDir" + } +] \ No newline at end of file diff --git a/interfaces/kits/c/fileio/BUILD.gn b/interfaces/kits/c/fileio/BUILD.gn new file mode 100644 index 000000000..6de8decca --- /dev/null +++ b/interfaces/kits/c/fileio/BUILD.gn @@ -0,0 +1,50 @@ +# Copyright (C) 2024 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") +import("//build/ohos/ndk/ndk.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_shared_library("ohfileio") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + include_dirs = [ "./" ] + + sources = [ "./fileio.c" ] + deps = [ "${file_api_path}/interfaces/kits/native:fileio_native" ] + external_deps = [] + + output_name = "ohfileio" + output_extension = "so" + relative_install_dir = "ndk" + part_name = "file_api" + subsystem_name = "filemanagement" +} + +ohos_ndk_library("libohfileio") { + ndk_description_file = "./libfileio.ndk.json" + output_name = "ohfileio" + output_extension = "so" + system_capability = "SystemCapability.FileManagement.File.FileIO" +} + +ohos_ndk_headers("oh_fileio_header") { + dest_dir = "$ndk_headers_out_dir/filemanagement/fileio/" + sources = [ "./fileio.h" ] +} diff --git a/interfaces/kits/c/fileio/fileio.c b/interfaces/kits/c/fileio/fileio.c new file mode 100644 index 000000000..ab1ebbeec --- /dev/null +++ b/interfaces/kits/c/fileio/fileio.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 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 "fileio.h" + +#include + +#include "fileio_native.h" +#include "../common/error_code.h" + +int OH_FileIO_GetFileLocation(char *uri, int uriLength, int *location) +{ + if (uri == NULL || location == NULL) { + return GetErrorCode(-ENOMEM); + } + int ret = GetFileLocation(uri, uriLength, location); + if (ret < 0) { + return GetErrorCode(ret); + } + return ret; +} diff --git a/interfaces/kits/c/fileio/fileio.h b/interfaces/kits/c/fileio/fileio.h new file mode 100644 index 000000000..6c75f1252 --- /dev/null +++ b/interfaces/kits/c/fileio/fileio.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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 OHOS_FILEMANAGEMENT_FILE_API_C_FILEIO_FILEIO_H +#define OHOS_FILEMANAGEMENT_FILE_API_C_FILEIO_FILEIO_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* End of #ifdef __cplusplus */ +int OH_FileIO_GetFileLocation(char *uri, int uriLength, int *location); +#ifdef __cplusplus +} +#endif /* End of #ifdef __cplusplus */ +#endif /* OHOS_FILEMANAGEMENT_FILE_API_C_FILEIO_FILEIO_H */ \ No newline at end of file diff --git a/interfaces/kits/c/fileio/libfileio.ndk.json b/interfaces/kits/c/fileio/libfileio.ndk.json new file mode 100644 index 000000000..7bacc3c07 --- /dev/null +++ b/interfaces/kits/c/fileio/libfileio.ndk.json @@ -0,0 +1,6 @@ +[ + { + "first_introduced": "12", + "name":"OH_Environment_GetFileLocation" + } +] \ No newline at end of file diff --git a/interfaces/kits/native/BUILD.gn b/interfaces/kits/native/BUILD.gn index 4a9848961..ceeb70833 100644 --- a/interfaces/kits/native/BUILD.gn +++ b/interfaces/kits/native/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-2024 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 @@ -51,6 +51,74 @@ ohos_shared_library("remote_uri_native") { subsystem_name = "filemanagement" } +config("environment_config") { + visibility = [ ":*" ] + include_dirs = [ "environment" ] +} + +ohos_shared_library("environment_native") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + sources = [ "environment/environment_native.cpp" ] + public_configs = [ ":environment_config" ] + + deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bounds_checking_function:libsec_shared", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "napi:ace_napi", + ] + innerapi_tags = [ "platformsdk" ] + part_name = "file_api" + subsystem_name = "filemanagement" +} + +config("fileio_config") { + visibility = [ ":*" ] + include_dirs = [ "fileio" ] +} + +ohos_shared_library("fileio_native") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + sources = [ "fileio/fileio_native.cpp" ] + + public_configs = [ ":fileio_config" ] + + deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog" ] + + external_deps = [ + "ability_base:zuri", + "app_file_service:fileuri_native", + "bounds_checking_function:libsec_shared", + "hilog:libhilog", + ] + innerapi_tags = [ "platformsdk" ] + part_name = "file_api" + subsystem_name = "filemanagement" +} + group("build_kits_native") { - deps = [ ":remote_uri_native" ] + deps = [ + ":environment_native", + ":fileio_native", + ":remote_uri_native", + ] } diff --git a/interfaces/kits/native/environment/environment_native.cpp b/interfaces/kits/native/environment/environment_native.cpp new file mode 100644 index 000000000..f55e5064b --- /dev/null +++ b/interfaces/kits/native/environment/environment_native.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2024 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_native.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "filemgmt_libhilog.h" +#include "ipc_skeleton.h" +#include "parameter.h" +#include "securec.h" +#include "tokenid_kit.h" + +using namespace OHOS; +using namespace OHOS::FileManagement; +namespace { +const std::string STORAGE_PATH = "/storage/Users/"; +const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; +const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; +const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; +const std::string DOWNLOAD_PATH = "/Download"; +const std::string DESKTOP_PATH = "/Desktop"; +const std::string DOCUMENTS_PATH = "/Documents"; +const std::string DEFAULT_USERNAME = "currentUser"; +const char *g_fileManagerFullMountEnableParameter = "const.filemanager.full_mount.enable"; +const int PERMISSION_ERROR = 201; +const int DEVICE_NOT_SUPPORTED = 801; + +static bool CheckCallingPermission(const std::string &permission) +{ + Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + return Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission) != + Security::AccessToken::PermissionState::PERMISSION_GRANTED; +} + +static std::string GetUserName() +{ + return DEFAULT_USERNAME; +} + +static std::string GetPublicPath(const std::string &directoryName) +{ + return STORAGE_PATH + GetUserName() + directoryName; +} + +static bool CheckFileManagerFullMountEnable() +{ + char value[] = "false"; + int retSystem = GetParameter(g_fileManagerFullMountEnableParameter, "false", value, sizeof(value)); + return (retSystem > 0) && (!std::strcmp(value, "true")); +} + +static int CheckInvalidAccess(const std::string &permission) +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGD("Failed to enable the parameter: %{public}s", g_fileManagerFullMountEnableParameter); + return -DEVICE_NOT_SUPPORTED; + } + if (!CheckCallingPermission(permission)) { + HILOGD("Failed to own the permission: %{public}s", permission.c_str()); + return -PERMISSION_ERROR; + } + return 0; +} +} + +int GetUserDir(char *permission, char **result) +{ + std::string perm(permission); + int ret = CheckInvalidAccess(perm); + if (ret != 0) { + return ret; + } + std::string path = ""; + if (perm == READ_WRITE_DESKTOP_PERMISSION) { + path = GetPublicPath(DESKTOP_PATH); + } else if (perm == READ_WRITE_DOCUMENTS_PERMISSION) { + path = GetPublicPath(DOCUMENTS_PATH); + } else if (perm == READ_WRITE_DOWNLOAD_PERMISSION) { + path = GetPublicPath(DOWNLOAD_PATH); + } + *result = (char *) malloc((path.length() + 1) * sizeof(char)); + if (*result == nullptr) { + return -ENOMEM; + } + ret = strcpy_s(*result, (path.length() + 1) * sizeof(char), path.c_str()); + if (ret != 0) { + HILOGE("Failed to copy memory"); + return -ENOMEM; + } + return ret; +} diff --git a/interfaces/kits/native/environment/environment_native.h b/interfaces/kits/native/environment/environment_native.h new file mode 100644 index 000000000..ef18e5f07 --- /dev/null +++ b/interfaces/kits/native/environment/environment_native.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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 FILEMANAGEMENT_FILE_API_NATIVE_ENVIRONMENT_ENVIRONMENT_NATIVE_H +#define FILEMANAGEMENT_FILE_API_NATIVE_ENVIRONMENT_ENVIRONMENT_NATIVE_H + +#ifdef __cplusplus +extern "C" { +#endif /* End of #ifdef __cplusplus */ + +int GetUserDir(char *permission, char **result); + +#ifdef __cplusplus +} +#endif /* End of #ifdef __cplusplus */ +#endif \ No newline at end of file diff --git a/interfaces/kits/native/fileio/fileio_native.cpp b/interfaces/kits/native/fileio/fileio_native.cpp new file mode 100644 index 000000000..8a040ddf8 --- /dev/null +++ b/interfaces/kits/native/fileio/fileio_native.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2024 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 "fileio_native.h" + +#include + +#include "file_uri.h" +#include "filemgmt_libhilog.h" + +using namespace OHOS; +using namespace OHOS::FileManagement; + +namespace { + enum Location { + LOCAL = 1, + CLOUD = 2, + LOCAL_AND_CLOUD = 3 + }; + + const size_t MAX_ATTR_NAME = 64; + const std::string CLOUD_LOCATION_ATTR = "user.cloud.location"; + + bool CheckLocation(const std::string &location) + { + if (!std::all_of(location.begin(), location.end(), ::isdigit)) { + return false; + } + int fileLocation = atoi(location.c_str()); + if (fileLocation < Location::LOCAL || fileLocation > Location::LOCAL_AND_CLOUD) { + return false; + } + return true; + } + + int GetLocationFromPath(const std::string &path) + { + std::unique_ptr value = std::make_unique(MAX_ATTR_NAME); + if (value == nullptr) { + HILOGE("Failed to request heap memory"); + return -ENOMEM; + } + ssize_t size = 0; + size = getxattr(path.c_str(), CLOUD_LOCATION_ATTR.c_str(), value.get(), MAX_ATTR_NAME); + Location defaultLocation = LOCAL; + if (size <= 0) { + if (errno == ENOENT) { + return -ENOENT; + } else if (errno != ENODATA && errno != EOPNOTSUPP) { + HILOGE("Failed to getxattr, errno: %{public}d", errno); + } + return static_cast(defaultLocation); + } + std::string location = std::string(value.get(), static_cast(size)); + if (!CheckLocation(location)) { + HILOGE("Invalid location from getxattr, location: %{public}s", location.c_str()); + return static_cast(defaultLocation); + } + defaultLocation = static_cast(atoi(location.c_str())); + return static_cast(defaultLocation); + } +} + +int GetFileLocation(char *uri, int uriLength, int *location) +{ + std::string uriStr(uri, uriLength); + AppFileService::ModuleFileUri::FileUri fileUri(uriStr); + int ret = GetLocationFromPath(fileUri.GetRealPath()); + if (ret > 0) { + *location = ret; + return 0; + } + return ret; +} diff --git a/interfaces/kits/native/fileio/fileio_native.h b/interfaces/kits/native/fileio/fileio_native.h new file mode 100644 index 000000000..23cbf11aa --- /dev/null +++ b/interfaces/kits/native/fileio/fileio_native.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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 FILEMANAGEMENT_FILE_API_NATIVE_FILEIO_FILEIO_NATIVE_H +#define FILEMANAGEMENT_FILE_API_NATIVE_FILEIO_FILEIO_NATIVE_H + +#ifdef __cplusplus +extern "C" { +#endif /* End of #ifdef __cplusplus */ + +int GetFileLocation(char *uri, int uriLength, int *location); + +#ifdef __cplusplus +} +#endif /* End of #ifdef __cplusplus */ +#endif \ No newline at end of file -- Gitee