blob: 9847a7b237a114fc2a002847877bb5c13a5391ab [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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_FILEUTILS_H_
12#define WEBRTC_BASE_FILEUTILS_H_
13
14#include <string>
15
xians@webrtc.orge46bc772014-10-10 08:36:56 +000016#if !defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include <dirent.h>
18#include <stdio.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22#endif
23
zijiehedd87d582016-12-06 15:04:02 -080024#include "webrtc/base/checks.h"
kwiberg4485ffb2016-04-26 08:14:39 -070025#include "webrtc/base/constructormagic.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000026#include "webrtc/base/platform_file.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
28namespace rtc {
29
30class FileStream;
31class Pathname;
32
33//////////////////////////
34// Directory Iterator //
35//////////////////////////
36
37// A DirectoryIterator is created with a given directory. It originally points
38// to the first file in the directory, and can be advanecd with Next(). This
39// allows you to get information about each file.
40
41class DirectoryIterator {
42 friend class Filesystem;
43 public:
44 // Constructor
45 DirectoryIterator();
46 // Destructor
47 virtual ~DirectoryIterator();
48
49 // Starts traversing a directory
50 // dir is the directory to traverse
51 // returns true if the directory exists and is valid
52 // The iterator will point to the first entry in the directory
53 virtual bool Iterate(const Pathname &path);
54
55 // Advances to the next file
56 // returns true if there were more files in the directory.
57 virtual bool Next();
58
59 // returns true if the file currently pointed to is a directory
60 virtual bool IsDirectory() const;
61
62 // returns the name of the file currently pointed to
63 virtual std::string Name() const;
64
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065 private:
66 std::string directory_;
67#if defined(WEBRTC_WIN)
68 WIN32_FIND_DATA data_;
69 HANDLE handle_;
70#else
71 DIR *dir_;
72 struct dirent *dirent_;
73 struct stat stat_;
74#endif
75};
76
77enum FileTimeType { FTT_CREATED, FTT_MODIFIED, FTT_ACCESSED };
78
79class FilesystemInterface {
80 public:
81 virtual ~FilesystemInterface() {}
82
83 // Returns a DirectoryIterator for a given pathname.
84 // TODO: Do fancy abstracted stuff
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000085 virtual DirectoryIterator* IterateDirectory();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086
87 // Opens a file. Returns an open StreamInterface if function succeeds.
deadbeef37f5ecf2017-02-27 14:06:41 -080088 // Otherwise, returns null.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089 // TODO: Add an error param to indicate failure reason, similar to
90 // FileStream::Open
91 virtual FileStream *OpenFile(const Pathname &filename,
92 const std::string &mode) = 0;
93
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094 // This will attempt to delete the path located at filename.
nisse0ebdf272017-01-23 07:43:05 -080095 // It DCHECKs and returns false if the path points to a folder or a
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 // non-existent file.
97 virtual bool DeleteFile(const Pathname &filename) = 0;
98
99 // This will attempt to delete the empty folder located at 'folder'
nisse0ebdf272017-01-23 07:43:05 -0800100 // It DCHECKs and returns false if the path points to a file or a non-existent
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101 // folder. It fails normally if the folder is not empty or can otherwise
102 // not be deleted.
103 virtual bool DeleteEmptyFolder(const Pathname &folder) = 0;
104
105 // This will call IterateDirectory, to get a directory iterator, and then
106 // call DeleteFolderAndContents and DeleteFile on every path contained in this
107 // folder. If the folder is empty, this returns true.
108 virtual bool DeleteFolderContents(const Pathname &folder);
109
110 // This deletes the contents of a folder, recursively, and then deletes
111 // the folder itself.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000112 virtual bool DeleteFolderAndContents(const Pathname& folder);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 // Creates a directory. This will call itself recursively to create /foo/bar
115 // even if /foo does not exist. Returns true if the function succeeds.
116 virtual bool CreateFolder(const Pathname &pathname) = 0;
117
118 // This moves a file from old_path to new_path, where "old_path" is a
nisse0ebdf272017-01-23 07:43:05 -0800119 // plain file. This DCHECKs and returns false if old_path points to a
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120 // directory, and returns true if the function succeeds.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0;
122
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 // Returns true if pathname refers to a directory
124 virtual bool IsFolder(const Pathname& pathname) = 0;
125
126 // Returns true if pathname refers to a file
127 virtual bool IsFile(const Pathname& pathname) = 0;
128
129 // Returns true if pathname refers to no filesystem object, every parent
130 // directory either exists, or is also absent.
131 virtual bool IsAbsent(const Pathname& pathname) = 0;
132
133 // Returns true if pathname represents a temporary location on the system.
134 virtual bool IsTemporaryPath(const Pathname& pathname) = 0;
135
136 // A folder appropriate for storing temporary files (Contents are
137 // automatically deleted when the program exits)
138 virtual bool GetTemporaryFolder(Pathname &path, bool create,
139 const std::string *append) = 0;
140
141 virtual std::string TempFilename(const Pathname &dir,
142 const std::string &prefix) = 0;
143
144 // Determines the size of the file indicated by path.
145 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
146
147 // Determines a timestamp associated with the file indicated by path.
148 virtual bool GetFileTime(const Pathname& path, FileTimeType which,
149 time_t* time) = 0;
150
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151 // Get a temporary folder that is unique to the current user and application.
152 // TODO: Re-evaluate the goals of this function. We probably just need any
153 // directory that won't collide with another existing directory, and which
154 // will be cleaned up when the program exits.
155 virtual bool GetAppTempFolder(Pathname* path) = 0;
156
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157 // Note: These might go into some shared config section later, but they're
158 // used by some methods in this interface, so we're leaving them here for now.
159 void SetOrganizationName(const std::string& organization) {
160 organization_name_ = organization;
161 }
162 void GetOrganizationName(std::string* organization) {
zijiehedd87d582016-12-06 15:04:02 -0800163 RTC_DCHECK(organization);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000164 *organization = organization_name_;
165 }
166 void SetApplicationName(const std::string& application) {
167 application_name_ = application;
168 }
169 void GetApplicationName(std::string* application) {
zijiehedd87d582016-12-06 15:04:02 -0800170 RTC_DCHECK(application);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 *application = application_name_;
172 }
173
174 protected:
175 std::string organization_name_;
176 std::string application_name_;
177};
178
179class Filesystem {
180 public:
181 static FilesystemInterface *default_filesystem() {
zijiehedd87d582016-12-06 15:04:02 -0800182 RTC_DCHECK(default_filesystem_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183 return default_filesystem_;
184 }
185
186 static void set_default_filesystem(FilesystemInterface *filesystem) {
187 default_filesystem_ = filesystem;
188 }
189
190 static FilesystemInterface *swap_default_filesystem(
191 FilesystemInterface *filesystem) {
192 FilesystemInterface *cur = default_filesystem_;
193 default_filesystem_ = filesystem;
194 return cur;
195 }
196
197 static DirectoryIterator *IterateDirectory() {
198 return EnsureDefaultFilesystem()->IterateDirectory();
199 }
200
201 static bool CreateFolder(const Pathname &pathname) {
202 return EnsureDefaultFilesystem()->CreateFolder(pathname);
203 }
204
205 static FileStream *OpenFile(const Pathname &filename,
206 const std::string &mode) {
207 return EnsureDefaultFilesystem()->OpenFile(filename, mode);
208 }
209
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000210 static bool DeleteFile(const Pathname &filename) {
211 return EnsureDefaultFilesystem()->DeleteFile(filename);
212 }
213
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000214 static bool DeleteFolderContents(const Pathname &folder) {
215 return EnsureDefaultFilesystem()->DeleteFolderContents(folder);
216 }
217
218 static bool DeleteFolderAndContents(const Pathname &folder) {
219 return EnsureDefaultFilesystem()->DeleteFolderAndContents(folder);
220 }
221
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
223 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
224 }
225
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000226 static bool IsFolder(const Pathname& pathname) {
227 return EnsureDefaultFilesystem()->IsFolder(pathname);
228 }
229
230 static bool IsFile(const Pathname &pathname) {
231 return EnsureDefaultFilesystem()->IsFile(pathname);
232 }
233
234 static bool IsAbsent(const Pathname &pathname) {
235 return EnsureDefaultFilesystem()->IsAbsent(pathname);
236 }
237
238 static bool IsTemporaryPath(const Pathname& pathname) {
239 return EnsureDefaultFilesystem()->IsTemporaryPath(pathname);
240 }
241
242 static bool GetTemporaryFolder(Pathname &path, bool create,
243 const std::string *append) {
244 return EnsureDefaultFilesystem()->GetTemporaryFolder(path, create, append);
245 }
246
247 static std::string TempFilename(const Pathname &dir,
248 const std::string &prefix) {
249 return EnsureDefaultFilesystem()->TempFilename(dir, prefix);
250 }
251
252 static bool GetFileSize(const Pathname& path, size_t* size) {
253 return EnsureDefaultFilesystem()->GetFileSize(path, size);
254 }
255
256 static bool GetFileTime(const Pathname& path, FileTimeType which,
257 time_t* time) {
258 return EnsureDefaultFilesystem()->GetFileTime(path, which, time);
259 }
260
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000261 static bool GetAppTempFolder(Pathname* path) {
262 return EnsureDefaultFilesystem()->GetAppTempFolder(path);
263 }
264
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265 static void SetOrganizationName(const std::string& organization) {
266 EnsureDefaultFilesystem()->SetOrganizationName(organization);
267 }
268
269 static void GetOrganizationName(std::string* organization) {
270 EnsureDefaultFilesystem()->GetOrganizationName(organization);
271 }
272
273 static void SetApplicationName(const std::string& application) {
274 EnsureDefaultFilesystem()->SetApplicationName(application);
275 }
276
277 static void GetApplicationName(std::string* application) {
278 EnsureDefaultFilesystem()->GetApplicationName(application);
279 }
280
281 private:
282 static FilesystemInterface* default_filesystem_;
283
284 static FilesystemInterface *EnsureDefaultFilesystem();
henrikg3c089d72015-09-16 05:37:44 -0700285 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286};
287
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288} // namespace rtc
289
290#endif // WEBRTC_BASE_FILEUTILS_H_