diff --git a/cpp/platform/impl/shared/file.cc b/cpp/platform/impl/shared/file.cc index 9c8571d5..cadbadb6 100644 --- a/cpp/platform/impl/shared/file.cc +++ b/cpp/platform/impl/shared/file.cc @@ -27,7 +27,7 @@ namespace shared { // InputFile InputFile::InputFile(const std::string& path, std::int64_t size) - : file_(path), path_(path), total_size_(size) {} + : file_(path, std::ios::binary), path_(path), total_size_(size) {} ExceptionOr InputFile::Read(std::int64_t size) { if (!file_.is_open()) { @@ -62,7 +62,8 @@ Exception InputFile::Close() { // OutputFile -OutputFile::OutputFile(absl::string_view path) : file_(std::string(path)) {} +OutputFile::OutputFile(absl::string_view path) + : file_(std::string(path), std::ios::binary) {} Exception OutputFile::Write(const ByteArray& data) { if (!file_.is_open()) { diff --git a/cpp/platform/impl/windows/BUILD b/cpp/platform/impl/windows/BUILD index 603cb1ab..beade04c 100644 --- a/cpp/platform/impl/windows/BUILD +++ b/cpp/platform/impl/windows/BUILD @@ -150,8 +150,6 @@ cc_test( "count_down_latch_test.cc", "crypto_test.cc", "executor_test.cc", - "input_file_test.cc", - "output_file_test.cc", "scheduled_executor_test.cc", "submittable_executor_test.cc", ], diff --git a/cpp/platform/impl/windows/bluetooth_classic_socket.cc b/cpp/platform/impl/windows/bluetooth_classic_socket.cc index c146d473..89694b52 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_socket.cc +++ b/cpp/platform/impl/windows/bluetooth_classic_socket.cc @@ -105,7 +105,7 @@ ExceptionOr BluetoothSocket::BluetoothInputStream::Read( Buffer buffer = Buffer(size); - winrt_stream_.ReadAsync(buffer, size, InputStreamOptions::None).get(); + winrt_stream_.ReadAsync(buffer, size, InputStreamOptions::Partial).get(); DataReader dataReader = DataReader::FromBuffer(buffer); diff --git a/cpp/platform/impl/windows/input_file_test.cc b/cpp/platform/impl/windows/input_file_test.cc index af70914d..5d796cf8 100644 --- a/cpp/platform/impl/windows/input_file_test.cc +++ b/cpp/platform/impl/windows/input_file_test.cc @@ -94,7 +94,7 @@ TEST_F(InputFileTests, SuccessfulGetFilePath) { EXPECT_EQ(inputFile->Close(), location::nearby::Exception{location::nearby::Exception::kSuccess}); - EXPECT_EQ(fileName, TEST_PATH); + EXPECT_EQ(fileName, test_utils::GetPayloadPath(payloadId).c_str()); } TEST_F(InputFileTests, SuccessfulGetTotalSize) { diff --git a/cpp/platform/impl/windows/platform.cc b/cpp/platform/impl/windows/platform.cc index d2b08779..e882f12c 100644 --- a/cpp/platform/impl/windows/platform.cc +++ b/cpp/platform/impl/windows/platform.cc @@ -14,6 +14,9 @@ #include "platform/api/platform.h" +#include + +#include "platform/impl/shared/count_down_latch.h" #include "platform/impl/shared/file.h" #include "platform/impl/windows/atomic_boolean.h" #include "platform/impl/windows/atomic_reference.h" @@ -22,7 +25,6 @@ #include "platform/impl/windows/bluetooth_classic_medium.h" #include "platform/impl/windows/cancelable.h" #include "platform/impl/windows/condition_variable.h" -#include "platform/impl/shared/count_down_latch.h" #include "platform/impl/windows/executor.h" #include "platform/impl/windows/future.h" #include "platform/impl/windows/listenable_future.h" @@ -41,7 +43,28 @@ namespace api { namespace { std::string GetPayloadPath(PayloadId payload_id) { - return absl::StrCat("/tmp/", payload_id); + PWSTR basePath; + + // Retrieves the full path of a known folder identified by the folder's + // KNOWNFOLDERID. + // https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath + SHGetKnownFolderPath( + FOLDERID_Downloads, // rfid: A reference to the KNOWNFOLDERID that + // identifies the folder. + 0, // dwFlags: Flags that specify special retrieval options. + NULL, // hToken: An access token that represents a particular user. + &basePath); // ppszPath: When this method returns, contains the address + // of a pointer to a null-terminated Unicode string that + // specifies the path of the known folder. The calling + // process is responsible for freeing this resource once it + // is no longer needed by calling CoTaskMemFree, whether + // SHGetKnownFolderPath succeeds or not. + + char* fullpathUTF8 = new char((wcslen(basePath) + 1) * sizeof(char)); + wcstombs(fullpathUTF8, basePath, (wcslen(basePath) + 1) * sizeof(char)); + std::string fullPath = std::string(fullpathUTF8); + auto retval = absl::StrCat(fullPath += "/", payload_id); + return retval; } } // namespace @@ -71,14 +94,13 @@ ImplementationPlatform::CreateConditionVariable(Mutex* mutex) { std::unique_ptr ImplementationPlatform::CreateInputFile( PayloadId payload_id, std::int64_t total_size) { - return absl::make_unique( - GetPayloadPath(payload_id), total_size); + return absl::make_unique(GetPayloadPath(payload_id), + total_size); } std::unique_ptr ImplementationPlatform::CreateOutputFile( PayloadId payload_id) { - return absl::make_unique( - GetPayloadPath(payload_id)); + return absl::make_unique(GetPayloadPath(payload_id)); } // TODO(b/184975123): replace with real implementation. @@ -111,8 +133,7 @@ ImplementationPlatform::CreateBluetoothAdapter() { std::unique_ptr ImplementationPlatform::CreateBluetoothClassicMedium( nearby::api::BluetoothAdapter& adapter) { - return absl::make_unique( - adapter); + return absl::make_unique(adapter); } // TODO(b/184975123): replace with real implementation. diff --git a/cpp/platform/impl/windows/test_utils.cc b/cpp/platform/impl/windows/test_utils.cc index 340b0883..96ad7d42 100644 --- a/cpp/platform/impl/windows/test_utils.cc +++ b/cpp/platform/impl/windows/test_utils.cc @@ -14,6 +14,8 @@ #include "platform/impl/windows/test_utils.h" +#include + #include "absl/strings/str_cat.h" namespace test_utils { @@ -29,8 +31,27 @@ std::wstring StringToWideString(const std::string& s) { } std::string GetPayloadPath(location::nearby::PayloadId payload_id) { - auto returnString = absl::StrCat("/tmp/", payload_id); + PWSTR basePath; - return returnString; + // Retrieves the full path of a known folder identified by the folder's + // KNOWNFOLDERID. + // https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath + SHGetKnownFolderPath( + FOLDERID_Downloads, // rfid: A reference to the KNOWNFOLDERID that + // identifies the folder. + 0, // dwFlags: Flags that specify special retrieval options. + NULL, // hToken: An access token that represents a particular user. + &basePath); // ppszPath: When this method returns, contains the address + // of a pointer to a null-terminated Unicode string that + // specifies the path of the known folder. The calling + // process is responsible for freeing this resource once it + // is no longer needed by calling CoTaskMemFree, whether + // SHGetKnownFolderPath succeeds or not. + + char* fullpathUTF8 = new char((wcslen(basePath) + 1) * sizeof(char)); + wcstombs(fullpathUTF8, basePath, (wcslen(basePath) + 1) * sizeof(char)); + std::string fullPath = std::string(fullpathUTF8); + auto retval = absl::StrCat(fullPath += "/", payload_id); + return retval; } } // namespace test_utils diff --git a/cpp/platform/impl/windows/test_utils.h b/cpp/platform/impl/windows/test_utils.h index 7c6a5f2e..d8b8c32f 100644 --- a/cpp/platform/impl/windows/test_utils.h +++ b/cpp/platform/impl/windows/test_utils.h @@ -24,7 +24,6 @@ #include "platform/base/payload_id.h" #define TEST_BUFFER_SIZE 256 -#define TEST_PATH "/tmp/64" #define TEST_PAYLOAD_ID 64l #define TEST_STRING \ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas " \