blob: a7b061cb3413880c29d8ff972d232f7fd8e790eb [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_
12#define SYSTEM_WRAPPERS_INCLUDE_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 "common_types.h"
18#include "rtc_base/criticalsection.h"
19#include "typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21// Implementation of an InStream and OutStream that can read (exclusive) or
22// write from/to a file.
23
24namespace webrtc {
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000025
tommia6219cc2016-06-15 10:30:14 -070026// TODO(tommi): Remove the base classes, rename to rtc::File and move to base.
phoglund@webrtc.org740be442012-12-12 12:52:15 +000027class FileWrapper : public InStream, public OutStream {
28 public:
29 static const size_t kMaxFileNameSize = 1024;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
tommia6219cc2016-06-15 10:30:14 -070031 // Factory methods.
32 // TODO(tommi): Remove Create().
phoglund@webrtc.org740be442012-12-12 12:52:15 +000033 static FileWrapper* Create();
tommia6219cc2016-06-15 10:30:14 -070034 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.com470e71d2011-07-07 08:21:25 +000042
phoglund@webrtc.org740be442012-12-12 12:52:15 +000043 // Returns true if a file has been opened.
tommia6219cc2016-06-15 10:30:14 -070044 bool is_open() const { return file_ != nullptr; }
niklase@google.com470e71d2011-07-07 08:21:25 +000045
phoglund@webrtc.org740be442012-12-12 12:52:15 +000046 // Opens a file in read or write mode, decided by the read_only parameter.
tommia6219cc2016-06-15 10:30:14 -070047 bool OpenFile(const char* file_name_utf8, bool read_only);
niklase@google.com470e71d2011-07-07 08:21:25 +000048
tommia6219cc2016-06-15 10:30:14 -070049 // Initializes the wrapper from an existing handle. The wrapper
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000050 // takes ownership of |handle| and closes it in CloseFile().
tommia6219cc2016-06-15 10:30:14 -070051 bool OpenFromFileHandle(FILE* handle);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000052
tommia6219cc2016-06-15 10:30:14 -070053 void CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +000054
phoglund@webrtc.org740be442012-12-12 12:52:15 +000055 // Limits the file size to |bytes|. Writing will fail after the cap
56 // is hit. Pass zero to use an unlimited size.
tommia6219cc2016-06-15 10:30:14 -070057 // TODO(tommi): Could we move this out into a separate class?
58 void SetMaxFileSize(size_t bytes);
niklase@google.com470e71d2011-07-07 08:21:25 +000059
tommia6219cc2016-06-15 10:30:14 -070060 // Flush any pending writes. Note: Flushing when closing, is not required.
61 int Flush();
niklase@google.com470e71d2011-07-07 08:21:25 +000062
tommia6219cc2016-06-15 10:30:14 -070063 // Rewinds the file to the start.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 int Rewind() override;
tommia6219cc2016-06-15 10:30:14 -070065 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.com470e71d2011-07-07 08:21:25 +000084};
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000085
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000086} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000087
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020088#endif // SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_