2011-07-07 08:21:25 +00:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-03-23 10:39:34 +01:00
|
|
|
#ifndef RTC_BASE_SYSTEM_FILE_WRAPPER_H_
|
|
|
|
|
#define RTC_BASE_SYSTEM_FILE_WRAPPER_H_
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2011-12-13 22:59:33 +00:00
|
|
|
#include <stddef.h>
|
2023-10-16 16:41:58 +02:00
|
|
|
#include <stdint.h>
|
2013-12-06 16:05:17 +00:00
|
|
|
#include <stdio.h>
|
2011-12-13 22:59:33 +00:00
|
|
|
|
2024-08-29 13:00:40 +00:00
|
|
|
#include <optional>
|
2020-07-16 14:15:23 +02:00
|
|
|
#include <string>
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2022-04-13 12:55:15 +02:00
|
|
|
#include "absl/strings/string_view.h"
|
|
|
|
|
|
2018-04-11 19:04:37 +02:00
|
|
|
// Implementation that can read (exclusive) or write from/to a file.
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2011-12-13 22:59:33 +00:00
|
|
|
|
2019-01-22 11:01:24 +01:00
|
|
|
// This class is a thin wrapper around FILE*. It's main features are that it
|
|
|
|
|
// owns the FILE*, calling fclose on destruction, and that on windows, file
|
|
|
|
|
// names passed to the open methods are always treated as utf-8, regardless of
|
|
|
|
|
// system code page.
|
|
|
|
|
|
|
|
|
|
// Most of the methods return only a success/fail indication. When needed, an
|
|
|
|
|
// optional argument |int* error| should be added to all methods, in the same
|
|
|
|
|
// way as for the OpenWriteOnly methods.
|
2018-04-11 19:04:37 +02:00
|
|
|
class FileWrapper final {
|
2012-12-12 12:52:15 +00:00
|
|
|
public:
|
2019-01-21 11:59:10 +01:00
|
|
|
// Opens a file, in read or write mode. Use the is_open() method on the
|
2019-01-22 11:01:24 +01:00
|
|
|
// returned object to check if the open operation was successful. On failure,
|
2021-07-26 16:03:14 +02:00
|
|
|
// and if `error` is non-null, the system errno value is stored at |*error|.
|
2019-01-22 11:01:24 +01:00
|
|
|
// The file is closed by the destructor.
|
2022-04-13 12:55:15 +02:00
|
|
|
static FileWrapper OpenReadOnly(absl::string_view file_name_utf8);
|
|
|
|
|
static FileWrapper OpenWriteOnly(absl::string_view file_name_utf8,
|
2019-01-22 11:01:24 +01:00
|
|
|
int* error = nullptr);
|
2012-12-12 12:52:15 +00:00
|
|
|
|
2019-01-21 11:59:10 +01:00
|
|
|
FileWrapper() = default;
|
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
|
|
|
|
2021-07-26 16:03:14 +02:00
|
|
|
// Takes over ownership of `file`, closing it on destruction. Calling with
|
|
|
|
|
// null `file` is allowed, and results in a FileWrapper with is_open() false.
|
2019-01-21 11:59:10 +01:00
|
|
|
explicit FileWrapper(FILE* file) : file_(file) {}
|
|
|
|
|
~FileWrapper() { Close(); }
|
|
|
|
|
|
|
|
|
|
// Copying is not supported.
|
|
|
|
|
FileWrapper(const FileWrapper&) = delete;
|
|
|
|
|
FileWrapper& operator=(const FileWrapper&) = delete;
|
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.
|
2019-01-21 11:59:10 +01:00
|
|
|
FileWrapper(FileWrapper&&);
|
|
|
|
|
FileWrapper& operator=(FileWrapper&&);
|
2012-12-12 12:52:15 +00:00
|
|
|
|
2019-01-21 11:59:10 +01:00
|
|
|
// Returns true if a file has been opened. If the file is not open, no methods
|
|
|
|
|
// but is_open and Close may be called.
|
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; }
|
2012-12-12 12:52:15 +00:00
|
|
|
|
2019-01-21 11:59:10 +01:00
|
|
|
// Closes the file, and implies Flush. Returns true on success, false if
|
|
|
|
|
// writing buffered data fails. On failure, the file is nevertheless closed.
|
|
|
|
|
// Calling Close on an already closed file does nothing and returns success.
|
|
|
|
|
bool Close();
|
2013-12-06 16:05:17 +00:00
|
|
|
|
2020-05-07 15:58:55 +02:00
|
|
|
// Releases and returns the wrapped file without closing it. This call passes
|
|
|
|
|
// the ownership of the file to the caller, and the wrapper is no longer
|
|
|
|
|
// responsible for closing it. Similarly the previously wrapped file is no
|
|
|
|
|
// longer available for the wrapper to use in any aspect.
|
|
|
|
|
FILE* Release();
|
|
|
|
|
|
2019-01-21 11:59:10 +01:00
|
|
|
// Write any buffered data to the underlying file. Returns true on success,
|
|
|
|
|
// false on write error. Note: Flushing when closing, is not required.
|
|
|
|
|
bool Flush();
|
2012-12-12 12:52:15 +00:00
|
|
|
|
2019-01-21 11:59:10 +01:00
|
|
|
// Seeks to the beginning of file. Returns true on success, false on failure,
|
|
|
|
|
// e.g., if the underlying file isn't seekable.
|
2019-08-06 09:58:56 +02:00
|
|
|
bool Rewind() { return SeekTo(0); }
|
|
|
|
|
// TODO(nisse): The seek functions are used only by the WavReader. If that
|
|
|
|
|
// code is demoted to test code, seek functions can be deleted from this
|
|
|
|
|
// utility.
|
|
|
|
|
// Seek relative to current file position.
|
|
|
|
|
bool SeekRelative(int64_t offset);
|
|
|
|
|
// Seek to given position.
|
|
|
|
|
bool SeekTo(int64_t position);
|
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
|
|
|
|
2021-03-25 15:52:16 +01:00
|
|
|
// Returns the file size or -1 if a size could not be determined.
|
|
|
|
|
// (A file size might not exists for non-seekable files or file-like
|
|
|
|
|
// objects, for example /dev/tty on unix.)
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<size_t> FileSize();
|
2021-03-25 15:52:16 +01:00
|
|
|
|
2019-01-21 11:59:10 +01:00
|
|
|
// Returns number of bytes read. Short count indicates EOF or error.
|
|
|
|
|
size_t Read(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
|
|
|
|
2019-08-06 09:58:56 +02:00
|
|
|
// If the most recent Read() returned a short count, this methods returns true
|
|
|
|
|
// if the short count was due to EOF, and false it it was due to some i/o
|
|
|
|
|
// error.
|
|
|
|
|
bool ReadEof() const;
|
|
|
|
|
|
2019-01-21 11:59:10 +01:00
|
|
|
// Returns true if all data was successfully written (or buffered), or false
|
|
|
|
|
// if there was an error. Writing buffered data can fail later, and is
|
|
|
|
|
// reported with return value from Flush or Close.
|
2018-04-11 19:04:37 +02:00
|
|
|
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:
|
|
|
|
|
FILE* file_ = nullptr;
|
2011-07-07 08:21:25 +00:00
|
|
|
};
|
2011-12-13 22:59:33 +00:00
|
|
|
|
2013-07-03 15:12:26 +00:00
|
|
|
} // namespace webrtc
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2018-03-23 10:39:34 +01:00
|
|
|
#endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_
|