webrtc_m130/rtc_base/system/file_wrapper.h

86 lines
2.4 KiB
C
Raw Normal View History

/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_SYSTEM_FILE_WRAPPER_H_
#define RTC_BASE_SYSTEM_FILE_WRAPPER_H_
#include <stddef.h>
#include <stdio.h>
#include "rtc_base/critical_section.h"
// Implementation that can read (exclusive) or write from/to a file.
namespace webrtc {
// TODO(tommi): Rename to rtc::File and move to base.
class FileWrapper final {
public:
static const size_t kMaxFileNameSize = 1024;
// Factory methods.
// TODO(tommi): Remove Create().
static FileWrapper* Create();
static FileWrapper Open(const char* file_name_utf8, bool read_only);
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}
2016-06-15 10:30:14 -07:00
FileWrapper(FILE* file, size_t max_size);
~FileWrapper();
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}
2016-06-15 10:30:14 -07:00
// Support for move semantics.
FileWrapper(FileWrapper&& other);
FileWrapper& operator=(FileWrapper&& other);
// Returns true if a file has been opened.
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}
2016-06-15 10:30:14 -07:00
bool is_open() const { return file_ != nullptr; }
// Opens a file in read or write mode, decided by the read_only parameter.
bool OpenFile(const char* file_name_utf8, bool read_only);
// Initializes the wrapper from an existing handle. The wrapper
// takes ownership of |handle| and closes it in CloseFile().
bool OpenFromFileHandle(FILE* handle);
void CloseFile();
// Limits the file size to |bytes|. Writing will fail after the cap
// is hit. Pass zero to use an unlimited size.
// TODO(tommi): Could we move this out into a separate class?
void SetMaxFileSize(size_t bytes);
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}
2016-06-15 10:30:14 -07:00
// Flush any pending writes. Note: Flushing when closing, is not required.
int Flush();
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}
2016-06-15 10:30:14 -07:00
// Rewinds the file to the start.
int Rewind();
int Read(void* buf, size_t length);
bool Write(const void* buf, size_t length);
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}
2016-06-15 10:30:14 -07:00
private:
FileWrapper();
void CloseFileImpl();
int FlushImpl();
// TODO(tommi): Remove the lock.
rtc::CriticalSection lock_;
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}
2016-06-15 10:30:14 -07:00
FILE* file_ = nullptr;
size_t position_ = 0;
size_t max_size_in_bytes_ = 0;
// Copying is not supported.
FileWrapper(const FileWrapper&) = delete;
FileWrapper& operator=(const FileWrapper&) = delete;
};
} // namespace webrtc
#endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_