From 0ed3ee17e87ae35c34e7b194129e3ea9aa790e42 Mon Sep 17 00:00:00 2001 From: liyuke Date: Sat, 26 Jul 2025 18:17:13 +0800 Subject: [PATCH 1/9] stream TDD Signed-off-by: liyuke Change-Id: Idc9aeead87d7a8bcee419dddf2ac1dc5077b73e3 --- .../class_stream/fs_stream_mock_test.cpp | 191 +++++ .../js/mod_fs/class_stream/fs_stream_test.cpp | 791 ++++++++++++++++++ .../js/mod_fs/class_stream/mock/c_mock.cpp | 97 +++ .../js/mod_fs/class_stream/mock/c_mock.h | 49 ++ .../properties/trans_listener_mock_test.cpp | 237 ++++++ .../mod_fs/properties/trans_listener_test.cpp | 577 +++++++++++++ .../properties/unlink_core_mock_test.cpp | 5 +- 7 files changed, 1945 insertions(+), 2 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h create mode 100644 interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp new file mode 100644 index 000000000..5641bd86b --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "c_mock.h" +#include "create_stream_core.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace Test { +using namespace std; + +static const string g_streamFilePath = "/data/test/FsStreamCoreTest.txt"; + +class FsStreamMockTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + CMock::EnableMock(); + int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + CMock::DisableMock(); + rmdir(g_streamFilePath.c_str()); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: FsStreamSeekTest_0001 + * @tc.desc: Test function of Seek() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0001"; +} + +/** + * @tc.name: FsStreamSeekTest_0002 + * @tc.desc: Test function of Seek() interface for fail ftell. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamSeekTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + EXPECT_CALL(*mock_, ftell(testing::_)).WillOnce(testing::Return(-1)); + + auto retSk = result->Seek(1); + EXPECT_FALSE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamSeekTest_0002"; +} + +/** + * @tc.name: FsStreamWriteTest_0001 + * @tc.desc: Test function of Write() interface for string fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + auto retWr = result->Write("FsStreamWriteTest_0001", opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0001"; +} + +/** + * @tc.name: FsStreamWriteTest_0002 + * @tc.desc: Test function of Write() interface for ArrayBuffer fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamWriteTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + WriteOptions opt; + opt.offset = 5; + string buf = "FsStreamWriteTest_0002"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), buf.length()), opt); + EXPECT_FALSE(retWr.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamWriteTest_0002"; +} + +/** + * @tc.name: FsStreamReadTest_0001 + * @tc.desc: Test function of Read() interface for fail fseek. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamMockTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamMockTest-begin FsStreamReadTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(g_streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + + auto mock_ = CMock::GetMock(); + EXPECT_CALL(*mock_, fseek(testing::_, testing::_, testing::_)).WillOnce(testing::Return(-1)); + + ReadOptions opt; + opt.offset = 5; + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + + free(buffer); + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamMockTest-end FsStreamReadTest_0001"; +} + +} // namespace Test +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp new file mode 100644 index 000000000..770069c81 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp @@ -0,0 +1,791 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "create_stream_core.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static const string streamFilePath = "/data/test/FsStreamCoreTest.txt"; + +class FsStreamTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(streamFilePath.c_str(), CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(streamFilePath.c_str()); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: FsStreamCloseTest_0001 + * @tc.desc: Test function of Close() interface for close success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamCloseTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamCloseTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamCloseTest_0001"; +} + +/** + * @tc.name: FsStreamCloseTest_0002 + * @tc.desc: Test function of Close() interface for close fail. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamCloseTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamCloseTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + retCs = result->Close(); + EXPECT_FALSE(retCs.IsSuccess()); + auto err = retCs.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamCloseTest_0002"; +} + +/** + * @tc.name: FsStreamFlushTest_0001 + * @tc.desc: Test function of Flush() interface for null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamFlushTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamFlushTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + auto retFl = result->Flush(); + EXPECT_FALSE(retFl.IsSuccess()); + auto err = retFl.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamFlushTest_0001"; +} + +/** + * @tc.name: FsStreamFlushTest_0002 + * @tc.desc: Test function of Flush() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamFlushTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamFlushTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retFl = result->Flush(); + ASSERT_TRUE(retFl.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamFlushTest_0002"; +} + +/** + * @tc.name: FsStreamSeekTest_0001 + * @tc.desc: Test function of Seek() interface for null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamSeekTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + auto retSk = result->Seek(0); + EXPECT_FALSE(retSk.IsSuccess()); + auto err = retSk.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900002); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamSeekTest_0001"; +} + +/** + * @tc.name: FsStreamSeekTest_0002 + * @tc.desc: Test function of Seek() interface for error whence < SEEK_SET (0). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamSeekTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + optional opt = -1; + auto retSk = result->Seek(0, opt); + EXPECT_FALSE(retSk.IsSuccess()); + auto err = retSk.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamSeekTest_0002"; +} + +/** + * @tc.name: FsStreamSeekTest_0003 + * @tc.desc: Test function of Seek() interface for error whence > SEEK_END (2). + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamSeekTest_0003"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + optional opt = 3; + auto retSk = result->Seek(0, opt); + EXPECT_FALSE(retSk.IsSuccess()); + auto err = retSk.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamSeekTest_0003"; +} + +/** + * @tc.name: FsStreamSeekTest_0004 + * @tc.desc: Test function of Seek() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamSeekTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamSeekTest_0004"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retSk = result->Seek(1); + ASSERT_TRUE(retSk.IsSuccess()); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamSeekTest_0004"; +} + +/** + * @tc.name: FsStreamWriteTest_0001 + * @tc.desc: Test function of Write() interface for string single argument fail null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + auto retWr = result->Write("FsStreamWriteTest_0001"); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0001"; +} + +/** + * @tc.name: FsStreamWriteTest_0002 + * @tc.desc: Test function of Write() interface for ArrayBuffer single argument fail null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + string buf = "FsStreamWriteTest_0001"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22)); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0002"; +} + +/** + * @tc.name: FsStreamWriteTest_0003 + * @tc.desc: Test function of Write() interface for string error offset. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0003"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = -1; + + auto retWr = result->Write("FsStreamWriteTest_0003", opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0003"; +} + +/** + * @tc.name: FsStreamWriteTest_0004 + * @tc.desc: Test function of Write() interface for ArrayBuffer error offset. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0004"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = -1; + + string buf = "FsStreamWriteTest_0004"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0004"; +} + +/** + * @tc.name: FsStreamWriteTest_0005 + * @tc.desc: Test function of Write() interface for string error encoding. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0005"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.encoding = "utf-16"; + + auto retWr = result->Write("FsStreamWriteTest_0005", opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0005"; +} + +/** + * @tc.name: FsStreamWriteTest_0006 + * @tc.desc: Test function of Write() interface for ArrayBuffer error encoding. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0006"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.encoding = "utf-16"; + + string buf = "FsStreamWriteTest_0006"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0004"; +} + +#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) || defined(__LP64__) +/** + * @tc.name: FsStreamWriteTest_0007 + * @tc.desc: Test function of Write() interface for string > UINT_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0007"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + size_t largeLength = static_cast(UINT_MAX) + 1; + string largeString(largeLength, 'a'); + + auto retWr = result->Write(largeString); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0005"; +} + +/** + * @tc.name: FsStreamWriteTest_0008 + * @tc.desc: Test function of Write() interface for ArrayBuffer > UINT_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0008"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + size_t largeLength = static_cast(UINT_MAX) + 1; + string largeString(largeLength, 'a'); + + auto retWr = result->Write(ArrayBuffer(static_cast(largeString.data()), largeLength)); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0008"; +} +#endif + +/** + * @tc.name: FsStreamWriteTest_0009 + * @tc.desc: Test function of Write() interface for string length < 0. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0009"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.length = -1; + + auto retWr = result->Write("FsStreamWriteTest_0009", opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0009"; +} + +/** + * @tc.name: FsStreamWriteTest_0010 + * @tc.desc: Test function of Write() interface for ArrayBuffer length < 0. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0010"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.length = -1; + + string buf = "FsStreamWriteTest_0010"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0010"; +} + +/** + * @tc.name: FsStreamWriteTest_0013 + * @tc.desc: Test function of Write() interface for string no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0013"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retWr = result->Write("FsStreamWriteTest_0013"); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0013"; +} + +/** + * @tc.name: FsStreamWriteTest_0014 + * @tc.desc: Test function of Write() interface for ArrayBuffer no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0014"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + string buf = "FsStreamWriteTest_0014"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22)); + EXPECT_FALSE(retWr.IsSuccess()); + EXPECT_EQ(retWr.GetError().GetErrNo(), 13900005); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0014"; +} + +/** + * @tc.name: FsStreamWriteTest_0015 + * @tc.desc: Test function of Write() interface for string success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0015"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = 0; + opt.encoding = "utf-8"; + opt.length = 5; + + auto retWr = result->Write("FsStreamWriteTest_0015", opt); + ASSERT_TRUE(retWr.IsSuccess()); + + size_t retLen = retWr.GetData().value(); + EXPECT_EQ(retLen, 5); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0015"; +} + +/** + * @tc.name: FsStreamWriteTest_0016 + * @tc.desc: Test function of Write() interface for ArrayBuffer success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamWriteTest_0016, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamWriteTest_0016"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + WriteOptions opt; + opt.offset = 0; + opt.encoding = "utf-8"; + opt.length = 5; + + string buf = "FsStreamWriteTest_0016"; + auto retWr = result->Write(ArrayBuffer(static_cast(buf.data()), 22), opt); + ASSERT_TRUE(retWr.IsSuccess()); + + size_t retLen = retWr.GetData().value(); + EXPECT_EQ(retLen, 5); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamWriteTest_0016"; +} + +/** + * @tc.name: FsStreamReadTest_0001 + * @tc.desc: Test function of Read() interface for single argument null fp. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0001"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900005); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0001"; +} + +/** + * @tc.name: FsStreamReadTest_0002 + * @tc.desc: Test function of Read() interface for error offset. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0002"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + ReadOptions opt; + opt.offset = -1; + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0002"; +} + +#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) || defined(__LP64__) +/** + * @tc.name: FsStreamReadTest_0003 + * @tc.desc: Test function of Read() interface for bufLen > UINT_MAX. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0003"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + size_t largeLength = static_cast(UINT_MAX) + 1; + + void *buffer = std::malloc(largeLength); + ArrayBuffer arrayBuffer(buffer, largeLength); + auto retRd = result->Read(arrayBuffer); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0003"; +} +#endif + +/** + * @tc.name: FsStreamReadTest_0004 + * @tc.desc: Test function of Read() interface for error length. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0004"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + ReadOptions opt; + opt.length = -1; + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer, opt); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900020); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0004"; +} + +/** + * @tc.name: FsStreamReadTest_0005 + * @tc.desc: Test function of Read() interface for no permission. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0005"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "w"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + auto retRd = result->Read(arrayBuffer); + EXPECT_FALSE(retRd.IsSuccess()); + EXPECT_EQ(retRd.GetError().GetErrNo(), 13900005); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0005"; +} + +/** + * @tc.name: FsStreamReadTest_0006 + * @tc.desc: Test function of Read() interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsStreamTest, FsStreamReadTest_0006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsStreamTest-begin FsStreamReadTest_0006"; + auto ret = CreateStreamCore::DoCreateStream(streamFilePath, "r+"); + ASSERT_TRUE(ret.IsSuccess()); + auto result = ret.GetData().value(); + + ReadOptions opt; + opt.offset = 0; + opt.length = 5; + void *buffer = std::malloc(4096); + ArrayBuffer arrayBuffer(buffer, 4096); + + auto retRd = result->Read(arrayBuffer, opt); + ASSERT_TRUE(retRd.IsSuccess()); + + size_t retLen = retRd.GetData().value(); + EXPECT_EQ(retLen, 0); + + auto retCs = result->Close(); + ASSERT_TRUE(retCs.IsSuccess()); + + GTEST_LOG_(INFO) << "FsStreamTest-end FsStreamReadTest_0006"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp new file mode 100644 index 000000000..fa513e4fe --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "c_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { + +thread_local std::shared_ptr CMock::cMock = nullptr; +thread_local bool CMock::mockable = false; + +std::shared_ptr CMock::GetMock() +{ + if (cMock == nullptr) { + cMock = std::make_shared(); + } + return cMock; +} + +void CMock::EnableMock() +{ + mockable = true; +} + +void CMock::DisableMock() +{ + cMock = nullptr; + mockable = false; +} + +bool CMock::IsMockable() +{ + return mockable; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test + +#ifdef __cplusplus +extern "C" { +using namespace OHOS::FileManagement::ModuleFileIO::Test; + +int fseek(FILE *stream, long len, int offset) +{ + if (CMock::IsMockable()) { + return CMock::GetMock()->fseek(stream, len, offset); + } + + static int (*realFseek)(FILE *, long, int) = []() { + auto func = (int (*)(FILE *, long, int))dlsym(RTLD_NEXT, "fseek"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real fseek: " << dlerror(); + } + return func; + }(); + + if (!realFseek) { + return -1; + } + + return realFseek(stream, len, offset); +} + +long ftell(FILE *stream) +{ + if (CMock::IsMockable()) { + return CMock::GetMock()->ftell(stream); + } + + static long (*realFtell)(FILE *) = []() { + auto func = (long (*)(FILE *))dlsym(RTLD_NEXT, "ftell"); + if (!func) { + GTEST_LOG_(ERROR) << "Failed to resolve real ftell: " << dlerror(); + } + return func; + }(); + + if (!realFtell) { + return -1; + } + + return realFtell(stream); +} +} // extern "C" +#endif \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h new file mode 100644 index 000000000..9baf3240c --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_stream/mock/c_mock.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H +#define INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { + +class ICMock { +public: + virtual ~ICMock() = default; + virtual int fseek(FILE *, long, int) = 0; + virtual long ftell(FILE *) = 0; +}; + +class CMock : public ICMock { +public: + MOCK_METHOD(int, fseek, (FILE *, long, int), (override)); + MOCK_METHOD(long, ftell, (FILE *), (override)); + +public: + static std::shared_ptr GetMock(); + static void EnableMock(); + static void DisableMock(); + static bool IsMockable(); + +private: + static thread_local std::shared_ptr cMock; + static thread_local bool mockable; +}; + +} // namespace OHOS::FileManagement::ModuleFileIO::Test +#endif // INTERFACES_TEST_UNITTEST_JS_MOD_FS_CLASS_STREAM_MOCK_C_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp new file mode 100644 index 000000000..dbda35d77 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp @@ -0,0 +1,237 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "trans_listener_core.h" + +#include +#include +#include + +#include "copy_core.h" +#include "unistd_mock.h" + +using namespace OHOS; +using namespace OHOS::Storage::DistributedFile; + +class MockDistributedFileDaemonManager final : public DistributedFileDaemonManager { +public: + MOCK_METHOD(int32_t, PrepareSession, + (const std::string &srcUri, const std::string &dstUri, const std::string &srcDeviceId, + const sptr &listener, HmdfsInfo &info), + (override)); + + int32_t OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override + { + return 0; + } + int32_t CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo) override + { + return 0; + } + int32_t OpenP2PConnectionEx(const std::string &networkId, sptr remoteReverseObj) override + { + return 0; + } + int32_t CloseP2PConnectionEx(const std::string &networkId) override + { + return 0; + } + int32_t CancelCopyTask(const std::string &sessionName) override + { + return 0; + } + int32_t PushAsset( + int32_t userId, const sptr &assetObj, const sptr &sendCallback) override + { + return 0; + } + int32_t RegisterAssetCallback(const sptr &recvCallback) override + { + return 0; + } + int32_t UnRegisterAssetCallback(const sptr &recvCallback) override + { + return 0; + } + int32_t GetSize(const std::string &uri, bool isSrcUri, uint64_t &size) override + { + return 0; + } + int32_t IsDirectory(const std::string &uri, bool isSrcUri, bool &isDirectory) override + { + return 0; + } + int32_t Copy(const std::string &srcUri, const std::string &destUri, ProcessCallback processCallback) override + { + return 0; + } + int32_t Cancel(const std::string &srcUri, const std::string &destUri) override + { + return 0; + } + int32_t Cancel() override + { + return 0; + } + + MockDistributedFileDaemonManager() = default; + ~MockDistributedFileDaemonManager() = default; + +public: + static std::shared_ptr GetMock(); + static void DisableMock(); + +private: + static thread_local std::shared_ptr managerMock; +}; + +thread_local std::shared_ptr MockDistributedFileDaemonManager::managerMock = nullptr; + +std::shared_ptr MockDistributedFileDaemonManager::GetMock() +{ + if (managerMock == nullptr) { + managerMock = std::make_shared(); + } + return managerMock; +} + +void MockDistributedFileDaemonManager::DisableMock() +{ + managerMock = nullptr; +} + +class MockTaskSignalListener : public OHOS::DistributedFS::ModuleTaskSignal::TaskSignalListener { +public: + MOCK_METHOD(void, OnCancel, (), (override)); +}; + +#ifdef ENABLE_DISTRIBUTED_FILE_MOCK +DistributedFileDaemonManager &DistributedFileDaemonManager::GetInstance() +{ + return *MockDistributedFileDaemonManager::GetMock(); +} +#endif + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +string g_path = "/data/test/TransListenerCoreMockTest.txt"; +const string FILE_MANAGER_AUTHORITY = "docs"; +const string MEDIA_AUTHORITY = "media"; + +class IProgressListenerTest : public IProgressListener { +public: + void InvokeListener(uint64_t progressSize, uint64_t totalSize) const override {} +}; + +class TransListenerCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void TransListenerCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + int32_t fd = open(g_path.c_str(), O_CREAT | O_RDWR, 0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + UnistdMock::EnableMock(); +} + +void TransListenerCoreMockTest::TearDownTestCase(void) +{ + rmdir(g_path.c_str()); + MockDistributedFileDaemonManager::DisableMock(); + UnistdMock::DisableMock(); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void TransListenerCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void TransListenerCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: TransListenerCoreMockTest_PrepareCopySession_001 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_PrepareCopySession_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreMockTest-begin TransListenerCoreMockTest_PrepareCopySession_001"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = FILE_MANAGER_AUTHORITY; + info.authority = MEDIA_AUTHORITY; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + string disSandboxPath = "disSandboxPath"; + auto unistdMock = UnistdMock::GetMock(); + auto managerMock = MockDistributedFileDaemonManager::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*managerMock, PrepareSession(testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(ERRNO_NOERR)); + auto result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "TransListenerCoreMockTest-end TransListenerCoreMockTest_PrepareCopySession_001"; +} + +/** + * @tc.name: TransListenerCoreMockTest_CopyFileFromSoftBus_001 + * @tc.desc: Test function of TransListenerCore::CopyFileFromSoftBus interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_CopyFileFromSoftBus_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreMockTest-begin TransListenerCoreMockTest_CopyFileFromSoftBus_001"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = FILE_MANAGER_AUTHORITY; + + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + std::shared_ptr transListener = std::make_shared(); + std::shared_ptr infos = std::make_shared(); + auto unistdMock = UnistdMock::GetMock(); + auto managerMock = MockDistributedFileDaemonManager::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*managerMock, PrepareSession(testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(ERRNO_NOERR)); + + auto res = transListener->CopyFileFromSoftBus(srcUri, "destUri", infos, nullptr); + EXPECT_EQ(res, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreMockTest-end TransListenerCoreMockTest_CopyFileFromSoftBus_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp new file mode 100644 index 000000000..5e6dca235 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp @@ -0,0 +1,577 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "trans_listener_core.h" + +#include +#include + +#include "copy_core.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +string g_path = "/data/test/TransListenerCoreTest.txt"; +const string FILE_MANAGER_AUTHORITY = "docs"; +const string MEDIA_AUTHORITY = "media"; + +class IProgressListenerTest : public IProgressListener { +public: + void InvokeListener(uint64_t progressSize, uint64_t totalSize) const override {} +}; + +class TransListenerCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void TransListenerCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + int32_t fd = open(g_path.c_str(), O_CREAT | O_RDWR, 0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); +} + +void TransListenerCoreTest::TearDownTestCase(void) +{ + rmdir(g_path.c_str()); + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void TransListenerCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void TransListenerCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: TransListenerCoreTest_CreateDfsCopyPath_001 + * @tc.desc: Test function of TransListenerCore::CreateDfsCopyPath interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CreateDfsCopyPath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CreateDfsCopyPath_001"; + + string result = TransListenerCore::CreateDfsCopyPath(); + EXPECT_FALSE(result.empty()); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CreateDfsCopyPath_001"; +} + +/** + * @tc.name: TransListenerCoreTest_HandleCopyFailure_001 + * @tc.desc: Test function of TransListenerCore::HandleCopyFailure interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_HandleCopyFailure_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_HandleCopyFailure_001"; + + string path = "/data/test/TransListenerCoreTest_HandleCopyFailure_001.txt"; + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + CopyEvent event; + event.errorCode = 0; + int result = TransListenerCore::HandleCopyFailure(event, info, path, ""); + EXPECT_EQ(result, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_HandleCopyFailure_001"; +} + +/** + * @tc.name: TransListenerCoreTest_HandleCopyFailure_002 + * @tc.desc: Test function of TransListenerCore::HandleCopyFailure interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_HandleCopyFailure_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_HandleCopyFailure_002"; + + string path = "/data/test/TransListenerCoreTest_HandleCopyFailure_002.txt"; + int32_t fd = open(path.c_str(), O_CREAT | O_RDWR, 0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + CopyEvent event; + event.errorCode = SOFTBUS_TRANS_FILE_EXISTED; + int result = TransListenerCore::HandleCopyFailure(event, info, path, ""); + EXPECT_EQ(result, EEXIST); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_HandleCopyFailure_002"; +} + +/** + * @tc.name: TransListenerCoreTest_HandleCopyFailure_003 + * @tc.desc: Test function of TransListenerCore::HandleCopyFailure interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_HandleCopyFailure_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_HandleCopyFailure_003"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = MEDIA_AUTHORITY; + CopyEvent event; + event.errorCode = DFS_CANCEL_SUCCESS; + int result = TransListenerCore::HandleCopyFailure(event, info, "", ""); + EXPECT_EQ(result, ECANCELED); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_HandleCopyFailure_003"; +} + +/** + * @tc.name: TransListenerCoreTest_WaitForCopyResult_001 + * @tc.desc: Test function of TransListenerCore::WaitForCopyResult interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_WaitForCopyResult_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_WaitForCopyResult_001"; + + std::shared_ptr transListener = std::make_shared(); + transListener->copyEvent_.copyResult = FAILED; + int result = TransListenerCore::WaitForCopyResult(transListener.get()); + EXPECT_EQ(result, 2); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_WaitForCopyResult_001"; +} + +/** + * @tc.name: TransListenerCoreTest_WaitForCopyResult_002 + * @tc.desc: Test function of TransListenerCore::WaitForCopyResult interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_WaitForCopyResult_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_WaitForCopyResult_002"; + + int result = TransListenerCore::WaitForCopyResult(nullptr); + EXPECT_TRUE(result); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_WaitForCopyResult_002"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_001 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_001"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = MEDIA_AUTHORITY; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + string disSandboxPath = "disSandboxPath"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_001"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_002 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_002"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + + string disSandboxPath = "disSandboxPath"; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ENOENT); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_002"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_003 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_003"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + info.sandboxPath = "abc"; + + string disSandboxPath = "disSandboxPath"; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ENOENT); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_003"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_004 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_004"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + info.sandboxPath = "/data/test/PrepareCopySession_004.txt"; + + string disSandboxPath = "disSandboxPath"; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ENOENT); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_004"; +} + +/** + * @tc.name: TransListenerCoreTest_PrepareCopySession_005 + * @tc.desc: Test function of TransListenerCore::PrepareCopySession interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_PrepareCopySession_005"; + + Storage::DistributedFile::HmdfsInfo info; + info.authority = "abc"; + info.sandboxPath = "/data/test/PrepareCopySession_004.txt"; + int32_t fd = open(info.sandboxPath.c_str(), O_CREAT | O_RDWR, 0644); + close(fd); + + string disSandboxPath = "disSandboxPath"; + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + int result = TransListenerCore::PrepareCopySession(srcUri, "destUri", nullptr, info, disSandboxPath); + EXPECT_EQ(result, ENOENT); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_PrepareCopySession_005"; +} + +/** + * @tc.name: TransListenerCoreTest_CopyToSandBox_001 + * @tc.desc: Test function of TransListenerCore::CopyToSandBox interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CopyToSandBox_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CopyToSandBox_001"; + + string disSandboxPath = "disSandboxPath"; + string sandboxPath = "sandboxPath"; + string currentId = "currentId"; + + int result = TransListenerCore::CopyToSandBox("srcUri", disSandboxPath, sandboxPath, currentId); + EXPECT_EQ(result, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CopyToSandBox_001"; +} + +/** + * @tc.name: TransListenerCoreTest_CopyToSandBox_002 + * @tc.desc: Test function of TransListenerCore::CopyToSandBox interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CopyToSandBox_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CopyToSandBox_002"; + + string disSandboxPath = g_path; + string sandboxPath = "/data/test"; + string currentId = "currentId"; + + int result = TransListenerCore::CopyToSandBox("srcUri", disSandboxPath, sandboxPath, currentId); + EXPECT_EQ(result, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CopyToSandBox_002"; +} + +/** + * @tc.name: TransListenerCoreTest_GetFileName_001 + * @tc.desc: Test function of TransListenerCore::GetFileName interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_GetFileName_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_GetFileName_001"; + + string path = "abc"; + + auto result = TransListenerCore::GetFileName(path); + EXPECT_EQ(result, ""); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_GetFileName_001"; +} + +/** + * @tc.name: TransListenerCoreTest_GetFileName_002 + * @tc.desc: Test function of TransListenerCore::GetFileName interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_GetFileName_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_GetFileName_002"; + + auto result = TransListenerCore::GetFileName(g_path); + EXPECT_EQ(result, "/TransListenerCoreTest.txt"); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_GetFileName_002"; +} + +/** + * @tc.name: TransListenerCoreTest_GetNetworkIdFromUri_001 + * @tc.desc: Test function of TransListenerCore::GetNetworkIdFromUri interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_GetNetworkIdFromUri_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_GetNetworkIdFromUri_001"; + + string uri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + + auto result = TransListenerCore::GetNetworkIdFromUri(uri); + EXPECT_EQ(result, "AD125AD1CF"); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_GetNetworkIdFromUri_001"; +} + +/** + * @tc.name: TransListenerCoreTest_CallbackComplete_001 + * @tc.desc: Test function of TransListenerCore::CallbackComplete interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_001"; + + TransListenerCore::CallbackComplete(nullptr); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CallbackComplete_001"; +} + +/** + * @tc.name: TransListenerCoreTest_CallbackComplete_002 + * @tc.desc: Test function of TransListenerCore::CallbackComplete interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_002"; + + auto entry = make_shared(make_shared(std::make_shared())); + entry->callback = nullptr; + TransListenerCore::CallbackComplete(entry); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CallbackComplete_002"; +} + +/** + * @tc.name: TransListenerCoreTest_CallbackComplete_003 + * @tc.desc: Test function of TransListenerCore::CallbackComplete interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_003"; + + auto entry = make_shared(make_shared(std::make_shared())); + entry->callback->listener = nullptr; + TransListenerCore::CallbackComplete(entry); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CallbackComplete_003"; +} + +/** + * @tc.name: TransListenerCoreTest_CallbackComplete_004 + * @tc.desc: Test function of TransListenerCore::CallbackComplete interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_004"; + + auto entry = make_shared(make_shared(std::make_shared())); + TransListenerCore::CallbackComplete(entry); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CallbackComplete_004"; +} + +/** + * @tc.name: TransListenerCoreTest_OnFileReceive_001 + * @tc.desc: Test function of TransListenerCore::OnFileReceive interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFileReceive_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFileReceive_001"; + + std::shared_ptr transListener = std::make_shared(); + transListener->callback_ = nullptr; + auto res = transListener->OnFileReceive(0, 0); + EXPECT_EQ(res, ENOMEM); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_OnFileReceive_001"; +} + +/** + * @tc.name: TransListenerCoreTest_OnFileReceive_002 + * @tc.desc: Test function of TransListenerCore::OnFileReceive interface for SUCC. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFileReceive_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFileReceive_002"; + + std::shared_ptr transListener = std::make_shared(); + transListener->callback_ = make_shared(std::make_shared()); + auto res = transListener->OnFileReceive(0, 0); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_OnFileReceive_002"; +} + +/** + * @tc.name: TransListenerCoreTest_OnFinished_001 + * @tc.desc: Test function of TransListenerCore::OnFinished interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFinished_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFinished_001"; + + std::shared_ptr transListener = std::make_shared(); + auto res = transListener->OnFinished("sessionName"); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_OnFinished_001"; +} + +/** + * @tc.name: TransListenerCoreTest_OnFailed_001 + * @tc.desc: Test function of TransListenerCore::OnFailed interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFailed_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFailed_001"; + + std::shared_ptr transListener = std::make_shared(); + auto res = transListener->OnFailed("sessionName", 0); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_OnFailed_001"; +} + +/** + * @tc.name: TransListenerCoreTest_CopyFileFromSoftBus_001 + * @tc.desc: Test function of TransListenerCore::CopyFileFromSoftBus interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CopyFileFromSoftBus_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CopyFileFromSoftBus_001"; + + string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; + std::shared_ptr transListener = std::make_shared(); + std::shared_ptr infos = std::make_shared(); + transListener->copyEvent_.copyResult = FAILED; + + auto res = transListener->CopyFileFromSoftBus(srcUri, "destUri", infos, nullptr); + EXPECT_EQ(res, EIO); + + GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CopyFileFromSoftBus_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp index fcd294b5f..160a4b63b 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/unlink_core_mock_test.cpp @@ -15,11 +15,12 @@ #include #include -#include + #include +#include -#include "mock/uv_fs_mock.h" #include "unlink_core.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; -- Gitee From 1bf3a33451ee28a969b368e98d1270b5ec61ca76 Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 28 Jul 2025 15:24:27 +0800 Subject: [PATCH 2/9] =?UTF-8?q?stream=E5=8A=9F=E8=83=BD=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke Change-Id: Idf337fb17066bc1d6fe1a51b734d57c7c956c501 --- .../src/common/ani_helper/ani_signature.cpp | 2 +- .../ani/randomaccessfile_ani.cpp | 12 ++++----- .../ani/task_signal_wrapper.cpp | 4 +-- .../class_watcher/ani/fs_watcher_wrapper.cpp | 2 +- .../class_stream/fs_stream_mock_test.cpp | 4 +++ .../js/mod_fs/class_stream/fs_stream_test.cpp | 3 +++ .../properties/trans_listener_mock_test.cpp | 7 +++--- .../mod_fs/properties/trans_listener_test.cpp | 25 +++++++++++-------- 8 files changed, 34 insertions(+), 25 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp index 1599dfa75..d484b3d58 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -163,7 +163,7 @@ const string FS::ReadStreamOptionsInner::ctorSig = Builder::BuildSignatureDescri const string FS::ReadStreamOptionsInner::ctorDesc = Builder::BuildConstructorName(); // FS::WriteStreamOptionsInner const Type FS::WriteStreamOptionsInner::classType = Builder::BuildClass("@ohos.file.fs.WriteStreamOptionsInner"); -const string FS::WriteStreamOptionsInner::classDesc = FS::ReadStreamOptionsInner::classType.Descriptor(); +const string FS::WriteStreamOptionsInner::classDesc = FS::WriteStreamOptionsInner::classType.Descriptor(); const string FS::WriteStreamOptionsInner::ctorSig = Builder::BuildSignatureDescriptor({}); const string FS::WriteStreamOptionsInner::ctorDesc = Builder::BuildConstructorName(); diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp index a220f84ba..953755eb2 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp @@ -285,15 +285,15 @@ static ani_string GetFilePath(ani_env *env, const int fd) static ani_object CreateReadStreamOptions(ani_env *env, int64_t start, int64_t end) { - static const char *className = FS::ReadStreamOptionsInner::classType; + static const char *className = FS::ReadStreamOptionsInner::classDesc.c_str(); ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class %{public}s", className); return nullptr; } ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, FS::ReadStreamOptionsInner::ctorDesc, - FS::ReadStreamOptionsInner::ctorSig, &ctor)) { + if (ANI_OK != env->Class_FindMethod(cls, FS::ReadStreamOptionsInner::ctorDesc.c_str(), + FS::ReadStreamOptionsInner::ctorSig.c_str(), &ctor)) { HILOGE("Cannot find constructor method for class %{public}s", className); return nullptr; } @@ -329,15 +329,15 @@ static ani_object CreateReadStreamOptions(ani_env *env, int64_t start, int64_t e static ani_object CreateWriteStreamOptions(ani_env *env, int64_t start, int flags) { - static const char *className = FS::WriteStreamOptionsInner::classType; + static const char *className = FS::WriteStreamOptionsInner::classDesc.c_str(); ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class %{public}s", className); return nullptr; } ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, FS::WriteStreamOptionsInner::ctorDesc, - FS::WriteStreamOptionsInner::ctorSig, &ctor)) { + if (ANI_OK != env->Class_FindMethod(cls, FS::WriteStreamOptionsInner::ctorDesc.c_str(), + FS::WriteStreamOptionsInner::ctorSig.c_str(), &ctor)) { HILOGE("Cannot find constructor method for class %{public}s", className); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp index cc3085418..79be64231 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp @@ -32,7 +32,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; FsTaskSignal *TaskSignalWrapper::Unwrap(ani_env *env, ani_object object) { ani_long nativePtr; - auto status = env->Object_GetFieldByName_Long(object, FS::TaskSignal::nativeTaskSignal, &nativePtr); + auto status = env->Object_GetFieldByName_Long(object, "nativeTaskSignal", &nativePtr); if (status != ANI_OK) { HILOGE("Unwrap taskSignal obj failed! status: %{public}d", status); return nullptr; @@ -56,7 +56,7 @@ bool TaskSignalWrapper::Wrap(ani_env *env, ani_object object, const FsTaskSignal ani_long ptr = static_cast(reinterpret_cast(signal)); - auto status = env->Object_SetFieldByName_Long(object, FS::TaskSignal::nativeTaskSignal, ptr); + auto status = env->Object_SetFieldByName_Long(object, "nativeTaskSignal", ptr); if (status != ANI_OK) { HILOGE("Wrap taskSignal obj failed! status: %{public}d", status); return false; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp index 51ea2e2d3..f769d533a 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp @@ -29,7 +29,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; FsWatcher *FsWatcherWrapper::Unwrap(ani_env *env, ani_object object) { ani_long nativePtr; - auto ret = env->Object_GetFieldByName_Long(object, FS::WatcherInner::nativePtr, &nativePtr); + auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr); if (ret != ANI_OK) { HILOGE("Unwrap fsWatcher err: %{public}d", ret); return nullptr; diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp index 5641bd86b..4c7cf2dd2 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_mock_test.cpp @@ -14,6 +14,7 @@ */ #include + #include "c_mock.h" #include "create_stream_core.h" #include "fs_utils.h" @@ -32,6 +33,9 @@ public: { CMock::EnableMock(); int32_t fd = open(g_streamFilePath.c_str(), CREATE | O_RDWR, 0644); + if (fd <= 0) { + ASSERT_TRUE(false); + } close(fd); }; static void TearDownTestCase() diff --git a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp index 770069c81..3ba5155b3 100644 --- a/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/class_stream/fs_stream_test.cpp @@ -29,6 +29,9 @@ public: static void SetUpTestCase(void) { int32_t fd = open(streamFilePath.c_str(), CREATE | O_RDWR, 0644); + if (fd <= 0) { + ASSERT_TRUE(false); + } close(fd); }; static void TearDownTestCase() diff --git a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp index dbda35d77..6b5ddccee 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp @@ -16,8 +16,8 @@ #include "trans_listener_core.h" #include -#include #include +#include #include "copy_core.h" #include "unistd_mock.h" @@ -89,7 +89,6 @@ public: MockDistributedFileDaemonManager() = default; ~MockDistributedFileDaemonManager() = default; -public: static std::shared_ptr GetMock(); static void DisableMock(); @@ -182,7 +181,7 @@ void TransListenerCoreMockTest::TearDown(void) * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_PrepareCopySession_001, testing::ext::TestSize.Level1) +HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_PrepareCopySession_001, TestSize.Level1) { GTEST_LOG_(INFO) << "TransListenerCoreMockTest-begin TransListenerCoreMockTest_PrepareCopySession_001"; @@ -211,7 +210,7 @@ HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_PrepareCopySession * @tc.type: FUNC * @tc.level Level 1 */ -HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_CopyFileFromSoftBus_001, testing::ext::TestSize.Level1) +HWTEST_F(TransListenerCoreMockTest, TransListenerCoreMockTest_CopyFileFromSoftBus_001, TestSize.Level1) { GTEST_LOG_(INFO) << "TransListenerCoreMockTest-begin TransListenerCoreMockTest_CopyFileFromSoftBus_001"; diff --git a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp index 5e6dca235..3bfc837d4 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_test.cpp @@ -167,7 +167,7 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_WaitForCopyResult_001, tes { GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_WaitForCopyResult_001"; - std::shared_ptr transListener = std::make_shared(); + shared_ptr transListener = make_shared(); transListener->copyEvent_.copyResult = FAILED; int result = TransListenerCore::WaitForCopyResult(transListener.get()); EXPECT_EQ(result, 2); @@ -301,6 +301,9 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_PrepareCopySession_005, te info.authority = "abc"; info.sandboxPath = "/data/test/PrepareCopySession_004.txt"; int32_t fd = open(info.sandboxPath.c_str(), O_CREAT | O_RDWR, 0644); + if (fd < 0) { + EXPECT_TRUE(false); + } close(fd); string disSandboxPath = "disSandboxPath"; @@ -436,7 +439,7 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_002, test { GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_002"; - auto entry = make_shared(make_shared(std::make_shared())); + auto entry = make_shared(make_shared(make_shared())); entry->callback = nullptr; TransListenerCore::CallbackComplete(entry); @@ -454,7 +457,7 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_003, test { GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_003"; - auto entry = make_shared(make_shared(std::make_shared())); + auto entry = make_shared(make_shared(make_shared())); entry->callback->listener = nullptr; TransListenerCore::CallbackComplete(entry); @@ -472,7 +475,7 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CallbackComplete_004, test { GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CallbackComplete_004"; - auto entry = make_shared(make_shared(std::make_shared())); + auto entry = make_shared(make_shared(make_shared())); TransListenerCore::CallbackComplete(entry); GTEST_LOG_(INFO) << "TransListenerCoreTest-end TransListenerCoreTest_CallbackComplete_004"; @@ -489,7 +492,7 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFileReceive_001, testing { GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFileReceive_001"; - std::shared_ptr transListener = std::make_shared(); + shared_ptr transListener = make_shared(); transListener->callback_ = nullptr; auto res = transListener->OnFileReceive(0, 0); EXPECT_EQ(res, ENOMEM); @@ -508,8 +511,8 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFileReceive_002, testing { GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFileReceive_002"; - std::shared_ptr transListener = std::make_shared(); - transListener->callback_ = make_shared(std::make_shared()); + shared_ptr transListener = make_shared(); + transListener->callback_ = make_shared(make_shared()); auto res = transListener->OnFileReceive(0, 0); EXPECT_EQ(res, ERRNO_NOERR); @@ -527,7 +530,7 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFinished_001, testing::e { GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFinished_001"; - std::shared_ptr transListener = std::make_shared(); + shared_ptr transListener = make_shared(); auto res = transListener->OnFinished("sessionName"); EXPECT_EQ(res, ERRNO_NOERR); @@ -545,7 +548,7 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_OnFailed_001, testing::ext { GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_OnFailed_001"; - std::shared_ptr transListener = std::make_shared(); + shared_ptr transListener = make_shared(); auto res = transListener->OnFailed("sessionName", 0); EXPECT_EQ(res, ERRNO_NOERR); @@ -564,8 +567,8 @@ HWTEST_F(TransListenerCoreTest, TransListenerCoreTest_CopyFileFromSoftBus_001, t GTEST_LOG_(INFO) << "TransListenerCoreTest-begin TransListenerCoreTest_CopyFileFromSoftBus_001"; string srcUri = "http://translistener.preparecopysession?networkid=AD125AD1CF"; - std::shared_ptr transListener = std::make_shared(); - std::shared_ptr infos = std::make_shared(); + shared_ptr transListener = make_shared(); + shared_ptr infos = make_shared(); transListener->copyEvent_.copyResult = FAILED; auto res = transListener->CopyFileFromSoftBus(srcUri, "destUri", infos, nullptr); -- Gitee From 80a27a1d4a2e9046fbdca383f8b4b3dadf2bd55f Mon Sep 17 00:00:00 2001 From: liyuke Date: Sat, 26 Jul 2025 16:11:11 +0800 Subject: [PATCH 3/9] =?UTF-8?q?fdata=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke Change-Id: I38aa6a22dba957370265d7d81666ee443bfc9659 --- .../mod_fs/properties/ani/fdatasync_ani.cpp | 44 +++++++ .../src/mod_fs/properties/ani/fdatasync_ani.h | 36 ++++++ .../properties/ani/fdopen_stream_ani.cpp | 62 ++++++++++ .../mod_fs/properties/ani/fdopen_stream_ani.h | 33 +++++ .../src/mod_fs/properties/ani/fsync_ani.cpp | 41 ++++++ .../js/src/mod_fs/properties/ani/fsync_ani.h | 35 ++++++ .../mod_fs/properties/ani/listfile_ani.cpp | 4 +- .../src/mod_fs/properties/ani/lseek_ani.cpp | 63 ++++++++++ .../js/src/mod_fs/properties/ani/lseek_ani.h | 34 +++++ .../src/mod_fs/properties/fdatasync_core.cpp | 45 +++++++ .../js/src/mod_fs/properties/fdatasync_core.h | 29 +++++ .../mod_fs/properties/fdopen_stream_core.cpp | 49 ++++++++ .../mod_fs/properties/fdopen_stream_core.h | 39 ++++++ .../js/src/mod_fs/properties/fsync_core.cpp | 42 +++++++ .../js/src/mod_fs/properties/fsync_core.h | 29 +++++ .../js/src/mod_fs/properties/lseek_core.cpp | 53 ++++++++ .../js/src/mod_fs/properties/lseek_core.h | 33 +++++ .../properties/fdatasync_core_mock_test.cpp | 102 +++++++++++++++ .../properties/fdopen_stream_core_test.cpp | 117 ++++++++++++++++++ .../properties/fsync_core_mock_test.cpp | 98 +++++++++++++++ .../mod_fs/properties/listfile_core_test.cpp | 17 --- .../js/mod_fs/properties/lseek_core_test.cpp | 115 +++++++++++++++++ 22 files changed, 1101 insertions(+), 19 deletions(-) create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/fsync_core.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/lseek_core.h create mode 100644 interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp new file mode 100644 index 000000000..7ce4a8bfc --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fdatasync_ani.h" + +#include + +#include "error_handler.h" +#include "fdatasync_core.h" +#include "file_utils.h" +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +void FDataSyncAni::FDataSyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd) +{ + auto ret = FDataSyncCore::DoFDataSync(static_cast(fd)); + if (!ret.IsSuccess()) { + HILOGE("Fdatasync failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h new file mode 100644 index 000000000..ce0b83261 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdatasync_ani.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FDATASYNC_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FDATASYNC_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class FDataSyncAni final { +public: + static void FDataSyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FDATASYNC_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp new file mode 100644 index 000000000..bc6e8d274 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fdopen_stream_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "fdopen_stream_core.h" +#include "stream_wrapper.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; + +ani_object FdopenStreamAni::FdopenStreamSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_string mode) +{ + auto [succMode, openMode] = TypeConverter::ToUTF8String(env, mode); + if (!succMode) { + HILOGE("Invalid mode"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + FsResult ret = FdopenStreamCore::DoFdopenStream(static_cast(fd), openMode); + if (!ret.IsSuccess()) { + HILOGE("fdopen stream failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + const FsStream *stream = ret.GetData().value(); + auto result = StreamWrapper::Wrap(env, move(stream)); + if (result == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.h new file mode 100644 index 000000000..81f0fde31 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +class FdopenStreamAni final { +public: + static ani_object FdopenStreamSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_string mode); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.cpp new file mode 100644 index 000000000..f2680914f --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fsync_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "fsync_core.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace OHOS::FileManagement::ModuleFileIO; + +void FsyncAni::FsyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd) +{ + auto ret = FsyncCore::DoFsync(static_cast(fd)); + if (!ret.IsSuccess()) { + HILOGE("DoFsync failed!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } +} +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.h new file mode 100644 index 000000000..ad9d936fe --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fsync_ani.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FSYNC_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FSYNC_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class FsyncAni final { +public: + static void FsyncSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_FSYNC_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp index 6b65ec5ce..0299d1102 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp @@ -65,7 +65,7 @@ tuple ParseIntParam(ani_env *env, ani_object obj, string tag) } ani_int resultRefRes; if (ANI_OK != env->Object_CallMethodByName_Int( - static_cast(resultRef), "intValue", nullptr, &resultRefRes)) { + static_cast(resultRef), "toInt", nullptr, &resultRefRes)) { result = -1; return { false, result }; } @@ -87,7 +87,7 @@ tuple> ParseDoubleParam(ani_env *env, ani_object obj, str ani_double resultRefRes; if (ANI_OK != env->Object_CallMethodByName_Double( - static_cast(resultRef), "doubleValue", nullptr, &resultRefRes)) { + static_cast(resultRef), "toDouble", nullptr, &resultRefRes)) { return { false, nullopt }; } double result = static_cast(resultRefRes); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp new file mode 100644 index 000000000..1d689c696 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lseek_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "lseek_core.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace std; + +optional ParseSeekPos(const optional &whence) +{ + if (!whence.has_value()) { + return nullopt; + } + + return make_optional(static_cast(move(whence.value()))); +} + +ani_double LseekAni::LseekSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_double offset, ani_enum_item whence) +{ + auto [succWhence, whenceOp] = TypeConverter::EnumToInt32(env, whence); + if (!succWhence) { + HILOGE("Invalid whence"); + ErrorHandler::Throw(env, EINVAL); + return -1; + } + auto pos = ParseSeekPos(whenceOp); + + auto ret = LseekCore::DoLseek(static_cast(fd), static_cast(offset), pos); + if (!ret.IsSuccess()) { + HILOGE("DoLseek failed!"); + const FsError &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return -1; + } + + return ani_double(static_cast(ret.GetData().value())); +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h new file mode 100644 index 000000000..5a7995d79 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_LSEEK_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_LSEEK_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +class LseekAni final { +public: + static ani_double LseekSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_double offset, ani_enum_item whence); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_LSEEK_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp new file mode 100644 index 000000000..38996fda0 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fdatasync_core.h" + +#include +#include +#include +#include +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::ModuleFileIO { + +FsResult FDataSyncCore::DoFDataSync(const int32_t &fd) +{ + std::unique_ptr fDataSyncReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!fDataSyncReq) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + + int ret = uv_fs_fdatasync(nullptr, fDataSyncReq.get(), fd, nullptr); + if (ret < 0) { + HILOGE("Failed to transfer data associated with file descriptor: %{public}d, ret:%{public}d", fd, ret); + return FsResult::Error(ret); + } + + return FsResult::Success(); +} + +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h new file mode 100644 index 000000000..5e26c9140 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_utils.h" + +namespace OHOS::FileManagement::ModuleFileIO { +class FDataSyncCore final { +public: + static FsResult DoFDataSync(const int32_t &fd); +}; + +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp new file mode 100644 index 000000000..4337cd766 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fdopen_stream_core.h" + +#include + +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "fs_utils.h" +#include "stream_instantiator.h" +#include "stream_entity.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +FsResult FdopenStreamCore::DoFdopenStream(const int &fd, const string &mode) +{ + if (fd < 0) { + HILOGE("Invalid fd"); + return FsResult::Error(EINVAL); + } + + FILE *file = fdopen(fd, mode.c_str()); + if (!file) { + HILOGE("Failed to fopen file by fd"); + return FsResult::Error(errno); + } + + return StreamInstantiator::InstantiateStream(move(file)); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h new file mode 100644 index 000000000..a319480fd --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_stream.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { + +class FdopenStreamCore final { +public: + static FsResult DoFdopenStream(const int &fd, const string &mode); +}; + +struct AsyncFdopenStreamArg { + std::shared_ptr fp { nullptr }; +}; + +const std::string PROCEDURE_FDOPENSTREAM_NAME = "FileIOFdopenStream"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp new file mode 100644 index 000000000..d902ff1f3 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fsync_core.h" + +#include +#include +#include + +#include "filemgmt_libhilog.h" +#include "fs_utils.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; + +FsResult FsyncCore::DoFsync(const int32_t &fd) +{ + std::unique_ptr fsyncReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!fsyncReq) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + int ret = uv_fs_fsync(nullptr, fsyncReq.get(), fd, nullptr); + if (ret < 0) { + HILOGE("Failed to transfer data associated with file descriptor: %{public}d", fd); + return FsResult::Error(ret); + } + return FsResult::Success(); +} +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync_core.h b/interfaces/kits/js/src/mod_fs/properties/fsync_core.h new file mode 100644 index 000000000..ce2b66835 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/fsync_core.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_CORE_H + +#include "filemgmt_libfs.h" + +namespace OHOS::FileManagement::ModuleFileIO { + +class FsyncCore final { +public: + static FsResult DoFsync(const int32_t &fd); +}; +const std::string PROCEDURE_FSYNC_NAME = "FileIOFsync"; +} // namespace OHOS::FileManagement::ModuleFileIO +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_CORE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp b/interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp new file mode 100644 index 000000000..dfec873c9 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek_core.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lseek_core.h" + +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +FsResult LseekCore::DoLseek(const int32_t &fd, const int64_t &offset, + const optional &pos) +{ + if (fd < 0) { + HILOGE("Invalid fd from JS first argument"); + return FsResult::Error(EINVAL); + } + + SeekPos whence = SeekPos::START; + if (pos.has_value()) { + if (pos.value() < SeekPos::START || pos.value() > SeekPos::END) { + HILOGE("Invalid whence from JS third argument"); + return FsResult::Error(EINVAL); + } + whence = pos.value(); + } + + int64_t ret = ::Lseek(fd, offset, whence); + if (ret < 0) { + HILOGE("Failed to lseek, error:%{public}d", errno); + return FsResult::Error(errno); + } + + return FsResult::Success(ret); +} + +} // ModuleFileIO +} // FileManagement +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/lseek_core.h b/interfaces/kits/js/src/mod_fs/properties/lseek_core.h new file mode 100644 index 000000000..dd399f652 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/lseek_core.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_CORE_H + +#include "filemgmt_libfs.h" +#include "rust_file.h" + +namespace OHOS::FileManagement::ModuleFileIO { +using namespace std; + +class LseekCore final { +public: + static FsResult DoLseek(const int32_t &fd, const int64_t &offset, + const optional &pos = nullopt); +}; + +} // namespace OHOS::FileManagement::ModuleFileIO + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSEEK_CORE_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp new file mode 100644 index 000000000..c6308de50 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fdatasync_core_mock_test.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "fdatasync_core.h" +#include "mock/uv_fs_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FDataSyncCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; +}; + +void FDataSyncCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + uvMock = std::make_shared(); + Uvfs::ins = uvMock; +} + +void FDataSyncCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + Uvfs::ins = nullptr; + uvMock = nullptr; +} + +void FDataSyncCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FDataSyncCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FDataSyncCoreMockTest_DoFDataSync_001 + * @tc.desc: Test function of FDataSyncCore::DoFDataSync interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-begin FDataSyncCoreMockTest_DoFDataSync_001"; + + int fd = 3; + + EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(1)); + auto res = FDataSyncCore::DoFDataSync(fd); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-end FDataSyncCoreMockTest_DoFDataSync_001"; +} + +/** + * @tc.name: FDataSyncCoreMockTest_DoFDataSync_002 + * @tc.desc: Test function of FDataSyncCore::DoFDataSync interface for FAILED. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FDataSyncCoreMockTest, FDataSyncCoreMockTest_DoFDataSync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-begin FDataSyncCoreMockTest_DoFDataSync_002"; + + int fd = 3; + + EXPECT_CALL(*uvMock, uv_fs_fdatasync(_, _, _, _)).WillOnce(Return(-1)); + auto res = FDataSyncCore::DoFDataSync(fd); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FDataSyncCoreMockTest-end FDataSyncCoreMockTest_DoFDataSync_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp new file mode 100644 index 000000000..f3be0e56b --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fdopen_stream_core_test.cpp @@ -0,0 +1,117 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include "fdopen_stream_core.h" + +#define FDOPEN_STREAM_FILE_PATH "/data/test/FdopenStreamCoreTest.txt" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +class FdopenStreamCoreTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(FDOPEN_STREAM_FILE_PATH, CREATE | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(FDOPEN_STREAM_FILE_PATH); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** +* @tc.name: DoFdopenStreamTest_0001 +* @tc.desc: Test function of DoFdopenStream() interface for success. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +*/ +HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0001"; + int32_t fd = open(FDOPEN_STREAM_FILE_PATH, O_RDWR); + if (fd <= 0) { + close(fd); + ASSERT_TRUE(false); + } + + auto ret = FdopenStreamCore::DoFdopenStream(fd, "r"); + ASSERT_TRUE(ret.IsSuccess()); + + auto stream = ret.GetData().value(); + ASSERT_NE(stream, nullptr); + auto retClose = stream->Close(); + EXPECT_TRUE(retClose.IsSuccess()); + + close(fd); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0001"; +} + +/** +* @tc.name: DoFdopenStreamTest_0002 +* @tc.desc: Test function of DoFdopenStream() interface for fd < 0. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +*/ +HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0002"; + auto ret = FdopenStreamCore::DoFdopenStream(-1, "r"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0002"; +} + +/** +* @tc.name: DoFdopenStreamTest_0003 +* @tc.desc: Test function of DoFdopenStream() interface for fail. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +*/ +HWTEST_F(FdopenStreamCoreTest, DoFdopenStreamTest_0003, testing::ext::TestSize.Level1) +{ + int32_t fd = open(FDOPEN_STREAM_FILE_PATH, O_RDWR); + if (fd <= 0) { + close(fd); + ASSERT_TRUE(false); + } + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-begin DoFdopenStreamTest_0003"; + auto ret = FdopenStreamCore::DoFdopenStream(fd, "sssssss"); + EXPECT_FALSE(ret.IsSuccess()); + + auto err = ret.GetError(); + EXPECT_EQ(err.GetErrNo(), 13900020); + + close(fd); + + GTEST_LOG_(INFO) << "FdopenStreamCoreTest-end DoFdopenStreamTest_0003"; +} + +} +} +} \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp new file mode 100644 index 000000000..688a5bca0 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/fsync_core_mock_test.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fsync_core.h" +#include "uv_fs_mock.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class FsyncCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvfs = nullptr; +}; + +void FsyncCoreMockTest::SetUpTestCase(void) +{ + uvfs = std::make_shared(); + Uvfs::ins = uvfs; + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void FsyncCoreMockTest::TearDownTestCase(void) +{ + Uvfs::ins = nullptr; + uvfs = nullptr; + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void FsyncCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void FsyncCoreMockTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: FsyncCoreMockTest_DoFsync_001 + * @tc.desc: Test function of RenameCore::DoFsync interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_001"; + + EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(-1)); + + auto res = FsyncCore::DoFsync(1); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_001"; +} + +/** + * @tc.name: FsyncCoreMockTest_DoFsync_002 + * @tc.desc: Test function of RenameCore::DoFsync interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_002"; + + EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(1)); + + auto res = FsyncCore::DoFsync(1); + EXPECT_EQ(res.IsSuccess(), true); + + GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_002"; +} + + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp index dbea771a4..64387d5b4 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/listfile_core_test.cpp @@ -90,7 +90,6 @@ public: * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0001, testing::ext::TestSize.Level1) { @@ -117,7 +116,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0001, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0002, testing::ext::TestSize.Level1) { @@ -144,7 +142,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0002, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0003, testing::ext::TestSize.Level1) { @@ -167,7 +164,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0003, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0004, testing::ext::TestSize.Level1) { @@ -195,7 +191,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0004, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0005, testing::ext::TestSize.Level1) { @@ -223,7 +218,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0005, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0006, testing::ext::TestSize.Level1) { @@ -251,7 +245,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0006, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0007, testing::ext::TestSize.Level1) { @@ -278,7 +271,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0007, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0008, testing::ext::TestSize.Level1) { @@ -305,7 +297,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0008, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0009, testing::ext::TestSize.Level1) { @@ -330,7 +321,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0009, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0010, testing::ext::TestSize.Level1) { @@ -355,7 +345,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0010, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0011, testing::ext::TestSize.Level1) { @@ -379,7 +368,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0011, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0012, testing::ext::TestSize.Level1) { @@ -399,7 +387,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0012, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0013, testing::ext::TestSize.Level1) { @@ -422,7 +409,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0013, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0014, testing::ext::TestSize.Level1) { @@ -442,7 +428,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0014, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0015, testing::ext::TestSize.Level1) { @@ -476,7 +461,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0015, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0016, testing::ext::TestSize.Level1) { @@ -503,7 +487,6 @@ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0016, testing::ext::TestSize.Level * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 -* @tc.require: AR000IGDNF */ HWTEST_F(ListFileCoreTest, DoListFileCoreTest_0017, testing::ext::TestSize.Level1) { diff --git a/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp new file mode 100644 index 000000000..f5bd78bb7 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/lseek_core_test.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lseek_core.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class LseekCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void LseekCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void LseekCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void LseekCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void LseekCoreTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: LseekCoreTest_DoLseek_001 + * @tc.desc: Test function of LseekCore::DoLseek interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LseekCoreTest, LseekCoreTest_DoLseek_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LseekCoreTest-begin LseekCoreTest_DoLseek_001"; + + int32_t fd = -1; + int64_t offset = 0; + + auto res = LseekCore::DoLseek(fd, offset); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LseekCoreTest-end LseekCoreTest_DoLseek_001"; +} + +/** + * @tc.name: LseekCoreTest_DoLseek_002 + * @tc.desc: Test function of LseekCore::DoLseek interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LseekCoreTest, LseekCoreTest_DoLseek_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LseekCoreTest-begin LseekCoreTest_DoLseek_002"; + + int32_t fd = 1; + int64_t offset = 0; + optional pos = std::make_optional(static_cast(-1)); + + auto res = LseekCore::DoLseek(fd, offset, pos); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LseekCoreTest-end LseekCoreTest_DoLseek_002"; +} + +/** + * @tc.name: LseekCoreTest_DoLseek_003 + * @tc.desc: Test function of LseekCore::DoLseek interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(LseekCoreTest, LseekCoreTest_DoLseek_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LseekCoreTest-begin LseekCoreTest_DoLseek_003"; + + int32_t fd = 1; + int64_t offset = 0; + optional pos = std::make_optional(SeekPos::CURRENT); + + auto res = LseekCore::DoLseek(fd, offset, pos); + EXPECT_EQ(res.IsSuccess(), false); + + GTEST_LOG_(INFO) << "LseekCoreTest-end LseekCoreTest_DoLseek_003"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee From b0b92e6cbd317dc8564060745f82501bbbbf7fd0 Mon Sep 17 00:00:00 2001 From: liyuke Date: Sun, 27 Jul 2025 18:43:54 +0800 Subject: [PATCH 4/9] =?UTF-8?q?fdata=E5=8A=9F=E8=83=BD=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke Change-Id: I3007ff883c8e95838fc6bed4fc9cefb5f1d32043 --- interfaces/kits/js/src/common/ani_helper/ani_signature.cpp | 1 + interfaces/kits/js/src/common/ani_helper/ani_signature.h | 1 + .../kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp | 3 ++- interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp | 4 ++-- interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp | 2 +- interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp | 3 +-- .../kits/js/src/mod_fs/properties/fdopen_stream_core.cpp | 2 +- interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h | 1 - interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp | 1 + interfaces/kits/js/src/mod_fs/properties/fsync_core.h | 2 +- 10 files changed, 11 insertions(+), 9 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp index d484b3d58..00537d1be 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -30,6 +30,7 @@ const Type BasicTypes::doubleType = Builder::BuildDouble(); // BasicTypesConverter::toLong const string BasicTypesConverter::toLong = "toLong"; const string BasicTypesConverter::toInt = "toInt"; +const string BasicTypesConverter::toDouble = "toDouble"; // BoxedTypes::Boolean const Type BoxedTypes::Boolean::classType = Builder::BuildClass("std.core.Boolean"); diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.h b/interfaces/kits/js/src/common/ani_helper/ani_signature.h index c422af267..7118623aa 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.h @@ -41,6 +41,7 @@ struct BasicTypes { struct BasicTypesConverter { static const string toLong; static const string toInt; + static const string toDouble; }; struct BaseType { diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp index bc6e8d274..3ade60f9d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/fdopen_stream_ani.cpp @@ -41,7 +41,7 @@ ani_object FdopenStreamAni::FdopenStreamSync( FsResult ret = FdopenStreamCore::DoFdopenStream(static_cast(fd), openMode); if (!ret.IsSuccess()) { - HILOGE("fdopen stream failed"); + HILOGE("Fdopen stream failed"); const auto &err = ret.GetError(); ErrorHandler::Throw(env, err); return nullptr; @@ -53,6 +53,7 @@ ani_object FdopenStreamAni::FdopenStreamSync( ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } + return result; } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp index 0299d1102..16339ea59 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp @@ -65,7 +65,7 @@ tuple ParseIntParam(ani_env *env, ani_object obj, string tag) } ani_int resultRefRes; if (ANI_OK != env->Object_CallMethodByName_Int( - static_cast(resultRef), "toInt", nullptr, &resultRefRes)) { + static_cast(resultRef), BasicTypesConverter::toInt.c_str(), nullptr, &resultRefRes)) { result = -1; return { false, result }; } @@ -87,7 +87,7 @@ tuple> ParseDoubleParam(ani_env *env, ani_object obj, str ani_double resultRefRes; if (ANI_OK != env->Object_CallMethodByName_Double( - static_cast(resultRef), "toDouble", nullptr, &resultRefRes)) { + static_cast(resultRef), BasicTypesConverter::toDouble.c_str(), nullptr, &resultRefRes)) { return { false, nullopt }; } double result = static_cast(resultRefRes); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp index 1d689c696..3f4a4214a 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/lseek_ani.cpp @@ -44,8 +44,8 @@ ani_double LseekAni::LseekSync( ErrorHandler::Throw(env, EINVAL); return -1; } + auto pos = ParseSeekPos(whenceOp); - auto ret = LseekCore::DoLseek(static_cast(fd), static_cast(offset), pos); if (!ret.IsSuccess()) { HILOGE("DoLseek failed!"); diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp index 38996fda0..645cf65ae 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync_core.cpp @@ -17,9 +17,8 @@ #include #include -#include -#include #include +#include #include "filemgmt_libhilog.h" diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp index 4337cd766..3594d2a5d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.cpp @@ -20,8 +20,8 @@ #include "file_utils.h" #include "filemgmt_libhilog.h" #include "fs_utils.h" -#include "stream_instantiator.h" #include "stream_entity.h" +#include "stream_instantiator.h" namespace OHOS { namespace FileManagement { diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h index a319480fd..7e646f243 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream_core.h @@ -32,7 +32,6 @@ struct AsyncFdopenStreamArg { std::shared_ptr fp { nullptr }; }; -const std::string PROCEDURE_FDOPENSTREAM_NAME = "FileIOFdopenStream"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp b/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp index d902ff1f3..d35b60ed2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fsync_core.cpp @@ -32,6 +32,7 @@ FsResult FsyncCore::DoFsync(const int32_t &fd) HILOGE("Failed to request heap memory."); return FsResult::Error(ENOMEM); } + int ret = uv_fs_fsync(nullptr, fsyncReq.get(), fd, nullptr); if (ret < 0) { HILOGE("Failed to transfer data associated with file descriptor: %{public}d", fd); diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync_core.h b/interfaces/kits/js/src/mod_fs/properties/fsync_core.h index ce2b66835..4c6fb4d5d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fsync_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/fsync_core.h @@ -24,6 +24,6 @@ class FsyncCore final { public: static FsResult DoFsync(const int32_t &fd); }; -const std::string PROCEDURE_FSYNC_NAME = "FileIOFsync"; + } // namespace OHOS::FileManagement::ModuleFileIO #endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_CORE_H \ No newline at end of file -- Gitee From 8230707dcf00cbd03720d24a9b8edee2f4e11b80 Mon Sep 17 00:00:00 2001 From: zhouxin Date: Sat, 26 Jul 2025 15:15:50 +0800 Subject: [PATCH 5/9] =?UTF-8?q?copy=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Iabdfbea1a6fd1b84f00b24b192fad223a9c6358c Signed-off-by: zhouxin --- .../js/src/mod_fs/properties/copy_core.cpp | 887 +++++++++++++ .../kits/js/src/mod_fs/properties/copy_core.h | 192 +++ .../mod_fs/properties/copy_core_mock_test.cpp | 548 ++++++++ .../js/mod_fs/properties/copy_core_test.cpp | 1145 +++++++++++++++++ 4 files changed, 2772 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/properties/copy_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/copy_core.h create mode 100644 interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp create mode 100644 interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp new file mode 100644 index 000000000..45403ca6d --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp @@ -0,0 +1,887 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "copy_core.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "datashare_helper.h" +#include "file_uri.h" +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "fs_utils.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "ipc_skeleton.h" +#include "system_ability_definition.h" +#include "trans_listener_core.h" +#include "utils_log.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace AppFileService::ModuleFileUri; +namespace fs = std::filesystem; +const std::string FILE_PREFIX_NAME = "file://"; +const std::string NETWORK_PARA = "?networkid="; +const string PROCEDURE_COPY_NAME = "FileFSCopy"; +const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; +const std::string MEDIA = "media"; +const int SLEEP_TIME = 100000; +constexpr int DISMATCH = 0; +constexpr int MATCH = 1; +constexpr int BUF_SIZE = 1024; +constexpr size_t MAX_SIZE = 1024 * 1024 * 4; +constexpr std::chrono::milliseconds NOTIFY_PROGRESS_DELAY(300); +std::recursive_mutex CopyCore::mutex_; +std::map> CopyCore::callbackMap_; + +static int OpenSrcFile(const string &srcPth, std::shared_ptr infos, int32_t &srcFd) +{ + Uri uri(infos->srcUri); + if (uri.GetAuthority() == MEDIA) { + std::shared_ptr dataShareHelper = nullptr; + sptr remote = new (std::nothrow) IRemoteStub(); + if (!remote) { + HILOGE("Failed to get remote object"); + return ENOMEM; + } + dataShareHelper = DataShare::DataShareHelper::Creator(remote->AsObject(), MEDIALIBRARY_DATA_URI); + if (!dataShareHelper) { + HILOGE("Failed to connect to datashare"); + return E_PERMISSION; + } + srcFd = dataShareHelper->OpenFile(uri, FsUtils::GetModeFromFlags(O_RDONLY)); + if (srcFd < 0) { + HILOGE("Open media uri by data share fail. ret = %{public}d", srcFd); + return EPERM; + } + } else { + srcFd = open(srcPth.c_str(), O_RDONLY); + if (srcFd < 0) { + HILOGE("Error opening src file descriptor. errno = %{public}d", errno); + return errno; + } + } + return ERRNO_NOERR; +} + +static int SendFileCore(std::unique_ptr srcFdg, std::unique_ptr destFdg, + std::shared_ptr infos) +{ + std::unique_ptr sendFileReq = { new (nothrow) uv_fs_t, + FsUtils::FsReqCleanup }; + if (sendFileReq == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int64_t offset = 0; + struct stat srcStat {}; + if (fstat(srcFdg->GetFD(), &srcStat) < 0) { + HILOGE("Failed to get stat of file by fd: %{public}d ,errno = %{public}d", srcFdg->GetFD(), errno); + return errno; + } + int32_t ret = 0; + int64_t size = static_cast(srcStat.st_size); + while (size >= 0) { + ret = uv_fs_sendfile(nullptr, sendFileReq.get(), destFdg->GetFD(), srcFdg->GetFD(), offset, MAX_SIZE, nullptr); + if (ret < 0) { + HILOGE("Failed to sendfile by errno : %{public}d", errno); + return errno; + } + if (infos != nullptr && infos->taskSignal != nullptr) { + if (infos->taskSignal->CheckCancelIfNeed(infos->srcPath)) { + return ECANCELED; + } + } + offset += static_cast(ret); + size -= static_cast(ret); + if (ret == 0) { + break; + } + } + if (size != 0) { + HILOGE("The execution of the sendfile task was terminated, remaining file size %{public}" PRIu64, size); + return EIO; + } + return ERRNO_NOERR; +} + +bool CopyCore::IsValidUri(const std::string &uri) +{ + return uri.find(FILE_PREFIX_NAME) == 0; +} + +bool CopyCore::ValidOperand(std::string uriStr) +{ + return IsValidUri(uriStr); +} + +bool CopyCore::IsRemoteUri(const std::string &uri) +{ + // NETWORK_PARA + return uri.find(NETWORK_PARA) != uri.npos; +} + +bool CopyCore::IsDirectory(const std::string &path) +{ + struct stat buf {}; + int ret = stat(path.c_str(), &buf); + if (ret == -1) { + HILOGE("stat failed, errno is %{public}d", errno); + return false; + } + return (buf.st_mode & S_IFMT) == S_IFDIR; +} + +bool CopyCore::IsFile(const std::string &path) +{ + struct stat buf {}; + int ret = stat(path.c_str(), &buf); + if (ret == -1) { + HILOGI("stat failed, errno is %{public}d, ", errno); + return false; + } + return (buf.st_mode & S_IFMT) == S_IFREG; +} + +bool CopyCore::IsMediaUri(const std::string &uriPath) +{ + Uri uri(uriPath); + string bundleName = uri.GetAuthority(); + return bundleName == MEDIA; +} + +tuple CopyCore::GetFileSize(const std::string &path) +{ + struct stat buf {}; + int ret = stat(path.c_str(), &buf); + if (ret == -1) { + HILOGI("Stat failed."); + return { errno, 0 }; + } + return { ERRNO_NOERR, buf.st_size }; +} + +int CopyCore::CheckOrCreatePath(const std::string &destPath) +{ + std::error_code errCode; + if (!filesystem::exists(destPath, errCode) && errCode.value() == ERRNO_NOERR) { + HILOGI("destPath not exist"); + auto file = open(destPath.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + if (file < 0) { + HILOGE("Error opening file descriptor. errno = %{public}d", errno); + return errno; + } + close(file); + } else if (errCode.value() != 0) { + return errCode.value(); + } + return ERRNO_NOERR; +} + +int CopyCore::CopyFile(const string &src, const string &dest, std::shared_ptr infos) +{ + HILOGD("src = %{public}s, dest = %{public}s", GetAnonyString(src).c_str(), GetAnonyString(dest).c_str()); + int32_t srcFd = -1; + int32_t ret = OpenSrcFile(src, infos, srcFd); + if (srcFd < 0) { + return ret; + } + auto destFd = open(dest.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + if (destFd < 0) { + HILOGE("Error opening dest file descriptor. errno = %{public}d", errno); + close(srcFd); + return errno; + } + auto srcFdg = CreateUniquePtr(srcFd, true); + auto destFdg = CreateUniquePtr(destFd, true); + if (srcFdg == nullptr || destFdg == nullptr) { + HILOGE("Failed to request heap memory."); + close(srcFd); + close(destFd); + return ENOMEM; + } + return SendFileCore(move(srcFdg), move(destFdg), infos); +} + +int CopyCore::MakeDir(const string &path) +{ + filesystem::path destDir(path); + std::error_code errCode; + if (!filesystem::create_directory(destDir, errCode)) { + HILOGE("Failed to create directory, error code: %{public}d", errCode.value()); + return errCode.value(); + } + return ERRNO_NOERR; +} + +int CopyCore::CopySubDir(const string &srcPath, const string &destPath, std::shared_ptr infos) +{ + std::error_code errCode; + if (!filesystem::exists(destPath, errCode) && errCode.value() == ERRNO_NOERR) { + int res = MakeDir(destPath); + if (res != ERRNO_NOERR) { + HILOGE("Failed to mkdir"); + return res; + } + } else if (errCode.value() != ERRNO_NOERR) { + HILOGE("fs exists fail, errcode is %{public}d", errCode.value()); + return errCode.value(); + } + uint32_t watchEvents = IN_MODIFY; + if (infos->notifyFd >= 0) { + int newWd = inotify_add_watch(infos->notifyFd, destPath.c_str(), watchEvents); + if (newWd < 0) { + HILOGE("inotify_add_watch, newWd is unvaild, newWd = %{public}d", newWd); + return errno; + } + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + auto receiveInfo = CreateSharedPtr(); + if (receiveInfo == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + receiveInfo->path = destPath; + if (iter == CopyCore::callbackMap_.end() || iter->second == nullptr) { + HILOGE("Failed to find infos, srcPath = %{public}s, destPath = %{public}s", + GetAnonyString(infos->srcPath).c_str(), GetAnonyString(infos->destPath).c_str()); + return UNKNOWN_ERR; + } + iter->second->wds.push_back({ newWd, receiveInfo }); + } + } + return RecurCopyDir(srcPath, destPath, infos); +} + +static int FilterFunc(const struct dirent *filename) +{ + if (string_view(filename->d_name) == "." || string_view(filename->d_name) == "..") { + return DISMATCH; + } + return MATCH; +} + +struct NameList { + struct dirent **namelist = { nullptr }; + int direntNum = 0; +}; + +static void Deleter(struct NameList *arg) +{ + for (int i = 0; i < arg->direntNum; i++) { + free((arg->namelist)[i]); + (arg->namelist)[i] = nullptr; + } + free(arg->namelist); + arg->namelist = nullptr; + delete arg; + arg = nullptr; +} + +std::string CopyCore::GetRealPath(const std::string &path) +{ + fs::path tempPath(path); + fs::path realPath {}; + for (const auto &component : tempPath) { + if (component == ".") { + continue; + } else if (component == "..") { + realPath = realPath.parent_path(); + } else { + realPath /= component; + } + } + return realPath.string(); +} + +uint64_t CopyCore::GetDirSize(std::shared_ptr infos, std::string path) +{ + unique_ptr pNameList = { new (nothrow) struct NameList, Deleter }; + if (pNameList == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int num = scandir(path.c_str(), &(pNameList->namelist), FilterFunc, alphasort); + pNameList->direntNum = num; + + long int size = 0; + for (int i = 0; i < num; i++) { + string dest = path + '/' + string((pNameList->namelist[i])->d_name); + if ((pNameList->namelist[i])->d_type == DT_LNK) { + continue; + } + if ((pNameList->namelist[i])->d_type == DT_DIR) { + size += static_cast(GetDirSize(infos, dest)); + } else { + struct stat st {}; + if (stat(dest.c_str(), &st) == -1) { + return size; + } + size += st.st_size; + } + } + return size; +} + +int CopyCore::RecurCopyDir(const string &srcPath, const string &destPath, std::shared_ptr infos) +{ + unique_ptr pNameList = { new (nothrow) struct NameList, Deleter }; + if (pNameList == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int num = scandir(srcPath.c_str(), &(pNameList->namelist), FilterFunc, alphasort); + pNameList->direntNum = num; + + for (int i = 0; i < num; i++) { + string src = srcPath + '/' + string((pNameList->namelist[i])->d_name); + string dest = destPath + '/' + string((pNameList->namelist[i])->d_name); + if ((pNameList->namelist[i])->d_type == DT_LNK) { + continue; + } + int ret = ERRNO_NOERR; + if ((pNameList->namelist[i])->d_type == DT_DIR) { + ret = CopySubDir(src, dest, infos); + } else { + infos->filePaths.insert(dest); + ret = CopyFile(src, dest, infos); + } + if (ret != ERRNO_NOERR) { + return ret; + } + } + return ERRNO_NOERR; +} + +int CopyCore::CopyDirFunc(const string &src, const string &dest, std::shared_ptr infos) +{ + HILOGD("CopyDirFunc in, src = %{public}s, dest = %{public}s", GetAnonyString(src).c_str(), + GetAnonyString(dest).c_str()); + size_t found = dest.find(src); + if (found != std::string::npos && found == 0) { + return EINVAL; + } + fs::path srcPath = fs::u8path(src); + std::string dirName; + if (srcPath.has_parent_path()) { + dirName = srcPath.parent_path().filename(); + } + string destStr = dest + "/" + dirName; + return CopySubDir(src, destStr, infos); +} + +int CopyCore::ExecLocal(std::shared_ptr infos, std::shared_ptr callback) +{ + if (infos->isFile) { + if (infos->srcPath == infos->destPath) { + HILOGE("The src and dest is same"); + return EINVAL; + } + int ret = CheckOrCreatePath(infos->destPath); + if (ret != ERRNO_NOERR) { + HILOGE("check or create fail, error code is %{public}d", ret); + return ret; + } + } + if (!infos->hasListener) { + return ExecCopy(infos); + } + auto ret = SubscribeLocalListener(infos, callback); + if (ret != ERRNO_NOERR) { + HILOGE("Failed to subscribe local listener, errno = %{public}d", ret); + return ret; + } + StartNotify(infos, callback); + return ExecCopy(infos); +} + +int CopyCore::SubscribeLocalListener(std::shared_ptr infos, std::shared_ptr callback) +{ + infos->notifyFd = inotify_init(); + if (infos->notifyFd < 0) { + HILOGE("Failed to init inotify, errno:%{public}d", errno); + return errno; + } + infos->eventFd = eventfd(0, EFD_CLOEXEC); + if (infos->eventFd < 0) { + HILOGE("Failed to init eventFd, errno:%{public}d", errno); + return errno; + } + callback->notifyFd = infos->notifyFd; + callback->eventFd = infos->eventFd; + int newWd = inotify_add_watch(infos->notifyFd, infos->destPath.c_str(), IN_MODIFY); + if (newWd < 0) { + auto errCode = errno; + HILOGE("Failed to add watch, errno = %{public}d, notifyFd: %{public}d, destPath: %{public}s", errno, + infos->notifyFd, infos->destPath.c_str()); + CloseNotifyFdLocked(infos, callback); + return errCode; + } + auto receiveInfo = CreateSharedPtr(); + if (receiveInfo == nullptr) { + HILOGE("Failed to request heap memory."); + inotify_rm_watch(infos->notifyFd, newWd); + CloseNotifyFdLocked(infos, callback); + return ENOMEM; + } + receiveInfo->path = infos->destPath; + callback->wds.push_back({ newWd, receiveInfo }); + if (!infos->isFile) { + callback->totalSize = GetDirSize(infos, infos->srcPath); + return ERRNO_NOERR; + } + auto [err, fileSize] = GetFileSize(infos->srcPath); + if (err == ERRNO_NOERR) { + callback->totalSize = fileSize; + } + return err; +} + +std::shared_ptr CopyCore::RegisterListener(const std::shared_ptr &infos) +{ + auto callback = CreateSharedPtr(infos->listener); + if (callback == nullptr) { + HILOGE("Failed to request heap memory."); + return nullptr; + } + std::lock_guard lock(mutex_); + auto iter = callbackMap_.find(*infos); + if (iter != callbackMap_.end()) { + HILOGE("CopyCore::RegisterListener, already registered."); + return nullptr; + } + callbackMap_.insert({ *infos, callback }); + return callback; +} + +void CopyCore::UnregisterListener(std::shared_ptr fileInfos) +{ + if (fileInfos == nullptr) { + HILOGE("fileInfos is nullptr"); + return; + } + std::lock_guard lock(mutex_); + auto iter = callbackMap_.find(*fileInfos); + if (iter == callbackMap_.end()) { + HILOGI("It is not be registered."); + return; + } + callbackMap_.erase(*fileInfos); +} + +void CopyCore::ReceiveComplete(std::shared_ptr entry) +{ + if (entry == nullptr) { + HILOGE("entry pointer is nullptr."); + return; + } + if (entry->callback == nullptr) { + HILOGE("entry callback pointer is nullptr."); + return; + } + auto processedSize = entry->progressSize; + if (processedSize < entry->callback->maxProgressSize) { + return; + } + entry->callback->maxProgressSize = processedSize; + auto listener = entry->callback->listener; + if (listener == nullptr) { + HILOGE("listener pointer is nullptr."); + return; + } + listener->InvokeListener(processedSize, entry->totalSize); +} + +FsUvEntry *CopyCore::GetUVEntry(std::shared_ptr infos) +{ + FsUvEntry *entry = nullptr; + { + std::lock_guard lock(mutex_); + auto iter = callbackMap_.find(*infos); + if (iter == callbackMap_.end()) { + HILOGE("Failed to find callback"); + return nullptr; + } + auto callback = iter->second; + entry = new (std::nothrow) FsUvEntry(iter->second, infos); + if (entry == nullptr) { + HILOGE("entry ptr is nullptr."); + return nullptr; + } + entry->progressSize = callback->progressSize; + entry->totalSize = callback->totalSize; + } + return entry; +} + +void CopyCore::OnFileReceive(std::shared_ptr infos) +{ + std::shared_ptr entry(GetUVEntry(infos)); + if (entry == nullptr) { + HILOGE("failed to get uv entry"); + return; + } + ReceiveComplete(entry); +} + +std::shared_ptr CopyCore::GetReceivedInfo(int wd, std::shared_ptr callback) +{ + auto it = find_if(callback->wds.begin(), callback->wds.end(), [wd](const auto &item) { return item.first == wd; }); + if (it != callback->wds.end()) { + return it->second; + } + return nullptr; +} + +bool CopyCore::CheckFileValid(const std::string &filePath, std::shared_ptr infos) +{ + return infos->filePaths.count(filePath) != 0; +} + +int CopyCore::UpdateProgressSize( + const std::string &filePath, std::shared_ptr receivedInfo, std::shared_ptr callback) +{ + auto [err, fileSize] = GetFileSize(filePath); + if (err != ERRNO_NOERR) { + HILOGE("GetFileSize failed, err: %{public}d.", err); + return err; + } + auto size = fileSize; + auto iter = receivedInfo->fileList.find(filePath); + if (iter == receivedInfo->fileList.end()) { + receivedInfo->fileList.insert({ filePath, size }); + callback->progressSize += size; + } else { // file + if (size > iter->second) { + callback->progressSize += (size - iter->second); + iter->second = size; + } + } + return ERRNO_NOERR; +} + +std::shared_ptr CopyCore::GetRegisteredListener(std::shared_ptr infos) +{ + std::lock_guard lock(mutex_); + auto iter = callbackMap_.find(*infos); + if (iter == callbackMap_.end()) { + HILOGE("It is not registered."); + return nullptr; + } + return iter->second; +} + +void CopyCore::CloseNotifyFd(std::shared_ptr infos, std::shared_ptr callback) +{ + callback->closed = false; + infos->eventFd = -1; + infos->notifyFd = -1; + { + std::unique_lock lock(callback->cvLock); + callback->CloseFd(); + callback->cv.notify_one(); + } +} + +void CopyCore::CloseNotifyFdLocked(std::shared_ptr infos, std::shared_ptr callback) +{ + { + lock_guard lock(callback->readMutex); + callback->closed = true; + if (callback->reading) { + HILOGE("close while reading"); + return; + } + } + CloseNotifyFd(infos, callback); +} + +tuple CopyCore::HandleProgress( + inotify_event *event, std::shared_ptr infos, std::shared_ptr callback) +{ + if (callback == nullptr) { + return { true, EINVAL, false }; + } + auto receivedInfo = GetReceivedInfo(event->wd, callback); + if (receivedInfo == nullptr) { + return { true, EINVAL, false }; + } + std::string fileName = receivedInfo->path; + if (!infos->isFile) { // files under subdir + fileName += "/" + string(event->name); + if (!CheckFileValid(fileName, infos)) { + return { true, EINVAL, false }; + } + auto err = UpdateProgressSize(fileName, receivedInfo, callback); + if (err != ERRNO_NOERR) { + return { false, err, false }; + } + } else { + auto [err, fileSize] = GetFileSize(fileName); + if (err != ERRNO_NOERR) { + return { false, err, false }; + } + callback->progressSize = fileSize; + } + return { true, callback->errorCode, true }; +} + +void CopyCore::ReadNotifyEvent(std::shared_ptr infos) +{ + char buf[BUF_SIZE] = { 0 }; + struct inotify_event *event = nullptr; + int len = 0; + int64_t index = 0; + auto callback = GetRegisteredListener(infos); + while (infos->run && ((len = read(infos->notifyFd, &buf, sizeof(buf))) < 0) && (errno == EINTR)) { + } + while (infos->run && index < len) { + event = reinterpret_cast(buf + index); + auto [needContinue, errCode, needSend] = HandleProgress(event, infos, callback); + if (!needContinue) { + infos->exceptionCode = errCode; + return; + } + if (needContinue && !needSend) { + index += static_cast(sizeof(struct inotify_event) + event->len); + continue; + } + if (!callback || (callback->progressSize == callback->totalSize)) { + infos->run = false; + return; + } + auto currentTime = std::chrono::steady_clock::now(); + if (currentTime >= infos->notifyTime) { + OnFileReceive(infos); + infos->notifyTime = currentTime + NOTIFY_PROGRESS_DELAY; + } + index += static_cast(sizeof(struct inotify_event) + event->len); + } +} + +void CopyCore::ReadNotifyEventLocked(std::shared_ptr infos, std::shared_ptr callback) +{ + { + std::lock_guard lock(callback->readMutex); + if (callback->closed) { + HILOGE("read after close"); + return; + } + callback->reading = true; + } + ReadNotifyEvent(infos); + { + std::lock_guard lock(callback->readMutex); + callback->reading = false; + if (callback->closed) { + HILOGE("close after read"); + CloseNotifyFd(infos, callback); + return; + } + } +} + +void CopyCore::GetNotifyEvent(std::shared_ptr infos) +{ + auto callback = GetRegisteredListener(infos); + if (callback == nullptr) { + infos->exceptionCode = EINVAL; + return; + } + prctl(PR_SET_NAME, "NotifyThread"); + nfds_t nfds = 2; + struct pollfd fds[2]; + fds[0].events = 0; + fds[1].events = POLLIN; + fds[0].fd = infos->eventFd; + fds[1].fd = infos->notifyFd; + while (infos->run && infos->exceptionCode == ERRNO_NOERR && infos->eventFd != -1 && infos->notifyFd != -1) { + auto ret = poll(fds, nfds, -1); + if (ret > 0) { + if (static_cast(fds[0].revents) & POLLNVAL) { + infos->run = false; + return; + } + if (static_cast(fds[1].revents) & POLLIN) { + ReadNotifyEventLocked(infos, callback); + } + } else if (ret < 0 && errno == EINTR) { + continue; + } else { + infos->exceptionCode = errno; + return; + } + { + std::unique_lock lock(callback->cvLock); + callback->cv.wait_for( + lock, std::chrono::microseconds(SLEEP_TIME), [callback]() -> bool { return callback->notifyFd == -1; }); + } + } +} + +tuple> CopyCore::CreateFileInfos( + const std::string &srcUri, const std::string &destUri, const std::optional &options) +{ + auto infos = CreateSharedPtr(); + if (infos == nullptr) { + HILOGE("Failed to request heap memory."); + return { ENOMEM, nullptr }; + } + infos->srcUri = srcUri; + infos->destUri = destUri; + FileUri srcFileUri(infos->srcUri); + infos->srcPath = srcFileUri.GetRealPath(); + FileUri dstFileUri(infos->destUri); + infos->destPath = dstFileUri.GetPath(); + infos->srcPath = GetRealPath(infos->srcPath); + infos->destPath = GetRealPath(infos->destPath); + infos->isFile = IsMediaUri(infos->srcUri) || IsFile(infos->srcPath); + infos->notifyTime = std::chrono::steady_clock::now() + NOTIFY_PROGRESS_DELAY; + if (options.has_value()) { + auto listener = options.value().progressListener; + if (listener) { + infos->hasListener = true; + infos->listener = listener; + } + auto copySignal = options.value().copySignal; + if (copySignal) { + infos->taskSignal = copySignal->GetTaskSignal(); + } + } + + return { ERRNO_NOERR, infos }; +} + +void CopyCore::StartNotify(std::shared_ptr infos, std::shared_ptr callback) +{ + if (infos->hasListener && callback != nullptr) { + callback->notifyHandler = std::thread([infos] { GetNotifyEvent(infos); }); + } +} + +int CopyCore::ExecCopy(std::shared_ptr infos) +{ + if (infos->isFile && IsFile(infos->destPath)) { + // copyFile + return CopyFile(infos->srcPath.c_str(), infos->destPath.c_str(), infos); + } + if (!infos->isFile && IsDirectory(infos->destPath)) { + if (infos->srcPath.back() != '/') { + infos->srcPath += '/'; + } + if (infos->destPath.back() != '/') { + infos->destPath += '/'; + } + // copyDir + return CopyDirFunc(infos->srcPath.c_str(), infos->destPath.c_str(), infos); + } + return EINVAL; +} + +bool CopyCore::ValidParams(const string &src, const string &dest) +{ + auto succSrc = ValidOperand(src); + auto succDest = ValidOperand(dest); + if (!succSrc || !succDest) { + HILOGE("The first/second argument requires uri/uri"); + return false; + } + return true; +} + +void CopyCore::WaitNotifyFinished(std::shared_ptr callback) +{ + if (callback != nullptr) { + if (callback->notifyHandler.joinable()) { + callback->notifyHandler.join(); + } + } +} + +void CopyCore::CopyComplete(std::shared_ptr infos, std::shared_ptr callback) +{ + if (callback != nullptr && infos->hasListener) { + callback->progressSize = callback->totalSize; + OnFileReceive(infos); + } +} + +FsResult CopyCore::DoCopy(const string &src, const string &dest, std::optional &options) +{ + auto isValid = ValidParams(src, dest); + if (!isValid) { + return FsResult::Error(E_PARAMS); + } + + auto [errCode, infos] = CreateFileInfos(src, dest, options); + if (errCode != ERRNO_NOERR) { + return FsResult::Error(errCode); + } + + auto callback = RegisterListener(infos); + if (callback == nullptr) { + return FsResult::Error(EINVAL); + } + + if (IsRemoteUri(infos->srcUri)) { + if (infos->taskSignal != nullptr) { + infos->taskSignal->MarkRemoteTask(); + } + auto ret = TransListenerCore::CopyFileFromSoftBus(infos->srcUri, infos->destUri, infos, std::move(callback)); + UnregisterListener(infos); + if (ret != ERRNO_NOERR) { + return FsResult::Error(ret); + } else { + return FsResult::Success(); + } + } + auto result = CopyCore::ExecLocal(infos, callback); + CloseNotifyFdLocked(infos, callback); + infos->run = false; + WaitNotifyFinished(callback); + if (result != ERRNO_NOERR) { + infos->exceptionCode = result; + UnregisterListener(infos); + return FsResult::Error(infos->exceptionCode); + } + CopyComplete(infos, callback); + UnregisterListener(infos); + if (infos->exceptionCode != ERRNO_NOERR) { + return FsResult::Error(infos->exceptionCode); + } else { + return FsResult::Success(); + } +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_core.h b/interfaces/kits/js/src/mod_fs/properties/copy_core.h new file mode 100644 index 000000000..387792260 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/copy_core.h @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_COPY_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_COPY_CORE_H + +#include +#include +#include +#include +#include + +#include "bundle_mgr_client_impl.h" +#include "filemgmt_libfs.h" +#include "filemgmt_libhilog.h" +#include "fs_task_signal.h" +#include "i_progress_listener.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::AppExecFwk; +using namespace DistributedFS::ModuleTaskSignal; + +struct CopyOptions { + std::shared_ptr progressListener; + std::shared_ptr copySignal; +}; + +struct ReceiveInfo { + std::string path; // dir name + std::map fileList; // filename, proceededSize +}; + +struct FsCallbackObject { + std::shared_ptr listener = nullptr; + int32_t notifyFd = -1; + int32_t eventFd = -1; + std::vector>> wds; + uint64_t totalSize = 0; + uint64_t progressSize = 0; + uint64_t maxProgressSize = 0; + int32_t errorCode = 0; + std::thread notifyHandler; + std::mutex readMutex; + std::condition_variable cv; + std::mutex cvLock; + bool reading = false; + bool closed = false; + explicit FsCallbackObject(std::shared_ptr listener) : listener(listener) {} + + void CloseFd() + { + if (eventFd != -1) { + close(eventFd); + eventFd = -1; + } + if (notifyFd == -1) { + return; + } + for (auto item : wds) { + inotify_rm_watch(notifyFd, item.first); + } + close(notifyFd); + notifyFd = -1; + } + + ~FsCallbackObject() + { + CloseFd(); + } +}; + +struct FsFileInfos { + std::string srcUri; + std::string destUri; + std::string srcPath; + std::string destPath; + bool isFile = false; + std::chrono::steady_clock::time_point notifyTime; + int32_t notifyFd = -1; + int32_t eventFd = -1; + bool run = true; + bool hasListener = false; + std::shared_ptr listener = nullptr; + std::shared_ptr taskSignal = nullptr; + std::set filePaths; + int exceptionCode = ERRNO_NOERR; // notify copy thread or listener thread has exceptions. + bool operator==(const FsFileInfos &infos) const + { + return (srcUri == infos.srcUri && destUri == infos.destUri); + } + bool operator<(const FsFileInfos &infos) const + { + if (srcUri == infos.srcUri) { + return destUri < infos.destUri; + } + return srcUri < infos.srcUri; + } +}; + +struct FsUvEntry { + std::shared_ptr callback; + std::shared_ptr fileInfos; + uint64_t progressSize = 0; + uint64_t totalSize = 0; + FsUvEntry(const std::shared_ptr &cb, std::shared_ptr fileInfos) + : callback(cb), fileInfos(fileInfos) + { + } + explicit FsUvEntry(const std::shared_ptr &cb) : callback(cb) {} +}; + +class CopyCore final { +public: + static FsResult DoCopy(const string &src, const string &dest, std::optional &options); + +private: + // operator of params + static bool ValidOperand(std::string uriStr); + static int CheckOrCreatePath(const std::string &destPath); + static bool ValidParams(const string &src, const string &dest); + + // operator of local listener + static std::shared_ptr RegisterListener(const std::shared_ptr &infos); + static std::shared_ptr GetRegisteredListener(std::shared_ptr infos); + static void UnregisterListener(std::shared_ptr fileInfos); + static int ExecLocal(std::shared_ptr infos, std::shared_ptr callback); + static void CopyComplete(std::shared_ptr infos, std::shared_ptr callback); + static void WaitNotifyFinished(std::shared_ptr callback); + static void ReadNotifyEvent(std::shared_ptr infos); + static void ReadNotifyEventLocked(std::shared_ptr infos, std::shared_ptr callback); + static int SubscribeLocalListener(std::shared_ptr infos, std::shared_ptr callback); + static void OnFileReceive(std::shared_ptr infos); + static void GetNotifyEvent(std::shared_ptr infos); + static void StartNotify(std::shared_ptr infos, std::shared_ptr callback); + static FsUvEntry *GetUVEntry(std::shared_ptr infos); + static void ReceiveComplete(std::shared_ptr entry); + static void CloseNotifyFd(std::shared_ptr infos, std::shared_ptr callback); + static void CloseNotifyFdLocked(std::shared_ptr infos, std::shared_ptr callback); + + // operator of file + static int RecurCopyDir(const string &srcPath, const string &destPath, std::shared_ptr infos); + static tuple GetFileSize(const std::string &path); + static uint64_t GetDirSize(std::shared_ptr infos, std::string path); + static int CopyFile(const string &src, const string &dest, std::shared_ptr infos); + static int MakeDir(const string &path); + static int CopySubDir(const string &srcPath, const string &destPath, std::shared_ptr infos); + static int CopyDirFunc(const string &src, const string &dest, std::shared_ptr infos); + static tuple> CreateFileInfos( + const std::string &srcUri, const std::string &destUri, const std::optional &options); + static int ExecCopy(std::shared_ptr infos); + + // operator of file size + static int UpdateProgressSize(const std::string &filePath, std::shared_ptr receivedInfo, + std::shared_ptr callback); + static tuple HandleProgress( + inotify_event *event, std::shared_ptr infos, std::shared_ptr callback); + static std::shared_ptr GetReceivedInfo(int wd, std::shared_ptr callback); + static bool CheckFileValid(const std::string &filePath, std::shared_ptr infos); + + // operator of uri or path + static bool IsValidUri(const std::string &uri); + static bool IsRemoteUri(const std::string &uri); + static bool IsDirectory(const std::string &path); + static bool IsFile(const std::string &path); + static bool IsMediaUri(const std::string &uriPath); + static std::string ConvertUriToPath(const std::string &uri); + static std::string GetRealPath(const std::string &path); + +private: + static std::recursive_mutex mutex_; + static std::map> callbackMap_; +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_COPY_CORE_H \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp new file mode 100644 index 000000000..6e832264a --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp @@ -0,0 +1,548 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "copy_core.h" +#include "inotify_mock.h" +#include "mock_progress_listener.h" +#include "mock/uv_fs_mock.h" +#include "poll_mock.h" +#include "unistd_mock.h" + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyCoreMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr uvMock = nullptr; + + static const string testDir; + static const string srcDir; + static const string destDir; + static const string srcFile; + static const string destFile; + +private: + static constexpr mode_t permission0755 = 0755; + static constexpr mode_t permission0644 = 0644; +}; + +const string CopyCoreMockTest::testDir = "/data/test"; +const string CopyCoreMockTest::srcDir = testDir + "/src"; +const string CopyCoreMockTest::destDir = testDir + "/dest"; +const string CopyCoreMockTest::srcFile = srcDir + "/src.txt"; +const string CopyCoreMockTest::destFile = destDir + "/dest.txt"; + +void CopyCoreMockTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + mkdir(testDir.c_str(), permission0755); + mkdir(srcDir.c_str(), permission0755); + mkdir(destDir.c_str(), permission0755); + int32_t fd = open(srcFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + uvMock = std::make_shared(); + Uvfs::ins = uvMock; + InotifyMock::EnableMock(); + PollMock::EnableMock(); + UnistdMock::EnableMock(); +} + +void CopyCoreMockTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + int ret = remove(srcFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(srcDir.c_str()); + rmdir(destDir.c_str()); + rmdir(testDir.c_str()); + Uvfs::ins = nullptr; + uvMock = nullptr; + InotifyMock::DisableMock(); + PollMock::DisableMock(); + UnistdMock::DisableMock(); +} + +void CopyCoreMockTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyCoreMockTest::TearDown(void) +{ + CopyCore::callbackMap_.clear(); + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: CopyCoreMockTest_CopyFile_001 + * @tc.desc: Test function of CopyCore::CopyFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CopyFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CopyFile_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = CopyCoreMockTest::srcFile; + infos->destPath = CopyCoreMockTest::destFile; + auto unistdMock = UnistdMock::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).WillOnce(Return(-1)); + + auto res = CopyCore::CopyFile(srcFile, destFile, infos); + EXPECT_EQ(res, errno); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CopyFile_001"; +} + +/** + * @tc.name: CopyCoreMockTest_DoCopy_001 + * @tc.desc: Test function of CopyCore::DoCopy interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_DoCopy_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_DoCopy_001"; + + string srcUri = "file://" + srcFile; + string destUri = "file://" + destFile; + optional options; + auto unistdMock = UnistdMock::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*uvMock, uv_fs_sendfile(_, _, _, _, _, _, _)).WillOnce(Return(0)); + + auto res = CopyCore::DoCopy(srcUri, destUri, options); + EXPECT_EQ(res.IsSuccess(), true); + EXPECT_TRUE(filesystem::exists(destFile)); + int ret = remove(destFile.c_str()); + EXPECT_TRUE(ret == 0); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_DoCopy_001"; +} + +/** + * @tc.name: CopyCoreMockTest_CopySubDir_001 + * @tc.desc: Test function of CopyCore::CopySubDir interface for success. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CopySubDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CopySubDir_001"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + auto inotifyMock = InotifyMock::GetMock(); + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + infos->notifyFd = 1; + auto unistdMock = UnistdMock::GetMock(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + EXPECT_EQ(res, UNKNOWN_ERR); + + int ret = remove(subFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(subDir.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CopySubDir_001"; +} + +/** + * @tc.name: CopyCoreMockTest_CopySubDir_002 + * @tc.desc: Test CopyCore::CopySubDir when iter == CopyCore::callbackMap_.end() + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CopySubDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CopySubDir_002"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + auto inotifyMock = InotifyMock::GetMock(); + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + infos->notifyFd = 1; + auto unistdMock = UnistdMock::GetMock(); + CopyCore::callbackMap_.clear(); + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + EXPECT_EQ(res, UNKNOWN_ERR); + + int ret = remove(subFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(subDir.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CopySubDir_002"; +} + +/** + * @tc.name: CopyCoreMockTest_CopySubDir_003 + * @tc.desc: Test CopyCore::CopySubDir when iter->second == nullptr + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CopySubDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CopySubDir_003"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + GTEST_LOG_(INFO) << "Open test file failed! ret: " << fd << ", errno: " << errno; + EXPECT_TRUE(false); + } + close(fd); + + auto inotifyMock = InotifyMock::GetMock(); + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + infos->notifyFd = 1; + auto unistdMock = UnistdMock::GetMock(); + CopyCore::callbackMap_[*infos] = nullptr; + + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).WillRepeatedly(testing::Return(1)); + EXPECT_CALL(*inotifyMock, inotify_add_watch(testing::_, testing::_, testing::_)).WillOnce(testing::Return(0)); + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + EXPECT_EQ(res, UNKNOWN_ERR); + + int ret = remove(subFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(subDir.c_str()); + rmdir(destSubDir.c_str()); + CopyCore::callbackMap_.clear(); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CopySubDir_003"; +} + +/** + * @tc.name: CopyCoreMockTest_ReceiveComplete_001 + * @tc.desc: Test CopyCore::ReceiveComplete in normal case + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReceiveComplete_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReceiveComplete_001"; + + auto mockListener = std::make_shared(); + auto callback = std::make_shared(mockListener); + callback->maxProgressSize = 50; + auto entry = std::make_shared(callback); + entry->progressSize = 100; + entry->totalSize = 200; + + EXPECT_CALL(*mockListener, InvokeListener(entry->progressSize, entry->totalSize)).Times(1); + CopyCore::ReceiveComplete(entry); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReceiveComplete_001"; +} + +/** + * @tc.name: CopyCoreMockTest_ReceiveComplete_002 + * @tc.desc: Test CopyCore::ReceiveComplete when processedSize < entry->callback->maxProgressSize + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReceiveComplete_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReceiveComplete_002"; + + auto mockListener = std::make_shared(); + auto callback = std::make_shared(mockListener); + callback->maxProgressSize = 100; + auto entry = std::make_shared(callback); + entry->progressSize = 50; // Mock valid progressSize + entry->totalSize = 200; // Mock valid totalSize, and progressSize < totalSize + + EXPECT_CALL(*mockListener, InvokeListener(testing::_, testing::_)).Times(0); + CopyCore::ReceiveComplete(entry); + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReceiveComplete_002"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEvent_001 + * @tc.desc: Test function of CopyCoreMockTest::ReadNotifyEvent interface for SUCCESS when read valid event data. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 0 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEvent_001, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEvent_001"; + // Prepare test condition + auto infos = make_shared(); + infos->run = true; + auto callback = CopyCore::RegisterListener(infos); + int32_t len = static_cast(sizeof(struct inotify_event)); + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(len)); + // Do testing + CopyCore::ReadNotifyEvent(infos); + // Verify results + EXPECT_NE(callback, nullptr); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEvent_001"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEvent_002 + * @tc.desc: Test function of CopyCore::ReadNotifyEvent interface for FAILURE when read returns -1. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEvent_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEvent_002"; + // Prepare test condition + auto infos = make_shared(); + infos->run = true; + auto callback = CopyCore::RegisterListener(infos); + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(EIO, -1)); + // Do testing + CopyCore::ReadNotifyEvent(infos); + // Verify results + EXPECT_NE(callback, nullptr); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + EXPECT_EQ(errno, EIO); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEvent_002"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEvent_003 + * @tc.desc: Test function of CopyCore::ReadNotifyEvent interface for SUCCESS when read returns 0 (EOF). + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEvent_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEvent_003"; + // Prepare test condition + auto infos = make_shared(); + infos->run = true; + auto callback = CopyCore::RegisterListener(infos); + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::SetErrnoAndReturn(0, 0)); + // Do testing + CopyCore::ReadNotifyEvent(infos); + // Verify results + EXPECT_NE(callback, nullptr); + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + EXPECT_EQ(errno, 0); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEvent_003"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEventLocked_001 + * @tc.desc: Test ReadNotifyEventLocked when closed is false. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 0 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEventLocked_001, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEventLocked_001"; + // Prepare test condition + auto infos = make_shared(); + auto callback = std::make_shared(nullptr); + callback->closed = false; + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)).Times(1).WillOnce(testing::Return(0)); + EXPECT_CALL(*unistdMock, close(testing::_)).Times(0); + // Do testing + CopyCore::ReadNotifyEventLocked(infos, callback); + // Verify results + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + EXPECT_FALSE(callback->reading); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEventLocked_001"; +} + +/** + * @tc.name: CopyCoreMockTest_ReadNotifyEventLocked_002 + * @tc.desc: Test ReadNotifyEventLocked when close after read. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReadNotifyEventLocked_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_ReadNotifyEventLocked_002"; + // Prepare test condition + auto infos = make_shared(); + auto callback = std::make_shared(nullptr); + callback->closed = false; + // Set mock behaviors + auto unistdMock = UnistdMock::GetMock(); + EXPECT_CALL(*unistdMock, read(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([callback](int fd, void *buf, size_t count) { + errno = EIO; + callback->closed = true; + return 0; + }); + EXPECT_CALL(*unistdMock, close(testing::_)).WillRepeatedly(testing::Return(0)); + // Do testing + CopyCore::ReadNotifyEventLocked(infos, callback); + // Verify results + testing::Mock::VerifyAndClearExpectations(unistdMock.get()); + EXPECT_FALSE(callback->closed); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_ReadNotifyEventLocked_002"; +} + +/** + * @tc.name: CopyCoreMockTest_GetNotifyId_001 + * @tc.desc: Test function of CopyCore::GetNotifyId interface for SUCCESS. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 0 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_GetNotifyId_001, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_GetNotifyId_001"; + // Prepare test condition + auto infos = make_shared(); + infos->run = true; + infos->exceptionCode = ERRNO_NOERR; + infos->eventFd = 1; + infos->notifyFd = 1; + + auto callback = CopyCore::RegisterListener(infos); + // Set mock behaviors + auto pollMock = PollMock::GetMock(); + EXPECT_CALL(*pollMock, poll(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce([infos](struct pollfd *fds, nfds_t n, int timeout) { + fds[1].revents = POLLIN; + infos->run = false; // Ensure the loop will exit + return 1; + }); + // Do testing + CopyCore::GetNotifyEvent(infos); + // Verify results + testing::Mock::VerifyAndClearExpectations(pollMock.get()); + EXPECT_NE(callback, nullptr); + EXPECT_FALSE(infos->run); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_GetNotifyId_001"; +} + +/** + * @tc.name: CopyCoreMockTest_GetNotifyId_002 + * @tc.desc: Test function of CopyCore::GetNotifyId interface fails when callback is nullptr. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 0 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_GetNotifyId_002, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_GetNotifyId_002"; + // Prepare test condition + auto infos = make_shared(); + // Do testing + CopyCore::GetNotifyEvent(infos); + // Verify results + EXPECT_EQ(infos->exceptionCode, EINVAL); + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_GetNotifyId_002"; +} + +/** + * @tc.name: CopyCoreMockTest_CreateFileInfos_001 + * @tc.desc: Test function of CopyCore::CreateFileInfos interface with listener and copySignal. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CreateFileInfos_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreMockTest-begin CopyCoreMockTest_CreateFileInfos_001"; + + auto copySignal = std::make_shared(); + copySignal->taskSignal_ = std::make_shared(); + auto options = std::make_optional(); + options->progressListener = std::make_shared(); + options->copySignal = std::move(copySignal); + + auto [errCode, infos] = CopyCore::CreateFileInfos(srcFile, destFile, options); + EXPECT_EQ(errCode, ERRNO_NOERR); + EXPECT_NE(infos, nullptr); + if (infos) { + EXPECT_TRUE(infos->hasListener); + EXPECT_NE(infos->listener, nullptr); + EXPECT_NE(infos->taskSignal, nullptr); + } + + GTEST_LOG_(INFO) << "CopyCoreMockTest-end CopyCoreMockTest_CreateFileInfos_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp new file mode 100644 index 000000000..6c9da75ac --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp @@ -0,0 +1,1145 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "copy_core.h" + +#include +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CopyCoreTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + static const string testDir; + static const string srcDir; + static const string destDir; + static const string srcFile; + static const string destFile; + +private: + static constexpr mode_t permission0755 = 0755; // rwxr-xr-x + static constexpr mode_t permission0644 = 0644; // rw-r--r-- +}; + +const string CopyCoreTest::testDir = "/data/test"; +const string CopyCoreTest::srcDir = testDir + "/src"; +const string CopyCoreTest::destDir = testDir + "/dest"; +const string CopyCoreTest::srcFile = srcDir + "/src.txt"; +const string CopyCoreTest::destFile = destDir + "/dest.txt"; + +void CopyCoreTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + mkdir(testDir.c_str(), permission0755); + mkdir(srcDir.c_str(), permission0755); + mkdir(destDir.c_str(), permission0755); + int32_t fd = open(srcFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); +} + +void CopyCoreTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + int ret = remove(srcFile.c_str()); + EXPECT_TRUE(ret == 0); + rmdir(srcDir.c_str()); + rmdir(destDir.c_str()); + rmdir(testDir.c_str()); +} + +void CopyCoreTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void CopyCoreTest::TearDown(void) +{ + CopyCore::callbackMap_.clear(); + GTEST_LOG_(INFO) << "TearDown"; +} + +inline const int32_t EXPECTED_WD = 100; +inline const int32_t UNEXPECTED_WD = 200; + +/** + * @tc.name: CopyCoreTest_IsValidUri_001 + * @tc.desc: Test function of CopyCore::IsValidUri interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsValidUri_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsValidUri_001"; + + string validUri = "file://data/test/file.txt"; + auto res = CopyCore::IsValidUri(validUri); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsValidUri_001"; +} + +/** + * @tc.name: CopyCoreTest_IsValidUri_002 + * @tc.desc: Test function of CopyCore::IsValidUri interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsValidUri_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsValidUri_002"; + + string invalidUri = "invalid://data/test/file.txt"; + auto res = CopyCore::IsValidUri(invalidUri); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsValidUri_002"; +} + +/** + * @tc.name: CopyCoreTest_IsRemoteUri_001 + * @tc.desc: Test function of CopyCore::IsRemoteUri interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsRemoteUri_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsRemoteUri_001"; + + string remoteUri = "file://data/test/file.txt?networkid=123"; + auto res = CopyCore::IsRemoteUri(remoteUri); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsRemoteUri_001"; +} + +/** + * @tc.name: CopyCoreTest_IsRemoteUri_002 + * @tc.desc: Test function of CopyCore::IsRemoteUri interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsRemoteUri_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsRemoteUri_002"; + + string localUri = "file://data/test/file.txt"; + auto res = CopyCore::IsRemoteUri(localUri); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsRemoteUri_002"; +} + +/** + * @tc.name: CopyCoreTest_IsDirectory_001 + * @tc.desc: Test function of CopyCore::IsDirectory interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsDirectory_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsDirectory_001"; + + auto res = CopyCore::IsDirectory(srcDir); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsDirectory_001"; +} + +/** + * @tc.name: CopyCoreTest_IsDirectory_002 + * @tc.desc: Test function of CopyCore::IsDirectory interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsDirectory_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsDirectory_002"; + + auto res = CopyCore::IsDirectory(srcFile); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsDirectory_002"; +} + +/** + * @tc.name: CopyCoreTest_IsFile_001 + * @tc.desc: Test function of CopyCore::IsFile interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsFile_001"; + + auto res = CopyCore::IsFile(srcFile); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsFile_001"; +} + +/** + * @tc.name: CopyCoreTest_IsFile_002 + * @tc.desc: Test function of CopyCore::IsFile interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsFile_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsFile_002"; + + auto res = CopyCore::IsFile(srcDir); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsFile_002"; +} + +/** + * @tc.name: CopyCoreTest_IsMediaUri_001 + * @tc.desc: Test function of CopyCore::IsMediaUri interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_IsMediaUri_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsMediaUri_001"; + + auto res = CopyCore::IsMediaUri(srcFile); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_IsMediaUri_001"; +} + +/** + * @tc.name: CopyCoreTest_GetFileSize_001 + * @tc.desc: Test function of CopyCore::GetFileSize interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetFileSize_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetFileSize_001"; + + auto [err, size] = CopyCore::GetFileSize(srcFile); + EXPECT_EQ(err, ERRNO_NOERR); + EXPECT_GE(size, 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetFileSize_001"; +} + +/** + * @tc.name: CopyCoreTest_GetFileSize_002 + * @tc.desc: Test function of CopyCore::GetFileSize interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetFileSize_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetFileSize_002"; + + string nonExistentFile = "/data/test/non_existent.txt"; + auto [err, size] = CopyCore::GetFileSize(nonExistentFile); + EXPECT_NE(err, ERRNO_NOERR); + EXPECT_EQ(size, 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetFileSize_002"; +} + +/** + * @tc.name: CopyCoreTest_CheckOrCreatePath_001 + * @tc.desc: Test function of CopyCore::CheckOrCreatePath interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CheckOrCreatePath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CheckOrCreatePath_001"; + + auto res = CopyCore::CheckOrCreatePath(srcFile); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CheckOrCreatePath_001"; +} + +/** + * @tc.name: CopyCoreTest_CheckOrCreatePath_002 + * @tc.desc: Test function of CopyCore::CheckOrCreatePath interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CheckOrCreatePath_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CheckOrCreatePath_002"; + + string newFile = destDir + "/new_file.txt"; + auto res = CopyCore::CheckOrCreatePath(newFile); + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsFile(newFile)); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CheckOrCreatePath_002"; +} + +/** + * @tc.name: CopyCoreTest_MakeDir_001 + * @tc.desc: Test function of CopyCore::MakeDir interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_MakeDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_MakeDir_001"; + + string newDir = destDir + "/new_dir"; + auto res = CopyCore::MakeDir(newDir); + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsDirectory(newDir)); + + rmdir(newDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_MakeDir_001"; +} + +/** + * @tc.name: CopyCoreTest_MakeDir_002 + * @tc.desc: Test function of CopyCore::MakeDir interface for TRUE. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_MakeDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_MakeDir_002"; + + auto res = CopyCore::MakeDir(srcDir); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_MakeDir_002"; +} + +/** + * @tc.name: CopyCoreTest_MakeDir_003 + * @tc.desc: Test function of CopyCore::MakeDir interface for FALSE. + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_MakeDir_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_MakeDir_003"; + + string invalidPath = "/invalid/path/dir"; + auto res = CopyCore::MakeDir(invalidPath); + EXPECT_NE(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_MakeDir_003"; +} + +/** + * @tc.name: CopyCoreTest_ValidParams_001 + * @tc.desc: Test function of CopyCore::ValidParams interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidParams_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidParams_001"; + + string srcFile = "invalid://data/test/src.txt"; + + auto res = CopyCore::ValidParams(srcFile, destFile); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidParams_001"; +} + +/** + * @tc.name: CopyCoreTest_ValidParams_002 + * @tc.desc: Test function of CopyCore::ValidParams interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidParams_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidParams_002"; + + string destFile = "invalid://data/test/dest.txt"; + + auto res = CopyCore::ValidParams(srcFile, destFile); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidParams_002"; +} + +/** + * @tc.name: CopyCoreTest_ValidParams_003 + * @tc.desc: Test function of CopyCore::ValidParams interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ValidParams_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ValidParams_003"; + + string src = "file://data/test/src.txt"; + string dest = "file://data/test/dest.txt"; + + auto res = CopyCore::ValidParams(src, dest); + EXPECT_EQ(res, true); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ValidParams_003"; +} + +/** + * @tc.name: CopyCoreTest_CreateFileInfos_001 + * @tc.desc: Test function of CopyCore::CreateFileInfos interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CreateFileInfos_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CreateFileInfos_001"; + + optional options = std::make_optional(); + + auto [errCode, infos] = CopyCore::CreateFileInfos(srcFile, destFile, options); + EXPECT_EQ(errCode, ERRNO_NOERR); + EXPECT_NE(infos, nullptr); + if (infos) { + EXPECT_FALSE(infos->hasListener); + EXPECT_EQ(infos->listener, nullptr); + EXPECT_EQ(infos->taskSignal, nullptr); + } + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CreateFileInfos_001"; +} + +/** + * @tc.name: CopyCoreTest_CopySubDir_001 + * @tc.desc: Test function of CopyCore::CopySubDir interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CopySubDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CopySubDir_001"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + string destSubFile = destSubDir + "/sub_file.txt"; + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsDirectory(destSubDir)); + EXPECT_TRUE(CopyCore::IsFile(destSubFile)); + + remove(subFile.c_str()); + rmdir(subDir.c_str()); + remove(destSubFile.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CopySubDir_001"; +} + +/** + * @tc.name: CopyCoreTest_CopySubDir_002 + * @tc.desc: Test function of CopyCore::CopySubDir interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CopySubDir_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CopySubDir_002"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + infos->notifyFd = 1; + auto res = CopyCore::CopySubDir(subDir, destSubDir, infos); + EXPECT_EQ(res, errno); + + remove(subFile.c_str()); + rmdir(subDir.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CopySubDir_002"; +} + +/** + * @tc.name: CopyCoreTest_RecurCopyDir_001 + * @tc.desc: Test function of CopyCore::RecurCopyDir interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_RecurCopyDir_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_RecurCopyDir_001"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + string destSubDir = destDir + "/sub_dir"; + auto infos = make_shared(); + auto res = CopyCore::RecurCopyDir(srcDir, destDir, infos); + string destSubFile = destSubDir + "/sub_file.txt"; + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsDirectory(destSubDir)); + EXPECT_TRUE(CopyCore::IsFile(destSubDir + "/sub_file.txt")); + + remove(subFile.c_str()); + rmdir(subDir.c_str()); + remove(destSubFile.c_str()); + rmdir(destSubDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_RecurCopyDir_001"; +} + +/** + * @tc.name: CopyCoreTest_CopyDirFunc_001 + * @tc.desc: Test function of CopyCore::CopyDirFunc interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CopyDirFunc_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CopyDirFunc_001"; + + string subDir = srcDir + "/sub_dir"; + mkdir(subDir.c_str(), permission0755); + string subFile = subDir + "/sub_file.txt"; + int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); + if (fd < 0) { + EXPECT_TRUE(false); + } + close(fd); + + string destSubDir = destDir + "/src/sub_dir"; + string destSubFile = destSubDir + "/sub_file.txt"; + string destSrcDir = destDir + "/src"; + auto infos = make_shared(); + auto res = CopyCore::CopyDirFunc(srcDir, destDir, infos); + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_EQ(CopyCore::IsDirectory(destSubDir), false); + EXPECT_EQ(CopyCore::IsFile(destSubDir + "/sub_file.txt"), false); + + remove(subFile.c_str()); + rmdir(subDir.c_str()); + remove(destSubFile.c_str()); + rmdir(destSubDir.c_str()); + rmdir(destSrcDir.c_str()); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CopyDirFunc_001"; +} + +/** + * @tc.name: CopyCoreTest_ExecLocal_001 + * @tc.desc: Test function of CopyCore::ExecLocal interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ExecLocal_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ExecLocal_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + auto callback = make_shared(nullptr); + + auto res = CopyCore::ExecLocal(infos, callback); + EXPECT_EQ(res, ERRNO_NOERR); + EXPECT_TRUE(CopyCore::IsFile(destFile)); + int ret = remove(destFile.c_str()); + EXPECT_TRUE(ret == 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ExecLocal_001"; +} + +/** + * @tc.name: CopyCoreTest_ExecLocal_002 + * @tc.desc: Test function of CopyCore::ExecLocal interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ExecLocal_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ExecLocal_002"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = srcFile; + auto callback = make_shared(nullptr); + + auto res = CopyCore::ExecLocal(infos, callback); + EXPECT_EQ(res, EINVAL); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ExecLocal_002"; +} + +/** + * @tc.name: CopyCoreTest_RegisterListener_001 + * @tc.desc: Test function of CopyCore::RegisterListener interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_RegisterListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_RegisterListener_001"; + + auto infos = make_shared(); + auto callback = CopyCore::RegisterListener(infos); + EXPECT_NE(callback, nullptr); + + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + EXPECT_NE(iter, CopyCore::callbackMap_.end()); + } + + CopyCore::UnregisterListener(infos); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_RegisterListener_001"; +} + +/** + * @tc.name: CopyCoreTest_RegisterListener_002 + * @tc.desc: Test function of CopyCore::RegisterListener interface for the case when the info already exists in + * callbackMap_. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_RegisterListener_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_RegisterListener_002"; + + auto infos = make_shared(); + auto firstCallback = CopyCore::RegisterListener(infos); + EXPECT_NE(firstCallback, nullptr); + auto secondCallback = CopyCore::RegisterListener(infos); + EXPECT_EQ(secondCallback, nullptr); + + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + EXPECT_NE(iter, CopyCore::callbackMap_.end()); + } + + CopyCore::UnregisterListener(infos); + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_RegisterListener_002"; +} + +/** + * @tc.name: CopyCoreTest_UnregisterListener_001 + * @tc.desc: Test function of CopyCore::UnregisterListener interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_UnregisterListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_UnregisterListener_001"; + + auto infos = make_shared(); + auto callback = CopyCore::RegisterListener(infos); + EXPECT_NE(callback, nullptr); + + CopyCore::UnregisterListener(infos); + + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + EXPECT_EQ(iter, CopyCore::callbackMap_.end()); + } + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_UnregisterListener_001"; +} + +/** + * @tc.name: CopyCoreTest_UnregisterListener_002 + * @tc.desc: Test function of CopyCore::UnregisterListener interface for the case when the info is not registered in + * callbackMap_. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_UnregisterListener_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_UnregisterListener_002"; + + auto infos = make_shared(); + + CopyCore::UnregisterListener(infos); + + { + std::lock_guard lock(CopyCore::mutex_); + auto iter = CopyCore::callbackMap_.find(*infos); + EXPECT_EQ(iter, CopyCore::callbackMap_.end()); + } + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_UnregisterListener_002"; +} + +/** + * @tc.name: CopyCoreTest_DoCopy_001 + * @tc.desc: Test function of CopyCore::DoCopy interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_DoCopy_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_DoCopy_001"; + + string src = "invalid:/" + srcFile; + string dest = "invalid:/" + destFile; + optional options; + + auto res = CopyCore::DoCopy(src, dest, options); + EXPECT_FALSE(res.IsSuccess()); + auto err = res.GetError(); + EXPECT_EQ(err.GetErrNo(), E_PARAMS); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_DoCopy_001"; +} + +/** + * @tc.name: CopyCoreTest_DoCopy_002 + * @tc.desc: Test function of CopyCore::DoCopy interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_DoCopy_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_DoCopy_002"; + + string src = "file:///data/test/src/src.txt"; + string dest = "file:///data/test/dest/dest.txt"; + optional options; + + auto res = CopyCore::DoCopy(src, dest, options); + EXPECT_TRUE(res.IsSuccess()); + int ret = remove(destFile.c_str()); + EXPECT_TRUE(ret == 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_DoCopy_002"; +} + +/** + * @tc.name: CopyCoreTest_GetDirSize_001 + * @tc.desc: Test function of CopyCore::GetDirSize interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetDirSize_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetDirSize_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + + auto res = CopyCore::GetDirSize(infos, srcDir); + EXPECT_EQ(res, 0); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetDirSize_001"; +} + +/** + * @tc.name: CopyCoreTest_GetUVEntry_001 + * @tc.desc: Test function of CopyCore::GetUVEntry interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetUVEntry_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetUVEntry_001"; + + auto infos = make_shared(); + auto res = CopyCore::GetUVEntry(infos); + EXPECT_EQ(res, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetUVEntry_001"; +} + +/** + * @tc.name: CopyCoreTest_CheckFileValid_001 + * @tc.desc: Test function of CopyCore::CheckFileValid interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CheckFileValid_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CheckFileValid_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + + auto res = CopyCore::CheckFileValid(srcFile, infos); + EXPECT_EQ(res, false); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CheckFileValid_001"; +} + +/** + * @tc.name: CopyCoreTest_UpdateProgressSize_001 + * @tc.desc: Test function of CopyCore::UpdateProgressSize interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_UpdateProgressSize_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_UpdateProgressSize_001"; + + auto receivedInfo = make_shared(); + auto callback = make_shared(nullptr); + + auto res = CopyCore::UpdateProgressSize(srcFile, receivedInfo, callback); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_UpdateProgressSize_001"; +} + +/** + * @tc.name: CopyCoreTest_GetRegisteredListener_001 + * @tc.desc: Test function of CopyCore::GetRegisteredListener interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetRegisteredListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetRegisteredListener_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + + auto res = CopyCore::GetRegisteredListener(infos); + EXPECT_EQ(res, nullptr); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetRegisteredListener_001"; +} + +/** + * @tc.name: CopyCoreTest_SubscribeLocalListener_001 + * @tc.desc: Test function of CopyCore::SubscribeLocalListener interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_SubscribeLocalListener_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_SubscribeLocalListener_001"; + + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = srcFile; + infos->destPath = destFile; + auto callback = make_shared(nullptr); + + auto res = CopyCore::SubscribeLocalListener(infos, callback); + EXPECT_EQ(res, errno); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_SubscribeLocalListener_001"; +} + +/** + * @tc.name: CopyCoreTest_GetRealPath_001 + * @tc.desc: Test function of CopyCore::GetRealPath interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetRealPath_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetRealPath_001"; + + string path = "./data/test/src/src.txt"; + + auto res = CopyCore::GetRealPath(path); + EXPECT_EQ(res, "data/test/src/src.txt"); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetRealPath_001"; +} + +/** + * @tc.name: CopyCoreTest_GetRealPath_002 + * @tc.desc: Test function of CopyCore::GetRealPath interface for TRUE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_GetRealPath_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_GetRealPath_002"; + + string path = "../data/test/src/src.txt"; + + auto res = CopyCore::GetRealPath(path); + EXPECT_EQ(res, "data/test/src/src.txt"); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_GetRealPath_002"; +} + +/** + * @tc.name: CopyCoreTest_ExecCopy_001 + * @tc.desc: Test function of CopyCore::ExecCopy interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_ExecCopy_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_ExecCopy_001"; + + auto infos = make_shared(); + infos->isFile = false; + infos->srcPath = "/data/test/src"; + infos->destPath = "/data/test/dest"; + + auto res = CopyCore::ExecCopy(infos); + EXPECT_EQ(res, ERRNO_NOERR); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_ExecCopy_001"; +} + +/** + * @tc.name: CopyCoreTest_CopyFile_001 + * @tc.desc: Test function of CopyCore::CopyFile interface for file copy FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_CopyFile_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CopyFile_001"; + + string src = "datashare:///media/src_test.jpg"; + string dest = "datashare:///media/dest_test.jpg"; + auto infos = make_shared(); + infos->isFile = true; + infos->srcPath = src; + infos->destPath = dest; + + auto res = CopyCore::CopyFile(src, dest, infos); + EXPECT_EQ(res, errno); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_CopyFile_001"; +} + +/** + * @tc.name: CopyCoreTest_HandleProgress_001 + * @tc.desc: Test function of CopyCore::HandleProgress interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_HandleProgress_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_HandleProgress_001"; + + auto infos = make_shared(); + infos->isFile = false; + infos->srcPath = "/data/test/src"; + infos->destPath = "/data/test/dest"; + + auto event = make_unique(); + event->wd = EXPECTED_WD; + event->mask = IN_MODIFY; + event->len = 0; + auto [continueProcess, errCode, needSend] = CopyCore::HandleProgress(event.get(), infos, nullptr); + + EXPECT_TRUE(continueProcess); + EXPECT_EQ(errCode, EINVAL); + EXPECT_FALSE(needSend); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_HandleProgress_001"; +} + +/** + * @tc.name: CopyCoreTest_HandleProgress_002 + * @tc.desc: Test function of CopyCore::HandleProgress interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_HandleProgress_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_HandleProgress_002"; + + auto infos = make_shared(); + infos->srcPath = srcDir; + infos->destPath = destDir; + infos->isFile = true; + + auto callback = make_shared(nullptr); + + auto receiveInfo = make_shared(); + receiveInfo->path = testDir; + callback->wds.push_back({ UNEXPECTED_WD, receiveInfo }); + + auto event = make_unique(); + event->wd = EXPECTED_WD; + event->mask = IN_MODIFY; + event->len = 0; + + auto [continueProcess, errCode, needSend] = CopyCore::HandleProgress(event.get(), infos, callback); + + EXPECT_TRUE(continueProcess); + EXPECT_EQ(errCode, EINVAL); + EXPECT_FALSE(needSend); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_HandleProgress_002"; +} + +/** + * @tc.name: CopyCoreTest_HandleProgress_003 + * @tc.desc: Test function of CopyCore::HandleProgress interface for FALSE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_HandleProgress_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_HandleProgress_003"; + + auto infos = make_shared(); + infos->srcPath = srcDir; + infos->destPath = destDir; + infos->isFile = true; + + auto callback = make_shared(nullptr); + + auto receiveInfo = make_shared(); + receiveInfo->path = srcFile; + callback->wds.push_back({ EXPECTED_WD, receiveInfo }); + + auto event = make_unique(); + event->wd = EXPECTED_WD; + event->mask = IN_MODIFY; + event->len = 0; + + auto [continueProcess, errCode, needSend] = CopyCore::HandleProgress(event.get(), infos, callback); + + EXPECT_TRUE(continueProcess); + EXPECT_EQ(errCode, ERRNO_NOERR); + EXPECT_TRUE(needSend); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_HandleProgress_003"; +} + +/** + * @tc.name: CopyCoreTest_OnFileReceive_001 + * @tc.desc: Test function of CopyCore::OnFileReceive interface fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_OnFileReceive_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_OnFileReceive_001"; + + auto infos = make_shared(); + CopyCore::OnFileReceive(infos); + EXPECT_TRUE(CopyCore::callbackMap_.empty()); + + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_OnFileReceive_001"; +} + +/** + * @tc.name: CopyCoreTest_OnFileReceive_002 + * @tc.desc: Test function of CopyCore::OnFileReceive interface when listener is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(CopyCoreTest, CopyCoreTest_OnFileReceive_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_OnFileReceive_002"; + + auto infos = make_shared(); + auto callback = CopyCore::RegisterListener(infos); + CopyCore::OnFileReceive(infos); + EXPECT_NE(callback, nullptr); + if (callback) { + EXPECT_EQ(callback->listener, nullptr); + } + GTEST_LOG_(INFO) << "CopyCoreTest-end CopyCoreTest_OnFileReceive_002"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test -- Gitee From 644aca9db3e947015ab88d0608df84eb9e44873e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sun, 27 Jul 2025 15:02:55 +0800 Subject: [PATCH 6/9] =?UTF-8?q?copy=E5=8A=9F=E8=83=BD=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A3=80=E8=A7=86=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 Change-Id: Ifd137e2bfac30a049cddef8c69f9215d982eac14 --- .../kits/js/src/mod_fs/properties/copy_core.cpp | 14 +++++++++----- .../kits/js/src/mod_fs/properties/copy_core.h | 10 ++++------ .../mod_fs/properties/copy_core_mock_test.cpp | 12 ++++++++---- .../js/mod_fs/properties/copy_core_test.cpp | 17 +++++++++++------ .../statvfs_core_test.cpp | 0 interfaces/test/unittest/task_signal/BUILD.gn | 1 + 6 files changed, 33 insertions(+), 21 deletions(-) rename interfaces/test/unittest/js/{mode_statvfs => mod_statvfs}/statvfs_core_test.cpp (100%) diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp index 45403ca6d..612326ba4 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp @@ -37,8 +37,8 @@ #include "filemgmt_libhilog.h" #include "fs_utils.h" #include "if_system_ability_manager.h" -#include "iservice_registry.h" #include "ipc_skeleton.h" +#include "iservice_registry.h" #include "system_ability_definition.h" #include "trans_listener_core.h" #include "utils_log.h" @@ -154,7 +154,7 @@ bool CopyCore::IsDirectory(const std::string &path) struct stat buf {}; int ret = stat(path.c_str(), &buf); if (ret == -1) { - HILOGE("stat failed, errno is %{public}d", errno); + HILOGE("Stat failed, errno is %{public}d", errno); return false; } return (buf.st_mode & S_IFMT) == S_IFDIR; @@ -165,7 +165,7 @@ bool CopyCore::IsFile(const std::string &path) struct stat buf {}; int ret = stat(path.c_str(), &buf); if (ret == -1) { - HILOGI("stat failed, errno is %{public}d, ", errno); + HILOGI("Stat failed, errno is %{public}d, ", errno); return false; } return (buf.st_mode & S_IFMT) == S_IFREG; @@ -193,7 +193,7 @@ int CopyCore::CheckOrCreatePath(const std::string &destPath) { std::error_code errCode; if (!filesystem::exists(destPath, errCode) && errCode.value() == ERRNO_NOERR) { - HILOGI("destPath not exist"); + HILOGI("DestPath not exist"); auto file = open(destPath.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (file < 0) { HILOGE("Error opening file descriptor. errno = %{public}d", errno); @@ -209,17 +209,20 @@ int CopyCore::CheckOrCreatePath(const std::string &destPath) int CopyCore::CopyFile(const string &src, const string &dest, std::shared_ptr infos) { HILOGD("src = %{public}s, dest = %{public}s", GetAnonyString(src).c_str(), GetAnonyString(dest).c_str()); + int32_t srcFd = -1; int32_t ret = OpenSrcFile(src, infos, srcFd); if (srcFd < 0) { return ret; } + auto destFd = open(dest.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (destFd < 0) { HILOGE("Error opening dest file descriptor. errno = %{public}d", errno); close(srcFd); return errno; } + auto srcFdg = CreateUniquePtr(srcFd, true); auto destFdg = CreateUniquePtr(destFd, true); if (srcFdg == nullptr || destFdg == nullptr) { @@ -408,7 +411,7 @@ int CopyCore::ExecLocal(std::shared_ptr infos, std::shared_ptrdestPath); if (ret != ERRNO_NOERR) { - HILOGE("check or create fail, error code is %{public}d", ret); + HILOGE("Check or create fail, error code is %{public}d", ret); return ret; } } @@ -575,6 +578,7 @@ int CopyCore::UpdateProgressSize( HILOGE("GetFileSize failed, err: %{public}d.", err); return err; } + auto size = fileSize; auto iter = receivedInfo->fileList.find(filePath); if (iter == receivedInfo->fileList.end()) { diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_core.h b/interfaces/kits/js/src/mod_fs/properties/copy_core.h index 387792260..245dff3f0 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/copy_core.h @@ -36,8 +36,8 @@ using namespace OHOS::AppExecFwk; using namespace DistributedFS::ModuleTaskSignal; struct CopyOptions { - std::shared_ptr progressListener; - std::shared_ptr copySignal; + shared_ptr progressListener; + shared_ptr copySignal; }; struct ReceiveInfo { @@ -118,9 +118,8 @@ struct FsUvEntry { uint64_t progressSize = 0; uint64_t totalSize = 0; FsUvEntry(const std::shared_ptr &cb, std::shared_ptr fileInfos) - : callback(cb), fileInfos(fileInfos) - { - } + : callback(cb), fileInfos(fileInfos) {} + explicit FsUvEntry(const std::shared_ptr &cb) : callback(cb) {} }; @@ -181,7 +180,6 @@ private: static std::string ConvertUriToPath(const std::string &uri); static std::string GetRealPath(const std::string &path); -private: static std::recursive_mutex mutex_; static std::map> callbackMap_; }; diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp index 6e832264a..2b5266ae4 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp @@ -17,14 +17,13 @@ #include #include #include -#include #include "copy_core.h" #include "inotify_mock.h" #include "mock_progress_listener.h" -#include "mock/uv_fs_mock.h" #include "poll_mock.h" #include "unistd_mock.h" +#include "uv_fs_mock.h" namespace OHOS::FileManagement::ModuleFileIO::Test { using namespace testing; @@ -46,11 +45,13 @@ public: static const string destFile; private: + // rwxr-xr-x static constexpr mode_t permission0755 = 0755; + // rw-r--r-- static constexpr mode_t permission0644 = 0644; }; -const string CopyCoreMockTest::testDir = "/data/test"; +const string CopyCoreMockTest::testDir = "/data/test/CopyCoreMockTest"; const string CopyCoreMockTest::srcDir = testDir + "/src"; const string CopyCoreMockTest::destDir = testDir + "/dest"; const string CopyCoreMockTest::srcFile = srcDir + "/src.txt"; @@ -288,7 +289,9 @@ HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_ReceiveComplete_001, testing::ext::T auto callback = std::make_shared(mockListener); callback->maxProgressSize = 50; auto entry = std::make_shared(callback); + // 当前已经拷贝的字节数 entry->progressSize = 100; + // 需要拷贝的字节数 entry->totalSize = 200; EXPECT_CALL(*mockListener, InvokeListener(entry->progressSize, entry->totalSize)).Times(1); @@ -485,7 +488,8 @@ HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_GetNotifyId_001, testing::ext::TestS .Times(1) .WillOnce([infos](struct pollfd *fds, nfds_t n, int timeout) { fds[1].revents = POLLIN; - infos->run = false; // Ensure the loop will exit + // Ensure the loop will exit + infos->run = false; return 1; }); // Do testing diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp index 6c9da75ac..febb6cf12 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_core_test.cpp @@ -32,19 +32,23 @@ public: void TearDown(); static const string testDir; + static const string copyCoreDir; static const string srcDir; static const string destDir; static const string srcFile; static const string destFile; private: - static constexpr mode_t permission0755 = 0755; // rwxr-xr-x - static constexpr mode_t permission0644 = 0644; // rw-r--r-- + // rwxr-xr-x + static constexpr mode_t permission0755 = 0755; + // rw-r--r-- + static constexpr mode_t permission0644 = 0644; }; const string CopyCoreTest::testDir = "/data/test"; -const string CopyCoreTest::srcDir = testDir + "/src"; -const string CopyCoreTest::destDir = testDir + "/dest"; +const string CopyCoreTest::copyCoreDir = testDir + "/CopyCoreTest"; +const string CopyCoreTest::srcDir = copyCoreDir + "/src"; +const string CopyCoreTest::destDir = copyCoreDir + "/dest"; const string CopyCoreTest::srcFile = srcDir + "/src.txt"; const string CopyCoreTest::destFile = destDir + "/dest.txt"; @@ -52,6 +56,7 @@ void CopyCoreTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; mkdir(testDir.c_str(), permission0755); + mkdir(copyCoreDir.c_str(), permission0755); mkdir(srcDir.c_str(), permission0755); mkdir(destDir.c_str(), permission0755); int32_t fd = open(srcFile.c_str(), O_CREAT | O_RDWR, permission0644); @@ -114,7 +119,7 @@ HWTEST_F(CopyCoreTest, CopyCoreTest_IsValidUri_002, testing::ext::TestSize.Level { GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_IsValidUri_002"; - string invalidUri = "invalid://data/test/file.txt"; + string invalidUri = "invalid://data/test/CopyCoreTest_IsValidUri_002/file.txt"; auto res = CopyCore::IsValidUri(invalidUri); EXPECT_EQ(res, false); @@ -464,7 +469,7 @@ HWTEST_F(CopyCoreTest, CopyCoreTest_CopySubDir_001, testing::ext::TestSize.Level { GTEST_LOG_(INFO) << "CopyCoreTest-begin CopyCoreTest_CopySubDir_001"; - string subDir = srcDir + "/sub_dir"; + string subDir = srcDir + "/CopyCoreTest_CopySubDir_001_sub_dir"; mkdir(subDir.c_str(), permission0755); string subFile = subDir + "/sub_file.txt"; int fd = open(subFile.c_str(), O_CREAT | O_RDWR, permission0644); diff --git a/interfaces/test/unittest/js/mode_statvfs/statvfs_core_test.cpp b/interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp similarity index 100% rename from interfaces/test/unittest/js/mode_statvfs/statvfs_core_test.cpp rename to interfaces/test/unittest/js/mod_statvfs/statvfs_core_test.cpp diff --git a/interfaces/test/unittest/task_signal/BUILD.gn b/interfaces/test/unittest/task_signal/BUILD.gn index 2f9900498..a475b0fff 100644 --- a/interfaces/test/unittest/task_signal/BUILD.gn +++ b/interfaces/test/unittest/task_signal/BUILD.gn @@ -35,5 +35,6 @@ ohos_unittest("task_signal_test") { "c_utils:utils", "googletest:gtest_main", "hilog:libhilog", + "ipc:ipc_napi", ] } -- Gitee From 79bf3ba424fab2b32f6680f10189e1ff36f9ddda Mon Sep 17 00:00:00 2001 From: zhouxin Date: Sat, 26 Jul 2025 18:40:27 +0800 Subject: [PATCH 7/9] =?UTF-8?q?build.gn=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhouxin Change-Id: I4c88a32c9003d8bea788c16617942867a2bf0c7c --- interfaces/kits/js/BUILD.gn | 233 ++++++- .../js/src/mod_fs/ani/bind_function_class.cpp | 169 ++++- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 584 ++++++------------ interfaces/test/unittest/BUILD.gn | 2 + interfaces/test/unittest/js/BUILD.gn | 199 ++++-- interfaces/test/unittest/task_signal/BUILD.gn | 7 + 6 files changed, 762 insertions(+), 432 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index e8903bdb0..b6152a479 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -625,14 +625,17 @@ group("build_kits_js") { group("ani_file_api") { deps = [ + ":ani_file_environment", ":ani_file_fs", ":ani_file_hash", ":ani_file_securitylabel", + ":ani_file_statvfs", ] } config("ani_config") { include_dirs = [ + "./include", "${file_api_path}/interfaces/kits/rust/include", "${utils_path}/common/include", "${utils_path}/filemgmt_libfs/include", @@ -658,13 +661,28 @@ config("ani_config") { ohos_shared_library("ani_file_fs") { public_configs = [ ":ani_config" ] include_dirs = [ + "include/ipc", "src/mod_fs/ani", + "src/mod_fs/class_atomicfile", + "src/mod_fs/class_atomicfile/ani", "src/mod_fs/class_file", "src/mod_fs/class_file/ani", + "src/mod_fs/class_randomaccessfile", + "src/mod_fs/class_randomaccessfile/ani", + "src/mod_fs/class_readeriterator", + "src/mod_fs/class_readeriterator/ani", "src/mod_fs/class_stat", "src/mod_fs/class_stat/ani", + "src/mod_fs/class_stream", + "src/mod_fs/class_stream/ani", + "src/mod_fs/class_tasksignal", + "src/mod_fs/class_tasksignal/ani", + "src/mod_fs/class_watcher", + "src/mod_fs/class_watcher/ani", "src/mod_fs/properties", "src/mod_fs/properties/ani", + "src/mod_fs/properties/copy_listener", + "src/mod_fs/properties/copy_listener/ani", ] sources = [ "src/common/ani_helper/ani_signature.cpp", @@ -672,42 +690,107 @@ ohos_shared_library("ani_file_fs") { "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/mod_fs/ani/bind_function_class.cpp", + "src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp", + "src/mod_fs/class_atomicfile/fs_atomicfile.cpp", "src/mod_fs/class_file/ani/file_ani.cpp", "src/mod_fs/class_file/ani/file_wrapper.cpp", "src/mod_fs/class_file/file_instantiator.cpp", "src/mod_fs/class_file/fs_file.cpp", + "src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp", + "src/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp", + "src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp", + "src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp", + "src/mod_fs/class_readeriterator/fs_reader_iterator.cpp", + "src/mod_fs/class_stat/ani/lstat_ani.cpp", "src/mod_fs/class_stat/ani/stat_ani.cpp", "src/mod_fs/class_stat/ani/stat_wrapper.cpp", "src/mod_fs/class_stat/fs_stat.cpp", "src/mod_fs/class_stat/stat_instantiator.cpp", + "src/mod_fs/class_stream/ani/stream_ani.cpp", + "src/mod_fs/class_stream/ani/stream_wrapper.cpp", + "src/mod_fs/class_stream/fs_stream.cpp", + "src/mod_fs/class_stream/stream_instantiator.cpp", + "src/mod_fs/class_tasksignal/ani/task_signal_ani.cpp", + "src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.cpp", + "src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp", + "src/mod_fs/class_tasksignal/fs_task_signal.cpp", + "src/mod_fs/class_watcher/ani/fs_watcher_ani.cpp", + "src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp", + "src/mod_fs/class_watcher/ani/watch_event_listener.cpp", + "src/mod_fs/class_watcher/ani/watch_event_wrapper.cpp", + "src/mod_fs/class_watcher/fs_file_watcher.cpp", + "src/mod_fs/class_watcher/fs_watcher.cpp", + "src/mod_fs/class_watcher/watcher_data_cache.cpp", "src/mod_fs/fs_utils.cpp", "src/mod_fs/properties/access_core.cpp", "src/mod_fs/properties/ani/access_ani.cpp", "src/mod_fs/properties/ani/close_ani.cpp", + "src/mod_fs/properties/ani/connectdfs_ani.cpp", + "src/mod_fs/properties/ani/copy_ani.cpp", + "src/mod_fs/properties/ani/copy_dir_ani.cpp", "src/mod_fs/properties/ani/copy_file_ani.cpp", + "src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp", + "src/mod_fs/properties/ani/create_stream_ani.cpp", + "src/mod_fs/properties/ani/disconnectdfs_ani.cpp", + "src/mod_fs/properties/ani/dup_ani.cpp", + "src/mod_fs/properties/ani/fdatasync_ani.cpp", + "src/mod_fs/properties/ani/fdopen_stream_ani.cpp", + "src/mod_fs/properties/ani/fsync_ani.cpp", "src/mod_fs/properties/ani/listfile_ani.cpp", + "src/mod_fs/properties/ani/lseek_ani.cpp", "src/mod_fs/properties/ani/mkdir_ani.cpp", + "src/mod_fs/properties/ani/mkdtemp_ani.cpp", "src/mod_fs/properties/ani/move_ani.cpp", + "src/mod_fs/properties/ani/movedir_ani.cpp", "src/mod_fs/properties/ani/open_ani.cpp", "src/mod_fs/properties/ani/read_ani.cpp", + "src/mod_fs/properties/ani/read_lines_ani.cpp", "src/mod_fs/properties/ani/read_text_ani.cpp", + "src/mod_fs/properties/ani/rename_ani.cpp", "src/mod_fs/properties/ani/rmdir_ani.cpp", + "src/mod_fs/properties/ani/symlink_ani.cpp", "src/mod_fs/properties/ani/truncate_ani.cpp", "src/mod_fs/properties/ani/unlink_ani.cpp", + "src/mod_fs/properties/ani/utimes_ani.cpp", + "src/mod_fs/properties/ani/watcher_ani.cpp", "src/mod_fs/properties/ani/write_ani.cpp", + "src/mod_fs/properties/ani/xattr_ani.cpp", "src/mod_fs/properties/close_core.cpp", + "src/mod_fs/properties/connectdfs_core.cpp", + "src/mod_fs/properties/copy_core.cpp", + "src/mod_fs/properties/copy_dir_core.cpp", "src/mod_fs/properties/copy_file_core.cpp", + "src/mod_fs/properties/copy_listener/ani/progress_listener_ani.cpp", + "src/mod_fs/properties/copy_listener/trans_listener_core.cpp", + "src/mod_fs/properties/create_randomaccessfile_core.cpp", + "src/mod_fs/properties/create_stream_core.cpp", + "src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.cpp", + "src/mod_fs/properties/disconnectdfs_core.cpp", + "src/mod_fs/properties/dup_core.cpp", + "src/mod_fs/properties/fdatasync_core.cpp", + "src/mod_fs/properties/fdopen_stream_core.cpp", + "src/mod_fs/properties/fsync_core.cpp", "src/mod_fs/properties/listfile_core.cpp", + "src/mod_fs/properties/lseek_core.cpp", + "src/mod_fs/properties/lstat_core.cpp", "src/mod_fs/properties/mkdir_core.cpp", + "src/mod_fs/properties/mkdtemp_core.cpp", "src/mod_fs/properties/move_core.cpp", + "src/mod_fs/properties/movedir_core.cpp", "src/mod_fs/properties/open_core.cpp", "src/mod_fs/properties/read_core.cpp", + "src/mod_fs/properties/read_lines_core.cpp", "src/mod_fs/properties/read_text_core.cpp", + "src/mod_fs/properties/rename_core.cpp", "src/mod_fs/properties/rmdir_core.cpp", "src/mod_fs/properties/stat_core.cpp", + "src/mod_fs/properties/symlink_core.cpp", "src/mod_fs/properties/truncate_core.cpp", "src/mod_fs/properties/unlink_core.cpp", + "src/mod_fs/properties/utimes_core.cpp", + "src/mod_fs/properties/watcher_core.cpp", "src/mod_fs/properties/write_core.cpp", + "src/mod_fs/properties/xattr_core.cpp", ] deps = [ ":ohos_file_fs_abc_etc", @@ -721,7 +804,7 @@ ohos_shared_library("ani_file_fs") { external_deps = [ "ability_runtime:ability_manager", "app_file_service:fileuri_native", - "bounds_checking_function:libsec_static", + "bounds_checking_function:libsec_shared", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", @@ -786,6 +869,8 @@ ohos_shared_library("ani_file_hash") { "src/mod_fs/fs_utils.cpp", "src/mod_hash/ani/bind_function_class.cpp", "src/mod_hash/ani/hash_ani.cpp", + "src/mod_hash/class_hashstream/ani/hashstream_ani.cpp", + "src/mod_hash/class_hashstream/hs_hashstream.cpp", "src/mod_hash/hash_core.cpp", ] @@ -796,10 +881,8 @@ ohos_shared_library("ani_file_hash") { "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ - "bounds_checking_function:libsec_static", - "eventhandler:libeventhandler", + "bounds_checking_function:libsec_shared", "hilog:libhilog", - "ipc:ipc_core", "libuv:uv", "openssl:libcrypto_shared", "runtime_core:ani", @@ -861,10 +944,8 @@ ohos_shared_library("ani_file_securitylabel") { "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ - "bounds_checking_function:libsec_static", - "eventhandler:libeventhandler", + "bounds_checking_function:libsec_shared", "hilog:libhilog", - "ipc:ipc_core", "libuv:uv", "runtime_core:ani", "runtime_core:ani_helpers", @@ -900,3 +981,141 @@ ohos_prebuilt_etc("ohos_file_securityLabel_abc_etc") { part_name = "file_api" deps = [ ":ohos_file_securityLabel_abc" ] } + +ohos_shared_library("ani_file_environment") { + public_configs = [ ":ani_config" ] + include_dirs = [ + "src/mod_environment/ani", + "src/mod_environment", + ] + sources = [ + "src/common/ani_helper/ani_signature.cpp", + "src/common/ani_helper/error_handler.cpp", + "src/common/ani_helper/type_converter.cpp", + "src/common/file_helper/fd_guard.cpp", + "src/mod_environment/ani/bind_function_class.cpp", + "src/mod_environment/ani/environment_ani.cpp", + "src/mod_environment/environment_core.cpp", + "src/mod_fs/fs_utils.cpp", + ] + + deps = [ + ":ohos_file_environment_abc_etc", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bounds_checking_function:libsec_shared", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "libuv:uv", + "openssl:libcrypto_shared", + "os_account:os_account_innerkits", + "runtime_core:ani", + "runtime_core:ani_helpers", + "runtime_core:libarkruntime", + ] + use_exceptions = true + + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + output_extension = "so" + subsystem_name = "filemanagement" + part_name = "file_api" +} + +generate_static_abc("ohos_file_environment_abc") { + base_url = "./src/mod_environment/ani/ets" + files = [ "./src/mod_environment/ani/ets/@ohos.file.environment.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/ohos_file_environment_abc.abc" +} + +ohos_prebuilt_etc("ohos_file_environment_abc_etc") { + source = "$target_out_dir/ohos_file_environment_abc.abc" + module_install_dir = "framework" + subsystem_name = "filemanagement" + part_name = "file_api" + deps = [ ":ohos_file_environment_abc" ] +} + +ohos_shared_library("ani_file_statvfs") { + public_configs = [ ":ani_config" ] + include_dirs = [ + "src/mod_statvfs/ani", + "src/mod_statvfs", + ] + sources = [ + "src/common/ani_helper/ani_signature.cpp", + "src/common/ani_helper/error_handler.cpp", + "src/common/ani_helper/type_converter.cpp", + "src/common/file_helper/fd_guard.cpp", + "src/mod_fs/fs_utils.cpp", + "src/mod_statvfs/ani/bind_function_class.cpp", + "src/mod_statvfs/ani/statvfs_ani.cpp", + "src/mod_statvfs/statvfs_core.cpp", + ] + + deps = [ + ":ohos_file_statvfs_abc_etc", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + external_deps = [ + "bounds_checking_function:libsec_shared", + "hilog:libhilog", + "libuv:uv", + "runtime_core:ani", + "runtime_core:ani_helpers", + "runtime_core:libarkruntime", + ] + use_exceptions = true + cflags = [ + "-fdata-sections", + "-ffunction-sections", + "-Oz", + ] + cflags_cc = [ + "-fvisibility-inlines-hidden", + "-Oz", + ] + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + output_extension = "so" + subsystem_name = "filemanagement" + part_name = "file_api" +} + +generate_static_abc("ohos_file_statvfs_abc") { + base_url = "./src/mod_statvfs/ani/ets" + files = [ "./src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/ohos_file_statvfs_abc.abc" +} + +ohos_prebuilt_etc("ohos_file_statvfs_abc_etc") { + source = "$target_out_dir/ohos_file_statvfs_abc.abc" + module_install_dir = "framework" + subsystem_name = "filemanagement" + part_name = "file_api" + deps = [ ":ohos_file_statvfs_abc" ] +} diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index f8da76c82..f9ebd1051 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -13,31 +13,84 @@ * limitations under the License. */ -#include - #include +#include #include "access_ani.h" #include "ani_signature.h" +#include "atomicfile_ani.h" #include "bind_function.h" #include "close_ani.h" +#include "connectdfs_ani.h" +#include "copy_ani.h" +#include "copy_dir_ani.h" #include "copy_file_ani.h" +#include "create_randomaccessfile_ani.h" +#include "create_stream_ani.h" +#include "disconnectdfs_ani.h" +#include "dup_ani.h" +#include "fdatasync_ani.h" +#include "fdopen_stream_ani.h" #include "file_ani.h" +#include "filemgmt_libhilog.h" +#include "fs_watcher_ani.h" +#include "fsync_ani.h" #include "listfile_ani.h" +#include "lseek_ani.h" +#include "lstat_ani.h" #include "mkdir_ani.h" +#include "mkdtemp_ani.h" #include "move_ani.h" -#include "read_ani.h" -#include "rmdir_ani.h" +#include "movedir_ani.h" #include "open_ani.h" +#include "randomaccessfile_ani.h" +#include "read_ani.h" +#include "read_lines_ani.h" #include "read_text_ani.h" +#include "reader_iterator_ani.h" +#include "rename_ani.h" +#include "rmdir_ani.h" #include "stat_ani.h" +#include "stream_ani.h" +#include "symlink_ani.h" +#include "task_signal_ani.h" #include "truncate_ani.h" #include "unlink_ani.h" +#include "utimes_ani.h" +#include "watcher_ani.h" #include "write_ani.h" +#include "xattr_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; +static ani_status BindRafFileMethods(ani_env *env) +{ + auto classDesc = FS::RandomAccessFileInner::classDesc.c_str(); + + std::array methods = { + ani_native_function { + "setFilePointer0", nullptr, reinterpret_cast(RandomAccessFileAni::SetFilePointer) }, + ani_native_function { "close", nullptr, reinterpret_cast(RandomAccessFileAni::Close) }, + ani_native_function { "writeSync0", nullptr, reinterpret_cast(RandomAccessFileAni::WriteSync) }, + ani_native_function { "readSync0", nullptr, reinterpret_cast(RandomAccessFileAni::ReadSync) }, + }; + + return BindClass(env, classDesc, methods); +} + +static ani_status BindWatcherClassMethods(ani_env *env) +{ + auto classDesc = FS::WatcherInner::classDesc.c_str(); + + std::array methods = { + ani_native_function { "start", nullptr, reinterpret_cast(FsWatcherAni::Start) }, + ani_native_function { "stop", nullptr, reinterpret_cast(FsWatcherAni::Stop) }, + }; + + return BindClass(env, classDesc, methods); +} + static ani_status BindFileMethods(ani_env *env) { auto classDesc = FS::FileInner::classDesc.c_str(); @@ -52,6 +105,17 @@ static ani_status BindFileMethods(ani_env *env) return BindClass(env, classDesc, methods); } +static ani_status BindReaderIteratorMethods(ani_env *env) +{ + auto classDesc = FS::ReaderIteratorInner::classDesc.c_str(); + + std::array methods = { + ani_native_function { "next", nullptr, reinterpret_cast(ReaderIteratorAni::Next) }, + }; + + return BindClass(env, classDesc, methods); +} + static ani_status BindStatClassMethods(ani_env *env) { auto classDesc = FS::StatInner::classDesc.c_str(); @@ -69,6 +133,49 @@ static ani_status BindStatClassMethods(ani_env *env) return BindClass(env, classDesc, methods); } +static ani_status BindStreamMethods(ani_env *env) +{ + auto classDesc = FS::StreamInner::classDesc.c_str(); + + std::array methods = { + ani_native_function { "closeSync", nullptr, reinterpret_cast(StreamAni::Close) }, + ani_native_function { "flushSync", nullptr, reinterpret_cast(StreamAni::Flush) }, + ani_native_function { "readSync", nullptr, reinterpret_cast(StreamAni::Read) }, + ani_native_function { "writeSync", nullptr, reinterpret_cast(StreamAni::Write) }, + ani_native_function { "seek", nullptr, reinterpret_cast(StreamAni::Seek) }, + }; + + return BindClass(env, classDesc, methods); +} + +static ani_status BindTaskSignalClassMethods(ani_env *env) +{ + auto classDesc = FS::TaskSignal::classDesc.c_str(); + + std::array methods = { + ani_native_function { "cancel", nullptr, reinterpret_cast(TaskSignalAni::Cancel) }, + ani_native_function { "onCancelNative", nullptr, reinterpret_cast(TaskSignalAni::OnCancel) }, + }; + + return BindClass(env, classDesc, methods); +} + +static ani_status BindAtomicFileMethods(ani_env *env) +{ + auto classDesc = FS::AtomicFile::classDesc.c_str(); + auto ctorDesc = FS::AtomicFile::ctorDesc.c_str(); + auto ctorSig = FS::AtomicFile::ctorSig.c_str(); + + std::array methods = { + ani_native_function { "getPath", nullptr, reinterpret_cast(AtomicFileAni::GetPath) }, + ani_native_function { "getBaseFile", nullptr, reinterpret_cast(AtomicFileAni::GetBaseFile) }, + ani_native_function { "readFully", nullptr, reinterpret_cast(AtomicFileAni::ReadFully) }, + ani_native_function { "delete", nullptr, reinterpret_cast(AtomicFileAni::Delete) }, + ani_native_function { ctorDesc, ctorSig, reinterpret_cast(AtomicFileAni::Constructor) }, + }; + + return BindClass(env, classDesc, methods); +} const static string mkdirCtorSig0 = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); const static string mkdirCtorSig1 = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType, BasicTypes::booleanType }); @@ -79,19 +186,43 @@ static ani_status BindStaticMethods(ani_env *env) std::array methods = { ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, + ani_native_function { "connectDfs", nullptr, reinterpret_cast(ConnectDfsAni::ConnectDfsSync) }, + ani_native_function { "copyDirSync", nullptr, reinterpret_cast(CopyDirAni::CopyDirSync) }, ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, + ani_native_function { "copySync", nullptr, reinterpret_cast(CopyAni::CopySync) }, + ani_native_function { "createRandomAccessFileSync", nullptr, + reinterpret_cast(CreateRandomAccessFileAni::CreateRandomAccessFileSync) }, + ani_native_function { + "createStreamSync", nullptr, reinterpret_cast(CreateStreamAni::CreateStreamSync) }, + ani_native_function { "createWatcherSync", nullptr, reinterpret_cast(WatcherAni::CreateWatcherSync) }, + ani_native_function { "disConnectDfs", nullptr, reinterpret_cast(DisConnectDfsAni::DisConnectDfsSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, + ani_native_function { "dup", nullptr, reinterpret_cast(DupAni::Dup) }, + ani_native_function { "fdatasyncSync", nullptr, reinterpret_cast(FDataSyncAni::FDataSyncSync) }, + ani_native_function { + "fdopenStreamSync", nullptr, reinterpret_cast(FdopenStreamAni::FdopenStreamSync) }, + ani_native_function { "fsyncSync", nullptr, reinterpret_cast(FsyncAni::FsyncSync) }, + ani_native_function { "getxattrSync", nullptr, reinterpret_cast(XattrAni::GetXattrSync) }, ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, + ani_native_function { "lseekSync", nullptr, reinterpret_cast(LseekAni::LseekSync) }, + ani_native_function { "lstatSync", nullptr, reinterpret_cast(LstatAni::LstatSync) }, ani_native_function { "mkdirSync", mkdirCtorSig0.c_str(), reinterpret_cast(MkdirkAni::MkdirSync0) }, ani_native_function { "mkdirSync", mkdirCtorSig1.c_str(), reinterpret_cast(MkdirkAni::MkdirSync1) }, + ani_native_function { "mkdtempSync", nullptr, reinterpret_cast(MkdtempAni::MkdtempSync) }, + ani_native_function { "movedirSync", nullptr, reinterpret_cast(MoveDirAni::MoveDirSync) }, ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, + ani_native_function { "readlinesSync", nullptr, reinterpret_cast(ReadLinesAni::ReadLinesSync) }, ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, + ani_native_function { "renameSync", nullptr, reinterpret_cast(RenameAni::RenameSync) }, ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, + ani_native_function { "setxattrSync", nullptr, reinterpret_cast(XattrAni::SetXattrSync) }, ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, + ani_native_function { "symlinkSync", nullptr, reinterpret_cast(SymlinkAni::SymlinkSync) }, ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, + ani_native_function { "utimes", nullptr, reinterpret_cast(UtimesAni::Utimes) }, ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, }; return BindClass(env, classDesc, methods); @@ -110,11 +241,41 @@ static ani_status DoBindMethods(ani_env *env) return status; }; + if ((status = BindReaderIteratorMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for ReaderIterator Class"); + return status; + }; + if ((status = BindStatClassMethods(env)) != ANI_OK) { HILOGE("Cannot bind native methods for Stat Class!"); return status; }; + if ((status = BindRafFileMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for RafFile Class"); + return status; + }; + + if ((status = BindStreamMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for Stream Class!"); + return status; + }; + + if ((status = BindTaskSignalClassMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for TaskSignal Class!"); + return status; + }; + + if ((status = BindWatcherClassMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for Watcher Class"); + return status; + }; + + if ((status = BindAtomicFileMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for AtomicFile Class!"); + return status; + }; + return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index 5de05d13c..bf94fd628 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -13,7 +13,6 @@ * limitations under the License. */ import { BusinessError, AsyncCallback } from '@ohos.base'; -// import stream from '@ohos.util.stream'; const UNKNOWN_ERR: number = 13900042 const UNKNOWN_MSG: string = "Unknown error" @@ -47,8 +46,8 @@ function access(path: string, mode?: AccessModeType): Promise { promise.then((ret: NullishType): void => { let result = ret as boolean; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -62,8 +61,8 @@ function access(path: string, callback: AsyncCallback): void { e.code = 0; let result = ret as boolean; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, false); + }).catch((e: Error): void => { + callback(e as BusinessError, false); }); } @@ -75,8 +74,8 @@ function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promi promise.then((ret: NullishType): void => { let result = ret as boolean; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }) } @@ -94,8 +93,8 @@ function close(file: number | File): Promise { let promise = taskpool.execute((file: number | File): undefined => FileIoImpl.closeSync(file), file); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -106,8 +105,8 @@ function close(file: number | File, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -121,8 +120,8 @@ function connectDfs(networkId: string, listeners: DfsListeners): Promise { FileIoImpl.connectDfs(networkId, listeners), networkId, listeners); promise.then((): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -132,8 +131,8 @@ function disconnectDfs(networkId: string): Promise { let promise = taskpool.execute((networkId: string): void => FileIoImpl.disConnectDfs(networkId), networkId); promise.then((): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -150,8 +149,8 @@ function getxattr(path: string, key: string): Promise { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -171,8 +170,8 @@ function copyDir(src: string, dest: string, mode?: number): Promise { FileIoImpl.copyDirSync(src, dest, mode), src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError>); }); }); } @@ -185,8 +184,8 @@ function copyDir(src: string, dest: string, callback: AsyncCallback(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -198,8 +197,8 @@ function copyDir(src: string, dest: string, mode: number, callback: AsyncCallbac e.code = 0; e.data = new Array(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -237,7 +236,7 @@ function fdatasync(fd: number): Promise { let promise = taskpool.execute((fd: number): undefined => FileIoImpl.fdatasyncSync(fd), fd); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -249,8 +248,8 @@ function fdatasync(fd: number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -273,7 +272,7 @@ function mkdir(path: string): Promise { let promise = taskpool.execute((path: string): undefined => mkdirSync1(path), path); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -285,8 +284,8 @@ function mkdir(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -309,8 +308,8 @@ function mkdir(path: string, recursion: boolean, callback: AsyncCallback): let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -326,8 +325,8 @@ function moveDir(src: string, dest: string, mode?: number): Promise { }, src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError>); }); }) } @@ -341,8 +340,8 @@ function moveDir(src: string, dest: string, callback: AsyncCallback(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -355,8 +354,8 @@ function moveDir(src: string, dest: string, mode: number, callback: AsyncCallbac e.code = 0; e.data = new Array(0); callback(e, undefined); - }).catch((e: BusinessError>): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError>, undefined); }); } @@ -393,8 +392,8 @@ function mkdtemp(prefix: string): Promise { promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -408,8 +407,8 @@ function mkdtemp(prefix: string, callback: AsyncCallback): void { e.code = 0; let result = ret as string; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -424,8 +423,8 @@ function moveFile(src: string, dest: string, mode?: number): Promise { }, src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -438,8 +437,8 @@ function moveFile(src: string, dest: string, mode: number, callback: AsyncCallba let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -451,8 +450,8 @@ function moveFile(src: string, dest: string, callback: AsyncCallback): voi let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -468,8 +467,8 @@ function open(path: String, mode?: number): Promise { promise.then((ret: NullishType): void => { let file = ret as File; resolve(file); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -483,9 +482,9 @@ function open(path: String, mode: number, callback: AsyncCallback): e.code = 0; let file = ret as File; callback(e, file); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: File = new FileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -498,9 +497,9 @@ function open(path: String, callback: AsyncCallback): void { e.code = 0; let file = ret as File; callback(e, file); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: File = new FileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -516,8 +515,8 @@ function write(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions) promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -532,8 +531,8 @@ function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -546,8 +545,8 @@ function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -563,8 +562,8 @@ function read(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise { let result = ret as number; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -578,8 +577,8 @@ function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -592,8 +591,8 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: A e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -610,8 +609,8 @@ function readLines(filePath: string, options?: Options): Promise promise.then((ret: NullishType): void => { let it = ret as ReaderIterator; resolve(it); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -625,9 +624,9 @@ function readLines(filePath: string, callback: AsyncCallback): v e.code = 0; let it = ret as ReaderIterator; callback(e, it); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let r: ReaderIterator = new ReaderIteratorInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -640,9 +639,9 @@ function readLines(filePath: string, options: Options, callback: AsyncCallback { + }).catch((e: Error): void => { let r: ReaderIterator = new ReaderIteratorInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -656,8 +655,8 @@ function rmdir(path: string): Promise { let promise = taskpool.execute((path: string): void => FileIoImpl.rmdirSync(path), path); promise.then((ret: NullishType) => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -668,8 +667,8 @@ function rmdir(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -684,8 +683,8 @@ function truncate(file: string | number, len?: number): Promise { }, file, len); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }) } @@ -698,8 +697,8 @@ function truncate(file: string | number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -711,8 +710,8 @@ function truncate(file: string | number, len: number, callback: AsyncCallback(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -726,8 +725,8 @@ function unlink(path: string): Promise { let promise = taskpool.execute((path: string): undefined => unlinkSync(path), path); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -738,8 +737,8 @@ function unlink(path: string, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -751,9 +750,9 @@ function readText(filePath: string, options?: ReadTextOptions): Promise promise.then((ret: NullishType): void => { let r = ret as string; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); - }); + }).catch((e: Error): void => { + reject(e as BusinessError); + }); }); } @@ -766,8 +765,8 @@ function readText(filePath: string, callback: AsyncCallback): void { e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -780,8 +779,8 @@ function readText(filePath: string, options: ReadTextOptions, callback: AsyncCal e.code = 0; let r = ret as string; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); + }).catch((e: Error): void => { + callback(e as BusinessError, ""); }); } @@ -797,8 +796,8 @@ function listFile(path: string, options?: ListFileOptions): Promise { promise.then((ret: NullishType): void => { let r = ret as string[]; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -812,8 +811,8 @@ function listFile(path: string, callback: AsyncCallback): void { e.code = 0; let r = ret as string[]; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, []); + }).catch((e: Error): void => { + callback(e as BusinessError, []); }); } @@ -826,8 +825,8 @@ function listFile(path: string, options: ListFileOptions, callback: AsyncCallbac e.code = 0; let r = ret as string[]; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, []); + }).catch((e: Error): void => { + callback(e as BusinessError, []); }); } @@ -851,8 +850,8 @@ function stat(file: string | number): Promise { promise.then((ret: NullishType): void => { let r = ret as Stat; resolve(r); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -866,8 +865,8 @@ function stat(file: string | number, callback: AsyncCallback): void e.code = 0; let r = ret as Stat; callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, new StatInner(0)); + }).catch((e: Error): void => { + callback(e as BusinessError, new StatInner(0)); }); } @@ -882,8 +881,8 @@ function fsync(fd: number): Promise { }, fd); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -896,8 +895,8 @@ function fsync(fd: number, callback: AsyncCallback): void { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -913,8 +912,8 @@ function symlink(target: string, srcPath: string): Promise { }, target, srcPath); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -929,8 +928,8 @@ function rename(oldPath: string, newPath: string): Promise { }, oldPath, newPath); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -943,8 +942,8 @@ function rename(oldPath: string, newPath: string, callback: AsyncCallback) let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -964,8 +963,8 @@ function createRandomAccessFile(file: string | File, mode?: number, promise.then((ret: NullishType): void => { let raffile = ret as RandomAccessFileInner; resolve(raffile); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -978,8 +977,8 @@ function fdopenStream(fd: number, mode: string): Promise { promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -994,7 +993,7 @@ function setxattr(path: string, key: string, value: string): Promise { FileIoImpl.setxattrSync(path, key, value), path, key, value); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { reject(e as BusinessError); }); }); @@ -1009,9 +1008,9 @@ function createRandomAccessFile(file: string | File, callback: AsyncCallback { + }).catch((e: Error): void => { let f: RandomAccessFile = new RandomAccessFileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -1025,9 +1024,9 @@ function createRandomAccessFile(file: string | File, mode: number, e.code = 0; let raffile = ret as RandomAccessFile; callback(e, raffile); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let f: RandomAccessFile = new RandomAccessFileInner(0); - callback(e, f); + callback(e as BusinessError, f); }); } @@ -1040,9 +1039,9 @@ function fdopenStream(fd: number, mode: string, callback: AsyncCallback) e.code = 0; let stream = ret as Stream; callback(e, stream); - }).catch((e: BusinessError): void => { + }).catch((e: Error): void => { let r: Stream = new StreamInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -1058,8 +1057,8 @@ function createStream(path: string, mode: string): Promise { promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1073,9 +1072,9 @@ function createStream(path: string, mode: string, callback: AsyncCallback { + }).catch((e: Error): void => { let r: Stream = new StreamInner(0); - callback(e, r); + callback(e as BusinessError, r); }); } @@ -1083,14 +1082,6 @@ function createStreamSync(path: string, mode: string): Stream { return FileIoImpl.createStreamSync(path, mode); } -// function createReadStream(path: string, options?: ReadStreamOptions): ReadStream { -// return new ReadStream(path, options) -// } - -// function createWriteStream(path: string, options?: WriteStreamOptions): WriteStream { -// return new WriteStream(path, options); -// } - function createWatcher(path: string, events: number, listener: WatchEventListener): Watcher { return FileIoImpl.createWatcherSync(path, events, listener); } @@ -1103,8 +1094,8 @@ function symlink(target: string, srcPath: string, callback: AsyncCallback) let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1130,8 +1121,8 @@ function lstat(path: string): Promise { let r = ret as Stat; resolve(r); } - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1151,8 +1142,8 @@ function lstat(path: string, callback: AsyncCallback): void { let r = ret as Stat; callback(e, r); } - }).catch((e: BusinessError): void => { - callback(e, new StatInner(0)); + }).catch((e: Error): void => { + callback(e as BusinessError, new StatInner(0)); }); } function copyFile(src: string | number, dest: string | number, mode?: number): Promise { @@ -1161,8 +1152,8 @@ function copyFile(src: string | number, dest: string | number, mode?: number): P FileIoImpl.copyFileSync(src, dest, mode), src, dest, mode); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1173,8 +1164,8 @@ function copy(srcUri: string, destUri: string, options?: CopyOptions): Promise { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1186,8 +1177,8 @@ function copyFile(src: string | number, dest: string | number, mode: number, cal let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1198,8 +1189,8 @@ function copy(srcUri: string, destUri: string, options: CopyOptions, callback: A let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1210,8 +1201,8 @@ function copyFile(src: string | number, dest: string | number, callback: AsyncCa let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1222,8 +1213,8 @@ function copy(srcUri: string, destUri: string, callback: AsyncCallback): v let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1263,20 +1254,31 @@ export type ProgressListener = (progress: Progress) => void; export class TaskSignal { private nativeTaskSignal: long = 0; + private native onCancelNative(): void; + private onCancelResolve: (path: string) => void = (path: string): void => {}; private onCancelCallback(path: string): void { if (this.onCancelResolve) { this.onCancelResolve(path); } } + native cancel(): void; + onCancel(): Promise { - return new Promise((resolve: (path: string) => void, reject: (e: BusinessError) => void): void => { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { this.onCancelResolve = resolve; - this.onCancelNative(); + try { + this.onCancelNative(); + } catch (e: BusinessError) { + reject(e); + } catch (e: Error) { + reject(e as BusinessError); + } }); } + } export interface CopyOptions { @@ -1309,8 +1311,6 @@ export interface RandomAccessFile { read(buffer: ArrayBuffer, callback: AsyncCallback): void; read(buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void; readSync(buffer: ArrayBuffer, options?: ReadOptions): number; - // getReadStream(): ReadStream; - // getWriteStream(): WriteStream; } export class RandomAccessFileInner implements RandomAccessFile { @@ -1331,7 +1331,7 @@ export class RandomAccessFileInner implements RandomAccessFile { } native setFilePointer0(filePointer: number): void; - + native close(): void; writeSync(buffer: ArrayBuffer | string, options?: WriteOptions): number { @@ -1350,8 +1350,8 @@ export class RandomAccessFileInner implements RandomAccessFile { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1365,8 +1365,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1379,8 +1379,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1400,8 +1400,8 @@ export class RandomAccessFileInner implements RandomAccessFile { promise.then((ret: NullishType): void => { let result = ret as number; resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1415,8 +1415,8 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1429,13 +1429,11 @@ export class RandomAccessFileInner implements RandomAccessFile { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } - // native getReadStream(): ReadStream; - // native getWriteStream(): WriteStream; } export interface File { @@ -1474,8 +1472,8 @@ export class FileInner implements File { }, exclusive); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1488,8 +1486,8 @@ export class FileInner implements File { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1501,8 +1499,8 @@ export class FileInner implements File { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1590,7 +1588,7 @@ export class StatInner implements Stat { this.nativeStat = stat; } } - + native isBlockDevice(): boolean; native isCharacterDevice(): boolean; native isDirectory(): boolean; @@ -1632,8 +1630,8 @@ export class StreamInner implements Stream { let promise = taskpool.execute((): undefined => this.closeSync()); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1644,8 +1642,8 @@ export class StreamInner implements Stream { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1656,8 +1654,8 @@ export class StreamInner implements Stream { let promise = taskpool.execute((): undefined => this.flushSync()); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1668,8 +1666,8 @@ export class StreamInner implements Stream { let e = new BusinessError(); e.code = 0; callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); + }).catch((e: Error): void => { + callback(e as BusinessError, undefined); }); } @@ -1683,8 +1681,8 @@ export class StreamInner implements Stream { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1698,8 +1696,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1712,8 +1710,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1727,8 +1725,8 @@ export class StreamInner implements Stream { promise.then((ret: NullishType): void => { let result = ret as number resolve(result); - }).catch((e: BusinessError): void => { - reject(e); + }).catch((e: Error): void => { + reject(e as BusinessError); }); }); } @@ -1742,8 +1740,8 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } @@ -1756,163 +1754,15 @@ export class StreamInner implements Stream { e.code = 0; let result = ret as number; callback(e, result); - }).catch((e: BusinessError): void => { - callback(e, 0); + }).catch((e: Error): void => { + callback(e as BusinessError, 0); }); } - + native readSync(buffer: ArrayBuffer, options?: ReadOptions): number; native seek(offset: number, whence?: number): number; } -// export class ReadStream extends stream.Readable { -// path: string; -// bytesRead: number; -// private offset: number; -// private start?: number; -// private end?: number; -// private stream?: Stream; - -// constructor(path: string, options?: ReadStreamOptions) { -// super(); -// this.path = path; -// this.bytesRead = 0; -// this.start = options?.start; -// this.end = options?.end; -// this.stream = createStreamSync(this.path, 'r'); -// this.offset = this.start ?? 0; -// } - -// seek(offset: number, whence?: WhenceType): number { -// if (whence === undefined) { -// let off = this.stream?.seek(offset); -// if (off !== undefined) { -// this.offset = off -// } -// } else { -// let off = this.stream?.seek(offset, whence); -// if (off !== undefined) { -// this.offset = off -// } -// } -// return this.offset; -// } - -// close(): void { -// this.stream?.close(); -// } - -// doInitialize(callback: () => void): void { -// callback(); -// } - -// doRead(size: number): void { -// let readSize = size; -// let end = this.end -// if (end !== undefined) { -// if (this.offset > end) { -// this.push(null); -// return; -// } -// if (this.offset + readSize > end) { -// readSize = end - this.offset; -// } -// } -// let buffer = new ArrayBuffer(readSize); -// const off = this.offset; -// this.offset += readSize; -// this.stream?.read(buffer, { offset: off, length: readSize }) -// .then((readOut: number) => { -// if (readOut > 0) { -// this.bytesRead += readOut; -// this.push(new Uint8Array(buffer.slice(0, readOut))); -// } -// if (readOut !== readSize || readOut < size) { -// this.offset = this.offset - readSize + readOut; -// this.push(null); -// } -// }); -// } -// } - -// export class WriteStream extends stream.Writable { -// path: string; -// bytesWritten: number; -// private offset: number; -// private mode: string; -// private start?: number; -// private stream?: Stream; - -// constructor(path: string, options?: WriteStreamOptions) { -// super(); -// this.path = path; -// this.bytesWritten = 0; -// this.start = options?.start; -// this.mode = this.convertOpenMode(options?.mode); -// this.stream = createStreamSync(this.path, this.mode); -// this.offset = this.start ?? 0; -// } - -// seek(offset: number, whence?: WhenceType): number { -// if (whence === undefined) { -// let off = this.stream?.seek(offset); -// if (off !== undefined) { -// this.offset = off -// } -// } else { -// let off = this.stream?.seek(offset, whence); -// if (off !== undefined) { -// this.offset = off -// } -// } -// return this.offset; -// } - -// close(): void { -// this.stream?.close(); -// } - -// closeSync(): void { -// this.stream?.closeSync(); -// } - -// doInitialize(callback: () => void): void { -// callback(); -// } - -// doWrite(chunk: string | ArrayBuffer, encoding: string, callback: () => void): void { -// this.stream?.write(chunk, { offset: this.offset }) -// .then((writeIn: number) => { -// this.offset += writeIn; -// this.bytesWritten += writeIn; -// callback(); -// }) -// .finally(() => { -// this.stream?.flush(); -// }); -// } - -// convertOpenMode(mode?: number): string { -// let modeStr = 'w'; -// if (mode === undefined) { -// return modeStr; -// } -// if ((mode as number) & fileIo.OpenMode.WRITE_ONLY) { -// modeStr = 'w'; -// } -// if ((mode as number) & fileIo.OpenMode.READ_WRITE) { -// modeStr = 'w+'; -// } -// if (((mode as number) & fileIo.OpenMode.WRITE_ONLY) && ((mode as number) & fileIo.OpenMode.APPEND)) { -// modeStr = 'a'; -// } -// if (((mode as number) & fileIo.OpenMode.READ_WRITE) && ((mode as number) & fileIo.OpenMode.APPEND)) { -// modeStr = 'a+'; -// } -// return modeStr; -// } -// } - export class AtomicFile { static { loadLibrary("ani_file_fs"); @@ -1920,43 +1770,13 @@ export class AtomicFile { private nativePtr: long = 0; private native getPath(): string; - // private writeStream: WriteStream | null = null; native constructor(path: string); native getBaseFile(): File; - // native openRead(): ReadStream; - native readFully(): ArrayBuffer; - // native nativeStartWrite(): WriteStream; - // startWrite(): WriteStream { - // let ws = this.nativeStartWrite(); - // this.writeStream = ws; - // return ws; - // } - - // native nativeFinishWrite(): void; - // finishWrite(): void { - // if (!this.writeStream) { - // throw createBusinessError(UNKNOWN_ERR, UNKNOWN_MSG); - // } - // this.writeStream?.close(); - // this.nativeFinishWrite(); - // this.writeStream = null; - // }; - - // native nativeFailWrite(): void; - // failWrite(): void { - // if (!this.writeStream) { - // throw createBusinessError(UNKNOWN_ERR, UNKNOWN_MSG); - // } - // this.writeStream?.close(); - // this.nativeFailWrite(); - // this.writeStream = null; - // }; - native delete(): void; } @@ -2119,15 +1939,15 @@ export class FileIoImpl { static native createStreamSync(path: string, mode: string): fileIo.Stream; static native createWatcherSync(path: string, events: number, listener: WatchEventListener): fileIo.Watcher; - + static native fdopenStreamSync(fd: number, mode: string): fileIo.Stream; - + static native dup(fd: number): fileIo.File; static native listFileSync(path: string, options?: ListFileOptions): string[]; static native lstatSync(path: string): fileIo.Stat; - + static native lseekSync(fd: number, offset: number, whence?: fileIo.WhenceType): number; static native mkdirSync(path: string): void; @@ -2143,7 +1963,7 @@ export class FileIoImpl { static native openSync(path: String, mode?: number): fileIo.File; static native readlinesSync(filePath: string, options?: Options): fileIo.ReaderIterator; - + static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; static native readTextSync(filePath: string, options?: ReadTextOptions): string; diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index c6df1457c..9a3e2915e 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -19,10 +19,12 @@ group("file_api_unittest") { "class_atomicfile:class_atomicfile_test", "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", + "js:ani_file_environment_test", "js:ani_file_fs_mock_test", "js:ani_file_fs_test", "js:ani_file_hash_test", "js:ani_file_securitylabel_test", + "js:ani_file_statvfs_test", "js:napi_file_fs_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index c6fc874c1..af69f1996 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -14,61 +14,46 @@ import("//build/test.gni") import("//foundation/filemanagement/file_api/file_api.gni") -ohos_unittest("ani_file_fs_mock_test") { +ohos_unittest("ani_file_environment_test") { branch_protector_ret = "pac_ret" testonly = true module_out_path = "file_api/file_api" - include_dirs = [ - "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", - "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", - "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", - "${file_api_path}/interfaces/test/unittest/js/mod_fs/mock", - "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + "${src_path}/mod_environment", + "${src_path}/mod_environment/ani", + "${file_api_path}/interfaces/test/unittest/js/mod_environment/mock", ] sources = [ - "mod_fs/class_file/fs_file_mock_test.cpp", - "mod_fs/class_stat/fs_stat_mock_test.cpp", - "mod_fs/mock/unistd_mock.cpp", - "mod_fs/properties/access_core_mock_test.cpp", - "mod_fs/properties/copy_file_core_mock_test.cpp", - "mod_fs/properties/mkdir_core_mock_test.cpp", - "mod_fs/properties/mock/system_mock.cpp", - "mod_fs/properties/mock/uv_fs_mock.cpp", - "mod_fs/properties/move_core_mock_test.cpp", - "mod_fs/properties/open_core_mock_test.cpp", - "mod_fs/properties/read_core_mock_test.cpp", - "mod_fs/properties/stat_core_mock_test.cpp", - "mod_fs/properties/truncate_core_mock_test.cpp", - "mod_fs/properties/unlink_core_mock_test.cpp", - "mod_fs/properties/write_core_mock_test.cpp", + "mod_environment/environment_core_mock_test.cpp", + "mod_environment/mock/accesstoken_kit_mock.cpp", + "mod_environment/mock/ipc_skeleton_mock.cpp", + "mod_environment/mock/parameter_mock.cpp", ] deps = [ - "${file_api_path}/interfaces/kits/js:ani_file_fs", - "${file_api_path}/interfaces/kits/native:remote_uri_native", - "${file_api_path}/interfaces/kits/native:task_signal_native", - "${file_api_path}/interfaces/kits/rust:rust_file", + "${file_api_path}/interfaces/kits/js:ani_file_environment", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ - "ability_runtime:ability_manager", - "app_file_service:fileuri_native", - "c_utils:utils", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", "libuv:uv", + "openssl:libcrypto_shared", + "os_account:os_account_innerkits", + "runtime_core:ani", + "runtime_core:libarkruntime", ] defines = [ "private=public" ] - - use_exceptions = true } ohos_unittest("ani_file_fs_test") { @@ -77,8 +62,6 @@ ohos_unittest("ani_file_fs_test") { module_out_path = "file_api/file_api" - resource_config_file = "../resource/ohos_test.xml" - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", @@ -88,23 +71,42 @@ ohos_unittest("ani_file_fs_test") { "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_tasksignal", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties/copy_listener", ] sources = [ + "mod_fs/class_atomicfile/fs_atomicfile_test.cpp", "mod_fs/class_file/fs_file_test.cpp", + "mod_fs/class_randomaccessfile/fs_randomaccessfile_test.cpp", + "mod_fs/class_readeriterator/fs_reader_iterator_test.cpp", "mod_fs/class_stat/fs_stat_test.cpp", + "mod_fs/class_stream/fs_stream_test.cpp", + "mod_fs/class_tasksignal/fs_task_signal_test.cpp", "mod_fs/properties/access_core_test.cpp", "mod_fs/properties/close_core_test.cpp", + "mod_fs/properties/copy_core_test.cpp", + "mod_fs/properties/copy_dir_core_test.cpp", "mod_fs/properties/copy_file_core_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_test.cpp", + "mod_fs/properties/create_stream_core_test.cpp", + "mod_fs/properties/dup_core_test.cpp", + "mod_fs/properties/fdopen_stream_core_test.cpp", "mod_fs/properties/listfile_core_test.cpp", + "mod_fs/properties/lseek_core_test.cpp", + "mod_fs/properties/lstat_core_test.cpp", "mod_fs/properties/move_core_test.cpp", + "mod_fs/properties/movedir_core_test.cpp", "mod_fs/properties/open_core_test.cpp", "mod_fs/properties/read_core_test.cpp", + "mod_fs/properties/read_lines_core_test.cpp", "mod_fs/properties/read_text_core_test.cpp", "mod_fs/properties/rmdir_core_test.cpp", "mod_fs/properties/stat_core_test.cpp", + "mod_fs/properties/trans_listener_test.cpp", "mod_fs/properties/truncate_core_test.cpp", + "mod_fs/properties/utimes_core_test.cpp", "mod_fs/properties/write_core_test.cpp", + "mod_fs/properties/xattr_core_test.cpp", ] deps = [ @@ -120,6 +122,8 @@ ohos_unittest("ani_file_fs_test") { "ability_runtime:ability_manager", "app_file_service:fileuri_native", "c_utils:utils", + "dfs_service:distributed_file_daemon_kit_inner", + "dfs_service:libdistributedfileutils", "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", @@ -127,8 +131,98 @@ ohos_unittest("ani_file_fs_test") { ] defines = [ "private=public" ] +} - use_exceptions = true +ohos_unittest("ani_file_fs_mock_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_readeriterator", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_tasksignal", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", + "${file_api_path}/interfaces/kits/js/src/mod_fs/properties/copy_listener", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_stream/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/common", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/mock", + "${file_api_path}/interfaces/test/unittest/js/mod_fs/properties/mock", + ] + + sources = [ + "mod_fs/class_file/fs_file_mock_test.cpp", + "mod_fs/class_randomaccessfile/fs_randomaccessfile_mock_test.cpp", + "mod_fs/class_readeriterator/fs_reader_iterator_mock_test.cpp", + "mod_fs/class_stat/fs_stat_mock_test.cpp", + "mod_fs/class_stream/fs_stream_mock_test.cpp", + "mod_fs/class_stream/mock/c_mock.cpp", + "mod_fs/class_watcher/fs_file_watcher_mock_test.cpp", + "mod_fs/class_watcher/fs_watcher_mock_test.cpp", + "mod_fs/mock/eventfd_mock.cpp", + "mod_fs/mock/inotify_mock.cpp", + "mod_fs/mock/poll_mock.cpp", + "mod_fs/mock/unistd_mock.cpp", + "mod_fs/properties/access_core_mock_test.cpp", + "mod_fs/properties/copy_core_mock_test.cpp", + "mod_fs/properties/copy_file_core_mock_test.cpp", + "mod_fs/properties/create_randomaccessfile_core_mock_test.cpp", + "mod_fs/properties/dup_core_mock_test.cpp", + "mod_fs/properties/fdatasync_core_mock_test.cpp", + "mod_fs/properties/fsync_core_mock_test.cpp", + "mod_fs/properties/lstat_core_mock_test.cpp", + "mod_fs/properties/mkdir_core_mock_test.cpp", + "mod_fs/properties/mkdtemp_core_mock_test.cpp", + "mod_fs/properties/mock/system_mock.cpp", + "mod_fs/properties/mock/uv_fs_mock.cpp", + "mod_fs/properties/move_core_mock_test.cpp", + "mod_fs/properties/open_core_mock_test.cpp", + "mod_fs/properties/read_core_mock_test.cpp", + "mod_fs/properties/read_lines_core_mock_test.cpp", + "mod_fs/properties/rename_core_mock_test.cpp", + "mod_fs/properties/stat_core_mock_test.cpp", + "mod_fs/properties/symlink_core_mock_test.cpp", + "mod_fs/properties/trans_listener_mock_test.cpp", + "mod_fs/properties/unlink_core_mock_test.cpp", + "mod_fs/properties/utimes_core_mock_test.cpp", + "mod_fs/properties/watcher_core_mock_test.cpp", + "mod_fs/properties/xattr_core_mock_test.cpp", + ] + + deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_fs", + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/native:task_signal_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + + external_deps = [ + "ability_runtime:ability_manager", + "app_file_service:fileuri_native", + "c_utils:utils", + "dfs_service:distributed_file_daemon_kit_inner", + "dfs_service:libdistributedfileutils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "libuv:uv", + ] + + libs = [ "dl" ] + + defines = [ + "ENABLE_DISTRIBUTED_FILE_MOCK", + "private=public", + ] } ohos_unittest("ani_file_hash_test") { @@ -156,8 +250,6 @@ ohos_unittest("ani_file_hash_test") { "hilog:libhilog", "libuv:uv", ] - - use_exceptions = true } ohos_unittest("ani_file_securitylabel_test") { @@ -167,7 +259,10 @@ ohos_unittest("ani_file_securitylabel_test") { sources = [ "mod_securitylabel/securitylabel_core_test.cpp" ] - include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_securitylabel" ] + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_securitylabel", + ] deps = [ "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", @@ -180,9 +275,35 @@ ohos_unittest("ani_file_securitylabel_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "libuv:uv", ] +} - use_exceptions = true +ohos_unittest("ani_file_statvfs_test") { + module_out_path = "file_api/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ "mod_statvfs/statvfs_core_test.cpp" ] + + include_dirs = [ + "mock/libuv", + "${file_api_path}/interfaces/kits/js/src/mod_statvfs", + ] + + deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_statvfs", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "libuv:uv", + ] } ohos_unittest("napi_file_fs_test") { @@ -215,4 +336,4 @@ ohos_unittest("napi_file_fs_test") { defines = [ "private=public" ] use_exceptions = true -} \ No newline at end of file +} diff --git a/interfaces/test/unittest/task_signal/BUILD.gn b/interfaces/test/unittest/task_signal/BUILD.gn index a475b0fff..a24cb369d 100644 --- a/interfaces/test/unittest/task_signal/BUILD.gn +++ b/interfaces/test/unittest/task_signal/BUILD.gn @@ -33,8 +33,15 @@ ohos_unittest("task_signal_test") { external_deps = [ "c_utils:utils", + "dfs_service:distributed_file_daemon_kit_inner", + "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_napi", ] + + defines = [ + "ENABLE_DISTRIBUTED_FILE_MOCK", + "private=public", + ] } -- Gitee From 5ae81cf0eec201f2ddf67a0fef12075d0459d512 Mon Sep 17 00:00:00 2001 From: zhouxin Date: Mon, 28 Jul 2025 15:37:01 +0800 Subject: [PATCH 8/9] =?UTF-8?q?build.gn=E6=96=87=E4=BB=B6=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhouxin Change-Id: I83b5c5e205e582fb7793c807ba4f5a4a3a140796 --- interfaces/kits/js/BUILD.gn | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index b6152a479..2e90a0c4f 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -988,6 +988,7 @@ ohos_shared_library("ani_file_environment") { "src/mod_environment/ani", "src/mod_environment", ] + sources = [ "src/common/ani_helper/ani_signature.cpp", "src/common/ani_helper/error_handler.cpp", @@ -1005,6 +1006,7 @@ ohos_shared_library("ani_file_environment") { "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] + external_deps = [ "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", -- Gitee From 935483d76b5523834cf8c9b438b8283af2fc4104 Mon Sep 17 00:00:00 2001 From: zhouxin Date: Tue, 29 Jul 2025 21:58:01 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhouxin Change-Id: I32a073e780e34079ee11bcf7a1121cb30068484e Signed-off-by: tianp --- interfaces/kits/js/BUILD.gn | 11 +- .../js/src/mod_fs/ani/bind_function_class.cpp | 4 - .../src/mod_fs/properties/read_lines_core.cpp | 106 ++++++++++++++++++ .../properties/trans_listener_mock_test.cpp | 16 +++ 4 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 2e90a0c4f..9ed76cd29 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -725,13 +725,11 @@ ohos_shared_library("ani_file_fs") { "src/mod_fs/properties/access_core.cpp", "src/mod_fs/properties/ani/access_ani.cpp", "src/mod_fs/properties/ani/close_ani.cpp", - "src/mod_fs/properties/ani/connectdfs_ani.cpp", "src/mod_fs/properties/ani/copy_ani.cpp", "src/mod_fs/properties/ani/copy_dir_ani.cpp", "src/mod_fs/properties/ani/copy_file_ani.cpp", "src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp", "src/mod_fs/properties/ani/create_stream_ani.cpp", - "src/mod_fs/properties/ani/disconnectdfs_ani.cpp", "src/mod_fs/properties/ani/dup_ani.cpp", "src/mod_fs/properties/ani/fdatasync_ani.cpp", "src/mod_fs/properties/ani/fdopen_stream_ani.cpp", @@ -756,7 +754,6 @@ ohos_shared_library("ani_file_fs") { "src/mod_fs/properties/ani/write_ani.cpp", "src/mod_fs/properties/ani/xattr_ani.cpp", "src/mod_fs/properties/close_core.cpp", - "src/mod_fs/properties/connectdfs_core.cpp", "src/mod_fs/properties/copy_core.cpp", "src/mod_fs/properties/copy_dir_core.cpp", "src/mod_fs/properties/copy_file_core.cpp", @@ -765,7 +762,6 @@ ohos_shared_library("ani_file_fs") { "src/mod_fs/properties/create_randomaccessfile_core.cpp", "src/mod_fs/properties/create_stream_core.cpp", "src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.cpp", - "src/mod_fs/properties/disconnectdfs_core.cpp", "src/mod_fs/properties/dup_core.cpp", "src/mod_fs/properties/fdatasync_core.cpp", "src/mod_fs/properties/fdopen_stream_core.cpp", @@ -881,8 +877,10 @@ ohos_shared_library("ani_file_hash") { "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ + "eventhandler:libeventhandler", "bounds_checking_function:libsec_shared", "hilog:libhilog", + "ipc:ipc_core", "libuv:uv", "openssl:libcrypto_shared", "runtime_core:ani", @@ -945,7 +943,9 @@ ohos_shared_library("ani_file_securitylabel") { ] external_deps = [ "bounds_checking_function:libsec_shared", + "eventhandler:libeventhandler", "hilog:libhilog", + "ipc:ipc_core", "libuv:uv", "runtime_core:ani", "runtime_core:ani_helpers", @@ -1011,6 +1011,7 @@ ohos_shared_library("ani_file_environment") { "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "bounds_checking_function:libsec_shared", + "eventhandler:libeventhandler", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_core", @@ -1077,7 +1078,9 @@ ohos_shared_library("ani_file_statvfs") { ] external_deps = [ "bounds_checking_function:libsec_shared", + "eventhandler:libeventhandler", "hilog:libhilog", + "ipc:ipc_core", "libuv:uv", "runtime_core:ani", "runtime_core:ani_helpers", diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index f9ebd1051..d1b34627f 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -21,13 +21,11 @@ #include "atomicfile_ani.h" #include "bind_function.h" #include "close_ani.h" -#include "connectdfs_ani.h" #include "copy_ani.h" #include "copy_dir_ani.h" #include "copy_file_ani.h" #include "create_randomaccessfile_ani.h" #include "create_stream_ani.h" -#include "disconnectdfs_ani.h" #include "dup_ani.h" #include "fdatasync_ani.h" #include "fdopen_stream_ani.h" @@ -186,7 +184,6 @@ static ani_status BindStaticMethods(ani_env *env) std::array methods = { ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, - ani_native_function { "connectDfs", nullptr, reinterpret_cast(ConnectDfsAni::ConnectDfsSync) }, ani_native_function { "copyDirSync", nullptr, reinterpret_cast(CopyDirAni::CopyDirSync) }, ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "copySync", nullptr, reinterpret_cast(CopyAni::CopySync) }, @@ -195,7 +192,6 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "createStreamSync", nullptr, reinterpret_cast(CreateStreamAni::CreateStreamSync) }, ani_native_function { "createWatcherSync", nullptr, reinterpret_cast(WatcherAni::CreateWatcherSync) }, - ani_native_function { "disConnectDfs", nullptr, reinterpret_cast(DisConnectDfsAni::DisConnectDfsSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, ani_native_function { "dup", nullptr, reinterpret_cast(DupAni::Dup) }, ani_native_function { "fdatasyncSync", nullptr, reinterpret_cast(FDataSyncAni::FDataSyncSync) }, diff --git a/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp new file mode 100644 index 000000000..1f9869151 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/read_lines_core.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "read_lines_core.h" + +#include + +#include "file_utils.h" +#include "filemgmt_libhilog.h" +#include "rust_file.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static int CheckOptionArg(Options option) +{ + auto encoding = option.encoding; + if (encoding != "utf-8") { + return EINVAL; + } + + return ERRNO_NOERR; +} + +static int GetFileSize(const string &path, int64_t &offset) +{ + std::unique_ptr readLinesReq = { new uv_fs_t, FsUtils::FsReqCleanup }; + if (!readLinesReq) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + + int ret = uv_fs_stat(nullptr, readLinesReq.get(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to get file stat by path"); + return ret; + } + + offset = static_cast(readLinesReq->statbuf.st_size); + return ERRNO_NOERR; +} + +static FsResult InstantiateReaderIterator(void *iterator, int64_t offset) +{ + if (iterator == nullptr) { + HILOGE("Invalid argument iterator"); + return FsResult::Error(EINVAL); + } + + auto readeriterator = FsReaderIterator::Constructor(); + if (!readeriterator.IsSuccess()) { + HILOGE("Failed to instantiate class ReaderIterator"); + return FsResult::Error(UNKNOWN_ERR); + } + + auto readerIteratorEntity = readeriterator.GetData().value()->GetReaderIteratorEntity(); + if (!readerIteratorEntity) { + HILOGE("Failed to get readerIteratorEntity"); + return FsResult::Error(UNKNOWN_ERR); + } + readerIteratorEntity->iterator = iterator; + readerIteratorEntity->offset = offset; + return FsResult::Success(readeriterator.GetData().value()); +} + +FsResult ReadLinesCore::DoReadLines(const string &path, optional option) +{ + if (option.has_value()) { + int ret = CheckOptionArg(option.value()); + if (ret) { + HILOGE("Invalid option.encoding parameter"); + return FsResult::Error(ret); + } + } + + auto iterator = ::ReaderIterator(path.c_str()); + if (iterator == nullptr) { + HILOGE("Failed to read lines of the file, error: %{public}d", errno); + return FsResult::Error(errno); + } + + int64_t offset = 0; + int ret = GetFileSize(path, offset); + if (ret != 0) { + HILOGE("Failed to get size of the file"); + return FsResult::Error(ret); + } + return InstantiateReaderIterator(iterator, offset); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp index 6b5ddccee..1ae60cef2 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/trans_listener_mock_test.cpp @@ -85,6 +85,22 @@ public: { return 0; } + int32_t CancelCopyTask(const std::string &srcUri, const std::string &dstUri) override + { + return 0; + } + int32_t GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus) override + { + return 0; + } + int32_t UpdateDfsSwitchStatus(int32_t switchStatus) override + { + return 0; + } + int32_t GetConnectedDeviceList(std::vector &deviceList) override + { + return 0; + } MockDistributedFileDaemonManager() = default; ~MockDistributedFileDaemonManager() = default; -- Gitee