From 8a87f75c33195ba79de161dbaf667121e96aaf37 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Thu, 20 Oct 2022 14:55:37 +0800 Subject: [PATCH 01/17] fileio_add_openflag Signed-off-by: zhuhongtao666 --- .../kits/js/src/mod_fileio/common_func.cpp | 21 +++++++++++++++++++ .../kits/js/src/mod_fileio/common_func.h | 12 +++++++++++ .../kits/js/src/mod_fileio/module_v9.cpp | 2 ++ 3 files changed, 35 insertions(+) diff --git a/interfaces/kits/js/src/mod_fileio/common_func.cpp b/interfaces/kits/js/src/mod_fileio/common_func.cpp index b03d1e07c..e03d5c9de 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.cpp +++ b/interfaces/kits/js/src/mod_fileio/common_func.cpp @@ -32,6 +32,27 @@ namespace DistributedFS { namespace ModuleFileIO { using namespace std; +void InitOpenMode(napi_env env, napi_value exports) +{ + char propertyName[] = "OpenMode"; + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("READ_ONLY", NVal::CreateInt32(env, RDONLY).val_), + DECLARE_NAPI_STATIC_PROPERTY("WRITE_ONLY", NVal::CreateInt32(env, WRONLY).val_), + DECLARE_NAPI_STATIC_PROPERTY("READ_WRITE", NVal::CreateInt32(env, RDWR).val_), + DECLARE_NAPI_STATIC_PROPERTY("CREATE", NVal::CreateInt32(env, CREATE).val_), + DECLARE_NAPI_STATIC_PROPERTY("TRUNC", NVal::CreateInt32(env, TRUNC).val_), + DECLARE_NAPI_STATIC_PROPERTY("APPEND", NVal::CreateInt32(env, APPEND).val_), + DECLARE_NAPI_STATIC_PROPERTY("NONBLOCK", NVal::CreateInt32(env, NONBLOCK).val_), + DECLARE_NAPI_STATIC_PROPERTY("DIR", NVal::CreateInt32(env, DIRECTORY).val_), + DECLARE_NAPI_STATIC_PROPERTY("NOFOLLOW", NVal::CreateInt32(env, NOFOLLOW).val_), + DECLARE_NAPI_STATIC_PROPERTY("SYNC", NVal::CreateInt32(env, SYNC).val_), + }; + napi_value obj = nullptr; + napi_create_object(env, &obj); + napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc); + napi_set_named_property(env, exports, propertyName, obj); +} + static tuple GetActualBuf(napi_env env, void *rawBuf, int64_t bufLen, NVal op) { bool succ = false; diff --git a/interfaces/kits/js/src/mod_fileio/common_func.h b/interfaces/kits/js/src/mod_fileio/common_func.h index 5c0adb2e1..2ef467d28 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.h +++ b/interfaces/kits/js/src/mod_fileio/common_func.h @@ -22,6 +22,18 @@ namespace OHOS { namespace DistributedFS { namespace ModuleFileIO { constexpr int64_t INVALID_POSITION = std::numeric_limits::max(); +constexpr int RDONLY = 00; +constexpr int WRONLY = 01; +constexpr int RDWR = 02; +constexpr int CREATE = 0100; +constexpr int TRUNC = 01000; +constexpr int APPEND = 02000; +constexpr int NONBLOCK = 04000; +constexpr int DIRECTORY = 0200000; +constexpr int NOFOLLOW = 0400000; +constexpr int SYNC = 04010000; + +void InitOpenMode(napi_env env, napi_value exports); struct CommonFunc { static int ConvertJsFlags(int &flags); diff --git a/interfaces/kits/js/src/mod_fileio/module_v9.cpp b/interfaces/kits/js/src/mod_fileio/module_v9.cpp index 7f2bb4bce..2e1cc8635 100644 --- a/interfaces/kits/js/src/mod_fileio/module_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/module_v9.cpp @@ -19,6 +19,7 @@ #include "../common/log.h" #include "class_file/file_n_exporter.h" #include "class_stat_v9/stat_n_exporter_v9.h" +#include "common_func.h" #include "properties/prop_n_exporter_v9.h" using namespace std; @@ -28,6 +29,7 @@ namespace DistributedFS { namespace ModuleFileIO { static napi_value Export(napi_env env, napi_value exports) { + InitOpenMode(env, exports); std::vector> products; products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); -- Gitee From 616706bb86cf2d28c5e2be426f811c0ea4654d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cgaoshunli?= <“gaoshunli@huawei.com> Date: Fri, 21 Oct 2022 15:20:06 +0800 Subject: [PATCH 02/17] codex clean MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: “gaoshunli" --- interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp | 4 ++-- interfaces/kits/js/src/mod_fileio/properties/open_v9.h | 2 +- interfaces/kits/js/src/mod_fileio/properties/stat_v9.cpp | 4 ++-- interfaces/kits/js/src/mod_fileio/properties/stat_v9.h | 2 +- interfaces/kits/js/src/mod_fileio/properties/truncate_v9.cpp | 5 ++--- interfaces/kits/js/src/mod_fileio/properties/truncate_v9.h | 2 +- 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp index 208a938e0..a8a1542f4 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp @@ -146,11 +146,11 @@ napi_value OpenV9::Async(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) { - return NAsyncWorkPromise(env, thisVar).Schedule(openV9ProcedureName, cbExec, cbCompl).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_OPEN_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::SECOND); NVal cb(env, funcArg[cbIdx]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(openV9ProcedureName, cbExec, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_OPEN_NAME, cbExec, cbCompl).val_; } } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.h b/interfaces/kits/js/src/mod_fileio/properties/open_v9.h index 5bb57a661..93fbfd3e0 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.h +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.h @@ -26,7 +26,7 @@ public: static napi_value Async(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); }; -const std::string openV9ProcedureName = "FILEIO_OPEN_V9"; +const std::string PROCEDURE_OPEN_NAME = "FileIOOpenV9"; } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/properties/stat_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/stat_v9.cpp index e777f821c..86322baa7 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/stat_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/stat_v9.cpp @@ -142,10 +142,10 @@ napi_value StatV9::Async(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureStatName, cbExec, cbCompl).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STAT_NAME, cbExec, cbCompl).val_; } else { NVal cb(env, funcArg[NARG_POS::SECOND]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureStatName, cbExec, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STAT_NAME, cbExec, cbCompl).val_; } } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fileio/properties/stat_v9.h b/interfaces/kits/js/src/mod_fileio/properties/stat_v9.h index 08eed2e5a..ecdd703cd 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/stat_v9.h +++ b/interfaces/kits/js/src/mod_fileio/properties/stat_v9.h @@ -26,7 +26,7 @@ public: static napi_value Async(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); }; -const std::string procedureStatName = "FILEIO_STAT_V9"; +const std::string PROCEDURE_STAT_NAME = "FileIOStatV9"; } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.cpp index e76741757..fd7777b63 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.cpp @@ -122,14 +122,13 @@ napi_value TruncateV9::Async(napi_env env, napi_callback_info info) return NVal::CreateUndefined(env); } }; - NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureTruncateName, cbExec, cbCompl).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; } else { NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureTruncateName, cbExec, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; } } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.h b/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.h index 37217db45..2defe6b2d 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.h +++ b/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.h @@ -29,7 +29,7 @@ public: static napi_value Sync(napi_env env, napi_callback_info info); static napi_value Async(napi_env env, napi_callback_info info); }; -const std::string procedureTruncateName = "FILEIO_TRUNCATE_V9"; +const std::string PROCEDURE_TRUNCATE_NAME = "FileIOTruncateV9"; } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS -- Gitee From b8650cd0c0bdc7dbb99cfe4386263b27063099ec Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Thu, 27 Oct 2022 11:37:04 +0800 Subject: [PATCH 03/17] bugfix_fileio_openv9 Signed-off-by: zhuhongtao666 --- interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp index a8a1542f4..46727c7e5 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp @@ -80,6 +80,7 @@ napi_value OpenV9::Sync(napi_env env, napi_callback_info info) } auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succPath) { + UniError(EINVAL).ThrowErr(env); return nullptr; } auto [succMode, mode] = GetJsFlags(env, funcArg); @@ -115,6 +116,7 @@ napi_value OpenV9::Async(napi_env env, napi_callback_info info) } auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succPath) { + UniError(EINVAL).ThrowErr(env); return nullptr; } auto [succMode, mode] = GetJsFlags(env, funcArg); -- Gitee From 0211490bdddae38359b16526844b5dc5251506fb Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Tue, 25 Oct 2022 12:39:12 +0000 Subject: [PATCH 04/17] add TDD testcases for remoteUri Signed-off-by: zhangkaixiang --- .../kits/native/remote_uri/remote_uri.cpp | 2 +- interfaces/test/unittest/BUILD.gn | 17 ++ interfaces/test/unittest/remote_uri/BUILD.gn | 40 ++++ .../unittest/remote_uri/remote_uri_test.cpp | 176 ++++++++++++++++++ .../test/unittest/resource/ohos_test.xml | 22 +++ .../unittest/resource/remote_uri_test.txt | 13 ++ 6 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 interfaces/test/unittest/BUILD.gn create mode 100644 interfaces/test/unittest/remote_uri/BUILD.gn create mode 100644 interfaces/test/unittest/remote_uri/remote_uri_test.cpp create mode 100644 interfaces/test/unittest/resource/ohos_test.xml create mode 100644 interfaces/test/unittest/resource/remote_uri_test.txt diff --git a/interfaces/kits/native/remote_uri/remote_uri.cpp b/interfaces/kits/native/remote_uri/remote_uri.cpp index 3a7958596..dddc68725 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.cpp +++ b/interfaces/kits/native/remote_uri/remote_uri.cpp @@ -89,7 +89,7 @@ bool RemoteUri::IsRemoteUri(const string& path, int &fd, const int& flags) int RemoteUri::ConvertUri(const int &fd, string &remoteUri) { if (fd < 0) { - return EINVAL; + return -EINVAL; } if (fdFromBinder.size() == MAX_URI_SIZE) { diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn new file mode 100644 index 000000000..4351bd3fe --- /dev/null +++ b/interfaces/test/unittest/BUILD.gn @@ -0,0 +1,17 @@ +# 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. + +group("unittest") { + testonly = true + deps = [ "remote_uri:remote_uri_test" ] +} diff --git a/interfaces/test/unittest/remote_uri/BUILD.gn b/interfaces/test/unittest/remote_uri/BUILD.gn new file mode 100644 index 000000000..fc1c7f1e7 --- /dev/null +++ b/interfaces/test/unittest/remote_uri/BUILD.gn @@ -0,0 +1,40 @@ +# 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. + +import("//build/test.gni") + +ohos_unittest("remote_uri_test") { + module_out_path = "filemanagement/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ "remote_uri_test.cpp" ] + + include_dirs = [ + "include", + "//utils/system/safwk/native/include", + "//commonlibrary/c_utils/base/include", + "//foundation/filemanagement/file_api/interfaces/kits/native/remote_uri", + ] + + deps = [ + "//foundation/filemanagement/file_api/interfaces/kits/native:remote_uri_native", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "ipc:ipc_core", + ] +} diff --git a/interfaces/test/unittest/remote_uri/remote_uri_test.cpp b/interfaces/test/unittest/remote_uri/remote_uri_test.cpp new file mode 100644 index 000000000..39d2951e2 --- /dev/null +++ b/interfaces/test/unittest/remote_uri/remote_uri_test.cpp @@ -0,0 +1,176 @@ +/* + * 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 +#include +#include +#include + +#include "remote_uri.h" + +namespace { + using namespace std; + using namespace OHOS::DistributedFS::ModuleRemoteUri; + + class RemoteUriTest : public testing::Test { + public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; + }; + + /** + * @tc.name: Remote_uri_ConvertUri_0000 + * @tc.desc: Test function of ConvertUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_ConvertUri_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_ConvertUri_0000"; + int fd = 10; + string remoteUri = ""; + string expectUri = "datashare:////#fdFromBinder=10"; + int ret = RemoteUri::ConvertUri(fd, remoteUri); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(remoteUri == expectUri); + + fd = -1; + ret = RemoteUri::ConvertUri(fd, remoteUri); + EXPECT_TRUE(ret == -EINVAL); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_ConvertUri_0000"; + } + + /** + * @tc.name: Remote_uri_ConvertUri_0001 + * @tc.desc: Test function of ConvertUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_ConvertUri_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_ConvertUri_0001"; + const string fileStr = "/data/test/remote_uri_test.txt"; + int maxUriSize = 128; + vectorremoteUriRecords; + vectorfdRecords; + for (int i = 0; i < maxUriSize + 1; i++) { + int fd = open(fileStr.c_str(), O_RDWR); + string remoteUri = ""; + int ret = RemoteUri::ConvertUri(fd, remoteUri); + EXPECT_TRUE(ret == 0); + remoteUriRecords.emplace_back(remoteUri); + fdRecords.emplace_back(fd); + } + for (int i = 0; i < maxUriSize + 1; i++) { + int fd = 0; + bool ret = RemoteUri::IsRemoteUri(remoteUriRecords[i], fd); + EXPECT_TRUE(ret == true); + EXPECT_EQ(fd, fdRecords[i]); + } + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_ConvertUri_0001"; + } + + /** + * @tc.name: Remote_uri_OpenRemoteUri_0002 + * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri_0002"; + string remoteUri = "datashar:////#fdFromBinder=10"; + int ret = RemoteUri::OpenRemoteUri(remoteUri); + EXPECT_TRUE(ret == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0002"; + } + + /** + * @tc.name: Remote_uri_OpenRemoteUri_0003 + * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri_0003"; + string remoteUri = "datashare:////fdFromBinder=10"; + int ret = RemoteUri::OpenRemoteUri(remoteUri); + EXPECT_TRUE(ret == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0003"; + } + + /** + * @tc.name: Remote_uri_OpenRemoteUri_0004 + * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri_0004"; + string remoteUri = "datashare:////#fdFromBinde=10"; + int ret = RemoteUri::OpenRemoteUri(remoteUri); + EXPECT_TRUE(ret == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0004"; + } + + /** + * @tc.name: Remote_uri_OpenRemoteUri_0005 + * @tc.desc: Test function of OpenRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_OpenRemoteUri_0005, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_OpenRemoteUri0005"; + string remoteUri = "datashare:////#fdFromBinder=10abc"; + int ret = RemoteUri::OpenRemoteUri(remoteUri); + EXPECT_TRUE(ret == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_OpenRemoteUri_0005"; + } + + /** + * @tc.name: Remote_uri_IsRemoteUri_006 + * @tc.desc: Test function of IsRemoteUri() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(RemoteUriTest, Remote_uri_IsRemoteUri_006, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "RemoteUriTest-begin Remote_uri_IsRemoteUri_0006"; + string remoteUri = "datashare:////#fdFromBinder=10"; + int fd = 0; + (void)RemoteUri::IsRemoteUri(remoteUri, fd, O_RDWR); + EXPECT_TRUE(fd == -1); + GTEST_LOG_(INFO) << "RemoteUriTest-end Remote_uri_IsRemoteUri_0006"; + } +} diff --git a/interfaces/test/unittest/resource/ohos_test.xml b/interfaces/test/unittest/resource/ohos_test.xml new file mode 100644 index 000000000..0cd402707 --- /dev/null +++ b/interfaces/test/unittest/resource/ohos_test.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/interfaces/test/unittest/resource/remote_uri_test.txt b/interfaces/test/unittest/resource/remote_uri_test.txt new file mode 100644 index 000000000..d0a7e644d --- /dev/null +++ b/interfaces/test/unittest/resource/remote_uri_test.txt @@ -0,0 +1,13 @@ +# 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. +remote_uri_test -- Gitee From de1287c22fc487e5d0abe112cec0e104a860572e Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Mon, 31 Oct 2022 02:31:07 +0000 Subject: [PATCH 05/17] fix up TDD test bug Signed-off-by: zhangkaixiang --- bundle.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bundle.json b/bundle.json index 8d2e75611..be6f41052 100644 --- a/bundle.json +++ b/bundle.json @@ -55,6 +55,9 @@ "header_base": "//foundation/filemanagement/file_api/interfaces/kits/native/remote_uri" } } + ], + "test": [ + "//foundation/filemanagement/file_api/interfaces/test/unittest:unittest" ] } } -- Gitee From e5956829224c25a6510e53b6fc6c87be96298bce Mon Sep 17 00:00:00 2001 From: fupc90 Date: Tue, 8 Nov 2022 16:14:07 +0800 Subject: [PATCH 06/17] =?UTF-8?q?write=E5=BC=82=E6=AD=A5=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E8=B5=8B=E7=A9=BA=E7=B1=BB=E5=9E=8B=E8=A7=84=E9=81=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fupc90 --- .../kits/js/src/mod_fileio/properties/prop_n_exporter.cpp | 5 +++-- .../kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp | 5 +++-- interfaces/kits/js/src/mod_fileio/properties/read_text.cpp | 4 ++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp index e7fbbb12e..a7a1acbac 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp @@ -510,7 +510,7 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) bool hasOp = false; if (argc == NARG_CNT::THREE) { NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length")) { + if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length")|| !op.TypeIs(napi_function)) { hasOp = true; } } @@ -589,7 +589,8 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) size_t argc = funcArg.GetArgc(); if (argc == NARG_CNT::THREE) { NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length") || op.HasProp("encoding")) { + if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length") || + op.HasProp("encoding") || !op.TypeIs(napi_function)) { hasOp = true; } } diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp index bd27e241c..0c94dcc94 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp @@ -135,7 +135,7 @@ napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) bool hasOp = false; if (funcArg.GetArgc() == NARG_CNT::THREE) { NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("length")) { + if (op.HasProp("offset") || op.HasProp("length")|| !op.TypeIs(napi_function)) { hasOp = true; } } @@ -215,7 +215,8 @@ napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) bool hasOp = false; if (funcArg.GetArgc() == NARG_CNT::THREE) { NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length") || op.HasProp("encoding")) { + if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length") || + op.HasProp("encoding") || !op.TypeIs(napi_function)) { hasOp = true; } } diff --git a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp index 331227632..afd1135f5 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp @@ -65,6 +65,10 @@ static tuple, bool> GetReadText hasOp = true; } + if (!op.TypeIs(napi_function)) { + hasOp = true; + } + return { true, position, hasLen, len, move(encoding), hasOp }; } -- Gitee From 6156fc610c5292d8d9da4a8363b74b4911618c35 Mon Sep 17 00:00:00 2001 From: fupc90 Date: Fri, 11 Nov 2022 02:02:17 +0000 Subject: [PATCH 07/17] update interfaces/kits/js/src/common/uni_error.cpp. Signed-off-by: fupc90 --- interfaces/kits/js/src/common/uni_error.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/kits/js/src/common/uni_error.cpp b/interfaces/kits/js/src/common/uni_error.cpp index 35f8db1b4..f03b2ea22 100644 --- a/interfaces/kits/js/src/common/uni_error.cpp +++ b/interfaces/kits/js/src/common/uni_error.cpp @@ -35,6 +35,7 @@ static napi_value GenerateBusinessError(napi_env env, int32_t errCode, string er msg = NVal::CreateUTF8String(env, errMsg).val_; napi_create_error(env, nullptr, msg, &businessError); napi_set_named_property(env, businessError, FILEIO_TAG_ERR_CODE.c_str(), code); + napi_set_named_property(env, businessError, FILEIO_TAG_ERR_MSG.c_str(), code); return businessError; } -- Gitee From 7ce18b6c04d41daf669af1d16572c1097d2f9e86 Mon Sep 17 00:00:00 2001 From: fupc90 Date: Fri, 11 Nov 2022 02:03:07 +0000 Subject: [PATCH 08/17] update interfaces/kits/js/src/common/uni_error.h. Signed-off-by: fupc90 --- interfaces/kits/js/src/common/uni_error.h | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/kits/js/src/common/uni_error.h b/interfaces/kits/js/src/common/uni_error.h index 80fdc437c..5c2f46100 100644 --- a/interfaces/kits/js/src/common/uni_error.h +++ b/interfaces/kits/js/src/common/uni_error.h @@ -26,6 +26,7 @@ namespace DistributedFS { constexpr int ERRNO_NOERR = 0; constexpr int FILEIO_SYS_CAP_TAG = 13900000; const std::string FILEIO_TAG_ERR_CODE = "code"; +const std::string FILEIO_TAG_ERR_MSG = "msg"; enum ELegacy { ELEGACY_INVAL = 202, -- Gitee From 2ef29c16ead97a04ec6f0523394203ead302baef Mon Sep 17 00:00:00 2001 From: fupc90 Date: Fri, 11 Nov 2022 03:08:44 +0000 Subject: [PATCH 09/17] update interfaces/kits/js/src/common/uni_error.cpp. Signed-off-by: fupc90 --- interfaces/kits/js/src/common/uni_error.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/common/uni_error.cpp b/interfaces/kits/js/src/common/uni_error.cpp index f03b2ea22..05395ca43 100644 --- a/interfaces/kits/js/src/common/uni_error.cpp +++ b/interfaces/kits/js/src/common/uni_error.cpp @@ -35,7 +35,7 @@ static napi_value GenerateBusinessError(napi_env env, int32_t errCode, string er msg = NVal::CreateUTF8String(env, errMsg).val_; napi_create_error(env, nullptr, msg, &businessError); napi_set_named_property(env, businessError, FILEIO_TAG_ERR_CODE.c_str(), code); - napi_set_named_property(env, businessError, FILEIO_TAG_ERR_MSG.c_str(), code); + napi_set_named_property(env, businessError, FILEIO_TAG_ERR_MSG.c_str(), msg); return businessError; } -- Gitee From 3558d9ea3e861c314906076706ec97c531d3d011 Mon Sep 17 00:00:00 2001 From: fupc90 Date: Fri, 11 Nov 2022 06:35:39 +0000 Subject: [PATCH 10/17] update interfaces/kits/js/src/common/uni_error.h. Signed-off-by: fupc90 --- interfaces/kits/js/src/common/uni_error.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/common/uni_error.h b/interfaces/kits/js/src/common/uni_error.h index 5c2f46100..b9d2b7a30 100644 --- a/interfaces/kits/js/src/common/uni_error.h +++ b/interfaces/kits/js/src/common/uni_error.h @@ -26,7 +26,7 @@ namespace DistributedFS { constexpr int ERRNO_NOERR = 0; constexpr int FILEIO_SYS_CAP_TAG = 13900000; const std::string FILEIO_TAG_ERR_CODE = "code"; -const std::string FILEIO_TAG_ERR_MSG = "msg"; +const std::string FILEIO_TAG_ERR_MSG = "message"; enum ELegacy { ELEGACY_INVAL = 202, -- Gitee From c892b7720246175453282f2ed43ec46e93bb7848 Mon Sep 17 00:00:00 2001 From: fupc90 Date: Mon, 14 Nov 2022 08:20:42 +0000 Subject: [PATCH 11/17] update interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp. Signed-off-by: fupc90 --- interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp index 46727c7e5..6ab20d052 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp @@ -40,7 +40,7 @@ static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg) { int mode = O_RDONLY; bool succ = false; - if (funcArg.GetArgc() == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) { + if (funcArg.GetArgc() >= NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) { tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); if (!succ) { UniError(EINVAL).ThrowErr(env); -- Gitee From b20498706a87eaed388ff210d5019ca7dcd39e0b Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Tue, 15 Nov 2022 12:53:00 +0000 Subject: [PATCH 12/17] update interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp. Signed-off-by: zhuhongtao666 --- interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp index 46727c7e5..78bbbf04e 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp @@ -64,7 +64,7 @@ static NVal InstantiateFile(napi_env env, int fd, string path) UniError(EIO).ThrowErr(env); return NVal(); } - auto fdg = make_unique(fd); + auto fdg = make_unique(fd, false); rafEntity->fd_.swap(fdg); rafEntity->path_ = path; rafEntity->uri_ = ""; -- Gitee From 14f2eac0167c8722750a7635346635328edc32db Mon Sep 17 00:00:00 2001 From: zhuhongtao66 Date: Thu, 17 Nov 2022 10:59:04 +0800 Subject: [PATCH 13/17] Add a sync lock to Dirent's Close interface Signed-off-by: zhuhongtao66 --- interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp index 43bae0a64..b94cfe121 100644 --- a/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp @@ -60,6 +60,7 @@ napi_value DirNExporter::CloseSync(napi_env env, napi_callback_info info) return nullptr; } + lock_guard(dirEntity->lock_); dirEntity->dir_.reset(); return nullptr; } @@ -84,6 +85,7 @@ napi_value DirNExporter::Close(napi_env env, napi_callback_info info) } auto cbExec = [dirEntity](napi_env env) -> UniError { + lock_guard(dirEntity->lock_); DIR *dir = dirEntity->dir_.release(); int ret = closedir(dir); if (ret == -1) { -- Gitee From 9a192354da4d4c2bc6e2ea3accbd00f75ce95094 Mon Sep 17 00:00:00 2001 From: onexiaomin Date: Wed, 16 Nov 2022 17:05:10 +0800 Subject: [PATCH 14/17] Description: Change the parameter name to be the same as the declaration Sig:OpenHarmony/filemanagement_file_api Feature or Bugfix:Bugfix Binary Source: No Signed-off-by: onexiaomin --- .../class_dirent/dirent_n_exporter.cpp | 44 +++++++++---------- .../properties/create_randomaccessfile.cpp | 16 +++---- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/interfaces/kits/js/src/mod_fileio/class_dirent/dirent_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_dirent/dirent_n_exporter.cpp index 1e316221e..29b78ca89 100644 --- a/interfaces/kits/js/src/mod_fileio/class_dirent/dirent_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_dirent/dirent_n_exporter.cpp @@ -34,9 +34,9 @@ namespace DistributedFS { namespace ModuleFileIO { using namespace std; -static DirentEntity *GetDirentEntity(napi_env env, napi_callback_info info) +static DirentEntity *GetDirentEntity(napi_env env, napi_callback_info cbinfo) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, cbinfo); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; @@ -50,9 +50,9 @@ static DirentEntity *GetDirentEntity(napi_env env, napi_callback_info info) return direntEntity; } -static napi_value CheckDirentDType(napi_env env, napi_callback_info info, unsigned char dType) +static napi_value CheckDirentDType(napi_env env, napi_callback_info cbinfo, unsigned char dType) { - DirentEntity *direntEntity = GetDirentEntity(env, info); + DirentEntity *direntEntity = GetDirentEntity(env, cbinfo); if (!direntEntity) { return nullptr; } @@ -60,53 +60,53 @@ static napi_value CheckDirentDType(napi_env env, napi_callback_info info, unsign return NVal::CreateBool(env, direntEntity->dirent_.d_type == dType).val_; } -napi_value DirentNExporter::isBlockDevice(napi_env env, napi_callback_info info) +napi_value DirentNExporter::isBlockDevice(napi_env env, napi_callback_info cbinfo) { - return CheckDirentDType(env, info, DT_BLK); + return CheckDirentDType(env, cbinfo, DT_BLK); } -napi_value DirentNExporter::isCharacterDevice(napi_env env, napi_callback_info info) +napi_value DirentNExporter::isCharacterDevice(napi_env env, napi_callback_info cbinfo) { - return CheckDirentDType(env, info, DT_CHR); + return CheckDirentDType(env, cbinfo, DT_CHR); } -napi_value DirentNExporter::isDirectory(napi_env env, napi_callback_info info) +napi_value DirentNExporter::isDirectory(napi_env env, napi_callback_info cbinfo) { - return CheckDirentDType(env, info, DT_DIR); + return CheckDirentDType(env, cbinfo, DT_DIR); } -napi_value DirentNExporter::isFIFO(napi_env env, napi_callback_info info) +napi_value DirentNExporter::isFIFO(napi_env env, napi_callback_info cbinfo) { - return CheckDirentDType(env, info, DT_FIFO); + return CheckDirentDType(env, cbinfo, DT_FIFO); } -napi_value DirentNExporter::isFile(napi_env env, napi_callback_info info) +napi_value DirentNExporter::isFile(napi_env env, napi_callback_info cbinfo) { - return CheckDirentDType(env, info, DT_REG); + return CheckDirentDType(env, cbinfo, DT_REG); } -napi_value DirentNExporter::isSocket(napi_env env, napi_callback_info info) +napi_value DirentNExporter::isSocket(napi_env env, napi_callback_info cbinfo) { - return CheckDirentDType(env, info, DT_SOCK); + return CheckDirentDType(env, cbinfo, DT_SOCK); } -napi_value DirentNExporter::isSymbolicLink(napi_env env, napi_callback_info info) +napi_value DirentNExporter::isSymbolicLink(napi_env env, napi_callback_info cbinfo) { - return CheckDirentDType(env, info, DT_LNK); + return CheckDirentDType(env, cbinfo, DT_LNK); } -napi_value DirentNExporter::GetName(napi_env env, napi_callback_info info) +napi_value DirentNExporter::GetName(napi_env env, napi_callback_info cbinfo) { - DirentEntity *direntEntity = GetDirentEntity(env, info); + DirentEntity *direntEntity = GetDirentEntity(env, cbinfo); if (!direntEntity) { return nullptr; } return NVal::CreateUTF8String(env, direntEntity->dirent_.d_name).val_; } -napi_value DirentNExporter::Constructor(napi_env env, napi_callback_info info) +napi_value DirentNExporter::Constructor(napi_env env, napi_callback_info cbinfo) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, cbinfo); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; diff --git a/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp b/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp index 8fa4fbaa7..461390164 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp @@ -140,15 +140,15 @@ napi_value CreateRandomAccessFile::Sync(napi_env env, napi_callback_info info) } uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); - uv_fs_t open_req; - int ret = uv_fs_open(loop, &open_req, fileInfo.path.get(), flags, S_IRUSR | + uv_fs_t openReq; + int ret = uv_fs_open(loop, &openReq, fileInfo.path.get(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, NULL); if (ret < 0) { UniError(errno).ThrowErr(env); return nullptr; } - fileInfo.fdg.SetFD(open_req.result, false); - uv_fs_req_cleanup(&open_req); + fileInfo.fdg.SetFD(openReq.result, false); + uv_fs_req_cleanup(&openReq); } return InstantiateRandomAccessFile(env, fileInfo.fdg.GetFD(), fp).val_; } @@ -179,14 +179,14 @@ napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info) if (fileInfo->isPath) { uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); - uv_fs_t open_req; - int ret = uv_fs_open(loop, &open_req, fileInfo->path.get(), flags, S_IRUSR | + uv_fs_t openReq; + int ret = uv_fs_open(loop, &openReq, fileInfo->path.get(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, NULL); if (ret < 0) { return UniError(errno); } - fileInfo->fdg.SetFD(open_req.result, false); - uv_fs_req_cleanup(&open_req); + fileInfo->fdg.SetFD(openReq.result, false); + uv_fs_req_cleanup(&openReq); } arg->fd = fileInfo->fdg.GetFD(); arg->fp = fp; -- Gitee From c57e6611437bcac12ae114e8a2ee5194a70c4560 Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Tue, 22 Nov 2022 04:54:50 +0000 Subject: [PATCH 15/17] support to open the media uri Signed-off-by: zhangkaixiang --- interfaces/kits/js/BUILD.gn | 19 ++++++- .../js/src/mod_fileio/properties/open_v9.cpp | 45 +++++++++++++++- .../js/src/mod_fileio/properties/open_v9.h | 12 +++++ .../kits/native/remote_uri/remote_uri.cpp | 51 +++++++++++++++++++ .../kits/native/remote_uri/remote_uri.h | 3 ++ 5 files changed, 127 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 8b4b5914d..dc04f46b1 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -123,9 +123,24 @@ ohos_shared_library("fs") { "src/mod_fileio/properties/truncate_v9.cpp", ] - deps = [ "//foundation/arkui/napi:ace_napi" ] + deps = [ + "//foundation/arkui/napi:ace_napi", + "//foundation/filemanagement/file_api/interfaces/kits/native:remote_uri_native", + ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "ability_runtime:abilitykit_native", + "ability_runtime:napi_base_context", + "ability_runtime:static_subscriber_extension", + "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] } ohos_shared_library("file") { diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp index fad436c57..35e3804b3 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp @@ -27,6 +27,10 @@ #include "../../common/napi/n_func_arg.h" #include "../../common/uni_error.h" #include "../common_func.h" +#include "datashare_helper.h" +#include "napi_base_context.h" +#include "remote_uri.h" +#include "ability.h" #include "../class_file/file_entity.h" #include "../class_file/file_n_exporter.h" @@ -71,6 +75,25 @@ static NVal InstantiateFile(napi_env env, int fd, string path) return { env, objRAF }; } +static int OpenFileByDatashare(napi_env env, napi_value argv, string path) +{ + std::shared_ptr dataShareHelper = nullptr; + int fd = -1; + sptr remote = new IRemoteStub(); + if (remote == nullptr) { + UniError(errno).ThrowErr(env); + return fd; + } + + dataShareHelper = DataShare::DataShareHelper::Creator(remote->AsObject(), MEDIALIBRARY_DATA_URI); + Uri uri(path); + fd = dataShareHelper->OpenFile(uri, MEDIA_FILEMODE_READONLY); + if (fd == -1) { + UniError(errno).ThrowErr(env); + } + return fd; +} + napi_value OpenV9::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -87,6 +110,15 @@ napi_value OpenV9::Sync(napi_env env, napi_callback_info info) if (!succMode) { return nullptr; } + if (DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path.get())) { + auto fd = OpenFileByDatashare(env, funcArg[NARG_POS::FIRST], path.get()); + if (fd >= 0) { + auto File = InstantiateFile(env, fd, path.get()).val_; + return File; + } + UniError(errno).ThrowErr(env); + return nullptr; + } uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); uv_fs_t open_req; @@ -124,7 +156,18 @@ napi_value OpenV9::Async(napi_env env, napi_callback_info info) return nullptr; } auto arg = make_shared(); - auto cbExec = [arg, path = string(path.get()), mode = mode](napi_env env) -> UniError { + auto argv = funcArg[NARG_POS::FIRST]; + auto cbExec = [arg, argv, path = string(path.get()), mode = mode](napi_env env) -> UniError { + if (DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path)) { + auto fd = OpenFileByDatashare(env, argv, path); + if (fd >= 0) { + arg->fd = fd; + arg->path = path; + arg->uri = ""; + return UniError(ERRNO_NOERR); + } + return UniError(errno); + } uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); uv_fs_t open_req; diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.h b/interfaces/kits/js/src/mod_fileio/properties/open_v9.h index 93fbfd3e0..ac8921b4c 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.h +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.h @@ -16,6 +16,7 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_OPEN_V9_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_OPEN_V9_H +#include "iremote_broker.h" #include "../../common/napi/uni_header.h" namespace OHOS { @@ -26,7 +27,18 @@ public: static napi_value Async(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); }; + +class FileIoToken : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileio.open"); + + FileIoToken() = default; + virtual ~FileIoToken() noexcept = default; +}; + const std::string PROCEDURE_OPEN_NAME = "FileIOOpenV9"; +const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; +const std::string MEDIA_FILEMODE_READONLY = "r"; } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/native/remote_uri/remote_uri.cpp b/interfaces/kits/native/remote_uri/remote_uri.cpp index dddc68725..6e88a13b5 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.cpp +++ b/interfaces/kits/native/remote_uri/remote_uri.cpp @@ -28,6 +28,57 @@ namespace ModuleRemoteUri { using namespace std; +bool RemoteUri::IsMediaUri(const string &path) +{ + string::size_type posDataShare = path.find(SCHEME_TAG); + if (posDataShare == string::npos) { + return false; + } + string scheme = path.substr(0, posDataShare); + if (scheme != SCHEME) { + return false; + } + + string::size_type pathSlashPos = path.find(PATH_SYMBOL); + if (pathSlashPos == string::npos) { + return false; + } + + string pathNoScheme = path.substr(pathSlashPos); + if (pathNoScheme.empty() || pathNoScheme.length() <= MEDIA.length()) { + return false; + } + + char s1 = pathNoScheme[0]; + char s2 = pathNoScheme[1]; + if (s1 != PATH_SYMBOL[0] || s2 != PATH_SYMBOL[0]) { + return false; + } + + string str = pathNoScheme.substr(2); + if (str.find(PATH_SYMBOL) == string::npos) { + return false; + } + + int position = str.find_first_of(PATH_SYMBOL); + int len = position + 1; + if (str.length() == len) { + return false; + } + + string s = str.substr(len); + if (s.empty() || s.length() < MEDIA.length()) { + return false; + } + + string media = str.substr(len, MEDIA.length()); + if (media != MEDIA) { + return false; + } + + return true; +} + static bool IsAllDigits(string fdStr) { for (size_t i = 0; i < fdStr.size(); i++) { diff --git a/interfaces/kits/native/remote_uri/remote_uri.h b/interfaces/kits/native/remote_uri/remote_uri.h index edbbc13aa..be6a535ea 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.h +++ b/interfaces/kits/native/remote_uri/remote_uri.h @@ -32,6 +32,8 @@ const string REMOTE_URI_TAG = "fdFromBinder"; const string SCHEME_TAG = ":"; const string SCHEME = "datashare"; const string ZERO_FD = "0"; +const string MEDIA = "media"; +const string PATH_SYMBOL = "/"; const int MAX_URI_SIZE = 128; class RemoteUri { static setfdFromBinder; @@ -41,6 +43,7 @@ public: static bool IsRemoteUri(const string& path, int &fd, const int& flags = O_RDONLY); static int ConvertUri(const int &fd, string &remoteUri); static int OpenRemoteUri(const string &remoteUri); + static bool IsMediaUri(const string &path); ~RemoteUri() {} }; std::setRemoteUri::fdFromBinder; -- Gitee From 7527f155250c299b645827dc1f3d2354c8abd62a Mon Sep 17 00:00:00 2001 From: zhuhongtao66 Date: Wed, 30 Nov 2022 14:41:02 +0800 Subject: [PATCH 16/17] =?UTF-8?q?libn=E5=B7=AE=E5=BC=82=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=A1=A5=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuhongtao66 --- utils/filemgmt_libhilog/filemgmt_libhilog.h | 22 +++++++++++++++++++-- utils/filemgmt_libn/include/n_val.h | 2 ++ utils/filemgmt_libn/src/n_val.cpp | 20 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/utils/filemgmt_libhilog/filemgmt_libhilog.h b/utils/filemgmt_libhilog/filemgmt_libhilog.h index 0cdf4267e..44acf1736 100644 --- a/utils/filemgmt_libhilog/filemgmt_libhilog.h +++ b/utils/filemgmt_libhilog/filemgmt_libhilog.h @@ -21,12 +21,13 @@ #include namespace OHOS { +namespace FileManagement { #ifndef LOG_DOMAIN -#define LOG_DOMAIN 0xD001600 +#define LOG_DOMAIN 0xD004388 #endif #ifndef LOG_TAG -#define LOG_TAG "FileManagement" +#define LOG_TAG "file_api" #endif static constexpr HiviewDFX::HiLogLabel FILEMGMT_LOG_LABEL = {LOG_CORE, LOG_DOMAIN, LOG_TAG}; @@ -42,11 +43,28 @@ static constexpr HiviewDFX::HiLogLabel FILEMGMT_LOG_LABEL = {LOG_CORE, LOG_DOMAI HiviewDFX::HiLog::Level(FILEMGMT_LOG_LABEL, "[%{public}s:%{public}d->%{public}s] " fmt, FILEMGMT_FILE_NAME, \ __LINE__, __FUNCTION__, ##__VA_ARGS__) +#ifdef HILOGF +#undef HILOGF +#endif + +#ifdef HILOGE +#undef HILOGE +#endif + +#ifdef HILOGW +#undef HILOGW +#endif + +#ifdef HILOGI +#undef HILOGI +#endif + #define HILOGD(fmt, ...) FILEMGMT_PRINT_LOG(Debug, fmt, ##__VA_ARGS__) #define HILOGI(fmt, ...) FILEMGMT_PRINT_LOG(Info, fmt, ##__VA_ARGS__) #define HILOGW(fmt, ...) FILEMGMT_PRINT_LOG(Warn, fmt, ##__VA_ARGS__) #define HILOGE(fmt, ...) FILEMGMT_PRINT_LOG(Error, fmt, ##__VA_ARGS__) #define HILOGF(fmt, ...) FILEMGMT_PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) +} // namespace FileManagement } // namespace OHOS #endif // FILEMGMT_LIBHILOG_H \ No newline at end of file diff --git a/utils/filemgmt_libn/include/n_val.h b/utils/filemgmt_libn/include/n_val.h index ccafe2a53..5a0d65255 100644 --- a/utils/filemgmt_libn/include/n_val.h +++ b/utils/filemgmt_libn/include/n_val.h @@ -52,6 +52,7 @@ public: std::tuple ToArraybuffer() const; std::tuple ToTypedArray() const; std::tuple, uint32_t> ToStringArray(); + std::tuple ToUint64() const; std::tuple ToDouble() const; /* Static helpers to create js objects */ @@ -63,6 +64,7 @@ public: static NVal CreateUTF8String(napi_env env, std::string str); static NVal CreateUTF8String(napi_env env, const char *str, ssize_t len); static NVal CreateUint8Array(napi_env env, void *buf, size_t bufLen); + static NVal CreateArrayString(napi_env env, std::vector strs); static std::tuple CreateArrayBuffer(napi_env env, size_t len); /* SHOULD ONLY BE USED FOR OBJECT */ diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 3c367a1be..3204d9e62 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -131,6 +131,14 @@ tuple NVal::ToDouble() const return make_tuple(status == napi_ok, res); } +tuple NVal::ToUint64() const +{ + uint64_t res = 0; + bool lossless = false; + napi_status status = napi_get_value_bigint_uint64(env_, val_, &res, &lossless); + return make_tuple(status == napi_ok, res, lossless); +} + tuple, uint32_t> NVal::ToStringArray() { napi_status status; @@ -282,6 +290,18 @@ NVal NVal::CreateUint8Array(napi_env env, void *buf, size_t bufLen) return {env, output_array}; } +NVal NVal::CreateArrayString(napi_env env, vector strs) +{ + napi_value res = nullptr; + napi_create_array(env, &res); + for (size_t i = 0; i < strs.size(); i++) { + napi_value filename; + napi_create_string_utf8(env, strs[i].c_str(), strs[i].length(), &filename); + napi_set_element(env, res, i, filename); + } + return {env, res}; +} + tuple NVal::CreateArrayBuffer(napi_env env, size_t len) { napi_value val; -- Gitee From cf63b771bd697ebd7ceda325df3d7161932128c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=87=AF=E7=A5=A5?= Date: Wed, 7 Dec 2022 14:44:28 +0800 Subject: [PATCH 17/17] code optimization,inheriting Uri classes. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张凯祥 --- interfaces/kits/js/BUILD.gn | 4 -- .../js/src/mod_fileio/properties/open_v9.cpp | 11 +--- interfaces/kits/native/BUILD.gn | 1 + .../kits/native/remote_uri/remote_uri.cpp | 56 +++---------------- .../kits/native/remote_uri/remote_uri.h | 9 +-- 5 files changed, 18 insertions(+), 63 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index dc04f46b1..f93675280 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -129,12 +129,8 @@ ohos_shared_library("fs") { ] external_deps = [ - "ability_base:want", "ability_base:zuri", - "ability_runtime:ability_manager", "ability_runtime:abilitykit_native", - "ability_runtime:napi_base_context", - "ability_runtime:static_subscriber_extension", "c_utils:utils", "data_share:datashare_common", "data_share:datashare_consumer", diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp index 35e3804b3..34955cac2 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp @@ -28,7 +28,6 @@ #include "../../common/uni_error.h" #include "../common_func.h" #include "datashare_helper.h" -#include "napi_base_context.h" #include "remote_uri.h" #include "ability.h" @@ -81,16 +80,12 @@ static int OpenFileByDatashare(napi_env env, napi_value argv, string path) int fd = -1; sptr remote = new IRemoteStub(); if (remote == nullptr) { - UniError(errno).ThrowErr(env); - return fd; + return ENOMEM; } dataShareHelper = DataShare::DataShareHelper::Creator(remote->AsObject(), MEDIALIBRARY_DATA_URI); Uri uri(path); fd = dataShareHelper->OpenFile(uri, MEDIA_FILEMODE_READONLY); - if (fd == -1) { - UniError(errno).ThrowErr(env); - } return fd; } @@ -116,7 +111,7 @@ napi_value OpenV9::Sync(napi_env env, napi_callback_info info) auto File = InstantiateFile(env, fd, path.get()).val_; return File; } - UniError(errno).ThrowErr(env); + UniError(-1).ThrowErr(env); return nullptr; } uv_loop_s *loop = nullptr; @@ -166,7 +161,7 @@ napi_value OpenV9::Async(napi_env env, napi_callback_info info) arg->uri = ""; return UniError(ERRNO_NOERR); } - return UniError(errno); + return UniError(-1); } uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); diff --git a/interfaces/kits/native/BUILD.gn b/interfaces/kits/native/BUILD.gn index b700811c7..1f585e054 100644 --- a/interfaces/kits/native/BUILD.gn +++ b/interfaces/kits/native/BUILD.gn @@ -29,6 +29,7 @@ ohos_shared_library("remote_uri_native") { public_configs = [ ":remote_uri_config" ] external_deps = [ + "ability_base:zuri", "access_token:libaccesstoken_sdk", "c_utils:utils", "ipc:ipc_core", diff --git a/interfaces/kits/native/remote_uri/remote_uri.cpp b/interfaces/kits/native/remote_uri/remote_uri.cpp index 6e88a13b5..2fe67fab1 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.cpp +++ b/interfaces/kits/native/remote_uri/remote_uri.cpp @@ -28,55 +28,17 @@ namespace ModuleRemoteUri { using namespace std; -bool RemoteUri::IsMediaUri(const string &path) +bool RemoteUri::IsMediaUri(const string &uriString) { - string::size_type posDataShare = path.find(SCHEME_TAG); - if (posDataShare == string::npos) { - return false; - } - string scheme = path.substr(0, posDataShare); - if (scheme != SCHEME) { - return false; - } - - string::size_type pathSlashPos = path.find(PATH_SYMBOL); - if (pathSlashPos == string::npos) { - return false; - } - - string pathNoScheme = path.substr(pathSlashPos); - if (pathNoScheme.empty() || pathNoScheme.length() <= MEDIA.length()) { - return false; - } - - char s1 = pathNoScheme[0]; - char s2 = pathNoScheme[1]; - if (s1 != PATH_SYMBOL[0] || s2 != PATH_SYMBOL[0]) { - return false; - } - - string str = pathNoScheme.substr(2); - if (str.find(PATH_SYMBOL) == string::npos) { - return false; - } - - int position = str.find_first_of(PATH_SYMBOL); - int len = position + 1; - if (str.length() == len) { - return false; - } - - string s = str.substr(len); - if (s.empty() || s.length() < MEDIA.length()) { - return false; - } - - string media = str.substr(len, MEDIA.length()); - if (media != MEDIA) { - return false; + RemoteUri remoteUri = RemoteUri(uriString); + string scheme = remoteUri.GetScheme(); + string path = remoteUri.GetPath(); + std::size_t len = MEDIA.length(); + if (path.length() > len) { + string media = path.substr(0, len); + return scheme == SCHEME && media == MEDIA; } - - return true; + return false; } static bool IsAllDigits(string fdStr) diff --git a/interfaces/kits/native/remote_uri/remote_uri.h b/interfaces/kits/native/remote_uri/remote_uri.h index be6a535ea..b8250fa77 100644 --- a/interfaces/kits/native/remote_uri/remote_uri.h +++ b/interfaces/kits/native/remote_uri/remote_uri.h @@ -19,6 +19,7 @@ #include #include #include +#include "uri.h" namespace OHOS { namespace DistributedFS { @@ -32,18 +33,18 @@ const string REMOTE_URI_TAG = "fdFromBinder"; const string SCHEME_TAG = ":"; const string SCHEME = "datashare"; const string ZERO_FD = "0"; -const string MEDIA = "media"; +const string MEDIA = "/media/"; const string PATH_SYMBOL = "/"; const int MAX_URI_SIZE = 128; -class RemoteUri { +class RemoteUri : public OHOS::Uri { static setfdFromBinder; static void RemoveFd(int fd); public: - RemoteUri() {} + explicit RemoteUri(const std::string& uriString): Uri(uriString) {} static bool IsRemoteUri(const string& path, int &fd, const int& flags = O_RDONLY); static int ConvertUri(const int &fd, string &remoteUri); static int OpenRemoteUri(const string &remoteUri); - static bool IsMediaUri(const string &path); + static bool IsMediaUri(const string &uriString); ~RemoteUri() {} }; std::setRemoteUri::fdFromBinder; -- Gitee