OLD | NEW |
| 1 |
1 /* | 2 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 3 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 4 * |
4 * Use of this source code is governed by a BSD-style license | 5 * 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 * 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 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 8 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 9 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 10 */ |
10 | 11 |
11 #ifndef WEBRTC_BASE_FILEUTILS_H_ | 12 #ifndef WEBRTC_BASE_FILEUTILS_H_ |
12 #define WEBRTC_BASE_FILEUTILS_H_ | 13 #define WEBRTC_BASE_FILEUTILS_H_ |
13 | 14 |
14 #include <string> | |
15 | 15 |
16 #if !defined(WEBRTC_WIN) | 16 // This header is deprecated and is just left here temporarily during |
17 #include <dirent.h> | 17 // refactoring. See https://bugs.webrtc.org/7634 for more details. |
18 #include <stdio.h> | 18 #include "webrtc/rtc_base/fileutils.h" |
19 #include <sys/stat.h> | |
20 #include <sys/types.h> | |
21 #include <unistd.h> | |
22 #endif | |
23 | |
24 #include "webrtc/base/checks.h" | |
25 #include "webrtc/base/constructormagic.h" | |
26 #include "webrtc/base/platform_file.h" | |
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 | |
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 class FilesystemInterface { | |
78 public: | |
79 virtual ~FilesystemInterface() {} | |
80 | |
81 // This will attempt to delete the path located at filename. | |
82 // It DCHECKs and returns false if the path points to a folder or a | |
83 // non-existent file. | |
84 virtual bool DeleteFile(const Pathname &filename) = 0; | |
85 | |
86 // Creates a directory. This will call itself recursively to create /foo/bar | |
87 // even if /foo does not exist. Returns true if the function succeeds. | |
88 virtual bool CreateFolder(const Pathname &pathname) = 0; | |
89 | |
90 // This moves a file from old_path to new_path, where "old_path" is a | |
91 // plain file. This DCHECKs and returns false if old_path points to a | |
92 // directory, and returns true if the function succeeds. | |
93 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0; | |
94 | |
95 // Returns true if pathname refers to a directory | |
96 virtual bool IsFolder(const Pathname& pathname) = 0; | |
97 | |
98 // Returns true if pathname refers to a file | |
99 virtual bool IsFile(const Pathname& pathname) = 0; | |
100 | |
101 // Returns true if pathname refers to no filesystem object, every parent | |
102 // directory either exists, or is also absent. | |
103 virtual bool IsAbsent(const Pathname& pathname) = 0; | |
104 | |
105 // A folder appropriate for storing temporary files (Contents are | |
106 // automatically deleted when the program exits) | |
107 virtual bool GetTemporaryFolder(Pathname &path, bool create, | |
108 const std::string *append) = 0; | |
109 | |
110 virtual std::string TempFilename(const Pathname &dir, | |
111 const std::string &prefix) = 0; | |
112 | |
113 // Determines the size of the file indicated by path. | |
114 virtual bool GetFileSize(const Pathname& path, size_t* size) = 0; | |
115 }; | |
116 | |
117 class Filesystem { | |
118 public: | |
119 static FilesystemInterface *default_filesystem() { | |
120 RTC_DCHECK(default_filesystem_); | |
121 return default_filesystem_; | |
122 } | |
123 | |
124 static void set_default_filesystem(FilesystemInterface *filesystem) { | |
125 default_filesystem_ = filesystem; | |
126 } | |
127 | |
128 static FilesystemInterface *swap_default_filesystem( | |
129 FilesystemInterface *filesystem) { | |
130 FilesystemInterface *cur = default_filesystem_; | |
131 default_filesystem_ = filesystem; | |
132 return cur; | |
133 } | |
134 | |
135 static bool CreateFolder(const Pathname &pathname) { | |
136 return EnsureDefaultFilesystem()->CreateFolder(pathname); | |
137 } | |
138 | |
139 static bool DeleteFile(const Pathname &filename) { | |
140 return EnsureDefaultFilesystem()->DeleteFile(filename); | |
141 } | |
142 | |
143 static bool MoveFile(const Pathname &old_path, const Pathname &new_path) { | |
144 return EnsureDefaultFilesystem()->MoveFile(old_path, new_path); | |
145 } | |
146 | |
147 static bool IsFolder(const Pathname& pathname) { | |
148 return EnsureDefaultFilesystem()->IsFolder(pathname); | |
149 } | |
150 | |
151 static bool IsFile(const Pathname &pathname) { | |
152 return EnsureDefaultFilesystem()->IsFile(pathname); | |
153 } | |
154 | |
155 static bool IsAbsent(const Pathname &pathname) { | |
156 return EnsureDefaultFilesystem()->IsAbsent(pathname); | |
157 } | |
158 | |
159 static bool GetTemporaryFolder(Pathname &path, bool create, | |
160 const std::string *append) { | |
161 return EnsureDefaultFilesystem()->GetTemporaryFolder(path, create, append); | |
162 } | |
163 | |
164 static std::string TempFilename(const Pathname &dir, | |
165 const std::string &prefix) { | |
166 return EnsureDefaultFilesystem()->TempFilename(dir, prefix); | |
167 } | |
168 | |
169 static bool GetFileSize(const Pathname& path, size_t* size) { | |
170 return EnsureDefaultFilesystem()->GetFileSize(path, size); | |
171 } | |
172 | |
173 private: | |
174 static FilesystemInterface* default_filesystem_; | |
175 | |
176 static FilesystemInterface *EnsureDefaultFilesystem(); | |
177 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem); | |
178 }; | |
179 | |
180 } // namespace rtc | |
181 | 19 |
182 #endif // WEBRTC_BASE_FILEUTILS_H_ | 20 #endif // WEBRTC_BASE_FILEUTILS_H_ |
OLD | NEW |