Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fork the path handling code to use wide strings on Windows. #1221

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions app/src/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <string>

#include "app/src/include/firebase/internal/platform.h"

namespace firebase {

/**
Expand Down Expand Up @@ -51,8 +53,25 @@ namespace firebase {
//
// TODO(b/171738655): use a separate function instead of the `should_create`
// flag. Use `StatusOr` for returning errors.
std::string AppDataDir(const char* app_name, bool should_create = true,
std::string* out_error = nullptr);

#if FIREBASE_PLATFORM_WINDOWS
typedef std::wstring PathString;
#define PathStringLiteral(x) PathString(L##x)
#define kPathStringEmpty PathString(L"")
#define kPathStringSep PathString(L"\\")
#define PathStringChar wchar_t
#define PathStringLiteralPrefix L
#else
typedef std::string PathString;
#define PathStringLiteral(x) PathString(x)
#define kPathStringEmpty PathString("")
#define kPathStringSep PathString("/")
#define PathStringChar char
#define PathStringLiteralPrefix
#endif // FIREBASE_PLATFORM_WINDOWS

PathString AppDataDir(const char* app_name, bool should_create = true,
std::string* out_error = nullptr);

} // namespace firebase

Expand Down
2 changes: 1 addition & 1 deletion app/src/filesystem_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool Mkdir(const std::string& path, std::string* out_error) {

} // namespace

std::string AppDataDir(const char* app_name, bool should_create, std::string* out_error) {
PathString AppDataDir(const char* app_name, bool should_create, std::string* out_error) {
if (!app_name || std::strlen(app_name) == 0) {
if (out_error) {
*out_error = "AppDataDir failed: no app_name provided";
Expand Down
4 changes: 2 additions & 2 deletions app/src/filesystem_desktop_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ bool PathExists(const std::string& path) {

} // namespace

std::string AppDataDir(const char* app_name, bool should_create,
std::string* out_error) {
PathString AppDataDir(const char* app_name, bool should_create,
std::string* out_error) {
if (!app_name || strlen(app_name) == 0) {
if (out_error) {
*out_error = "AppDataDir failed: no app_name provided";
Expand Down
8 changes: 4 additions & 4 deletions app/src/filesystem_desktop_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ bool Mkdir(const std::wstring& path, std::string* out_error) {

} // namespace

std::string AppDataDir(const char* app_name, bool should_create,
std::string* out_error) {
PathString AppDataDir(const char* app_name, bool should_create,
std::string* out_error) {
if (!app_name || std::strlen(app_name) == 0) {
if (out_error) {
*out_error = "AppDataDir failed: no app_name provided";
Expand Down Expand Up @@ -210,14 +210,14 @@ std::string AppDataDir(const char* app_name, bool should_create,
if (!created) return "";
}

return NativeToUtf8(current_path, out_error);
return PathString(current_path);

} else {
auto app_name_utf16 = Utf8ToNative(app_name, out_error);
if (app_name_utf16.empty()) {
return "";
}
return NativeToUtf8(base_dir + L"/" + app_name_utf16, out_error);
return PathString(base_dir + L"/" + app_name_utf16);
}
}

Expand Down
23 changes: 14 additions & 9 deletions app/src/heartbeat/heartbeat_storage_desktop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,30 @@ using com::google::firebase::cpp::heartbeat::VerifyLoggedHeartbeatsBuffer;
namespace {

const char kHeartbeatDir[] = "firebase-heartbeat";
#if FIREBASE_PLATFORM_WINDOWS
const wchar_t kHeartbeatFilenamePrefix[] = L"heartbeats-";
#else
const char kHeartbeatFilenamePrefix[] = "heartbeats-";
#endif

std::string CreateFilename(const std::string& app_id, const Logger& logger) {
PathString CreateFilename(const std::string& app_id, const Logger& logger) {
std::string error;
std::string app_dir =
PathString app_dir =
AppDataDir(kHeartbeatDir, /*should_create=*/true, &error);
if (!error.empty()) {
logger.LogError(error.c_str());
return "";
return kPathStringEmpty;
}
if (app_dir.empty()) {
return "";
return kPathStringEmpty;
}

// Remove any symbols from app_id that might not be allowed in filenames.
auto app_id_without_symbols =
std::regex_replace(app_id, std::regex("[/\\\\?%*:|\"<>.,;=]"), "");
// Note: fstream will convert / to \ if needed on windows.
return app_dir + "/" + kHeartbeatFilenamePrefix + app_id_without_symbols;
auto app_id_without_symbols = std::regex_replace(
app_id, std::regex(PathStringLiteral("[/\\\\?%*:|\"<>.,;=]")),
kPathStringEmpty);
return app_dir + kPathStringSep + kHeartbeatFilenamePrefix +
app_id_without_symbols;
}

} // namespace
Expand Down Expand Up @@ -127,7 +132,7 @@ bool HeartbeatStorageDesktop::Write(const LoggedHeartbeats& heartbeats) const {
return !file.fail();
}

const char* HeartbeatStorageDesktop::GetFilename() const {
const PathStringChar* HeartbeatStorageDesktop::GetFilename() const {
return filename_.c_str();
}

Expand Down
5 changes: 3 additions & 2 deletions app/src/heartbeat/heartbeat_storage_desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <vector>

#include "app/logged_heartbeats_generated.h"
#include "app/src/filesystem.h"
#include "app/src/logger.h"

namespace firebase {
Expand Down Expand Up @@ -52,7 +53,7 @@ class HeartbeatStorageDesktop {
// write operation fails.
bool Write(const LoggedHeartbeats& heartbeats) const;

const char* GetFilename() const;
const PathStringChar* GetFilename() const;

private:
LoggedHeartbeats LoggedHeartbeatsFromFlatbuffer(
Expand All @@ -61,7 +62,7 @@ class HeartbeatStorageDesktop {
const LoggedHeartbeats& heartbeats_struct) const;

// local variables for state
std::string filename_;
PathString filename_;
const Logger& logger_;
};

Expand Down