From f5f01d425b6d5cc8d442cac936faef9593227cf0 Mon Sep 17 00:00:00 2001 From: George Wright Date: Thu, 20 Feb 2020 15:05:23 -0800 Subject: [PATCH] Implement WriteAtomically using write/fsync on all platforms, and enable file unittests on Fuchsia --- fml/platform/posix/file_posix.cc | 22 +++++++++++++++------- testing/fuchsia/meta/fuchsia_test.cmx | 3 ++- testing/fuchsia/run_tests.sh | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/fml/platform/posix/file_posix.cc b/fml/platform/posix/file_posix.cc index ab64b5cc2de71..f6a90942a9391 100644 --- a/fml/platform/posix/file_posix.cc +++ b/fml/platform/posix/file_posix.cc @@ -206,15 +206,23 @@ bool WriteAtomically(const fml::UniqueFD& base_directory, return false; } - FileMapping mapping(temp_file, {FileMapping::Protection::kWrite}); - if (mapping.GetMutableMapping() == nullptr || - data.GetSize() != mapping.GetSize()) { - return false; - } + ssize_t remaining = data.GetSize(); + ssize_t written = 0; + ssize_t offset = 0; + + while (remaining > 0) { + written = FML_HANDLE_EINTR( + ::write(temp_file.get(), data.GetMapping() + offset, remaining)); - ::memcpy(mapping.GetMutableMapping(), data.GetMapping(), data.GetSize()); + if (written == -1) { + return false; + } + + remaining -= written; + offset += written; + } - if (::msync(mapping.GetMutableMapping(), data.GetSize(), MS_SYNC) != 0) { + if (::fsync(temp_file.get()) != 0) { return false; } diff --git a/testing/fuchsia/meta/fuchsia_test.cmx b/testing/fuchsia/meta/fuchsia_test.cmx index 02ac9472b0930..9a01e290f25e4 100644 --- a/testing/fuchsia/meta/fuchsia_test.cmx +++ b/testing/fuchsia/meta/fuchsia_test.cmx @@ -6,7 +6,8 @@ "features": [ "vulkan", "deprecated-ambient-replace-as-executable", - "isolated-cache-storage" + "isolated-cache-storage", + "isolated-temp" ], "services": [ "fuchsia.accessibility.semantics.SemanticsManager", diff --git a/testing/fuchsia/run_tests.sh b/testing/fuchsia/run_tests.sh index 2c39beb0565bd..44c6057da3e8a 100755 --- a/testing/fuchsia/run_tests.sh +++ b/testing/fuchsia/run_tests.sh @@ -100,7 +100,7 @@ echo "$(date) START:fml_tests ---------------------------------------" ./fuchsia_ctl -d $device_name test \ -f fml_tests-0.far \ -t fml_tests \ - -a "--gtest_filter=-FileTest*" \ + -a "--gtest_filter=-FileTest.CanTruncateAndWrite:FileTest.CreateDirectoryStructure" \ --identity-file $pkey \ --timeout-seconds $test_timeout_seconds \ --packages-directory packages