henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef RTC_BASE_FILEUTILS_H_ |
| 12 | #define RTC_BASE_FILEUTILS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 15 | |
Patrik Höglund | a8005cf | 2017-12-13 15:05:42 | [diff] [blame] | 16 | #if defined(WEBRTC_WIN) |
| 17 | #include <windows.h> |
| 18 | #else |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <stdio.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
Patrik Höglund | a8005cf | 2017-12-13 15:05:42 | [diff] [blame] | 24 | #endif // WEBRTC_WIN |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 25 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 26 | #include "rtc_base/checks.h" |
| 27 | #include "rtc_base/constructormagic.h" |
| 28 | #include "rtc_base/platform_file.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 29 | |
| 30 | namespace rtc { |
| 31 | |
| 32 | class FileStream; |
| 33 | class Pathname; |
| 34 | |
| 35 | ////////////////////////// |
| 36 | // Directory Iterator // |
| 37 | ////////////////////////// |
| 38 | |
| 39 | // A DirectoryIterator is created with a given directory. It originally points |
| 40 | // to the first file in the directory, and can be advanecd with Next(). This |
| 41 | // allows you to get information about each file. |
| 42 | |
| 43 | class DirectoryIterator { |
| 44 | friend class Filesystem; |
| 45 | public: |
| 46 | // Constructor |
| 47 | DirectoryIterator(); |
| 48 | // Destructor |
| 49 | virtual ~DirectoryIterator(); |
| 50 | |
| 51 | // Starts traversing a directory |
| 52 | // dir is the directory to traverse |
| 53 | // returns true if the directory exists and is valid |
| 54 | // The iterator will point to the first entry in the directory |
| 55 | virtual bool Iterate(const Pathname &path); |
| 56 | |
| 57 | // Advances to the next file |
| 58 | // returns true if there were more files in the directory. |
| 59 | virtual bool Next(); |
| 60 | |
| 61 | // returns true if the file currently pointed to is a directory |
| 62 | virtual bool IsDirectory() const; |
| 63 | |
| 64 | // returns the name of the file currently pointed to |
| 65 | virtual std::string Name() const; |
| 66 | |
| 67 | private: |
| 68 | std::string directory_; |
| 69 | #if defined(WEBRTC_WIN) |
| 70 | WIN32_FIND_DATA data_; |
| 71 | HANDLE handle_; |
| 72 | #else |
| 73 | DIR *dir_; |
| 74 | struct dirent *dirent_; |
| 75 | struct stat stat_; |
| 76 | #endif |
| 77 | }; |
| 78 | |
| 79 | class FilesystemInterface { |
| 80 | public: |
| 81 | virtual ~FilesystemInterface() {} |
| 82 | |
| 83 | // This will attempt to delete the path located at filename. |
| 84 | // It DCHECKs and returns false if the path points to a folder or a |
| 85 | // non-existent file. |
| 86 | virtual bool DeleteFile(const Pathname &filename) = 0; |
| 87 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 88 | // This moves a file from old_path to new_path, where "old_path" is a |
| 89 | // plain file. This DCHECKs and returns false if old_path points to a |
| 90 | // directory, and returns true if the function succeeds. |
| 91 | virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0; |
| 92 | |
| 93 | // Returns true if pathname refers to a directory |
| 94 | virtual bool IsFolder(const Pathname& pathname) = 0; |
| 95 | |
| 96 | // Returns true if pathname refers to a file |
| 97 | virtual bool IsFile(const Pathname& pathname) = 0; |
| 98 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 99 | // Determines the size of the file indicated by path. |
| 100 | virtual bool GetFileSize(const Pathname& path, size_t* size) = 0; |
| 101 | }; |
| 102 | |
| 103 | class Filesystem { |
| 104 | public: |
| 105 | static FilesystemInterface *default_filesystem() { |
| 106 | RTC_DCHECK(default_filesystem_); |
| 107 | return default_filesystem_; |
| 108 | } |
| 109 | |
| 110 | static void set_default_filesystem(FilesystemInterface *filesystem) { |
| 111 | default_filesystem_ = filesystem; |
| 112 | } |
| 113 | |
| 114 | static FilesystemInterface *swap_default_filesystem( |
| 115 | FilesystemInterface *filesystem) { |
| 116 | FilesystemInterface *cur = default_filesystem_; |
| 117 | default_filesystem_ = filesystem; |
| 118 | return cur; |
| 119 | } |
| 120 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 121 | static bool DeleteFile(const Pathname &filename) { |
| 122 | return EnsureDefaultFilesystem()->DeleteFile(filename); |
| 123 | } |
| 124 | |
| 125 | static bool MoveFile(const Pathname &old_path, const Pathname &new_path) { |
| 126 | return EnsureDefaultFilesystem()->MoveFile(old_path, new_path); |
| 127 | } |
| 128 | |
| 129 | static bool IsFolder(const Pathname& pathname) { |
| 130 | return EnsureDefaultFilesystem()->IsFolder(pathname); |
| 131 | } |
| 132 | |
| 133 | static bool IsFile(const Pathname &pathname) { |
| 134 | return EnsureDefaultFilesystem()->IsFile(pathname); |
| 135 | } |
| 136 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 137 | static bool GetFileSize(const Pathname& path, size_t* size) { |
| 138 | return EnsureDefaultFilesystem()->GetFileSize(path, size); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | static FilesystemInterface* default_filesystem_; |
| 143 | |
| 144 | static FilesystemInterface *EnsureDefaultFilesystem(); |
| 145 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem); |
| 146 | }; |
| 147 | |
| 148 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 149 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 150 | #endif // RTC_BASE_FILEUTILS_H_ |