blob: 42428fa103bf54a80ad3e02e5866c8e25cf52408 [file] [log] [blame]
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +02001/*
2 * Copyright (c) 2016 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 RTC_BASE_FILE_H_
12#define RTC_BASE_FILE_H_
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <stdint.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <string>
17
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/platform_file.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
21namespace rtc {
22
23// This class wraps the platform specific APIs for simple file interactions.
24//
25// The various read and write methods are best effort, i.e. if an underlying
26// call does not manage to read/write all the data more calls will be performed,
27// until an error is detected or all data is read/written.
28class File {
29 public:
30 // Wraps the given PlatformFile. This class is then responsible for closing
31 // the file, which will be done in the destructor if Close is never called.
32 explicit File(PlatformFile);
33 // The default constructor produces a closed file.
34 File();
35 ~File();
36
37 File(File&& other);
38 File& operator=(File&& other);
39
40 // Open and Create give files with both reading and writing enabled.
41 static File Open(const std::string& path);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042 // If the file already exists it will be overwritten.
43 static File Create(const std::string& path);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044
45 // Remove a file in the file system.
46 static bool Remove(const std::string& path);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047
48 size_t Write(const uint8_t* data, size_t length);
49 size_t Read(uint8_t* buffer, size_t length);
50
51 // The current position in the file after a call to these methods is platform
52 // dependent (MSVC gives position offset+length, most other
53 // compilers/platforms do not alter the position), i.e. do not depend on it,
54 // do a Seek before any subsequent Read/Write.
55 size_t WriteAt(const uint8_t* data, size_t length, size_t offset);
56 size_t ReadAt(uint8_t* buffer, size_t length, size_t offset);
57
58 // Attempt to position the file at the given offset from the start.
59 // Returns true if successful, false otherwise.
60 bool Seek(size_t offset);
61
62 // Attempt to close the file. Returns true if successful, false otherwise,
63 // most notably when the file is already closed.
64 bool Close();
65
66 bool IsOpen();
67
68 private:
69 PlatformFile file_;
70 RTC_DISALLOW_COPY_AND_ASSIGN(File);
71};
72
73} // namespace rtc
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020074
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020075#endif // RTC_BASE_FILE_H_