blob: 4672cc43e3bc38ade91b186a549a40315296ec05 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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 Wiberg6a4d4112018-03-23 10:39:34 +010011#ifndef RTC_BASE_SYSTEM_FILE_WRAPPER_H_
12#define RTC_BASE_SYSTEM_FILE_WRAPPER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000014#include <stddef.h>
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000015#include <stdio.h>
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000016
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/criticalsection.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000020
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020021// Implementation that can read (exclusive) or write from/to a file.
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000024
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020025// TODO(tommi): Rename to rtc::File and move to base.
26class FileWrapper final {
phoglund@webrtc.org740be442012-12-12 12:52:15 +000027 public:
28 static const size_t kMaxFileNameSize = 1024;
niklase@google.com470e71d2011-07-07 08:21:25 +000029
tommia6219cc2016-06-15 10:30:14 -070030 // Factory methods.
31 // TODO(tommi): Remove Create().
phoglund@webrtc.org740be442012-12-12 12:52:15 +000032 static FileWrapper* Create();
tommia6219cc2016-06-15 10:30:14 -070033 static FileWrapper Open(const char* file_name_utf8, bool read_only);
34
35 FileWrapper(FILE* file, size_t max_size);
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020036 ~FileWrapper();
tommia6219cc2016-06-15 10:30:14 -070037
38 // Support for move semantics.
39 FileWrapper(FileWrapper&& other);
40 FileWrapper& operator=(FileWrapper&& other);
niklase@google.com470e71d2011-07-07 08:21:25 +000041
phoglund@webrtc.org740be442012-12-12 12:52:15 +000042 // Returns true if a file has been opened.
tommia6219cc2016-06-15 10:30:14 -070043 bool is_open() const { return file_ != nullptr; }
niklase@google.com470e71d2011-07-07 08:21:25 +000044
phoglund@webrtc.org740be442012-12-12 12:52:15 +000045 // Opens a file in read or write mode, decided by the read_only parameter.
tommia6219cc2016-06-15 10:30:14 -070046 bool OpenFile(const char* file_name_utf8, bool read_only);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
tommia6219cc2016-06-15 10:30:14 -070048 // Initializes the wrapper from an existing handle. The wrapper
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000049 // takes ownership of |handle| and closes it in CloseFile().
tommia6219cc2016-06-15 10:30:14 -070050 bool OpenFromFileHandle(FILE* handle);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000051
tommia6219cc2016-06-15 10:30:14 -070052 void CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +000053
phoglund@webrtc.org740be442012-12-12 12:52:15 +000054 // Limits the file size to |bytes|. Writing will fail after the cap
55 // is hit. Pass zero to use an unlimited size.
tommia6219cc2016-06-15 10:30:14 -070056 // TODO(tommi): Could we move this out into a separate class?
57 void SetMaxFileSize(size_t bytes);
niklase@google.com470e71d2011-07-07 08:21:25 +000058
tommia6219cc2016-06-15 10:30:14 -070059 // Flush any pending writes. Note: Flushing when closing, is not required.
60 int Flush();
niklase@google.com470e71d2011-07-07 08:21:25 +000061
tommia6219cc2016-06-15 10:30:14 -070062 // Rewinds the file to the start.
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020063 int Rewind();
64 int Read(void* buf, size_t length);
65 bool Write(const void* buf, size_t length);
tommia6219cc2016-06-15 10:30:14 -070066
67 private:
68 FileWrapper();
69
70 void CloseFileImpl();
71 int FlushImpl();
72
73 // TODO(tommi): Remove the lock.
74 rtc::CriticalSection lock_;
75
76 FILE* file_ = nullptr;
77 size_t position_ = 0;
78 size_t max_size_in_bytes_ = 0;
79
80 // Copying is not supported.
81 FileWrapper(const FileWrapper&) = delete;
82 FileWrapper& operator=(const FileWrapper&) = delete;
niklase@google.com470e71d2011-07-07 08:21:25 +000083};
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000084
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000085} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000086
Karl Wiberg6a4d4112018-03-23 10:39:34 +010087#endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_