blob: 8f6ec521b2f5c566bcf50373adc5f88849470024 [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.
121 // If the new path is on a different volume than the old path, this function
122 // will attempt to copy and, if that succeeds, delete the old path.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0;
124
nisse0ebdf272017-01-23 07:43:05 -0800125 // This copies a file from old_path to new_path. This method DCHECKs and
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126 // returns false if old_path is a folder, and returns true if the copy
127 // succeeds.
128 virtual bool CopyFile(const Pathname &old_path, const Pathname &new_path) = 0;
129
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 // Returns true if pathname refers to a directory
131 virtual bool IsFolder(const Pathname& pathname) = 0;
132
133 // Returns true if pathname refers to a file
134 virtual bool IsFile(const Pathname& pathname) = 0;
135
136 // Returns true if pathname refers to no filesystem object, every parent
137 // directory either exists, or is also absent.
138 virtual bool IsAbsent(const Pathname& pathname) = 0;
139
140 // Returns true if pathname represents a temporary location on the system.
141 virtual bool IsTemporaryPath(const Pathname& pathname) = 0;
142
143 // A folder appropriate for storing temporary files (Contents are
144 // automatically deleted when the program exits)
145 virtual bool GetTemporaryFolder(Pathname &path, bool create,
146 const std::string *append) = 0;
147
148 virtual std::string TempFilename(const Pathname &dir,
149 const std::string &prefix) = 0;
150
151 // Determines the size of the file indicated by path.
152 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
153
154 // Determines a timestamp associated with the file indicated by path.
155 virtual bool GetFileTime(const Pathname& path, FileTimeType which,
156 time_t* time) = 0;
157
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158 // Get a temporary folder that is unique to the current user and application.
159 // TODO: Re-evaluate the goals of this function. We probably just need any
160 // directory that won't collide with another existing directory, and which
161 // will be cleaned up when the program exits.
162 virtual bool GetAppTempFolder(Pathname* path) = 0;
163
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000164 // Note: These might go into some shared config section later, but they're
165 // used by some methods in this interface, so we're leaving them here for now.
166 void SetOrganizationName(const std::string& organization) {
167 organization_name_ = organization;
168 }
169 void GetOrganizationName(std::string* organization) {
zijiehedd87d582016-12-06 15:04:02 -0800170 RTC_DCHECK(organization);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 *organization = organization_name_;
172 }
173 void SetApplicationName(const std::string& application) {
174 application_name_ = application;
175 }
176 void GetApplicationName(std::string* application) {
zijiehedd87d582016-12-06 15:04:02 -0800177 RTC_DCHECK(application);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178 *application = application_name_;
179 }
180
181 protected:
182 std::string organization_name_;
183 std::string application_name_;
184};
185
186class Filesystem {
187 public:
188 static FilesystemInterface *default_filesystem() {
zijiehedd87d582016-12-06 15:04:02 -0800189 RTC_DCHECK(default_filesystem_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000190 return default_filesystem_;
191 }
192
193 static void set_default_filesystem(FilesystemInterface *filesystem) {
194 default_filesystem_ = filesystem;
195 }
196
197 static FilesystemInterface *swap_default_filesystem(
198 FilesystemInterface *filesystem) {
199 FilesystemInterface *cur = default_filesystem_;
200 default_filesystem_ = filesystem;
201 return cur;
202 }
203
204 static DirectoryIterator *IterateDirectory() {
205 return EnsureDefaultFilesystem()->IterateDirectory();
206 }
207
208 static bool CreateFolder(const Pathname &pathname) {
209 return EnsureDefaultFilesystem()->CreateFolder(pathname);
210 }
211
212 static FileStream *OpenFile(const Pathname &filename,
213 const std::string &mode) {
214 return EnsureDefaultFilesystem()->OpenFile(filename, mode);
215 }
216
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000217 static bool DeleteFile(const Pathname &filename) {
218 return EnsureDefaultFilesystem()->DeleteFile(filename);
219 }
220
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221 static bool DeleteFolderContents(const Pathname &folder) {
222 return EnsureDefaultFilesystem()->DeleteFolderContents(folder);
223 }
224
225 static bool DeleteFolderAndContents(const Pathname &folder) {
226 return EnsureDefaultFilesystem()->DeleteFolderAndContents(folder);
227 }
228
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
230 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
231 }
232
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000233 static bool CopyFile(const Pathname &old_path, const Pathname &new_path) {
234 return EnsureDefaultFilesystem()->CopyFile(old_path, new_path);
235 }
236
237 static bool IsFolder(const Pathname& pathname) {
238 return EnsureDefaultFilesystem()->IsFolder(pathname);
239 }
240
241 static bool IsFile(const Pathname &pathname) {
242 return EnsureDefaultFilesystem()->IsFile(pathname);
243 }
244
245 static bool IsAbsent(const Pathname &pathname) {
246 return EnsureDefaultFilesystem()->IsAbsent(pathname);
247 }
248
249 static bool IsTemporaryPath(const Pathname& pathname) {
250 return EnsureDefaultFilesystem()->IsTemporaryPath(pathname);
251 }
252
253 static bool GetTemporaryFolder(Pathname &path, bool create,
254 const std::string *append) {
255 return EnsureDefaultFilesystem()->GetTemporaryFolder(path, create, append);
256 }
257
258 static std::string TempFilename(const Pathname &dir,
259 const std::string &prefix) {
260 return EnsureDefaultFilesystem()->TempFilename(dir, prefix);
261 }
262
263 static bool GetFileSize(const Pathname& path, size_t* size) {
264 return EnsureDefaultFilesystem()->GetFileSize(path, size);
265 }
266
267 static bool GetFileTime(const Pathname& path, FileTimeType which,
268 time_t* time) {
269 return EnsureDefaultFilesystem()->GetFileTime(path, which, time);
270 }
271
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272 static bool GetAppTempFolder(Pathname* path) {
273 return EnsureDefaultFilesystem()->GetAppTempFolder(path);
274 }
275
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000276 static void SetOrganizationName(const std::string& organization) {
277 EnsureDefaultFilesystem()->SetOrganizationName(organization);
278 }
279
280 static void GetOrganizationName(std::string* organization) {
281 EnsureDefaultFilesystem()->GetOrganizationName(organization);
282 }
283
284 static void SetApplicationName(const std::string& application) {
285 EnsureDefaultFilesystem()->SetApplicationName(application);
286 }
287
288 static void GetApplicationName(std::string* application) {
289 EnsureDefaultFilesystem()->GetApplicationName(application);
290 }
291
292 private:
293 static FilesystemInterface* default_filesystem_;
294
295 static FilesystemInterface *EnsureDefaultFilesystem();
henrikg3c089d72015-09-16 05:37:44 -0700296 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297};
298
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000299} // namespace rtc
300
301#endif // WEBRTC_BASE_FILEUTILS_H_