From c440cdec1d06f3c29d17908355982f99d8bb2697 Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Mon, 27 Jun 2022 11:36:49 +0800 Subject: [PATCH 1/5] add file extension stub code Signed-off-by: wangjianqiang --- .../include/file_access_ext_stub.h | 48 +++ .../include/file_access_ext_stub_impl.h | 51 +++ .../file_access/src/file_access_ext_stub.cpp | 290 ++++++++++++++++++ .../src/file_access_ext_stub_impl.cpp | 123 ++++++++ 4 files changed, 512 insertions(+) create mode 100644 frameworks/innerkits/file_access/include/file_access_ext_stub.h create mode 100644 frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h create mode 100644 frameworks/innerkits/file_access/src/file_access_ext_stub.cpp create mode 100644 frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp diff --git a/frameworks/innerkits/file_access/include/file_access_ext_stub.h b/frameworks/innerkits/file_access/include/file_access_ext_stub.h new file mode 100644 index 00000000..7116eaa4 --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILE_EXT_STUB_H +#define FILE_EXT_STUB_H + +#include +#include + +#include "file_access_extension_info.h" +#include "ifile_access_ext_base.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileAccessExtStub : public IRemoteStub { +public: + FileAccessExtStub(); + ~FileAccessExtStub(); + int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; +private: + ErrCode CmdOpenFile(MessageParcel &data, MessageParcel &reply); + ErrCode CmdCreateFile(MessageParcel &data, MessageParcel &reply); + ErrCode CmdMkdir(MessageParcel &data, MessageParcel &reply); + ErrCode CmdDelete(MessageParcel &data, MessageParcel &reply); + ErrCode CmdMove(MessageParcel &data, MessageParcel &reply); + ErrCode CmdRename(MessageParcel &data, MessageParcel &reply); + + ErrCode CmdListFile(MessageParcel &data, MessageParcel &reply); + ErrCode CmdGetRoots(MessageParcel &data, MessageParcel &reply); + + using RequestFuncType = int (FileAccessExtStub::*)(MessageParcel &data, MessageParcel &reply); + std::map stubFuncMap_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_EXT_STUB_H \ No newline at end of file diff --git a/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h new file mode 100644 index 00000000..a213afae --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILE_EXT_STUB_IMPL_H +#define FILE_EXT_STUB_IMPL_H + +#include +#include "file_access_ext_stub.h" +#include "file_access_extension_info.h" +#include "file_access_ext_ability.h" +#include "native_engine/native_value.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileAccessExtStubImpl : public FileAccessExtStub { +public: + explicit FileAccessExtStubImpl(const std::shared_ptr& extension, napi_env env) + : extension_(extension) {} + + virtual ~FileAccessExtStubImpl() {} + + int OpenFile(const Uri &uri, int flags) override; + int CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile) override; + int Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile) override; + int Delete(const Uri &sourceFile) override; + int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override; + int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override; + + std::vector ListFile(const Uri &sourceFileUri) override; + std::vector GetRoots() override; +private: + std::shared_ptr GetOwner(); + +private: + std::shared_ptr extension_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_EXT_STUB_IMPL_H \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp new file mode 100644 index 00000000..c3c8bdeb --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp @@ -0,0 +1,290 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "file_access_ext_stub.h" + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +FileAccessExtStub::FileAccessExtStub() +{ + stubFuncMap_[CMD_OPEN_FILE] = &FileAccessExtStub::CmdOpenFile; + stubFuncMap_[CMD_CREATE_FILE] = &FileAccessExtStub::CmdCreateFile; + stubFuncMap_[CMD_MKDIR] = &FileAccessExtStub::CmdMkdir; + stubFuncMap_[CMD_DELETE] = &FileAccessExtStub::CmdDelete; + stubFuncMap_[CMD_MOVE] = &FileAccessExtStub::CmdMove; + stubFuncMap_[CMD_RENAME] = &FileAccessExtStub::CmdRename; + stubFuncMap_[CMD_LIST_FILE] = &FileAccessExtStub::CmdListFile; + stubFuncMap_[CMD_GET_ROOTS] = &FileAccessExtStub::CmdGetRoots; +} + +FileAccessExtStub::~FileAccessExtStub() +{ + stubFuncMap_.clear(); +} + +int FileAccessExtStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, + MessageOption& option) +{ + std::u16string descriptor = FileAccessExtStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + return ERR_INVALID_STATE; + } + + const auto &itFunc = stubFuncMap_.find(code); + if (itFunc != stubFuncMap_.end()) { + return (this->*(itFunc->second))(data, reply); + } + + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} + +ErrCode FileAccessExtStub::CmdOpenFile(MessageParcel &data, MessageParcel &reply) +{ + std::shared_ptr uri(data.ReadParcelable()); + if (uri == nullptr) { + HILOG_ERROR("%{public}s uri is nullptr", __func__); + return ERR_INVALID_VALUE; + } + int flags = data.ReadInt32(); + if (flags < 0) { + HILOG_ERROR("%{public}s mode is invalid", __func__); + return ERR_INVALID_VALUE; + } + int fd = OpenFile(*uri, flags); + if (fd < 0) { + HILOG_ERROR("%{public}s OpenFile fail, fd is %{pubilc}d", __func__, fd); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteFileDescriptor(fd)) { + HILOG_ERROR("%{public}s fail to WriteFileDescriptor fd", __func__); + return ERR_INVALID_VALUE; + } + return NO_ERROR; +} + +ErrCode FileAccessExtStub::CmdCreateFile(MessageParcel &data, MessageParcel &reply) +{ + std::shared_ptr parent(data.ReadParcelable()); + if (parent == nullptr) { + HILOG_ERROR("%{public}s parent is nullptr", __func__); + return ERR_INVALID_VALUE; + } + std::string displayName = data.ReadString(); + if (displayName.empty()) { + HILOG_ERROR("%{public}s mode is nullptr", __func__); + return ERR_INVALID_VALUE; + } + std::shared_ptr fileNew(data.ReadParcelable()); + if (fileNew == nullptr) { + HILOG_ERROR("%{public}s fileNew is nullptr", __func__); + return ERR_INVALID_VALUE; + } + + int ret = CreateFile(*parent, displayName, *fileNew); + if (ret < 0) { + HILOG_ERROR("%{public}s CreateFile fail, ret is %{pubilc}d", __func__, ret); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("%{public}s fail to WriteInt32 ret", __func__); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteParcelable(&(*fileNew))) { + HILOG_ERROR("%{public}s fail to WriteParcelable type", __func__); + return ERR_INVALID_VALUE; + } + return NO_ERROR; +} + +ErrCode FileAccessExtStub::CmdMkdir(MessageParcel &data, MessageParcel &reply) +{ + std::shared_ptr parent(data.ReadParcelable()); + if (parent == nullptr) { + HILOG_ERROR("%{public}s parent is nullptr", __func__); + return ERR_INVALID_VALUE; + } + std::string displayName = data.ReadString(); + if (displayName.empty()) { + HILOG_ERROR("%{public}s mode is nullptr", __func__); + return ERR_INVALID_VALUE; + } + std::shared_ptr fileNew(data.ReadParcelable()); + if (fileNew == nullptr) { + HILOG_ERROR("%{public}s fileNew is nullptr", __func__); + return ERR_INVALID_VALUE; + } + + int ret = Mkdir(*parent, displayName, *fileNew); + if (ret < 0) { + HILOG_ERROR("%{public}s Mkdir fail, ret is %{pubilc}d", __func__, ret); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("%{public}s fail to WriteInt32 ret", __func__); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteParcelable(&(*fileNew))) { + HILOG_ERROR("%{public}s fail to WriteParcelable type", __func__); + return ERR_INVALID_VALUE; + } + return NO_ERROR; +} + +ErrCode FileAccessExtStub::CmdDelete(MessageParcel &data, MessageParcel &reply) +{ + std::shared_ptr uri(data.ReadParcelable()); + if (uri == nullptr) { + HILOG_ERROR("%{public}s uri is nullptr", __func__); + return ERR_INVALID_VALUE; + } + + int ret = Delete(*uri); + if (ret < 0) { + HILOG_ERROR("%{public}s Delete fail, ret is %{pubilc}d", __func__, ret); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("%{public}s fail to WriteFileDescriptor ret", __func__); + return ERR_INVALID_VALUE; + } + return NO_ERROR; +} + +ErrCode FileAccessExtStub::CmdMove(MessageParcel &data, MessageParcel &reply) +{ + std::shared_ptr sourceFile(data.ReadParcelable()); + if (sourceFile == nullptr) { + HILOG_ERROR(" %{public}s sourceFile is nullptr", __func__); + return ERR_INVALID_VALUE; + } + std::shared_ptr targetParent(data.ReadParcelable()); + if (targetParent == nullptr) { + HILOG_ERROR(" %{public}s targetParent is nullptr", __func__); + return ERR_INVALID_VALUE; + } + std::shared_ptr fileNew(data.ReadParcelable()); + if (fileNew == nullptr) { + HILOG_ERROR(" %{public}s fileNew is nullptr", __func__); + return ERR_INVALID_VALUE; + } + + int ret = Move(*sourceFile, *targetParent, *fileNew); + if (ret < 0) { + HILOG_ERROR(" %{public}s fail, ret is %{pubilc}d", __func__, ret); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteInt32(ret)) { + HILOG_ERROR(" %{public}s fail to WriteInt32 ret", __func__); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteParcelable(&(*fileNew))) { + HILOG_ERROR(" %{public}s fail to WriteParcelable type", __func__); + return ERR_INVALID_VALUE; + } + return NO_ERROR; +} + +ErrCode FileAccessExtStub::CmdRename(MessageParcel &data, MessageParcel &reply) +{ + std::shared_ptr sourceFile(data.ReadParcelable()); + if (sourceFile == nullptr) { + HILOG_ERROR("%{public}s sourceFileUri is nullptr", __func__); + return ERR_INVALID_VALUE; + } + std::string displayName = data.ReadString(); + if (displayName.empty()) { + HILOG_ERROR("%{public}s mode is nullptr", __func__); + return ERR_INVALID_VALUE; + } + std::shared_ptr fileNew(data.ReadParcelable()); + if (fileNew == nullptr) { + HILOG_ERROR("%{public}s fileUriNew is nullptr", __func__); + return ERR_INVALID_VALUE; + } + + int ret = Rename(*sourceFile, displayName, *fileNew); + if (ret < 0) { + HILOG_ERROR("%{public}s Rename fail, ret is %{pubilc}d", __func__, ret); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("%{public}s fail to WriteInt32 ret", __func__); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteParcelable(&(*fileNew))) { + HILOG_ERROR("%{public}s fail to WriteParcelable type", __func__); + return ERR_INVALID_VALUE; + } + + return NO_ERROR; +} + +ErrCode FileAccessExtStub::CmdListFile(MessageParcel &data, MessageParcel &reply) +{ + std::shared_ptr uri(data.ReadParcelable()); + if (uri == nullptr) { + HILOG_ERROR("%{public}s uri is nullptr", __func__); + return ERR_INVALID_VALUE; + } + + std::vector vec = ListFile(*uri); + uint64_t count {vec.size()}; + if (!reply.WriteUint64(count)) { + HILOG_ERROR("%{public}s fail to WriteInt32 count", __func__); + return ERR_INVALID_VALUE; + } + for (uint64_t i = 0; i < count; i++) { + if (!reply.WriteParcelable(&vec[i])) { + HILOG_ERROR("%{public}s fail to WriteParcelable vec", __func__); + return ERR_INVALID_VALUE; + } + } + + return NO_ERROR; +} + +ErrCode FileAccessExtStub::CmdGetRoots(MessageParcel &data, MessageParcel &reply) +{ + + std::vector vec = GetRoots(); + uint64_t count {vec.size()}; + if (!reply.WriteUint64(count)) { + HILOG_ERROR("%{public}s fail to WriteInt32 count", __func__); + return ERR_INVALID_VALUE; + } + for (uint64_t i = 0; i < count; i++) { + if (!reply.WriteParcelable(&vec[i])) { + HILOG_ERROR("%{public}s fail to WriteParcelable ret", __func__); + return ERR_INVALID_VALUE; + } + } + + return NO_ERROR; +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp new file mode 100644 index 00000000..cca13e62 --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "file_access_ext_stub_impl.h" + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +std::shared_ptr FileAccessExtStubImpl::GetOwner() +{ + return extension_; +} + +int FileAccessExtStubImpl::OpenFile(const Uri &uri, int flags) +{ + int ret = -1; + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ret; + } + ret = extension->OpenFile(uri, flags); + return ret; +} + +int FileAccessExtStubImpl::CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile) +{ + int ret = -1; + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ret; + } + ret = extension->CreateFile(parent, displayName, newFile); + return ret; +} + +int FileAccessExtStubImpl::Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile) +{ + int ret = -1; + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ret; + } + ret = extension->Mkdir(parent, displayName, newFile); + return ret; +} + +int FileAccessExtStubImpl::Delete(const Uri &sourceFile) +{ + int ret = -1; + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ret; + } + ret = extension->Delete(sourceFile); + return ret; +} + +int FileAccessExtStubImpl::Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) +{ + int ret = -1; + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ret; + } + ret = extension->Move(sourceFile, targetParent, newFile); + return ret; +} + +int FileAccessExtStubImpl::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) +{ + int ret = -1; + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ret; + } + ret = extension->Rename(sourceFile, displayName, newFile); + return ret; +} + +std::vector FileAccessExtStubImpl::ListFile(const Uri &sourceFile) +{ + std::vector vec; + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return vec; + } + vec = extension->ListFile(sourceFile); + return vec; +} + +std::vector FileAccessExtStubImpl::GetRoots() +{ + std::vector vec; + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return vec; + } + vec = extension->GetRoots(); + return vec; +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file -- Gitee From a2a75008b64b69e7b6cb94d23434b32808c1f295 Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Mon, 27 Jun 2022 11:43:06 +0800 Subject: [PATCH 2/5] add file extension stub code Signed-off-by: wangjianqiang --- frameworks/innerkits/file_access/src/file_access_ext_stub.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp index c3c8bdeb..52668414 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp @@ -270,7 +270,6 @@ ErrCode FileAccessExtStub::CmdListFile(MessageParcel &data, MessageParcel &reply ErrCode FileAccessExtStub::CmdGetRoots(MessageParcel &data, MessageParcel &reply) { - std::vector vec = GetRoots(); uint64_t count {vec.size()}; if (!reply.WriteUint64(count)) { -- Gitee From c7601d63cbf3f2e62ed52ce2e85aee9fea0fd52a Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Tue, 28 Jun 2022 10:29:37 +0800 Subject: [PATCH 3/5] add permission check Signed-off-by: wangjianqiang --- .../include/file_access_ext_stub.h | 3 +-- .../file_access/src/file_access_ext_stub.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/frameworks/innerkits/file_access/include/file_access_ext_stub.h b/frameworks/innerkits/file_access/include/file_access_ext_stub.h index 7116eaa4..dfb2d71e 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_stub.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub.h @@ -36,10 +36,9 @@ private: ErrCode CmdDelete(MessageParcel &data, MessageParcel &reply); ErrCode CmdMove(MessageParcel &data, MessageParcel &reply); ErrCode CmdRename(MessageParcel &data, MessageParcel &reply); - ErrCode CmdListFile(MessageParcel &data, MessageParcel &reply); ErrCode CmdGetRoots(MessageParcel &data, MessageParcel &reply); - + bool CheckCallingPermission(const std::string &permission); using RequestFuncType = int (FileAccessExtStub::*)(MessageParcel &data, MessageParcel &reply); std::map stubFuncMap_; }; diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp index 52668414..b1d90120 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp @@ -15,7 +15,9 @@ #include "file_access_ext_stub.h" +#include "accesstoken_kit.h" #include "hilog_wrapper.h" +#include "ipc_skeleton.h" namespace OHOS { namespace FileAccessFwk { @@ -39,6 +41,12 @@ FileAccessExtStub::~FileAccessExtStub() int FileAccessExtStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) { + std::string permission = "ohos.permission.FILE_ACCESS_MANAGER"; + if (!CheckCallingPermission(permission)) { + HILOG_ERROR("FileAccessExtStub::%{public}s permission error", __func__); + return ERR_UNKNOWN_REASON; + } + std::u16string descriptor = FileAccessExtStub::GetDescriptor(); std::u16string remoteDescriptor = data.ReadInterfaceToken(); if (descriptor != remoteDescriptor) { @@ -285,5 +293,16 @@ ErrCode FileAccessExtStub::CmdGetRoots(MessageParcel &data, MessageParcel &reply return NO_ERROR; } + +bool FileAccessExtStub::CheckCallingPermission(const std::string &permission) +{ + Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission); + if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + HILOG_ERROR("FileAccessExtStub::CheckCallingPermission have no fileAccess permission"); + return false; + } + return true; +} } // namespace FileAccessFwk } // namespace OHOS \ No newline at end of file -- Gitee From 46db0202a4ddf0f747209a38235ce1b7196a87e7 Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Tue, 28 Jun 2022 23:07:38 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=9B=91=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangjianqiang --- .../include/file_access_ext_stub.h | 2 +- .../include/file_access_ext_stub_impl.h | 5 +- .../file_access/src/file_access_ext_stub.cpp | 183 ++++++++++-------- .../src/file_access_ext_stub_impl.cpp | 15 +- 4 files changed, 111 insertions(+), 94 deletions(-) diff --git a/frameworks/innerkits/file_access/include/file_access_ext_stub.h b/frameworks/innerkits/file_access/include/file_access_ext_stub.h index dfb2d71e..73c1552a 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_stub.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub.h @@ -27,7 +27,7 @@ namespace FileAccessFwk { class FileAccessExtStub : public IRemoteStub { public: FileAccessExtStub(); - ~FileAccessExtStub(); + virtual ~FileAccessExtStub(); int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; private: ErrCode CmdOpenFile(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h index a213afae..0883ed76 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h @@ -17,6 +17,7 @@ #define FILE_EXT_STUB_IMPL_H #include + #include "file_access_ext_stub.h" #include "file_access_extension_info.h" #include "file_access_ext_ability.h" @@ -37,13 +38,11 @@ public: int Delete(const Uri &sourceFile) override; int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override; int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override; - std::vector ListFile(const Uri &sourceFileUri) override; std::vector GetRoots() override; -private: - std::shared_ptr GetOwner(); private: + std::shared_ptr GetOwner(); std::shared_ptr extension_; }; } // namespace FileAccessFwk diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp index b1d90120..8e795ce1 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp @@ -16,11 +16,15 @@ #include "file_access_ext_stub.h" #include "accesstoken_kit.h" +#include "file_access_framework_errno.h" #include "hilog_wrapper.h" #include "ipc_skeleton.h" namespace OHOS { namespace FileAccessFwk { +namespace { + const std::string FILE_ACCESS_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; +} FileAccessExtStub::FileAccessExtStub() { stubFuncMap_[CMD_OPEN_FILE] = &FileAccessExtStub::CmdOpenFile; @@ -41,10 +45,9 @@ FileAccessExtStub::~FileAccessExtStub() int FileAccessExtStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) { - std::string permission = "ohos.permission.FILE_ACCESS_MANAGER"; - if (!CheckCallingPermission(permission)) { - HILOG_ERROR("FileAccessExtStub::%{public}s permission error", __func__); - return ERR_UNKNOWN_REASON; + if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) { + HILOG_ERROR("permission error"); + return ERR_PERMISSION_DENIED; } std::u16string descriptor = FileAccessExtStub::GetDescriptor(); @@ -65,215 +68,226 @@ ErrCode FileAccessExtStub::CmdOpenFile(MessageParcel &data, MessageParcel &reply { std::shared_ptr uri(data.ReadParcelable()); if (uri == nullptr) { - HILOG_ERROR("%{public}s uri is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("uri is nullptr"); + return ERR_INVALID_URI; } + int flags = data.ReadInt32(); if (flags < 0) { - HILOG_ERROR("%{public}s mode is invalid", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("flags is invalid"); + return ERR_ERROR; } + int fd = OpenFile(*uri, flags); if (fd < 0) { - HILOG_ERROR("%{public}s OpenFile fail, fd is %{pubilc}d", __func__, fd); - return ERR_INVALID_VALUE; + HILOG_ERROR("OpenFile fail, fd is %{pubilc}d", fd); + return ERR_INVALID_FD; } if (!reply.WriteFileDescriptor(fd)) { - HILOG_ERROR("%{public}s fail to WriteFileDescriptor fd", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteFileDescriptor fd"); + return ERR_IPC_ERROR; } - return NO_ERROR; + return ERR_OK; } ErrCode FileAccessExtStub::CmdCreateFile(MessageParcel &data, MessageParcel &reply) { std::shared_ptr parent(data.ReadParcelable()); if (parent == nullptr) { - HILOG_ERROR("%{public}s parent is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("parent is nullptr"); + return ERR_INVALID_URI; } + std::string displayName = data.ReadString(); if (displayName.empty()) { - HILOG_ERROR("%{public}s mode is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("displayName is nullptr"); + return ERR_INVALID_PARAM; } + std::shared_ptr fileNew(data.ReadParcelable()); if (fileNew == nullptr) { - HILOG_ERROR("%{public}s fileNew is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fileNew is nullptr"); + return ERR_INVALID_URI; } int ret = CreateFile(*parent, displayName, *fileNew); if (ret < 0) { - HILOG_ERROR("%{public}s CreateFile fail, ret is %{pubilc}d", __func__, ret); - return ERR_INVALID_VALUE; + HILOG_ERROR("CreateFile fail, ret is %{pubilc}d", ret); + return ERR_CREATE; } if (!reply.WriteInt32(ret)) { - HILOG_ERROR("%{public}s fail to WriteInt32 ret", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteInt32 ret"); + return ERR_IPC_ERROR; } if (!reply.WriteParcelable(&(*fileNew))) { - HILOG_ERROR("%{public}s fail to WriteParcelable type", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteParcelable type"); + return ERR_IPC_ERROR; } - return NO_ERROR; + return ERR_OK; } ErrCode FileAccessExtStub::CmdMkdir(MessageParcel &data, MessageParcel &reply) { std::shared_ptr parent(data.ReadParcelable()); if (parent == nullptr) { - HILOG_ERROR("%{public}s parent is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("parent is nullptr"); + return ERR_INVALID_URI; } + std::string displayName = data.ReadString(); if (displayName.empty()) { - HILOG_ERROR("%{public}s mode is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("mode is nullptr"); + return ERR_INVALID_PARAM; } + std::shared_ptr fileNew(data.ReadParcelable()); if (fileNew == nullptr) { - HILOG_ERROR("%{public}s fileNew is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fileNew is nullptr"); + return ERR_INVALID_URI; } int ret = Mkdir(*parent, displayName, *fileNew); if (ret < 0) { - HILOG_ERROR("%{public}s Mkdir fail, ret is %{pubilc}d", __func__, ret); - return ERR_INVALID_VALUE; + HILOG_ERROR("Mkdir fail, ret is %{pubilc}d", ret); + return ERR_CREATE; } if (!reply.WriteInt32(ret)) { - HILOG_ERROR("%{public}s fail to WriteInt32 ret", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteInt32 ret"); + return ERR_IPC_ERROR; } if (!reply.WriteParcelable(&(*fileNew))) { - HILOG_ERROR("%{public}s fail to WriteParcelable type", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteParcelable type"); + return ERR_IPC_ERROR; } - return NO_ERROR; + return ERR_OK; } ErrCode FileAccessExtStub::CmdDelete(MessageParcel &data, MessageParcel &reply) { std::shared_ptr uri(data.ReadParcelable()); if (uri == nullptr) { - HILOG_ERROR("%{public}s uri is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("uri is nullptr"); + return ERR_INVALID_URI; } int ret = Delete(*uri); if (ret < 0) { - HILOG_ERROR("%{public}s Delete fail, ret is %{pubilc}d", __func__, ret); - return ERR_INVALID_VALUE; + HILOG_ERROR("Delete fail, ret is %{pubilc}d", ret); + return ERR_DELETE; } if (!reply.WriteInt32(ret)) { - HILOG_ERROR("%{public}s fail to WriteFileDescriptor ret", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteFileDescriptor ret"); + return ERR_IPC_ERROR; } - return NO_ERROR; + return ERR_OK; } ErrCode FileAccessExtStub::CmdMove(MessageParcel &data, MessageParcel &reply) { std::shared_ptr sourceFile(data.ReadParcelable()); if (sourceFile == nullptr) { - HILOG_ERROR(" %{public}s sourceFile is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("sourceFile is nullptr"); + return ERR_INVALID_URI; } + std::shared_ptr targetParent(data.ReadParcelable()); if (targetParent == nullptr) { - HILOG_ERROR(" %{public}s targetParent is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("targetParent is nullptr"); + return ERR_INVALID_URI; } + std::shared_ptr fileNew(data.ReadParcelable()); if (fileNew == nullptr) { - HILOG_ERROR(" %{public}s fileNew is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR(" fileNew is nullptr"); + return ERR_INVALID_URI; } int ret = Move(*sourceFile, *targetParent, *fileNew); if (ret < 0) { - HILOG_ERROR(" %{public}s fail, ret is %{pubilc}d", __func__, ret); - return ERR_INVALID_VALUE; + HILOG_ERROR("Move fail, ret is %{pubilc}d", ret); + return ERR_MOVE; } if (!reply.WriteInt32(ret)) { - HILOG_ERROR(" %{public}s fail to WriteInt32 ret", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteInt32 ret"); + return ERR_IPC_ERROR; } if (!reply.WriteParcelable(&(*fileNew))) { - HILOG_ERROR(" %{public}s fail to WriteParcelable type", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteParcelable type"); + return ERR_IPC_ERROR; } - return NO_ERROR; + return ERR_OK; } ErrCode FileAccessExtStub::CmdRename(MessageParcel &data, MessageParcel &reply) { std::shared_ptr sourceFile(data.ReadParcelable()); if (sourceFile == nullptr) { - HILOG_ERROR("%{public}s sourceFileUri is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("sourceFileUri is nullptr"); + return ERR_INVALID_URI; } + std::string displayName = data.ReadString(); if (displayName.empty()) { - HILOG_ERROR("%{public}s mode is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("mode is nullptr"); + return ERR_INVALID_PARAM; } + std::shared_ptr fileNew(data.ReadParcelable()); if (fileNew == nullptr) { - HILOG_ERROR("%{public}s fileUriNew is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fileUriNew is nullptr"); + return ERR_INVALID_URI; } int ret = Rename(*sourceFile, displayName, *fileNew); if (ret < 0) { - HILOG_ERROR("%{public}s Rename fail, ret is %{pubilc}d", __func__, ret); - return ERR_INVALID_VALUE; + HILOG_ERROR("Rename fail, ret is %{pubilc}d", ret); + return ERR_RENAME; } if (!reply.WriteInt32(ret)) { - HILOG_ERROR("%{public}s fail to WriteInt32 ret", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteInt32 ret"); + return ERR_IPC_ERROR; } if (!reply.WriteParcelable(&(*fileNew))) { - HILOG_ERROR("%{public}s fail to WriteParcelable type", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteParcelable type"); + return ERR_IPC_ERROR; } - return NO_ERROR; + return ERR_OK; } ErrCode FileAccessExtStub::CmdListFile(MessageParcel &data, MessageParcel &reply) { std::shared_ptr uri(data.ReadParcelable()); if (uri == nullptr) { - HILOG_ERROR("%{public}s uri is nullptr", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("uri is nullptr"); + return ERR_INVALID_URI; } std::vector vec = ListFile(*uri); uint64_t count {vec.size()}; if (!reply.WriteUint64(count)) { - HILOG_ERROR("%{public}s fail to WriteInt32 count", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteInt32 count"); + return ERR_IPC_ERROR; } + for (uint64_t i = 0; i < count; i++) { if (!reply.WriteParcelable(&vec[i])) { - HILOG_ERROR("%{public}s fail to WriteParcelable vec", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteParcelable vec"); + return ERR_IPC_ERROR; } } - return NO_ERROR; + return ERR_OK; } ErrCode FileAccessExtStub::CmdGetRoots(MessageParcel &data, MessageParcel &reply) @@ -281,17 +295,18 @@ ErrCode FileAccessExtStub::CmdGetRoots(MessageParcel &data, MessageParcel &reply std::vector vec = GetRoots(); uint64_t count {vec.size()}; if (!reply.WriteUint64(count)) { - HILOG_ERROR("%{public}s fail to WriteInt32 count", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteInt32 count"); + return ERR_IPC_ERROR; } + for (uint64_t i = 0; i < count; i++) { if (!reply.WriteParcelable(&vec[i])) { - HILOG_ERROR("%{public}s fail to WriteParcelable ret", __func__); - return ERR_INVALID_VALUE; + HILOG_ERROR("fail to WriteParcelable ret"); + return ERR_IPC_ERROR; } } - return NO_ERROR; + return ERR_OK; } bool FileAccessExtStub::CheckCallingPermission(const std::string &permission) diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp index cca13e62..d7355fa7 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp @@ -16,6 +16,7 @@ #include "file_access_ext_stub_impl.h" #include "hilog_wrapper.h" +#include "file_access_framework_errno.h" namespace OHOS { namespace FileAccessFwk { @@ -26,7 +27,7 @@ std::shared_ptr FileAccessExtStubImpl::GetOwner() int FileAccessExtStubImpl::OpenFile(const Uri &uri, int flags) { - int ret = -1; + int ret = ERR_ERROR; auto extension = GetOwner(); if (extension == nullptr) { HILOG_ERROR("get extension failed."); @@ -38,7 +39,7 @@ int FileAccessExtStubImpl::OpenFile(const Uri &uri, int flags) int FileAccessExtStubImpl::CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile) { - int ret = -1; + int ret = ERR_ERROR; auto extension = GetOwner(); if (extension == nullptr) { HILOG_ERROR("get extension failed."); @@ -50,7 +51,7 @@ int FileAccessExtStubImpl::CreateFile(const Uri &parent, const std::string &disp int FileAccessExtStubImpl::Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile) { - int ret = -1; + int ret = ERR_ERROR; auto extension = GetOwner(); if (extension == nullptr) { HILOG_ERROR("get extension failed."); @@ -62,7 +63,7 @@ int FileAccessExtStubImpl::Mkdir(const Uri &parent, const std::string &displayNa int FileAccessExtStubImpl::Delete(const Uri &sourceFile) { - int ret = -1; + int ret = ERR_ERROR; auto extension = GetOwner(); if (extension == nullptr) { HILOG_ERROR("get extension failed."); @@ -74,7 +75,7 @@ int FileAccessExtStubImpl::Delete(const Uri &sourceFile) int FileAccessExtStubImpl::Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) { - int ret = -1; + int ret = ERR_ERROR; auto extension = GetOwner(); if (extension == nullptr) { HILOG_ERROR("get extension failed."); @@ -86,7 +87,7 @@ int FileAccessExtStubImpl::Move(const Uri &sourceFile, const Uri &targetParent, int FileAccessExtStubImpl::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) { - int ret = -1; + int ret = ERR_ERROR; auto extension = GetOwner(); if (extension == nullptr) { HILOG_ERROR("get extension failed."); @@ -104,6 +105,7 @@ std::vector FileAccessExtStubImpl::ListFile(const Uri &sourceFile) HILOG_ERROR("get extension failed."); return vec; } + vec = extension->ListFile(sourceFile); return vec; } @@ -116,6 +118,7 @@ std::vector FileAccessExtStubImpl::GetRoots() HILOG_ERROR("get extension failed."); return vec; } + vec = extension->GetRoots(); return vec; } -- Gitee From c628a4f097f707fda750e6f84e5c7ffd644c727d Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Wed, 29 Jun 2022 16:06:54 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangjianqiang --- .../innerkits/file_access/include/file_access_ext_stub_impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h index 0883ed76..1cd130ba 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h @@ -18,9 +18,9 @@ #include +#include "file_access_ext_ability.h" #include "file_access_ext_stub.h" #include "file_access_extension_info.h" -#include "file_access_ext_ability.h" #include "native_engine/native_value.h" namespace OHOS { -- Gitee