Fix folder transfer.

PiperOrigin-RevId: 698044680
This commit is contained in:
Francis Tsui
2024-11-19 09:26:49 -08:00
committed by Copybara-Service
parent ecb75d2986
commit 7a02d2f888
3 changed files with 47 additions and 12 deletions
@@ -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",
@@ -28,6 +28,7 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <filesystem> // NOLINT
#include <memory>
#include <sstream>
#include <string>
@@ -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<OutputFile> ImplementationPlatform::CreateOutputFile(
std::unique_ptr<OutputFile> 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);
}
@@ -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 <memory>
#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<OutputFile> 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