From 0ccbcb524e70850343fe349c4ce22bc475800191 Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 6 Aug 2025 18:10:15 +0800 Subject: [PATCH 1/4] fileuri taihe modify Signed-off-by: BrainL --- .../taihe/fileuri/idl/ohos.file.fileuri.taihe | 28 +++++++++++++++---- .../taihe/fileuri/include/fileuri_taihe.h | 10 ++++--- .../kits/taihe/fileuri/src/fileuri_taihe.cpp | 14 +++++++--- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe b/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe index b2c25a848..75bf78748 100644 --- a/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe +++ b/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe @@ -20,12 +20,30 @@ static { loadLibrary("file_fileuri_taihe.z"); } """) @class -interface FileUri { - @get("name") - getName(): String; +interface FileUriInner { + @get getName(): String; } -@ctor("FileUri") -function makeFileUri(name: String): FileUri; +@ctor("FileUriInner") +function makeFileUri(name: String): FileUriInner; + +function createFileUri(name: String): FileUriInner; function getUriFromPath(path: String): String; + +@!sts_inject(""" + +export class FileUri { + inner: FileUriInner; + constructor(uri: String) { + this.inner = createFileUri(uri); + } + constructor(inner: FileUriInner) { + this.inner = inner; + } + get name(): string { + return this.inner.name; + } +} + +""") \ No newline at end of file diff --git a/interfaces/kits/taihe/fileuri/include/fileuri_taihe.h b/interfaces/kits/taihe/fileuri/include/fileuri_taihe.h index a6bc30905..ffcb69437 100644 --- a/interfaces/kits/taihe/fileuri/include/fileuri_taihe.h +++ b/interfaces/kits/taihe/fileuri/include/fileuri_taihe.h @@ -24,11 +24,11 @@ namespace ANI::FileUri { -class FileUriImpl { +class FileUriInner { public: - FileUriImpl() = delete; + FileUriInner() = delete; - FileUriImpl(taihe::string_view name); + FileUriInner(taihe::string_view name); std::string getName(); @@ -36,7 +36,9 @@ private: OHOS::Uri uri_; }; -ohos::file::fileuri::FileUri makeFileUri(taihe::string_view name); +ohos::file::fileuri::FileUriInner makeFileUri(taihe::string_view name); + +ohos::file::fileuri::FileUriInner createFileUri(taihe::string_view name); std::string getUriFromPath(taihe::string_view path); } // ANI::FileUri diff --git a/interfaces/kits/taihe/fileuri/src/fileuri_taihe.cpp b/interfaces/kits/taihe/fileuri/src/fileuri_taihe.cpp index 783f8b682..7f1fdc54d 100644 --- a/interfaces/kits/taihe/fileuri/src/fileuri_taihe.cpp +++ b/interfaces/kits/taihe/fileuri/src/fileuri_taihe.cpp @@ -23,13 +23,13 @@ namespace ANI::FileUri { const std::string FILE_SCHEME_PREFIX_TAIHE = "file://"; const std::string FILE_FILEURI_FAILED = ""; -FileUriImpl::FileUriImpl(taihe::string_view name) +FileUriInner::FileUriInner(taihe::string_view name) : uri_((std::string(name.c_str()).find(FILE_SCHEME_PREFIX_TAIHE) == 0) ? std::string(name.c_str()) : OHOS::AppFileService::CommonFunc::GetUriFromPath(std::string(name.c_str()))) { } -std::string FileUriImpl::getName() +std::string FileUriInner::getName() { std::string sandboxPath = OHOS::AppFileService::SandboxHelper::Decode(uri_.GetPath()); size_t posLast = sandboxPath.find_last_of("/"); @@ -42,9 +42,14 @@ std::string FileUriImpl::getName() return sandboxPath.substr(posLast + 1); } -ohos::file::fileuri::FileUri makeFileUri(taihe::string_view name) +ohos::file::fileuri::FileUriInner makeFileUri(taihe::string_view name) { - return taihe::make_holder(name); + return taihe::make_holder(name); +} + +ohos::file::fileuri::FileUriInner createFileUri(taihe::string_view name) +{ + return taihe::make_holder(name); } std::string getUriFromPath(taihe::string_view path) @@ -56,4 +61,5 @@ std::string getUriFromPath(taihe::string_view path) } // namespace ANI::FileUri TH_EXPORT_CPP_API_makeFileUri(ANI::FileUri::makeFileUri); +TH_EXPORT_CPP_API_createFileUri(ANI::FileUri::createFileUri); TH_EXPORT_CPP_API_getUriFromPath(ANI::FileUri::getUriFromPath); -- Gitee From 54b5f53efd9a012f7d08c1f1a1e89548c784a2ff Mon Sep 17 00:00:00 2001 From: BrainL Date: Fri, 8 Aug 2025 20:02:18 +0800 Subject: [PATCH 2/4] errMessage and return value Signed-off-by: BrainL --- .../taihe/fileshare/src/fileshare_taihe.cpp | 83 ++++++++++++++----- 1 file changed, 64 insertions(+), 19 deletions(-) diff --git a/interfaces/kits/taihe/fileshare/src/fileshare_taihe.cpp b/interfaces/kits/taihe/fileshare/src/fileshare_taihe.cpp index 65f4a3cb6..ab2fc87fb 100644 --- a/interfaces/kits/taihe/fileshare/src/fileshare_taihe.cpp +++ b/interfaces/kits/taihe/fileshare/src/fileshare_taihe.cpp @@ -52,6 +52,49 @@ const std::string FILE_NOPS = ""; const std::string FILE_READ = "r"; const std::string FILE_WRITE = "w"; +static int32_t ErrorCodeConversion(int32_t errorCode) +{ + int32_t errCode = ERR_UNKNOWN; + switch (errorCode) { + case static_cast(ERR_OK): + errCode = ERR_OK; + break; + case static_cast(ERR_PERMISSION_ERROR): + errCode = ERR_PERMISSION_ERROR; + break; + case static_cast(ERR_PARAMS): + errCode = ERR_PARAMS; + break; + case EPERM: + errCode = ERR_EPERM; + break; + default: + break; + } + return errCode; +} + +static std::string GetErrorMessage(int32_t errorCode) +{ + switch (errorCode) { + case ERR_DEVICE_NOT_SUPPORTED: + return "The device doesn't support this api"; + break; + case ERR_EPERM: + return "Operation not permitted"; + break; + case ERR_PERMISSION_ERROR: + return "Permission verification failed"; + break; + case ERR_PARAMS: + return "Parameter error"; + break; + default: + return "Unknown error"; + break; + } +} + ohos::fileshare::fileShare::PolicyInfo MakePolicyInfo(taihe::string_view uri, int32_t operationMode) { return {uri, operationMode}; @@ -120,22 +163,24 @@ void ActivatePermissionSync(taihe::array_view uriPolicies; if (GetUriPoliciesArg(policies, uriPolicies)) { LOGE("Failed to get URI policies"); - taihe::set_business_error(OHOS::FileManagement::LibN::E_PARAMS, "Failed to get URI policies"); + taihe::set_business_error(ErrorCodeConversion(OHOS::FileManagement::LibN::E_PARAMS), + GetErrorMessage(OHOS::FileManagement::LibN::E_PARAMS)); return; } std::shared_ptr arg = std::make_shared(); if (arg == nullptr) { LOGE("PolicyErrorArgs make make_shared failed"); - taihe::set_business_error(OHOS::FileManagement::LibN::E_UNKNOWN_ERROR, - "PolicyErrorArgs make make_shared failed"); + taihe::set_business_error(ErrorCodeConversion(OHOS::FileManagement::LibN::E_UNKNOWN_ERROR), + GetErrorMessage(OHOS::FileManagement::LibN::E_UNKNOWN_ERROR)); return; } - arg->errNo = OHOS::AppFileService::FilePermission::ActivatePermission(uriPolicies, arg->errorResults); + auto errCode = OHOS::AppFileService::FilePermission::ActivatePermission(uriPolicies, arg->errorResults); + arg->errNo = ErrorCodeConversion(errCode); if (arg->errNo) { LOGE("Activation failed"); - taihe::set_business_error(arg->errNo, "Activation failed"); + taihe::set_business_error(arg->errNo, GetErrorMessage(arg->errNo)); } } @@ -144,22 +189,23 @@ void DeactivatePermissionSync(taihe::array_view uriPolicies; if (GetUriPoliciesArg(policies, uriPolicies)) { LOGE("Failed to get URI policies"); - taihe::set_business_error(OHOS::FileManagement::LibN::E_PARAMS, "Failed to get URI policies"); + taihe::set_business_error(ErrorCodeConversion(OHOS::FileManagement::LibN::E_PARAMS), GetErrorMessage(OHOS::FileManagement::LibN::E_PARAMS)); return; } std::shared_ptr arg = std::make_shared(); if (arg == nullptr) { LOGE("PolicyErrorArgs make make_shared failed"); - taihe::set_business_error(OHOS::FileManagement::LibN::E_UNKNOWN_ERROR, - "PolicyErrorArgs make make_shared failed"); + taihe::set_business_error(ErrorCodeConversion(OHOS::FileManagement::LibN::E_UNKNOWN_ERROR), + GetErrorMessage(OHOS::FileManagement::LibN::E_UNKNOWN_ERROR)); return; } - arg->errNo = OHOS::AppFileService::FilePermission::DeactivatePermission(uriPolicies, arg->errorResults); + auto errCode = OHOS::AppFileService::FilePermission::DeactivatePermission(uriPolicies, arg->errorResults); + arg->errNo = ErrorCodeConversion(errCode); if (arg->errNo) { LOGE("Deactivation failed"); - taihe::set_business_error(arg->errNo, "Deactivation failed"); + taihe::set_business_error(arg->errNo, GetErrorMessage(arg->errNo)); return; } } @@ -304,8 +350,8 @@ void GrantUriPermissionSync(taihe::string_view uri, taihe::string_view bundleNam LOGD("fileShare::GrantUriPermission begin!"); if (!IsSystemApp()) { LOGE("fileShare::GrantUriPermission is not System App!"); - taihe::set_business_error(OHOS::FileManagement::LibN::E_PERMISSION_SYS, - "fileShare::GrantUriPermission is not System App!"); + taihe::set_business_error(ErrorCodeConversion(OHOS::FileManagement::LibN::E_PERMISSION_SYS), + GetErrorMessage(OHOS::FileManagement::LibN::E_PERMISSION_SYS)); return; } @@ -334,7 +380,7 @@ void GrantUriPermissionSync(taihe::string_view uri, taihe::string_view bundleNam int ret = DoGrantUriPermission(uriPermInfo); if (ret < 0) { LOGE("fileShare::GrantUriPermission DoGrantUriPermission failed with %{public}d", ret); - taihe::set_business_error(-ret, "fileShare::GrantUriPermission failed"); + taihe::set_business_error(ErrorCodeConversion(-ret), GetErrorMessage(-ret)); } } @@ -344,16 +390,16 @@ taihe::array CheckPathPermissionSync(int32_t tokenID, { if (!IsSystemApp()) { LOGE("fileShare::CheckPathPermissionSync is not System App!"); - taihe::set_business_error(OHOS::FileManagement::LibN::E_PERMISSION_SYS, - "fileShare::CheckPathPermissionSync is not System App!"); + taihe::set_business_error(ErrorCodeConversion(OHOS::FileManagement::LibN::E_PERMISSION_SYS), + GetErrorMessage(OHOS::FileManagement::LibN::E_PERMISSION_SYS)); return taihe::array::make(0); } int32_t callerTokenId = static_cast(OHOS::IPCSkeleton::GetCallingTokenID()); if (tokenID != callerTokenId) { if (!CheckTokenIdPermission(callerTokenId, "ohos.permission.CHECK_SANDBOX_POLICY")) { - taihe::set_business_error(OHOS::FileManagement::LibN::E_PERMISSION, - "fileShare::CheckPathPermissionSync checkPermission failed!"); + taihe::set_business_error(ErrorCodeConversion(OHOS::FileManagement::LibN::E_PERMISSION), + GetErrorMessage(OHOS::FileManagement::LibN::E_PERMISSION)); return taihe::array::make(0); } } @@ -380,8 +426,7 @@ taihe::array CheckPathPermissionSync(int32_t tokenID, taihe::set_business_error(arg->errNo, "Activation failed."); return taihe::array::make(0); } - taihe::array result(taihe::copy_data_t{}, arg->resultData.begin(), arg->resultData.size()); - return result; + return taihe::array(taihe::copy_data_t{}, arg->resultData.begin(), arg->resultData.size()); } } // namespace -- Gitee From bf3a6422e34460de7874406dc1cd3a7e00b9be7d Mon Sep 17 00:00:00 2001 From: BrainL Date: Sat, 9 Aug 2025 11:40:48 +0800 Subject: [PATCH 3/4] code check Signed-off-by: BrainL --- interfaces/kits/taihe/fileshare/src/fileshare_taihe.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/interfaces/kits/taihe/fileshare/src/fileshare_taihe.cpp b/interfaces/kits/taihe/fileshare/src/fileshare_taihe.cpp index ab2fc87fb..5f101495b 100644 --- a/interfaces/kits/taihe/fileshare/src/fileshare_taihe.cpp +++ b/interfaces/kits/taihe/fileshare/src/fileshare_taihe.cpp @@ -79,19 +79,14 @@ static std::string GetErrorMessage(int32_t errorCode) switch (errorCode) { case ERR_DEVICE_NOT_SUPPORTED: return "The device doesn't support this api"; - break; case ERR_EPERM: return "Operation not permitted"; - break; case ERR_PERMISSION_ERROR: return "Permission verification failed"; - break; case ERR_PARAMS: return "Parameter error"; - break; default: return "Unknown error"; - break; } } @@ -189,7 +184,8 @@ void DeactivatePermissionSync(taihe::array_view uriPolicies; if (GetUriPoliciesArg(policies, uriPolicies)) { LOGE("Failed to get URI policies"); - taihe::set_business_error(ErrorCodeConversion(OHOS::FileManagement::LibN::E_PARAMS), GetErrorMessage(OHOS::FileManagement::LibN::E_PARAMS)); + taihe::set_business_error(ErrorCodeConversion(OHOS::FileManagement::LibN::E_PARAMS), + GetErrorMessage(OHOS::FileManagement::LibN::E_PARAMS)); return; } -- Gitee From a96ceb25c90bf17b09662e672acf14f85e4ef3ee Mon Sep 17 00:00:00 2001 From: BrainL Date: Mon, 11 Aug 2025 18:35:16 +0800 Subject: [PATCH 4/4] fileuri add extend Signed-off-by: BrainL --- interfaces/kits/taihe/fileuri/BUILD.gn | 5 ++++- .../taihe/fileuri/idl/ohos.file.fileuri.taihe | 9 ++------- .../kits/taihe/fileuri/idl/ohos.fileuri.taihe | 20 +++++++++++++++++++ .../taihe/fileuri/include/fileuri_taihe.h | 3 +-- .../kits/taihe/fileuri/src/fileuri_taihe.cpp | 6 ------ 5 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 interfaces/kits/taihe/fileuri/idl/ohos.fileuri.taihe diff --git a/interfaces/kits/taihe/fileuri/BUILD.gn b/interfaces/kits/taihe/fileuri/BUILD.gn index f2f815cf4..f638ebf16 100644 --- a/interfaces/kits/taihe/fileuri/BUILD.gn +++ b/interfaces/kits/taihe/fileuri/BUILD.gn @@ -21,7 +21,10 @@ subsystem_name = "filemanagement" taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name/fileuri" copy_taihe_idl("copy_taihe") { - sources = [ "${app_file_service_path}/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe" ] + sources = [ + "${app_file_service_path}/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe", + "${app_file_service_path}/interfaces/kits/taihe/fileuri/idl/ohos.fileuri.taihe" + ] } ohos_taihe("run_taihe") { diff --git a/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe b/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe index 75bf78748..9b5488718 100644 --- a/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe +++ b/interfaces/kits/taihe/fileuri/idl/ohos.file.fileuri.taihe @@ -24,23 +24,18 @@ interface FileUriInner { @get getName(): String; } -@ctor("FileUriInner") -function makeFileUri(name: String): FileUriInner; - function createFileUri(name: String): FileUriInner; function getUriFromPath(path: String): String; @!sts_inject(""" -export class FileUri { +export class FileUri extends uri.URI{ inner: FileUriInner; constructor(uri: String) { + super(uri); this.inner = createFileUri(uri); } - constructor(inner: FileUriInner) { - this.inner = inner; - } get name(): string { return this.inner.name; } diff --git a/interfaces/kits/taihe/fileuri/idl/ohos.fileuri.taihe b/interfaces/kits/taihe/fileuri/idl/ohos.fileuri.taihe new file mode 100644 index 000000000..e732e5d8d --- /dev/null +++ b/interfaces/kits/taihe/fileuri/idl/ohos.fileuri.taihe @@ -0,0 +1,20 @@ +/* +* Copyright (c) 2025 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. +*/ + +@!namespace("@ohos.file.fileuri") + +@!sts_inject(""" +import uri from '@ohos.uri'; +""") \ No newline at end of file diff --git a/interfaces/kits/taihe/fileuri/include/fileuri_taihe.h b/interfaces/kits/taihe/fileuri/include/fileuri_taihe.h index ffcb69437..f9e94dc88 100644 --- a/interfaces/kits/taihe/fileuri/include/fileuri_taihe.h +++ b/interfaces/kits/taihe/fileuri/include/fileuri_taihe.h @@ -21,6 +21,7 @@ #include "ohos.file.fileuri.impl.hpp" #include "taihe/runtime.hpp" #include "file_uri.h" +#include "uri.h" namespace ANI::FileUri { @@ -36,8 +37,6 @@ private: OHOS::Uri uri_; }; -ohos::file::fileuri::FileUriInner makeFileUri(taihe::string_view name); - ohos::file::fileuri::FileUriInner createFileUri(taihe::string_view name); std::string getUriFromPath(taihe::string_view path); diff --git a/interfaces/kits/taihe/fileuri/src/fileuri_taihe.cpp b/interfaces/kits/taihe/fileuri/src/fileuri_taihe.cpp index 7f1fdc54d..59aaf85fa 100644 --- a/interfaces/kits/taihe/fileuri/src/fileuri_taihe.cpp +++ b/interfaces/kits/taihe/fileuri/src/fileuri_taihe.cpp @@ -42,11 +42,6 @@ std::string FileUriInner::getName() return sandboxPath.substr(posLast + 1); } -ohos::file::fileuri::FileUriInner makeFileUri(taihe::string_view name) -{ - return taihe::make_holder(name); -} - ohos::file::fileuri::FileUriInner createFileUri(taihe::string_view name) { return taihe::make_holder(name); @@ -60,6 +55,5 @@ std::string getUriFromPath(taihe::string_view path) } } // namespace ANI::FileUri -TH_EXPORT_CPP_API_makeFileUri(ANI::FileUri::makeFileUri); TH_EXPORT_CPP_API_createFileUri(ANI::FileUri::createFileUri); TH_EXPORT_CPP_API_getUriFromPath(ANI::FileUri::getUriFromPath); -- Gitee