niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #ifndef SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_ |
| 12 | #define SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 14 | #include <stddef.h> |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 17 | #include "common_types.h" |
| 18 | #include "rtc_base/criticalsection.h" |
| 19 | #include "typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
| 21 | // Implementation of an InStream and OutStream that can read (exclusive) or |
| 22 | // write from/to a file. |
| 23 | |
| 24 | namespace webrtc { |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 25 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 26 | // TODO(tommi): Remove the base classes, rename to rtc::File and move to base. |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 27 | class FileWrapper : public InStream, public OutStream { |
| 28 | public: |
| 29 | static const size_t kMaxFileNameSize = 1024; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 31 | // Factory methods. |
| 32 | // TODO(tommi): Remove Create(). |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 33 | static FileWrapper* Create(); |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 34 | static FileWrapper Open(const char* file_name_utf8, bool read_only); |
| 35 | |
| 36 | FileWrapper(FILE* file, size_t max_size); |
| 37 | ~FileWrapper() override; |
| 38 | |
| 39 | // Support for move semantics. |
| 40 | FileWrapper(FileWrapper&& other); |
| 41 | FileWrapper& operator=(FileWrapper&& other); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 43 | // Returns true if a file has been opened. |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 44 | bool is_open() const { return file_ != nullptr; } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 46 | // Opens a file in read or write mode, decided by the read_only parameter. |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 47 | bool OpenFile(const char* file_name_utf8, bool read_only); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 49 | // Initializes the wrapper from an existing handle. The wrapper |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 50 | // takes ownership of |handle| and closes it in CloseFile(). |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 51 | bool OpenFromFileHandle(FILE* handle); |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 52 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 53 | void CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 55 | // Limits the file size to |bytes|. Writing will fail after the cap |
| 56 | // is hit. Pass zero to use an unlimited size. |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 57 | // TODO(tommi): Could we move this out into a separate class? |
| 58 | void SetMaxFileSize(size_t bytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 60 | // Flush any pending writes. Note: Flushing when closing, is not required. |
| 61 | int Flush(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 63 | // Rewinds the file to the start. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 64 | int Rewind() override; |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 65 | int Read(void* buf, size_t length) override; |
| 66 | bool Write(const void* buf, size_t length) override; |
| 67 | |
| 68 | private: |
| 69 | FileWrapper(); |
| 70 | |
| 71 | void CloseFileImpl(); |
| 72 | int FlushImpl(); |
| 73 | |
| 74 | // TODO(tommi): Remove the lock. |
| 75 | rtc::CriticalSection lock_; |
| 76 | |
| 77 | FILE* file_ = nullptr; |
| 78 | size_t position_ = 0; |
| 79 | size_t max_size_in_bytes_ = 0; |
| 80 | |
| 81 | // Copying is not supported. |
| 82 | FileWrapper(const FileWrapper&) = delete; |
| 83 | FileWrapper& operator=(const FileWrapper&) = delete; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | }; |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 85 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 86 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 88 | #endif // SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_ |