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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 // returns the name of the file currently pointed to | 110 // returns the name of the file currently pointed to |
111 std::string DirectoryIterator::Name() const { | 111 std::string DirectoryIterator::Name() const { |
112 #if defined(WEBRTC_WIN) | 112 #if defined(WEBRTC_WIN) |
113 return ToUtf8(data_.cFileName); | 113 return ToUtf8(data_.cFileName); |
114 #else | 114 #else |
115 RTC_DCHECK(dirent_); | 115 RTC_DCHECK(dirent_); |
116 return dirent_->d_name; | 116 return dirent_->d_name; |
117 #endif | 117 #endif |
118 } | 118 } |
119 | 119 |
120 // returns the size of the file currently pointed to | |
121 size_t DirectoryIterator::FileSize() const { | |
122 #if !defined(WEBRTC_WIN) | |
123 return stat_.st_size; | |
124 #else | |
125 return data_.nFileSizeLow; | |
126 #endif | |
127 } | |
128 | |
129 bool DirectoryIterator::OlderThan(int seconds) const { | |
130 time_t file_modify_time; | |
131 #if defined(WEBRTC_WIN) | |
132 FileTimeToUnixTime(data_.ftLastWriteTime, &file_modify_time); | |
133 #else | |
134 file_modify_time = stat_.st_mtime; | |
135 #endif | |
136 return time(NULL) - file_modify_time >= seconds; | |
137 } | |
138 | |
139 FilesystemInterface* Filesystem::default_filesystem_ = NULL; | 120 FilesystemInterface* Filesystem::default_filesystem_ = NULL; |
140 | 121 |
141 FilesystemInterface *Filesystem::EnsureDefaultFilesystem() { | 122 FilesystemInterface *Filesystem::EnsureDefaultFilesystem() { |
142 if (!default_filesystem_) { | 123 if (!default_filesystem_) { |
143 #if defined(WEBRTC_WIN) | 124 #if defined(WEBRTC_WIN) |
144 default_filesystem_ = new Win32Filesystem(); | 125 default_filesystem_ = new Win32Filesystem(); |
145 #else | 126 #else |
146 default_filesystem_ = new UnixFilesystem(); | 127 default_filesystem_ = new UnixFilesystem(); |
147 #endif | 128 #endif |
148 } | 129 } |
149 return default_filesystem_; | 130 return default_filesystem_; |
150 } | 131 } |
151 | 132 |
152 DirectoryIterator* FilesystemInterface::IterateDirectory() { | 133 DirectoryIterator* FilesystemInterface::IterateDirectory() { |
153 return new DirectoryIterator(); | 134 return new DirectoryIterator(); |
154 } | 135 } |
155 | 136 |
156 bool FilesystemInterface::CopyFolder(const Pathname &old_path, | |
157 const Pathname &new_path) { | |
158 bool success = true; | |
159 VERIFY(IsFolder(old_path)); | |
160 Pathname new_dir; | |
161 new_dir.SetFolder(new_path.pathname()); | |
162 Pathname old_dir; | |
163 old_dir.SetFolder(old_path.pathname()); | |
164 if (!CreateFolder(new_dir)) | |
165 return false; | |
166 DirectoryIterator *di = IterateDirectory(); | |
167 if (!di) | |
168 return false; | |
169 if (di->Iterate(old_dir.pathname())) { | |
170 do { | |
171 if (di->Name() == "." || di->Name() == "..") | |
172 continue; | |
173 Pathname source; | |
174 Pathname dest; | |
175 source.SetFolder(old_dir.pathname()); | |
176 dest.SetFolder(new_path.pathname()); | |
177 source.SetFilename(di->Name()); | |
178 dest.SetFilename(di->Name()); | |
179 if (!CopyFileOrFolder(source, dest)) | |
180 success = false; | |
181 } while (di->Next()); | |
182 } | |
183 delete di; | |
184 return success; | |
185 } | |
186 | |
187 bool FilesystemInterface::DeleteFolderContents(const Pathname &folder) { | 137 bool FilesystemInterface::DeleteFolderContents(const Pathname &folder) { |
188 bool success = true; | 138 bool success = true; |
189 VERIFY(IsFolder(folder)); | 139 VERIFY(IsFolder(folder)); |
190 DirectoryIterator *di = IterateDirectory(); | 140 DirectoryIterator *di = IterateDirectory(); |
191 if (!di) | 141 if (!di) |
192 return false; | 142 return false; |
193 if (di->Iterate(folder)) { | 143 if (di->Iterate(folder)) { |
194 do { | 144 do { |
195 if (di->Name() == "." || di->Name() == "..") | 145 if (di->Name() == "." || di->Name() == "..") |
196 continue; | 146 continue; |
(...skipping 13 matching lines...) Expand all Loading... |
210 } while (di->Next()); | 160 } while (di->Next()); |
211 } | 161 } |
212 delete di; | 162 delete di; |
213 return success; | 163 return success; |
214 } | 164 } |
215 | 165 |
216 bool FilesystemInterface::DeleteFolderAndContents(const Pathname& folder) { | 166 bool FilesystemInterface::DeleteFolderAndContents(const Pathname& folder) { |
217 return DeleteFolderContents(folder) && DeleteEmptyFolder(folder); | 167 return DeleteFolderContents(folder) && DeleteEmptyFolder(folder); |
218 } | 168 } |
219 | 169 |
220 bool FilesystemInterface::CleanAppTempFolder() { | |
221 Pathname path; | |
222 if (!GetAppTempFolder(&path)) | |
223 return false; | |
224 if (IsAbsent(path)) | |
225 return true; | |
226 if (!IsTemporaryPath(path)) { | |
227 ASSERT(false); | |
228 return false; | |
229 } | |
230 return DeleteFolderContents(path); | |
231 } | |
232 | |
233 Pathname Filesystem::GetCurrentDirectory() { | |
234 return EnsureDefaultFilesystem()->GetCurrentDirectory(); | |
235 } | |
236 | |
237 bool CreateUniqueFile(Pathname& path, bool create_empty) { | |
238 LOG(LS_INFO) << "Path " << path.pathname() << std::endl; | |
239 // If no folder is supplied, use the temporary folder | |
240 if (path.folder().empty()) { | |
241 Pathname temporary_path; | |
242 if (!Filesystem::GetTemporaryFolder(temporary_path, true, NULL)) { | |
243 printf("Get temp failed\n"); | |
244 return false; | |
245 } | |
246 path.SetFolder(temporary_path.pathname()); | |
247 } | |
248 | |
249 // If no filename is supplied, use a temporary name | |
250 if (path.filename().empty()) { | |
251 std::string folder(path.folder()); | |
252 std::string filename = Filesystem::TempFilename(folder, "gt"); | |
253 path.SetPathname(filename); | |
254 if (!create_empty) { | |
255 Filesystem::DeleteFile(path.pathname()); | |
256 } | |
257 return true; | |
258 } | |
259 | |
260 // Otherwise, create a unique name based on the given filename | |
261 // foo.txt -> foo-N.txt | |
262 const std::string basename = path.basename(); | |
263 const size_t MAX_VERSION = 100; | |
264 size_t version = 0; | |
265 while (version < MAX_VERSION) { | |
266 std::string pathname = path.pathname(); | |
267 | |
268 if (!Filesystem::IsFile(pathname)) { | |
269 if (create_empty) { | |
270 FileStream* fs = Filesystem::OpenFile(pathname, "w"); | |
271 delete fs; | |
272 } | |
273 return true; | |
274 } | |
275 version += 1; | |
276 char version_base[MAX_PATH]; | |
277 sprintfn(version_base, arraysize(version_base), "%s-%u", basename.c_str(), | |
278 version); | |
279 path.SetBasename(version_base); | |
280 } | |
281 return true; | |
282 } | |
283 | |
284 } // namespace rtc | 170 } // namespace rtc |
OLD | NEW |