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