diff --git a/app/src/filesystem.h b/app/src/filesystem.h index bf55f3fa8b..0e22929509 100644 --- a/app/src/filesystem.h +++ b/app/src/filesystem.h @@ -19,6 +19,8 @@ #include +#include "app/src/include/firebase/internal/platform.h" + namespace firebase { /** @@ -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 diff --git a/app/src/filesystem_apple.mm b/app/src/filesystem_apple.mm index 5e1727aff6..72e83e3492 100644 --- a/app/src/filesystem_apple.mm +++ b/app/src/filesystem_apple.mm @@ -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"; diff --git a/app/src/filesystem_desktop_linux.cc b/app/src/filesystem_desktop_linux.cc index b6f268c55c..67c3b67f91 100644 --- a/app/src/filesystem_desktop_linux.cc +++ b/app/src/filesystem_desktop_linux.cc @@ -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"; diff --git a/app/src/filesystem_desktop_windows.cc b/app/src/filesystem_desktop_windows.cc index ceca19d6b7..a56dd48e1b 100644 --- a/app/src/filesystem_desktop_windows.cc +++ b/app/src/filesystem_desktop_windows.cc @@ -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"; @@ -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); } } diff --git a/app/src/heartbeat/heartbeat_storage_desktop.cc b/app/src/heartbeat/heartbeat_storage_desktop.cc index a9c54fb728..f8850228ae 100644 --- a/app/src/heartbeat/heartbeat_storage_desktop.cc +++ b/app/src/heartbeat/heartbeat_storage_desktop.cc @@ -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 @@ -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(); } diff --git a/app/src/heartbeat/heartbeat_storage_desktop.h b/app/src/heartbeat/heartbeat_storage_desktop.h index 8796a4455a..9f7d7c2760 100644 --- a/app/src/heartbeat/heartbeat_storage_desktop.h +++ b/app/src/heartbeat/heartbeat_storage_desktop.h @@ -23,6 +23,7 @@ #include #include "app/logged_heartbeats_generated.h" +#include "app/src/filesystem.h" #include "app/src/logger.h" namespace firebase { @@ -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( @@ -61,7 +62,7 @@ class HeartbeatStorageDesktop { const LoggedHeartbeats& heartbeats_struct) const; // local variables for state - std::string filename_; + PathString filename_; const Logger& logger_; };