diff --git a/webrtc/base/file.cc b/webrtc/base/file.cc index 9a865884ea..c97504f9c7 100644 --- a/webrtc/base/file.cc +++ b/webrtc/base/file.cc @@ -18,6 +18,14 @@ File::~File() { Close(); } +File File::Open(const std::string& path) { + return File(OpenPlatformFile(path)); +} + +File File::Create(const std::string& path) { + return File(CreatePlatformFile(path)); +} + File::File(File&& other) : file_(other.file_) { other.file_ = kInvalidPlatformFileValue; } diff --git a/webrtc/base/file.h b/webrtc/base/file.h index 3401c28abe..6ca4825ed0 100644 --- a/webrtc/base/file.h +++ b/webrtc/base/file.h @@ -35,7 +35,10 @@ class File { File(File&& other); File& operator=(File&& other); + // Open and Create give files with both reading and writing enabled. static File Open(const std::string& path); + // If the file already exists it will be overwritten. + static File Create(const std::string& path); size_t Write(const uint8_t* data, size_t length); size_t Read(uint8_t* buffer, size_t length); diff --git a/webrtc/base/file_posix.cc b/webrtc/base/file_posix.cc index 2dde7938c2..9c166ec1a2 100644 --- a/webrtc/base/file_posix.cc +++ b/webrtc/base/file_posix.cc @@ -22,10 +22,6 @@ namespace rtc { -File File::Open(const std::string& path) { - return File(::open(path.c_str(), O_RDWR)); -} - size_t File::Write(const uint8_t* data, size_t length) { size_t total_written = 0; do { diff --git a/webrtc/base/file_unittest.cc b/webrtc/base/file_unittest.cc index 458b6da75f..edf2b29127 100644 --- a/webrtc/base/file_unittest.cc +++ b/webrtc/base/file_unittest.cc @@ -8,10 +8,6 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include -#include -#include - #include #include #include @@ -32,14 +28,6 @@ namespace rtc { -void RemoveFile(const std::string& path) { -#if defined(WEBRTC_WIN) - ::DeleteFile(ToUtf16(path).c_str()); -#else - ::unlink(path.c_str()); -#endif -} - int LastError() { #if defined(WEBRTC_WIN) return ::GetLastError(); @@ -69,7 +57,7 @@ class FileTest : public ::testing::Test { ASSERT_FALSE(path_.empty()); } rtc::File OpenTempFile() { return rtc::File::Open(path_); } - void TearDown() { RemoveFile(path_); } + void TearDown() { rtc::RemoveFile(path_); } }; TEST_F(FileTest, DoubleClose) { diff --git a/webrtc/base/file_win.cc b/webrtc/base/file_win.cc index 72a680c88f..71e6effde6 100644 --- a/webrtc/base/file_win.cc +++ b/webrtc/base/file_win.cc @@ -19,13 +19,6 @@ namespace rtc { -File File::Open(const std::string& path) { - HANDLE handle = - ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0, - nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); - return File(handle); -} - size_t File::Write(const uint8_t* data, size_t length) { RTC_DCHECK_LT(length, std::numeric_limits::max()); size_t total_written = 0; diff --git a/webrtc/base/platform_file.cc b/webrtc/base/platform_file.cc index d518b74a65..a41324fcf9 100644 --- a/webrtc/base/platform_file.cc +++ b/webrtc/base/platform_file.cc @@ -13,6 +13,9 @@ #if defined(WEBRTC_WIN) #include #else +#include +#include +#include #include #endif @@ -34,7 +37,23 @@ FILE* FdopenPlatformFileForWriting(PlatformFile file) { bool ClosePlatformFile(PlatformFile file) { return CloseHandle(file) != 0; } -#else + +bool RemoveFile(const std::string& path) { + return ::DeleteFile(ToUtf16(path).c_str()) != 0; +} + +PlatformFile OpenPlatformFile(const std::string& path) { + return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0, + nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); +} + +PlatformFile CreatePlatformFile(const std::string& path) { + return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0, + nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); +} + +#else // defined(WEBRTC_WIN) + const PlatformFile kInvalidPlatformFileValue = -1; FILE* FdopenPlatformFileForWriting(PlatformFile file) { @@ -44,6 +63,19 @@ FILE* FdopenPlatformFileForWriting(PlatformFile file) { bool ClosePlatformFile(PlatformFile file) { return close(file); } + +bool RemoveFile(const std::string& path) { + return ::unlink(path.c_str()) == 0; +} + +PlatformFile OpenPlatformFile(const std::string& path) { + return ::open(path.c_str(), O_RDWR); +} + +PlatformFile CreatePlatformFile(const std::string& path) { + return ::open(path.c_str(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); +} + #endif } // namespace rtc diff --git a/webrtc/base/platform_file.h b/webrtc/base/platform_file.h index 12e08e9acc..27accf050b 100644 --- a/webrtc/base/platform_file.h +++ b/webrtc/base/platform_file.h @@ -12,9 +12,10 @@ #define WEBRTC_BASE_PLATFORM_FILE_H_ #include +#include #if defined(WEBRTC_WIN) -#include +#include "webrtc/base/win32.h" #endif namespace rtc { @@ -39,6 +40,17 @@ FILE* FdopenPlatformFileForWriting(PlatformFile file); // Use fclose instead. bool ClosePlatformFile(PlatformFile file); +// Removes a file in the filesystem. +bool RemoveFile(const std::string& path); + +// Opens a file for reading and writing. You might want to use base/file.h +// instead. +PlatformFile OpenPlatformFile(const std::string& path); + +// Creates a new file for reading and writing. If the file already exists it +// will be overwritten. You might want to use base/file.h instead. +PlatformFile CreatePlatformFile(const std::string& path); + } // namespace rtc #endif // WEBRTC_BASE_PLATFORM_FILE_H_