blob: f4806d1a02bd569ed0c181f944603851e57ecca9 [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
11#ifndef WEBRTC_BASE_FILE_H_
12#define WEBRTC_BASE_FILE_H_
13
14#include <stdint.h>
15
16#include <string>
17
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020018#include "webrtc/base/constructormagic.h"
zijiehedd87d582016-12-06 15:04:02 -080019#include "webrtc/base/pathutils.h"
20#include "webrtc/base/platform_file.h"
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +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);
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +020034 // The default constructor produces a closed file.
35 File();
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020036 ~File();
37
38 File(File&& other);
39 File& operator=(File&& other);
40
Viktor Palmkvist971eb272016-09-16 10:19:23 +020041 // Open and Create give files with both reading and writing enabled.
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020042 static File Open(const std::string& path);
zijiehedd87d582016-12-06 15:04:02 -080043 static File Open(Pathname&& path);
44 static File Open(const Pathname& path);
Viktor Palmkvist971eb272016-09-16 10:19:23 +020045 // If the file already exists it will be overwritten.
46 static File Create(const std::string& path);
zijiehedd87d582016-12-06 15:04:02 -080047 static File Create(Pathname&& path);
48 static File Create(const Pathname& path);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020049
zijiehe2769ec62016-12-14 15:03:03 -080050 // 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
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020055 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
81
82#endif // WEBRTC_BASE_FILE_H_