henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 16 | #if !defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | #include <dirent.h> |
| 18 | #include <stdio.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | #endif |
| 23 | |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 24 | #include "webrtc/base/checks.h" |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 25 | #include "webrtc/base/constructormagic.h" |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 26 | #include "webrtc/base/platform_file.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | |
| 28 | namespace rtc { |
| 29 | |
| 30 | class FileStream; |
| 31 | class 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 | |
| 41 | class 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | 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 | |
| 77 | enum FileTimeType { FTT_CREATED, FTT_MODIFIED, FTT_ACCESSED }; |
| 78 | |
| 79 | class FilesystemInterface { |
| 80 | public: |
| 81 | virtual ~FilesystemInterface() {} |
| 82 | |
| 83 | // Returns a DirectoryIterator for a given pathname. |
| 84 | // TODO: Do fancy abstracted stuff |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 85 | virtual DirectoryIterator* IterateDirectory(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | |
| 87 | // Opens a file. Returns an open StreamInterface if function succeeds. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 88 | // Otherwise, returns null. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | // This will attempt to delete the path located at filename. |
nisse | 0ebdf27 | 2017-01-23 07:43:05 -0800 | [diff] [blame] | 95 | // It DCHECKs and returns false if the path points to a folder or a |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 96 | // non-existent file. |
| 97 | virtual bool DeleteFile(const Pathname &filename) = 0; |
| 98 | |
| 99 | // This will attempt to delete the empty folder located at 'folder' |
nisse | 0ebdf27 | 2017-01-23 07:43:05 -0800 | [diff] [blame] | 100 | // It DCHECKs and returns false if the path points to a file or a non-existent |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 101 | // 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.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 112 | virtual bool DeleteFolderAndContents(const Pathname& folder); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 114 | // 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 |
nisse | 0ebdf27 | 2017-01-23 07:43:05 -0800 | [diff] [blame] | 119 | // plain file. This DCHECKs and returns false if old_path points to a |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0; |
| 124 | |
nisse | 0ebdf27 | 2017-01-23 07:43:05 -0800 | [diff] [blame] | 125 | // This copies a file from old_path to new_path. This method DCHECKs and |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 130 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 158 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 164 | // 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) { |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 170 | RTC_DCHECK(organization); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 171 | *organization = organization_name_; |
| 172 | } |
| 173 | void SetApplicationName(const std::string& application) { |
| 174 | application_name_ = application; |
| 175 | } |
| 176 | void GetApplicationName(std::string* application) { |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 177 | RTC_DCHECK(application); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 178 | *application = application_name_; |
| 179 | } |
| 180 | |
| 181 | protected: |
| 182 | std::string organization_name_; |
| 183 | std::string application_name_; |
| 184 | }; |
| 185 | |
| 186 | class Filesystem { |
| 187 | public: |
| 188 | static FilesystemInterface *default_filesystem() { |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 189 | RTC_DCHECK(default_filesystem_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 190 | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 217 | static bool DeleteFile(const Pathname &filename) { |
| 218 | return EnsureDefaultFilesystem()->DeleteFile(filename); |
| 219 | } |
| 220 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 221 | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 229 | static bool MoveFile(const Pathname &old_path, const Pathname &new_path) { |
| 230 | return EnsureDefaultFilesystem()->MoveFile(old_path, new_path); |
| 231 | } |
| 232 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 233 | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 272 | static bool GetAppTempFolder(Pathname* path) { |
| 273 | return EnsureDefaultFilesystem()->GetAppTempFolder(path); |
| 274 | } |
| 275 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 276 | 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(); |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 296 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 297 | }; |
| 298 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 299 | } // namespace rtc |
| 300 | |
| 301 | #endif // WEBRTC_BASE_FILEUTILS_H_ |