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 | |
Karl Wiberg | 6a4d411 | 2018-03-23 10:39:34 +0100 | [diff] [blame] | 11 | #ifndef RTC_BASE_SYSTEM_FILE_WRAPPER_H_ |
| 12 | #define RTC_BASE_SYSTEM_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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 17 | #include "rtc_base/critical_section.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
Fredrik Solenberg | 98a91ad | 2018-04-11 19:04:37 +0200 | [diff] [blame] | 19 | // Implementation that can read (exclusive) or write from/to a file. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 22 | |
Fredrik Solenberg | 98a91ad | 2018-04-11 19:04:37 +0200 | [diff] [blame] | 23 | // TODO(tommi): Rename to rtc::File and move to base. |
| 24 | class FileWrapper final { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 25 | public: |
| 26 | static const size_t kMaxFileNameSize = 1024; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 28 | // Factory methods. |
| 29 | // TODO(tommi): Remove Create(). |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 30 | static FileWrapper* Create(); |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 31 | static FileWrapper Open(const char* file_name_utf8, bool read_only); |
| 32 | |
| 33 | FileWrapper(FILE* file, size_t max_size); |
Fredrik Solenberg | 98a91ad | 2018-04-11 19:04:37 +0200 | [diff] [blame] | 34 | ~FileWrapper(); |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 35 | |
| 36 | // Support for move semantics. |
| 37 | FileWrapper(FileWrapper&& other); |
| 38 | FileWrapper& operator=(FileWrapper&& other); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 40 | // Returns true if a file has been opened. |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 41 | bool is_open() const { return file_ != nullptr; } |
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 | // 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] | 44 | bool OpenFile(const char* file_name_utf8, bool read_only); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 46 | // Initializes the wrapper from an existing handle. The wrapper |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 47 | // takes ownership of |handle| and closes it in CloseFile(). |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 48 | bool OpenFromFileHandle(FILE* handle); |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 49 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 50 | void CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 52 | // Limits the file size to |bytes|. Writing will fail after the cap |
| 53 | // is hit. Pass zero to use an unlimited size. |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 54 | // TODO(tommi): Could we move this out into a separate class? |
| 55 | void SetMaxFileSize(size_t bytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 57 | // Flush any pending writes. Note: Flushing when closing, is not required. |
| 58 | int Flush(); |
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 | // Rewinds the file to the start. |
Fredrik Solenberg | 98a91ad | 2018-04-11 19:04:37 +0200 | [diff] [blame] | 61 | int Rewind(); |
| 62 | int Read(void* buf, size_t length); |
| 63 | bool Write(const void* buf, size_t length); |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | FileWrapper(); |
| 67 | |
| 68 | void CloseFileImpl(); |
| 69 | int FlushImpl(); |
| 70 | |
| 71 | // TODO(tommi): Remove the lock. |
| 72 | rtc::CriticalSection lock_; |
| 73 | |
| 74 | FILE* file_ = nullptr; |
| 75 | size_t position_ = 0; |
| 76 | size_t max_size_in_bytes_ = 0; |
| 77 | |
| 78 | // Copying is not supported. |
| 79 | FileWrapper(const FileWrapper&) = delete; |
| 80 | FileWrapper& operator=(const FileWrapper&) = delete; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | }; |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 82 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 83 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | |
Karl Wiberg | 6a4d411 | 2018-03-23 10:39:34 +0100 | [diff] [blame] | 85 | #endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_ |