FileWrapper[Impl] modifications and actually remove the "Impl" class.
This is a somewhat involved refactoring of this class. Here's an overview of the changes:
* FileWrapper can now be used as a regular class and instances allocated on the stack.
* The type now has support for move semantics and copy isn't allowed.
* New public ctor with FILE* that can be used instead of OpenFromFileHandle.
* New static Open() method. The intent of this is to allow opening a file and getting back a FileWrapper instance. Using this method instead of Create(), will allow us in the future to make the FILE* member pointer, to be const and simplify threading (get rid of the lock).
* Rename the Open() method to is_open() and make it inline.
* The FileWrapper interface is no longer a pure virtual interface. There's only one implementation so there's no need to go through a vtable for everything.
* Functionality offered by the class, is now reduced. No support for looping (not clear if that was actually useful to users of that flag), no need to implement the 'read_only_' functionality in the class, since file APIs implement that already, no support for *not* managing the file handle (this wasn't used). OpenFromFileHandle always "manages" the file.
* Delete the unused WriteText() method and don't support opening files in text mode. Text mode is only different on Windows and on Windows it translates \n to \r\n, which means that files such as log files, could have a slightly different format on Windows than other platforms. Besides, tools on Windows can handle UNIX line endings.
* Remove FileName(), change Trace code to manage its own path.
* Rename id_ member variable to file_.
* Removed the open_ member variable since the same functionality can be gotten from just checking the file pointer.
* Don't call CloseFile inside of Write. Write shouldn't be changing the state of the class beyond just attempting to write.
* Remove concept of looping from FileWrapper and never close inside of Read()
* Changed stream base classes to inherit from a common base class instead of both defining the Rewind method. Ultimately, Id' like to remove these interfaces and just have FileWrapper.
* Remove read_only param from OpenFromFileHandle
* Renamed size_in_bytes_ to position_, since it gets set to 0 when Rewind() is called (and the size actually does not change).
* Switch out rw lock for CriticalSection. The r/w lock was only used for reading when checking the open_ flag.
BUG=
Review-Url: https://codereview.webrtc.org/2054373002
Cr-Commit-Position: refs/heads/master@{#13155}
diff --git a/webrtc/system_wrappers/source/file_impl.cc b/webrtc/system_wrappers/source/file_impl.cc
index e033c0e..8e1ba24 100644
--- a/webrtc/system_wrappers/source/file_impl.cc
+++ b/webrtc/system_wrappers/source/file_impl.cc
@@ -8,9 +8,7 @@
* be found in the AUTHORS file in the root of the source tree.
*/
-#include "webrtc/system_wrappers/source/file_impl.h"
-
-#include <assert.h>
+#include "webrtc/system_wrappers/include/file_wrapper.h"
#ifdef _WIN32
#include <Windows.h>
@@ -20,258 +18,135 @@
#endif
#include "webrtc/base/checks.h"
-#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
namespace webrtc {
-
-FileWrapper* FileWrapper::Create() {
- return new FileWrapperImpl();
-}
-
-FileWrapperImpl::FileWrapperImpl()
- : rw_lock_(RWLockWrapper::CreateRWLock()),
- id_(NULL),
- managed_file_handle_(true),
- open_(false),
- looping_(false),
- read_only_(false),
- max_size_in_bytes_(0),
- size_in_bytes_(0) {
- memset(file_name_utf8_, 0, kMaxFileNameSize);
-}
-
-FileWrapperImpl::~FileWrapperImpl() {
- if (id_ != NULL && managed_file_handle_) {
- fclose(id_);
- }
-}
-
-int FileWrapperImpl::CloseFile() {
- WriteLockScoped write(*rw_lock_);
- return CloseFileImpl();
-}
-
-int FileWrapperImpl::Rewind() {
- WriteLockScoped write(*rw_lock_);
- if (looping_ || !read_only_) {
- if (id_ != NULL) {
- size_in_bytes_ = 0;
- return fseek(id_, 0, SEEK_SET);
- }
- }
- return -1;
-}
-
-int FileWrapperImpl::SetMaxFileSize(size_t bytes) {
- WriteLockScoped write(*rw_lock_);
- max_size_in_bytes_ = bytes;
- return 0;
-}
-
-int FileWrapperImpl::Flush() {
- WriteLockScoped write(*rw_lock_);
- return FlushImpl();
-}
-
-int FileWrapperImpl::FileName(char* file_name_utf8, size_t size) const {
- ReadLockScoped read(*rw_lock_);
- size_t length = strlen(file_name_utf8_);
- if (length > kMaxFileNameSize) {
- assert(false);
- return -1;
- }
- if (length < 1) {
- return -1;
- }
-
- // Make sure to NULL terminate
- if (size < length) {
- length = size - 1;
- }
- memcpy(file_name_utf8, file_name_utf8_, length);
- file_name_utf8[length] = 0;
- return 0;
-}
-
-bool FileWrapperImpl::Open() const {
- ReadLockScoped read(*rw_lock_);
- return open_;
-}
-
-int FileWrapperImpl::OpenFile(const char* file_name_utf8, bool read_only,
- bool loop, bool text) {
- WriteLockScoped write(*rw_lock_);
- if (id_ != NULL && !managed_file_handle_)
- return -1;
- size_t length = strlen(file_name_utf8);
- if (length > kMaxFileNameSize - 1) {
- return -1;
- }
-
- read_only_ = read_only;
-
- FILE* tmp_id = NULL;
-#if defined _WIN32
- wchar_t wide_file_name[kMaxFileNameSize];
- wide_file_name[0] = 0;
-
- MultiByteToWideChar(CP_UTF8,
- 0, // UTF8 flag
- file_name_utf8,
- -1, // Null terminated string
- wide_file_name,
- kMaxFileNameSize);
- if (text) {
- if (read_only) {
- tmp_id = _wfopen(wide_file_name, L"rt");
- } else {
- tmp_id = _wfopen(wide_file_name, L"wt");
- }
- } else {
- if (read_only) {
- tmp_id = _wfopen(wide_file_name, L"rb");
- } else {
- tmp_id = _wfopen(wide_file_name, L"wb");
- }
- }
+namespace {
+FILE* FileOpen(const char* file_name_utf8, bool read_only) {
+#if defined(_WIN32)
+ int len = MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, nullptr, 0);
+ std::wstring wstr(len, 0);
+ MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, &wstr[0], len);
+ FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb");
#else
- if (text) {
- if (read_only) {
- tmp_id = fopen(file_name_utf8, "rt");
- } else {
- tmp_id = fopen(file_name_utf8, "wt");
- }
- } else {
- if (read_only) {
- tmp_id = fopen(file_name_utf8, "rb");
- } else {
- tmp_id = fopen(file_name_utf8, "wb");
- }
- }
+ FILE* file = fopen(file_name_utf8, read_only ? "rb" : "wb");
#endif
+ return file;
+}
+} // namespace
- if (tmp_id != NULL) {
- // +1 comes from copying the NULL termination character.
- memcpy(file_name_utf8_, file_name_utf8, length + 1);
- if (id_ != NULL) {
- fclose(id_);
- }
- id_ = tmp_id;
- managed_file_handle_ = true;
- looping_ = loop;
- open_ = true;
- return 0;
- }
- return -1;
+// static
+FileWrapper* FileWrapper::Create() {
+ return new FileWrapper();
}
-int FileWrapperImpl::OpenFromFileHandle(FILE* handle,
- bool manage_file,
- bool read_only,
- bool loop) {
- WriteLockScoped write(*rw_lock_);
- if (!handle)
- return -1;
-
- if (id_ != NULL) {
- if (managed_file_handle_)
- fclose(id_);
- else
- return -1;
- }
-
- id_ = handle;
- managed_file_handle_ = manage_file;
- read_only_ = read_only;
- looping_ = loop;
- open_ = true;
- return 0;
+// static
+FileWrapper FileWrapper::Open(const char* file_name_utf8, bool read_only) {
+ return FileWrapper(FileOpen(file_name_utf8, read_only), 0);
}
-int FileWrapperImpl::Read(void* buf, size_t length) {
- WriteLockScoped write(*rw_lock_);
- if (id_ == NULL)
- return -1;
+FileWrapper::FileWrapper() {}
- size_t bytes_read = fread(buf, 1, length, id_);
- if (bytes_read != length && !looping_) {
- CloseFileImpl();
- }
- return static_cast<int>(bytes_read);
+FileWrapper::FileWrapper(FILE* file, size_t max_size)
+ : file_(file), max_size_in_bytes_(max_size) {}
+
+FileWrapper::~FileWrapper() {
+ CloseFileImpl();
}
-int FileWrapperImpl::WriteText(const char* format, ...) {
- WriteLockScoped write(*rw_lock_);
- if (format == NULL)
- return -1;
-
- if (read_only_)
- return -1;
-
- if (id_ == NULL)
- return -1;
-
- va_list args;
- va_start(args, format);
- int num_chars = vfprintf(id_, format, args);
- va_end(args);
-
- if (num_chars >= 0) {
- return num_chars;
- } else {
- CloseFileImpl();
- return -1;
- }
+FileWrapper::FileWrapper(FileWrapper&& other) {
+ operator=(std::move(other));
}
-bool FileWrapperImpl::Write(const void* buf, size_t length) {
- WriteLockScoped write(*rw_lock_);
- if (buf == NULL)
- return false;
-
- if (read_only_)
- return false;
-
- if (id_ == NULL)
- return false;
-
- // Check if it's time to stop writing.
- if (max_size_in_bytes_ > 0 &&
- (size_in_bytes_ + length) > max_size_in_bytes_) {
- FlushImpl();
- return false;
- }
-
- size_t num_bytes = fwrite(buf, 1, length, id_);
- size_in_bytes_ += num_bytes;
- if (num_bytes != length) {
- CloseFileImpl();
- return false;
- }
- return true;
+FileWrapper& FileWrapper::operator=(FileWrapper&& other) {
+ file_ = other.file_;
+ max_size_in_bytes_ = other.max_size_in_bytes_;
+ position_ = other.position_;
+ other.file_ = nullptr;
+ return *this;
}
-int FileWrapperImpl::CloseFileImpl() {
- if (id_ != NULL) {
- if (managed_file_handle_)
- fclose(id_);
- id_ = NULL;
- }
- memset(file_name_utf8_, 0, kMaxFileNameSize);
- open_ = false;
- return 0;
-}
-
-int FileWrapperImpl::FlushImpl() {
- if (id_ != NULL) {
- return fflush(id_);
- }
- return -1;
+void FileWrapper::CloseFile() {
+ rtc::CritScope lock(&lock_);
+ CloseFileImpl();
}
int FileWrapper::Rewind() {
- RTC_DCHECK(false);
+ rtc::CritScope lock(&lock_);
+ if (file_ != nullptr) {
+ position_ = 0;
+ return fseek(file_, 0, SEEK_SET);
+ }
return -1;
}
+void FileWrapper::SetMaxFileSize(size_t bytes) {
+ rtc::CritScope lock(&lock_);
+ max_size_in_bytes_ = bytes;
+}
+
+int FileWrapper::Flush() {
+ rtc::CritScope lock(&lock_);
+ return FlushImpl();
+}
+
+bool FileWrapper::OpenFile(const char* file_name_utf8, bool read_only) {
+ size_t length = strlen(file_name_utf8);
+ if (length > kMaxFileNameSize - 1)
+ return false;
+
+ rtc::CritScope lock(&lock_);
+ if (file_ != nullptr)
+ return false;
+
+ file_ = FileOpen(file_name_utf8, read_only);
+ return file_ != nullptr;
+}
+
+bool FileWrapper::OpenFromFileHandle(FILE* handle) {
+ if (!handle)
+ return false;
+ rtc::CritScope lock(&lock_);
+ CloseFileImpl();
+ file_ = handle;
+ return true;
+}
+
+int FileWrapper::Read(void* buf, size_t length) {
+ rtc::CritScope lock(&lock_);
+ if (file_ == nullptr)
+ return -1;
+
+ size_t bytes_read = fread(buf, 1, length, file_);
+ return static_cast<int>(bytes_read);
+}
+
+bool FileWrapper::Write(const void* buf, size_t length) {
+ if (buf == nullptr)
+ return false;
+
+ rtc::CritScope lock(&lock_);
+
+ if (file_ == nullptr)
+ return false;
+
+ // Check if it's time to stop writing.
+ if (max_size_in_bytes_ > 0 && (position_ + length) > max_size_in_bytes_)
+ return false;
+
+ size_t num_bytes = fwrite(buf, 1, length, file_);
+ position_ += num_bytes;
+
+ return num_bytes == length;
+}
+
+void FileWrapper::CloseFileImpl() {
+ if (file_ != nullptr)
+ fclose(file_);
+ file_ = nullptr;
+}
+
+int FileWrapper::FlushImpl() {
+ return (file_ != nullptr) ? fflush(file_) : -1;
+}
+
} // namespace webrtc