blob: f2669281bd3add54ddda3e0d7f70f389d78d7cc9 [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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 // This will attempt to delete the path located at filename.
nisse0ebdf272017-01-23 07:43:05 -080084 // It DCHECKs and returns false if the path points to a folder or a
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 // non-existent file.
86 virtual bool DeleteFile(const Pathname &filename) = 0;
87
88 // This will attempt to delete the empty folder located at 'folder'
nisse0ebdf272017-01-23 07:43:05 -080089 // It DCHECKs and returns false if the path points to a file or a non-existent
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090 // folder. It fails normally if the folder is not empty or can otherwise
91 // not be deleted.
92 virtual bool DeleteEmptyFolder(const Pathname &folder) = 0;
93
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094 // Creates a directory. This will call itself recursively to create /foo/bar
95 // even if /foo does not exist. Returns true if the function succeeds.
96 virtual bool CreateFolder(const Pathname &pathname) = 0;
97
98 // This moves a file from old_path to new_path, where "old_path" is a
nisse0ebdf272017-01-23 07:43:05 -080099 // plain file. This DCHECKs and returns false if old_path points to a
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100 // directory, and returns true if the function succeeds.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0;
102
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 // Returns true if pathname refers to a directory
104 virtual bool IsFolder(const Pathname& pathname) = 0;
105
106 // Returns true if pathname refers to a file
107 virtual bool IsFile(const Pathname& pathname) = 0;
108
109 // Returns true if pathname refers to no filesystem object, every parent
110 // directory either exists, or is also absent.
111 virtual bool IsAbsent(const Pathname& pathname) = 0;
112
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 // A folder appropriate for storing temporary files (Contents are
114 // automatically deleted when the program exits)
115 virtual bool GetTemporaryFolder(Pathname &path, bool create,
116 const std::string *append) = 0;
117
118 virtual std::string TempFilename(const Pathname &dir,
119 const std::string &prefix) = 0;
120
121 // Determines the size of the file indicated by path.
122 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
123
124 // Determines a timestamp associated with the file indicated by path.
125 virtual bool GetFileTime(const Pathname& path, FileTimeType which,
126 time_t* time) = 0;
127
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 // Note: These might go into some shared config section later, but they're
129 // used by some methods in this interface, so we're leaving them here for now.
130 void SetOrganizationName(const std::string& organization) {
131 organization_name_ = organization;
132 }
133 void GetOrganizationName(std::string* organization) {
zijiehedd87d582016-12-06 15:04:02 -0800134 RTC_DCHECK(organization);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 *organization = organization_name_;
136 }
137 void SetApplicationName(const std::string& application) {
138 application_name_ = application;
139 }
140 void GetApplicationName(std::string* application) {
zijiehedd87d582016-12-06 15:04:02 -0800141 RTC_DCHECK(application);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142 *application = application_name_;
143 }
144
145 protected:
146 std::string organization_name_;
147 std::string application_name_;
148};
149
150class Filesystem {
151 public:
152 static FilesystemInterface *default_filesystem() {
zijiehedd87d582016-12-06 15:04:02 -0800153 RTC_DCHECK(default_filesystem_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 return default_filesystem_;
155 }
156
157 static void set_default_filesystem(FilesystemInterface *filesystem) {
158 default_filesystem_ = filesystem;
159 }
160
161 static FilesystemInterface *swap_default_filesystem(
162 FilesystemInterface *filesystem) {
163 FilesystemInterface *cur = default_filesystem_;
164 default_filesystem_ = filesystem;
165 return cur;
166 }
167
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 static bool CreateFolder(const Pathname &pathname) {
169 return EnsureDefaultFilesystem()->CreateFolder(pathname);
170 }
171
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172 static bool DeleteFile(const Pathname &filename) {
173 return EnsureDefaultFilesystem()->DeleteFile(filename);
174 }
175
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
177 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
178 }
179
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000180 static bool IsFolder(const Pathname& pathname) {
181 return EnsureDefaultFilesystem()->IsFolder(pathname);
182 }
183
184 static bool IsFile(const Pathname &pathname) {
185 return EnsureDefaultFilesystem()->IsFile(pathname);
186 }
187
188 static bool IsAbsent(const Pathname &pathname) {
189 return EnsureDefaultFilesystem()->IsAbsent(pathname);
190 }
191
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000192 static bool GetTemporaryFolder(Pathname &path, bool create,
193 const std::string *append) {
194 return EnsureDefaultFilesystem()->GetTemporaryFolder(path, create, append);
195 }
196
197 static std::string TempFilename(const Pathname &dir,
198 const std::string &prefix) {
199 return EnsureDefaultFilesystem()->TempFilename(dir, prefix);
200 }
201
202 static bool GetFileSize(const Pathname& path, size_t* size) {
203 return EnsureDefaultFilesystem()->GetFileSize(path, size);
204 }
205
206 static bool GetFileTime(const Pathname& path, FileTimeType which,
207 time_t* time) {
208 return EnsureDefaultFilesystem()->GetFileTime(path, which, time);
209 }
210
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 static void SetOrganizationName(const std::string& organization) {
212 EnsureDefaultFilesystem()->SetOrganizationName(organization);
213 }
214
215 static void GetOrganizationName(std::string* organization) {
216 EnsureDefaultFilesystem()->GetOrganizationName(organization);
217 }
218
219 static void SetApplicationName(const std::string& application) {
220 EnsureDefaultFilesystem()->SetApplicationName(application);
221 }
222
223 static void GetApplicationName(std::string* application) {
224 EnsureDefaultFilesystem()->GetApplicationName(application);
225 }
226
227 private:
228 static FilesystemInterface* default_filesystem_;
229
230 static FilesystemInterface *EnsureDefaultFilesystem();
henrikg3c089d72015-09-16 05:37:44 -0700231 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232};
233
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234} // namespace rtc
235
236#endif // WEBRTC_BASE_FILEUTILS_H_