Revert "Delete Filesystem::TempFilename."

This reverts commit 6de95f06d0512049553f8120547ab78675e3a76a.

Reason for revert: Broke internal projects.
Original change's description:
> Delete Filesystem::TempFilename.
> 
> Also delete a few unused private members of UnixFilesystem.
> 
> Bug: webrtc:6424
> Change-Id: Ib52f2d877690159d197fe767fd04a0d1ade7eb1a
> Reviewed-on: https://webrtc-review.googlesource.com/30301
> Commit-Queue: Taylor Brandstetter <deadbeef@webrtc.org>
> Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#21148}

TBR=deadbeef@webrtc.org,nisse@webrtc.org

Change-Id: I02874f80dbfcafc26dadbb5897e510613801713d
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: webrtc:6424
Reviewed-on: https://webrtc-review.googlesource.com/31160
Reviewed-by: Zhi Huang <zhihuang@webrtc.org>
Commit-Queue: Zhi Huang <zhihuang@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21152}
This commit is contained in:
Zhi Huang 2017-12-08 00:08:23 +00:00 committed by Commit Bot
parent 606a5971e3
commit 5329214838
5 changed files with 67 additions and 0 deletions

View File

@ -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);
}

View File

@ -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__)

View File

@ -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

View File

@ -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)) {

View File

@ -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;
};