diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index 88ba80bc..ebb73474 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -301,6 +301,7 @@ cc_test( "executor_test.cc", "file_path_test.cc", "http_loader_test.cc", + "platform_test.cc", "preferences_manager_test.cc", "preferences_repository_test.cc", "scheduled_executor_test.cc", diff --git a/internal/platform/implementation/windows/platform.cc b/internal/platform/implementation/windows/platform.cc index 6cb9474f..78080d46 100644 --- a/internal/platform/implementation/windows/platform.cc +++ b/internal/platform/implementation/windows/platform.cc @@ -28,6 +28,7 @@ #include #include #include +#include // NOLINT #include #include #include @@ -35,6 +36,7 @@ #include "absl/base/attributes.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" +#include "internal/base/files.h" #include "internal/platform/implementation/atomic_boolean.h" #include "internal/platform/implementation/atomic_reference.h" #include "internal/platform/implementation/ble.h" @@ -76,6 +78,7 @@ #include "internal/platform/implementation/windows/wifi.h" #include "internal/platform/implementation/windows/wifi_hotspot.h" #include "internal/platform/implementation/windows/wifi_lan.h" +#include "internal/platform/logging.h" #include "internal/platform/os_name.h" #include "internal/platform/payload_id.h" @@ -228,20 +231,17 @@ std::unique_ptr ImplementationPlatform::CreateOutputFile( std::unique_ptr ImplementationPlatform::CreateOutputFile( const std::string& file_path) { - std::string path(file_path); - - auto folder_path = windows::string_utils::StringToWideString( - path.substr(0, path.find_last_of('/'))); + std::filesystem::path path = std::filesystem::u8path(file_path); + std::filesystem::path folder_path = path.parent_path(); // Verifies that a path is a valid directory. - // https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-pathisdirectoryw - if (!PathIsDirectoryW(folder_path.data())) { - // This function creates a file system folder whose fully qualified path is - // given by pszPath. If one or more of the intermediate folders do not - // exist, they are created as well. - // https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shcreatedirectoryexw - int result = SHCreateDirectoryExW(nullptr, folder_path.data(), nullptr); + if (!sharing::DirectoryExists(folder_path)) { + if (!sharing::CreateDirectories(folder_path)) { + LOG(ERROR) << "Failed to create directory: " + << windows::string_utils::WideStringToString( + folder_path.wstring()); + return nullptr; + } } - return windows::IOFile::CreateOutputFile(file_path); } diff --git a/internal/platform/implementation/windows/platform_test.cc b/internal/platform/implementation/windows/platform_test.cc new file mode 100644 index 00000000..728f2e67 --- /dev/null +++ b/internal/platform/implementation/windows/platform_test.cc @@ -0,0 +1,34 @@ +// Copyright 2024 Google LLC +// +// 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 +// +// https://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 "internal/platform/implementation/platform.h" + +#include + +#include "gtest/gtest.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/implementation/output_file.h" + +namespace nearby::api { +namespace { + +TEST(PlatformTest, CreateOutputFileWithUnixPathSeparator) { + std::unique_ptr output_file = + ImplementationPlatform::CreateOutputFile("C:\\tmp\\path1/path2\\x.txt"); + EXPECT_NE(output_file, nullptr); + EXPECT_TRUE(output_file->Write(ByteArray("test")).Ok()); +} + +} // namespace +} // namespace nearby::api