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 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | // This will attempt to delete the path located at filename. |
nisse | 0ebdf27 | 2017-01-23 07:43:05 -0800 | [diff] [blame] | 84 | // 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] | 85 | // non-existent file. |
| 86 | virtual bool DeleteFile(const Pathname &filename) = 0; |
| 87 | |
| 88 | // This will attempt to delete the empty folder located at 'folder' |
nisse | 0ebdf27 | 2017-01-23 07:43:05 -0800 | [diff] [blame] | 89 | // 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] | 90 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | // 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 |
nisse | 0ebdf27 | 2017-01-23 07:43:05 -0800 | [diff] [blame] | 99 | // 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] | 100 | // directory, and returns true if the function succeeds. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 101 | virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0; |
| 102 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | // 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) { |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 134 | RTC_DCHECK(organization); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | *organization = organization_name_; |
| 136 | } |
| 137 | void SetApplicationName(const std::string& application) { |
| 138 | application_name_ = application; |
| 139 | } |
| 140 | void GetApplicationName(std::string* application) { |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 141 | RTC_DCHECK(application); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | *application = application_name_; |
| 143 | } |
| 144 | |
| 145 | protected: |
| 146 | std::string organization_name_; |
| 147 | std::string application_name_; |
| 148 | }; |
| 149 | |
| 150 | class Filesystem { |
| 151 | public: |
| 152 | static FilesystemInterface *default_filesystem() { |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 153 | RTC_DCHECK(default_filesystem_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 154 | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 168 | static bool CreateFolder(const Pathname &pathname) { |
| 169 | return EnsureDefaultFilesystem()->CreateFolder(pathname); |
| 170 | } |
| 171 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | static bool DeleteFile(const Pathname &filename) { |
| 173 | return EnsureDefaultFilesystem()->DeleteFile(filename); |
| 174 | } |
| 175 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 176 | static bool MoveFile(const Pathname &old_path, const Pathname &new_path) { |
| 177 | return EnsureDefaultFilesystem()->MoveFile(old_path, new_path); |
| 178 | } |
| 179 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 180 | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 192 | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 211 | 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(); |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 231 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 234 | } // namespace rtc |
| 235 | |
| 236 | #endif // WEBRTC_BASE_FILEUTILS_H_ |