diff --git a/rtc_base/fileutils.h b/rtc_base/fileutils.h index 69e64ae35e..a2544947f3 100644 --- a/rtc_base/fileutils.h +++ b/rtc_base/fileutils.h @@ -94,6 +94,9 @@ class FilesystemInterface { // Returns true if pathname refers to a file virtual bool IsFile(const Pathname& pathname) = 0; + virtual std::string TempFilename(const Pathname &dir, + const std::string &prefix) = 0; + // Determines the size of the file indicated by path. virtual bool GetFileSize(const Pathname& path, size_t* size) = 0; }; @@ -132,6 +135,11 @@ class Filesystem { return EnsureDefaultFilesystem()->IsFile(pathname); } + static std::string TempFilename(const Pathname &dir, + const std::string &prefix) { + return EnsureDefaultFilesystem()->TempFilename(dir, prefix); + } + static bool GetFileSize(const Pathname& path, size_t* size) { return EnsureDefaultFilesystem()->GetFileSize(path, size); } diff --git a/rtc_base/unixfilesystem.cc b/rtc_base/unixfilesystem.cc index a48aca1a33..8732d477a3 100644 --- a/rtc_base/unixfilesystem.cc +++ b/rtc_base/unixfilesystem.cc @@ -67,6 +67,22 @@ bool UnixFilesystem::DeleteFile(const Pathname &filename) { return ::unlink(filename.pathname().c_str()) == 0; } +std::string UnixFilesystem::TempFilename(const Pathname &dir, + const std::string &prefix) { + int len = dir.pathname().size() + prefix.size() + 2 + 6; + char *tempname = new char[len]; + + snprintf(tempname, len, "%s/%sXXXXXX", dir.pathname().c_str(), + prefix.c_str()); + int fd = ::mkstemp(tempname); + if (fd != -1) + ::close(fd); + std::string ret(tempname); + delete[] tempname; + + return ret; +} + bool UnixFilesystem::MoveFile(const Pathname &old_path, const Pathname &new_path) { if (!IsFile(old_path)) { @@ -103,6 +119,18 @@ bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) { return true; } +char* UnixFilesystem::CopyString(const std::string& str) { + size_t size = str.length() + 1; + + char* buf = new char[size]; + if (!buf) { + return nullptr; + } + + strcpyn(buf, size, str.c_str()); + return buf; +} + } // namespace rtc #if defined(__native_client__) diff --git a/rtc_base/unixfilesystem.h b/rtc_base/unixfilesystem.h index 711d7b3ea1..27966f3aec 100644 --- a/rtc_base/unixfilesystem.h +++ b/rtc_base/unixfilesystem.h @@ -37,7 +37,20 @@ class UnixFilesystem : public FilesystemInterface { // Returns true of pathname represents an existing file bool IsFile(const Pathname& pathname) override; + std::string TempFilename(const Pathname& dir, + const std::string& prefix) override; + bool GetFileSize(const Pathname& path, size_t* size) override; + + private: +#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC) + static char* provided_app_data_folder_; + static char* provided_app_temp_folder_; +#else + static char* app_temp_path_; +#endif + + static char* CopyString(const std::string& str); }; } // namespace rtc diff --git a/rtc_base/win32filesystem.cc b/rtc_base/win32filesystem.cc index 8ca84c34bb..511f9664e7 100644 --- a/rtc_base/win32filesystem.cc +++ b/rtc_base/win32filesystem.cc @@ -42,6 +42,16 @@ bool Win32Filesystem::DeleteFile(const Pathname &filename) { return ::DeleteFile(ToUtf16(filename.pathname()).c_str()) != 0; } +std::string Win32Filesystem::TempFilename(const Pathname &dir, + const std::string &prefix) { + wchar_t filename[MAX_PATH]; + if (::GetTempFileName(ToUtf16(dir.pathname()).c_str(), + ToUtf16(prefix).c_str(), 0, filename) != 0) + return ToUtf8(filename); + RTC_NOTREACHED(); + return ""; +} + bool Win32Filesystem::MoveFile(const Pathname &old_path, const Pathname &new_path) { if (!IsFile(old_path)) { diff --git a/rtc_base/win32filesystem.h b/rtc_base/win32filesystem.h index d26741e67b..e65d127cb2 100644 --- a/rtc_base/win32filesystem.h +++ b/rtc_base/win32filesystem.h @@ -33,6 +33,14 @@ class Win32Filesystem : public FilesystemInterface { // Returns true if a file exists at path bool IsFile(const Pathname& path) override; + // All of the following functions set pathname and return true if successful. + // Returned paths always include a trailing backslash. + // If create is true, the path will be recursively created. + // If append is non-null, it will be appended (and possibly created). + + std::string TempFilename(const Pathname& dir, + const std::string& prefix) override; + bool GetFileSize(const Pathname& path, size_t* size) override; };