blob: 0bb86a397f9060bfe433d9e356f4f989f6555f00 [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 Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/criticalsection.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020019// Implementation that can read (exclusive) or write from/to a file.
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000022
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020023// TODO(tommi): Rename to rtc::File and move to base.
24class FileWrapper final {
phoglund@webrtc.org740be442012-12-12 12:52:15 +000025 public:
26 static const size_t kMaxFileNameSize = 1024;
niklase@google.com470e71d2011-07-07 08:21:25 +000027
tommia6219cc2016-06-15 10:30:14 -070028 // Factory methods.
29 // TODO(tommi): Remove Create().
phoglund@webrtc.org740be442012-12-12 12:52:15 +000030 static FileWrapper* Create();
tommia6219cc2016-06-15 10:30:14 -070031 static FileWrapper Open(const char* file_name_utf8, bool read_only);
32
33 FileWrapper(FILE* file, size_t max_size);
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020034 ~FileWrapper();
tommia6219cc2016-06-15 10:30:14 -070035
36 // Support for move semantics.
37 FileWrapper(FileWrapper&& other);
38 FileWrapper& operator=(FileWrapper&& other);
niklase@google.com470e71d2011-07-07 08:21:25 +000039
phoglund@webrtc.org740be442012-12-12 12:52:15 +000040 // Returns true if a file has been opened.
tommia6219cc2016-06-15 10:30:14 -070041 bool is_open() const { return file_ != nullptr; }
niklase@google.com470e71d2011-07-07 08:21:25 +000042
phoglund@webrtc.org740be442012-12-12 12:52:15 +000043 // Opens a file in read or write mode, decided by the read_only parameter.
tommia6219cc2016-06-15 10:30:14 -070044 bool OpenFile(const char* file_name_utf8, bool read_only);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
tommia6219cc2016-06-15 10:30:14 -070046 // Initializes the wrapper from an existing handle. The wrapper
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000047 // takes ownership of |handle| and closes it in CloseFile().
tommia6219cc2016-06-15 10:30:14 -070048 bool OpenFromFileHandle(FILE* handle);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000049
tommia6219cc2016-06-15 10:30:14 -070050 void CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +000051
phoglund@webrtc.org740be442012-12-12 12:52:15 +000052 // Limits the file size to |bytes|. Writing will fail after the cap
53 // is hit. Pass zero to use an unlimited size.
tommia6219cc2016-06-15 10:30:14 -070054 // TODO(tommi): Could we move this out into a separate class?
55 void SetMaxFileSize(size_t bytes);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
tommia6219cc2016-06-15 10:30:14 -070057 // Flush any pending writes. Note: Flushing when closing, is not required.
58 int Flush();
niklase@google.com470e71d2011-07-07 08:21:25 +000059
tommia6219cc2016-06-15 10:30:14 -070060 // Rewinds the file to the start.
Fredrik Solenberg98a91ad2018-04-11 19:04:37 +020061 int Rewind();
62 int Read(void* buf, size_t length);
63 bool Write(const void* buf, size_t length);
tommia6219cc2016-06-15 10:30:14 -070064
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.com470e71d2011-07-07 08:21:25 +000081};
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000082
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000083} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000084
Karl Wiberg6a4d4112018-03-23 10:39:34 +010085#endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_