OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // Advances to the next file | 56 // Advances to the next file |
57 // returns true if there were more files in the directory. | 57 // returns true if there were more files in the directory. |
58 virtual bool Next(); | 58 virtual bool Next(); |
59 | 59 |
60 // returns true if the file currently pointed to is a directory | 60 // returns true if the file currently pointed to is a directory |
61 virtual bool IsDirectory() const; | 61 virtual bool IsDirectory() const; |
62 | 62 |
63 // returns the name of the file currently pointed to | 63 // returns the name of the file currently pointed to |
64 virtual std::string Name() const; | 64 virtual std::string Name() const; |
65 | 65 |
66 // returns the size of the file currently pointed to | |
67 virtual size_t FileSize() const; | |
68 | |
69 // returns true if the file is older than seconds | |
70 virtual bool OlderThan(int seconds) const; | |
71 | |
72 // checks whether current file is a special directory file "." or ".." | |
73 bool IsDots() const { | |
74 std::string filename(Name()); | |
75 return (filename.compare(".") == 0) || (filename.compare("..") == 0); | |
76 } | |
77 | |
78 private: | 66 private: |
79 std::string directory_; | 67 std::string directory_; |
80 #if defined(WEBRTC_WIN) | 68 #if defined(WEBRTC_WIN) |
81 WIN32_FIND_DATA data_; | 69 WIN32_FIND_DATA data_; |
82 HANDLE handle_; | 70 HANDLE handle_; |
83 #else | 71 #else |
84 DIR *dir_; | 72 DIR *dir_; |
85 struct dirent *dirent_; | 73 struct dirent *dirent_; |
86 struct stat stat_; | 74 struct stat stat_; |
87 #endif | 75 #endif |
88 }; | 76 }; |
89 | 77 |
90 enum FileTimeType { FTT_CREATED, FTT_MODIFIED, FTT_ACCESSED }; | 78 enum FileTimeType { FTT_CREATED, FTT_MODIFIED, FTT_ACCESSED }; |
91 | 79 |
92 class FilesystemInterface { | 80 class FilesystemInterface { |
93 public: | 81 public: |
94 virtual ~FilesystemInterface() {} | 82 virtual ~FilesystemInterface() {} |
95 | 83 |
96 // Returns a DirectoryIterator for a given pathname. | 84 // Returns a DirectoryIterator for a given pathname. |
97 // TODO: Do fancy abstracted stuff | 85 // TODO: Do fancy abstracted stuff |
98 virtual DirectoryIterator* IterateDirectory(); | 86 virtual DirectoryIterator* IterateDirectory(); |
99 | 87 |
100 // Opens a file. Returns an open StreamInterface if function succeeds. | 88 // Opens a file. Returns an open StreamInterface if function succeeds. |
101 // Otherwise, returns NULL. | 89 // Otherwise, returns NULL. |
102 // TODO: Add an error param to indicate failure reason, similar to | 90 // TODO: Add an error param to indicate failure reason, similar to |
103 // FileStream::Open | 91 // FileStream::Open |
104 virtual FileStream *OpenFile(const Pathname &filename, | 92 virtual FileStream *OpenFile(const Pathname &filename, |
105 const std::string &mode) = 0; | 93 const std::string &mode) = 0; |
106 | 94 |
107 // Atomically creates an empty file accessible only to the current user if one | |
108 // does not already exist at the given path, otherwise fails. This is the only | |
109 // secure way to create a file in a shared temp directory (e.g., C:\Temp on | |
110 // Windows or /tmp on Linux). | |
111 // Note that if it is essential that a file be successfully created then the | |
112 // app must generate random names and retry on failure, or else it will be | |
113 // vulnerable to a trivial DoS. | |
114 virtual bool CreatePrivateFile(const Pathname &filename) = 0; | |
115 | |
116 // This will attempt to delete the path located at filename. | 95 // This will attempt to delete the path located at filename. |
117 // It ASSERTS and returns false if the path points to a folder or a | 96 // It ASSERTS and returns false if the path points to a folder or a |
118 // non-existent file. | 97 // non-existent file. |
119 virtual bool DeleteFile(const Pathname &filename) = 0; | 98 virtual bool DeleteFile(const Pathname &filename) = 0; |
120 | 99 |
121 // This will attempt to delete the empty folder located at 'folder' | 100 // This will attempt to delete the empty folder located at 'folder' |
122 // It ASSERTS and returns false if the path points to a file or a non-existent | 101 // It ASSERTS and returns false if the path points to a file or a non-existent |
123 // folder. It fails normally if the folder is not empty or can otherwise | 102 // folder. It fails normally if the folder is not empty or can otherwise |
124 // not be deleted. | 103 // not be deleted. |
125 virtual bool DeleteEmptyFolder(const Pathname &folder) = 0; | 104 virtual bool DeleteEmptyFolder(const Pathname &folder) = 0; |
126 | 105 |
127 // This will call IterateDirectory, to get a directory iterator, and then | 106 // This will call IterateDirectory, to get a directory iterator, and then |
128 // call DeleteFolderAndContents and DeleteFile on every path contained in this | 107 // call DeleteFolderAndContents and DeleteFile on every path contained in this |
129 // folder. If the folder is empty, this returns true. | 108 // folder. If the folder is empty, this returns true. |
130 virtual bool DeleteFolderContents(const Pathname &folder); | 109 virtual bool DeleteFolderContents(const Pathname &folder); |
131 | 110 |
132 // This deletes the contents of a folder, recursively, and then deletes | 111 // This deletes the contents of a folder, recursively, and then deletes |
133 // the folder itself. | 112 // the folder itself. |
134 virtual bool DeleteFolderAndContents(const Pathname& folder); | 113 virtual bool DeleteFolderAndContents(const Pathname& folder); |
135 | 114 |
136 // This will delete whatever is located at path, be it a file or a folder. | |
137 // If it is a folder, it will delete it recursively by calling | |
138 // DeleteFolderAndContents | |
139 bool DeleteFileOrFolder(const Pathname &path) { | |
140 if (IsFolder(path)) | |
141 return DeleteFolderAndContents(path); | |
142 else | |
143 return DeleteFile(path); | |
144 } | |
145 | |
146 // Creates a directory. This will call itself recursively to create /foo/bar | 115 // Creates a directory. This will call itself recursively to create /foo/bar |
147 // even if /foo does not exist. Returns true if the function succeeds. | 116 // even if /foo does not exist. Returns true if the function succeeds. |
148 virtual bool CreateFolder(const Pathname &pathname) = 0; | 117 virtual bool CreateFolder(const Pathname &pathname) = 0; |
149 | 118 |
150 // This moves a file from old_path to new_path, where "old_path" is a | 119 // This moves a file from old_path to new_path, where "old_path" is a |
151 // plain file. This ASSERTs and returns false if old_path points to a | 120 // plain file. This ASSERTs and returns false if old_path points to a |
152 // directory, and returns true if the function succeeds. | 121 // directory, and returns true if the function succeeds. |
153 // If the new path is on a different volume than the old path, this function | 122 // If the new path is on a different volume than the old path, this function |
154 // will attempt to copy and, if that succeeds, delete the old path. | 123 // will attempt to copy and, if that succeeds, delete the old path. |
155 virtual bool MoveFolder(const Pathname &old_path, | |
156 const Pathname &new_path) = 0; | |
157 | |
158 // This moves a directory from old_path to new_path, where "old_path" is a | |
159 // directory. This ASSERTs and returns false if old_path points to a plain | |
160 // file, and returns true if the function succeeds. | |
161 // If the new path is on a different volume, this function will attempt to | |
162 // copy and if that succeeds, delete the old path. | |
163 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0; | 124 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0; |
164 | 125 |
165 // This attempts to move whatever is located at old_path to new_path, | |
166 // be it a file or folder. | |
167 bool MoveFileOrFolder(const Pathname &old_path, const Pathname &new_path) { | |
168 if (IsFile(old_path)) { | |
169 return MoveFile(old_path, new_path); | |
170 } else { | |
171 return MoveFolder(old_path, new_path); | |
172 } | |
173 } | |
174 | |
175 // This copies a file from old_path to new_path. This method ASSERTs and | 126 // This copies a file from old_path to new_path. This method ASSERTs and |
176 // returns false if old_path is a folder, and returns true if the copy | 127 // returns false if old_path is a folder, and returns true if the copy |
177 // succeeds. | 128 // succeeds. |
178 virtual bool CopyFile(const Pathname &old_path, const Pathname &new_path) = 0; | 129 virtual bool CopyFile(const Pathname &old_path, const Pathname &new_path) = 0; |
179 | 130 |
180 // This copies a folder from old_path to new_path. | |
181 bool CopyFolder(const Pathname &old_path, const Pathname &new_path); | |
182 | |
183 bool CopyFileOrFolder(const Pathname &old_path, const Pathname &new_path) { | |
184 if (IsFile(old_path)) | |
185 return CopyFile(old_path, new_path); | |
186 else | |
187 return CopyFolder(old_path, new_path); | |
188 } | |
189 | |
190 // Returns true if pathname refers to a directory | 131 // Returns true if pathname refers to a directory |
191 virtual bool IsFolder(const Pathname& pathname) = 0; | 132 virtual bool IsFolder(const Pathname& pathname) = 0; |
192 | 133 |
193 // Returns true if pathname refers to a file | 134 // Returns true if pathname refers to a file |
194 virtual bool IsFile(const Pathname& pathname) = 0; | 135 virtual bool IsFile(const Pathname& pathname) = 0; |
195 | 136 |
196 // Returns true if pathname refers to no filesystem object, every parent | 137 // Returns true if pathname refers to no filesystem object, every parent |
197 // directory either exists, or is also absent. | 138 // directory either exists, or is also absent. |
198 virtual bool IsAbsent(const Pathname& pathname) = 0; | 139 virtual bool IsAbsent(const Pathname& pathname) = 0; |
199 | 140 |
200 // Returns true if pathname represents a temporary location on the system. | 141 // Returns true if pathname represents a temporary location on the system. |
201 virtual bool IsTemporaryPath(const Pathname& pathname) = 0; | 142 virtual bool IsTemporaryPath(const Pathname& pathname) = 0; |
202 | 143 |
203 // A folder appropriate for storing temporary files (Contents are | 144 // A folder appropriate for storing temporary files (Contents are |
204 // automatically deleted when the program exits) | 145 // automatically deleted when the program exits) |
205 virtual bool GetTemporaryFolder(Pathname &path, bool create, | 146 virtual bool GetTemporaryFolder(Pathname &path, bool create, |
206 const std::string *append) = 0; | 147 const std::string *append) = 0; |
207 | 148 |
208 virtual std::string TempFilename(const Pathname &dir, | 149 virtual std::string TempFilename(const Pathname &dir, |
209 const std::string &prefix) = 0; | 150 const std::string &prefix) = 0; |
210 | 151 |
211 // Determines the size of the file indicated by path. | 152 // Determines the size of the file indicated by path. |
212 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0; | 153 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0; |
213 | 154 |
214 // Determines a timestamp associated with the file indicated by path. | 155 // Determines a timestamp associated with the file indicated by path. |
215 virtual bool GetFileTime(const Pathname& path, FileTimeType which, | 156 virtual bool GetFileTime(const Pathname& path, FileTimeType which, |
216 time_t* time) = 0; | 157 time_t* time) = 0; |
217 | 158 |
218 // Returns the path to the running application. | |
219 // Note: This is not guaranteed to work on all platforms. Be aware of the | |
220 // limitations before using it, and robustly handle failure. | |
221 virtual bool GetAppPathname(Pathname* path) = 0; | |
222 | |
223 // Get a folder that is unique to the current application, which is suitable | 159 // Get a folder that is unique to the current application, which is suitable |
224 // for sharing data between executions of the app. If the per_user arg is | 160 // for sharing data between executions of the app. If the per_user arg is |
225 // true, the folder is also specific to the current user. | 161 // true, the folder is also specific to the current user. |
226 virtual bool GetAppDataFolder(Pathname* path, bool per_user) = 0; | 162 virtual bool GetAppDataFolder(Pathname* path, bool per_user) = 0; |
227 | 163 |
228 // Get a temporary folder that is unique to the current user and application. | 164 // Get a temporary folder that is unique to the current user and application. |
229 // TODO: Re-evaluate the goals of this function. We probably just need any | 165 // TODO: Re-evaluate the goals of this function. We probably just need any |
230 // directory that won't collide with another existing directory, and which | 166 // directory that won't collide with another existing directory, and which |
231 // will be cleaned up when the program exits. | 167 // will be cleaned up when the program exits. |
232 virtual bool GetAppTempFolder(Pathname* path) = 0; | 168 virtual bool GetAppTempFolder(Pathname* path) = 0; |
233 | 169 |
234 // Delete the contents of the folder returned by GetAppTempFolder | |
235 bool CleanAppTempFolder(); | |
236 | |
237 virtual bool GetDiskFreeSpace(const Pathname& path, int64_t* freebytes) = 0; | 170 virtual bool GetDiskFreeSpace(const Pathname& path, int64_t* freebytes) = 0; |
238 | 171 |
239 // Returns the absolute path of the current directory. | |
240 virtual Pathname GetCurrentDirectory() = 0; | |
241 | |
242 // Note: These might go into some shared config section later, but they're | 172 // Note: These might go into some shared config section later, but they're |
243 // used by some methods in this interface, so we're leaving them here for now. | 173 // used by some methods in this interface, so we're leaving them here for now. |
244 void SetOrganizationName(const std::string& organization) { | 174 void SetOrganizationName(const std::string& organization) { |
245 organization_name_ = organization; | 175 organization_name_ = organization; |
246 } | 176 } |
247 void GetOrganizationName(std::string* organization) { | 177 void GetOrganizationName(std::string* organization) { |
248 ASSERT(NULL != organization); | 178 ASSERT(NULL != organization); |
249 *organization = organization_name_; | 179 *organization = organization_name_; |
250 } | 180 } |
251 void SetApplicationName(const std::string& application) { | 181 void SetApplicationName(const std::string& application) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 | 215 |
286 static bool CreateFolder(const Pathname &pathname) { | 216 static bool CreateFolder(const Pathname &pathname) { |
287 return EnsureDefaultFilesystem()->CreateFolder(pathname); | 217 return EnsureDefaultFilesystem()->CreateFolder(pathname); |
288 } | 218 } |
289 | 219 |
290 static FileStream *OpenFile(const Pathname &filename, | 220 static FileStream *OpenFile(const Pathname &filename, |
291 const std::string &mode) { | 221 const std::string &mode) { |
292 return EnsureDefaultFilesystem()->OpenFile(filename, mode); | 222 return EnsureDefaultFilesystem()->OpenFile(filename, mode); |
293 } | 223 } |
294 | 224 |
295 static bool CreatePrivateFile(const Pathname &filename) { | |
296 return EnsureDefaultFilesystem()->CreatePrivateFile(filename); | |
297 } | |
298 | |
299 static bool DeleteFile(const Pathname &filename) { | 225 static bool DeleteFile(const Pathname &filename) { |
300 return EnsureDefaultFilesystem()->DeleteFile(filename); | 226 return EnsureDefaultFilesystem()->DeleteFile(filename); |
301 } | 227 } |
302 | 228 |
303 static bool DeleteEmptyFolder(const Pathname &folder) { | |
304 return EnsureDefaultFilesystem()->DeleteEmptyFolder(folder); | |
305 } | |
306 | |
307 static bool DeleteFolderContents(const Pathname &folder) { | 229 static bool DeleteFolderContents(const Pathname &folder) { |
308 return EnsureDefaultFilesystem()->DeleteFolderContents(folder); | 230 return EnsureDefaultFilesystem()->DeleteFolderContents(folder); |
309 } | 231 } |
310 | 232 |
311 static bool DeleteFolderAndContents(const Pathname &folder) { | 233 static bool DeleteFolderAndContents(const Pathname &folder) { |
312 return EnsureDefaultFilesystem()->DeleteFolderAndContents(folder); | 234 return EnsureDefaultFilesystem()->DeleteFolderAndContents(folder); |
313 } | 235 } |
314 | 236 |
315 static bool MoveFolder(const Pathname &old_path, const Pathname &new_path) { | |
316 return EnsureDefaultFilesystem()->MoveFolder(old_path, new_path); | |
317 } | |
318 | |
319 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) { | 237 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) { |
320 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path); | 238 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path); |
321 } | 239 } |
322 | 240 |
323 static bool CopyFolder(const Pathname &old_path, const Pathname &new_path) { | |
324 return EnsureDefaultFilesystem()->CopyFolder(old_path, new_path); | |
325 } | |
326 | |
327 static bool CopyFile(const Pathname &old_path, const Pathname &new_path) { | 241 static bool CopyFile(const Pathname &old_path, const Pathname &new_path) { |
328 return EnsureDefaultFilesystem()->CopyFile(old_path, new_path); | 242 return EnsureDefaultFilesystem()->CopyFile(old_path, new_path); |
329 } | 243 } |
330 | 244 |
331 static bool IsFolder(const Pathname& pathname) { | 245 static bool IsFolder(const Pathname& pathname) { |
332 return EnsureDefaultFilesystem()->IsFolder(pathname); | 246 return EnsureDefaultFilesystem()->IsFolder(pathname); |
333 } | 247 } |
334 | 248 |
335 static bool IsFile(const Pathname &pathname) { | 249 static bool IsFile(const Pathname &pathname) { |
336 return EnsureDefaultFilesystem()->IsFile(pathname); | 250 return EnsureDefaultFilesystem()->IsFile(pathname); |
(...skipping 19 matching lines...) Expand all Loading... |
356 | 270 |
357 static bool GetFileSize(const Pathname& path, size_t* size) { | 271 static bool GetFileSize(const Pathname& path, size_t* size) { |
358 return EnsureDefaultFilesystem()->GetFileSize(path, size); | 272 return EnsureDefaultFilesystem()->GetFileSize(path, size); |
359 } | 273 } |
360 | 274 |
361 static bool GetFileTime(const Pathname& path, FileTimeType which, | 275 static bool GetFileTime(const Pathname& path, FileTimeType which, |
362 time_t* time) { | 276 time_t* time) { |
363 return EnsureDefaultFilesystem()->GetFileTime(path, which, time); | 277 return EnsureDefaultFilesystem()->GetFileTime(path, which, time); |
364 } | 278 } |
365 | 279 |
366 static bool GetAppPathname(Pathname* path) { | |
367 return EnsureDefaultFilesystem()->GetAppPathname(path); | |
368 } | |
369 | |
370 static bool GetAppDataFolder(Pathname* path, bool per_user) { | 280 static bool GetAppDataFolder(Pathname* path, bool per_user) { |
371 return EnsureDefaultFilesystem()->GetAppDataFolder(path, per_user); | 281 return EnsureDefaultFilesystem()->GetAppDataFolder(path, per_user); |
372 } | 282 } |
373 | 283 |
374 static bool GetAppTempFolder(Pathname* path) { | 284 static bool GetAppTempFolder(Pathname* path) { |
375 return EnsureDefaultFilesystem()->GetAppTempFolder(path); | 285 return EnsureDefaultFilesystem()->GetAppTempFolder(path); |
376 } | 286 } |
377 | 287 |
378 static bool CleanAppTempFolder() { | |
379 return EnsureDefaultFilesystem()->CleanAppTempFolder(); | |
380 } | |
381 | |
382 static bool GetDiskFreeSpace(const Pathname& path, int64_t* freebytes) { | 288 static bool GetDiskFreeSpace(const Pathname& path, int64_t* freebytes) { |
383 return EnsureDefaultFilesystem()->GetDiskFreeSpace(path, freebytes); | 289 return EnsureDefaultFilesystem()->GetDiskFreeSpace(path, freebytes); |
384 } | 290 } |
385 | 291 |
386 // Definition has to be in the .cc file due to returning forward-declared | |
387 // Pathname by value. | |
388 static Pathname GetCurrentDirectory(); | |
389 | |
390 static void SetOrganizationName(const std::string& organization) { | 292 static void SetOrganizationName(const std::string& organization) { |
391 EnsureDefaultFilesystem()->SetOrganizationName(organization); | 293 EnsureDefaultFilesystem()->SetOrganizationName(organization); |
392 } | 294 } |
393 | 295 |
394 static void GetOrganizationName(std::string* organization) { | 296 static void GetOrganizationName(std::string* organization) { |
395 EnsureDefaultFilesystem()->GetOrganizationName(organization); | 297 EnsureDefaultFilesystem()->GetOrganizationName(organization); |
396 } | 298 } |
397 | 299 |
398 static void SetApplicationName(const std::string& application) { | 300 static void SetApplicationName(const std::string& application) { |
399 EnsureDefaultFilesystem()->SetApplicationName(application); | 301 EnsureDefaultFilesystem()->SetApplicationName(application); |
(...skipping 16 matching lines...) Expand all Loading... |
416 old_fs_ = Filesystem::swap_default_filesystem(new_fs); | 318 old_fs_ = Filesystem::swap_default_filesystem(new_fs); |
417 } | 319 } |
418 ~FilesystemScope() { | 320 ~FilesystemScope() { |
419 Filesystem::set_default_filesystem(old_fs_); | 321 Filesystem::set_default_filesystem(old_fs_); |
420 } | 322 } |
421 private: | 323 private: |
422 FilesystemInterface* old_fs_; | 324 FilesystemInterface* old_fs_; |
423 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FilesystemScope); | 325 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FilesystemScope); |
424 }; | 326 }; |
425 | 327 |
426 // Generates a unique filename based on the input path. If no path component | |
427 // is specified, it uses the temporary directory. If a filename is provided, | |
428 // up to 100 variations of form basename-N.extension are tried. When | |
429 // create_empty is true, an empty file of this name is created (which | |
430 // decreases the chance of a temporary filename collision with another | |
431 // process). | |
432 bool CreateUniqueFile(Pathname& path, bool create_empty); | |
433 | |
434 } // namespace rtc | 328 } // namespace rtc |
435 | 329 |
436 #endif // WEBRTC_BASE_FILEUTILS_H_ | 330 #endif // WEBRTC_BASE_FILEUTILS_H_ |
OLD | NEW |