blob: 6f385d72b7c6a6f148a8ed2c06f69d3514d2ba2e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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#include <assert.h>
12
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include "webrtc/base/pathutils.h"
14#include "webrtc/base/fileutils.h"
15#include "webrtc/base/stringutils.h"
16#include "webrtc/base/stream.h"
17
18#if defined(WEBRTC_WIN)
19#include "webrtc/base/win32filesystem.h"
20#else
21#include "webrtc/base/unixfilesystem.h"
22#endif
23
24#if !defined(WEBRTC_WIN)
25#define MAX_PATH 260
26#endif
27
28namespace rtc {
29
30//////////////////////////
31// Directory Iterator //
32//////////////////////////
33
34// A DirectoryIterator is created with a given directory. It originally points
35// to the first file in the directory, and can be advanecd with Next(). This
36// allows you to get information about each file.
37
38 // Constructor
39DirectoryIterator::DirectoryIterator()
40#ifdef WEBRTC_WIN
41 : handle_(INVALID_HANDLE_VALUE) {
42#else
43 : dir_(NULL), dirent_(NULL) {
44#endif
45}
46
47 // Destructor
48DirectoryIterator::~DirectoryIterator() {
49#if defined(WEBRTC_WIN)
50 if (handle_ != INVALID_HANDLE_VALUE)
51 ::FindClose(handle_);
52#else
53 if (dir_)
54 closedir(dir_);
55#endif
56}
57
58 // Starts traversing a directory.
59 // dir is the directory to traverse
60 // returns true if the directory exists and is valid
61bool DirectoryIterator::Iterate(const Pathname &dir) {
62 directory_ = dir.pathname();
63#if defined(WEBRTC_WIN)
64 if (handle_ != INVALID_HANDLE_VALUE)
65 ::FindClose(handle_);
66 std::string d = dir.pathname() + '*';
67 handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
68 if (handle_ == INVALID_HANDLE_VALUE)
69 return false;
70#else
71 if (dir_ != NULL)
72 closedir(dir_);
73 dir_ = ::opendir(directory_.c_str());
74 if (dir_ == NULL)
75 return false;
76 dirent_ = readdir(dir_);
77 if (dirent_ == NULL)
78 return false;
79
80 if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)
81 return false;
82#endif
83 return true;
84}
85
86 // Advances to the next file
87 // returns true if there were more files in the directory.
88bool DirectoryIterator::Next() {
89#if defined(WEBRTC_WIN)
90 return ::FindNextFile(handle_, &data_) == TRUE;
91#else
92 dirent_ = ::readdir(dir_);
93 if (dirent_ == NULL)
94 return false;
95
96 return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;
97#endif
98}
99
100 // returns true if the file currently pointed to is a directory
101bool DirectoryIterator::IsDirectory() const {
102#if defined(WEBRTC_WIN)
103 return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
104#else
105 return S_ISDIR(stat_.st_mode);
106#endif
107}
108
109 // returns the name of the file currently pointed to
110std::string DirectoryIterator::Name() const {
111#if defined(WEBRTC_WIN)
112 return ToUtf8(data_.cFileName);
113#else
114 assert(dirent_ != NULL);
115 return dirent_->d_name;
116#endif
117}
118
119 // returns the size of the file currently pointed to
120size_t DirectoryIterator::FileSize() const {
121#if !defined(WEBRTC_WIN)
122 return stat_.st_size;
123#else
124 return data_.nFileSizeLow;
125#endif
126}
127
henrike@webrtc.org961293d2014-07-25 21:58:50 +0000128bool DirectoryIterator::OlderThan(int seconds) const {
129 time_t file_modify_time;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130#if defined(WEBRTC_WIN)
henrike@webrtc.org961293d2014-07-25 21:58:50 +0000131 FileTimeToUnixTime(data_.ftLastWriteTime, &file_modify_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132#else
henrike@webrtc.org961293d2014-07-25 21:58:50 +0000133 file_modify_time = stat_.st_mtime;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134#endif
henrike@webrtc.org961293d2014-07-25 21:58:50 +0000135 return TimeDiff(time(NULL), file_modify_time) >= seconds;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136}
137
138FilesystemInterface* Filesystem::default_filesystem_ = NULL;
139
140FilesystemInterface *Filesystem::EnsureDefaultFilesystem() {
141 if (!default_filesystem_) {
142#if defined(WEBRTC_WIN)
143 default_filesystem_ = new Win32Filesystem();
144#else
145 default_filesystem_ = new UnixFilesystem();
146#endif
147 }
148 return default_filesystem_;
149}
150
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000151DirectoryIterator* FilesystemInterface::IterateDirectory() {
152 return new DirectoryIterator();
153}
154
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000155bool FilesystemInterface::CopyFolder(const Pathname &old_path,
156 const Pathname &new_path) {
157 bool success = true;
158 VERIFY(IsFolder(old_path));
159 Pathname new_dir;
160 new_dir.SetFolder(new_path.pathname());
161 Pathname old_dir;
162 old_dir.SetFolder(old_path.pathname());
163 if (!CreateFolder(new_dir))
164 return false;
165 DirectoryIterator *di = IterateDirectory();
166 if (!di)
167 return false;
168 if (di->Iterate(old_dir.pathname())) {
169 do {
170 if (di->Name() == "." || di->Name() == "..")
171 continue;
172 Pathname source;
173 Pathname dest;
174 source.SetFolder(old_dir.pathname());
175 dest.SetFolder(new_path.pathname());
176 source.SetFilename(di->Name());
177 dest.SetFilename(di->Name());
178 if (!CopyFileOrFolder(source, dest))
179 success = false;
180 } while (di->Next());
181 }
182 delete di;
183 return success;
184}
185
186bool FilesystemInterface::DeleteFolderContents(const Pathname &folder) {
187 bool success = true;
188 VERIFY(IsFolder(folder));
189 DirectoryIterator *di = IterateDirectory();
190 if (!di)
191 return false;
192 if (di->Iterate(folder)) {
193 do {
194 if (di->Name() == "." || di->Name() == "..")
195 continue;
196 Pathname subdir;
197 subdir.SetFolder(folder.pathname());
198 if (di->IsDirectory()) {
199 subdir.AppendFolder(di->Name());
200 if (!DeleteFolderAndContents(subdir)) {
201 success = false;
202 }
203 } else {
204 subdir.SetFilename(di->Name());
205 if (!DeleteFile(subdir)) {
206 success = false;
207 }
208 }
209 } while (di->Next());
210 }
211 delete di;
212 return success;
213}
214
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000215bool FilesystemInterface::DeleteFolderAndContents(const Pathname& folder) {
216 return DeleteFolderContents(folder) && DeleteEmptyFolder(folder);
217}
218
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219bool FilesystemInterface::CleanAppTempFolder() {
220 Pathname path;
221 if (!GetAppTempFolder(&path))
222 return false;
223 if (IsAbsent(path))
224 return true;
225 if (!IsTemporaryPath(path)) {
226 ASSERT(false);
227 return false;
228 }
229 return DeleteFolderContents(path);
230}
231
232Pathname Filesystem::GetCurrentDirectory() {
233 return EnsureDefaultFilesystem()->GetCurrentDirectory();
234}
235
236bool CreateUniqueFile(Pathname& path, bool create_empty) {
237 LOG(LS_INFO) << "Path " << path.pathname() << std::endl;
238 // If no folder is supplied, use the temporary folder
239 if (path.folder().empty()) {
240 Pathname temporary_path;
241 if (!Filesystem::GetTemporaryFolder(temporary_path, true, NULL)) {
242 printf("Get temp failed\n");
243 return false;
244 }
245 path.SetFolder(temporary_path.pathname());
246 }
247
248 // If no filename is supplied, use a temporary name
249 if (path.filename().empty()) {
250 std::string folder(path.folder());
251 std::string filename = Filesystem::TempFilename(folder, "gt");
252 path.SetPathname(filename);
253 if (!create_empty) {
254 Filesystem::DeleteFile(path.pathname());
255 }
256 return true;
257 }
258
259 // Otherwise, create a unique name based on the given filename
260 // foo.txt -> foo-N.txt
261 const std::string basename = path.basename();
262 const size_t MAX_VERSION = 100;
263 size_t version = 0;
264 while (version < MAX_VERSION) {
265 std::string pathname = path.pathname();
266
267 if (!Filesystem::IsFile(pathname)) {
268 if (create_empty) {
269 FileStream* fs = Filesystem::OpenFile(pathname, "w");
270 delete fs;
271 }
272 return true;
273 }
274 version += 1;
275 char version_base[MAX_PATH];
276 sprintfn(version_base, ARRAY_SIZE(version_base), "%s-%u",
277 basename.c_str(), version);
278 path.SetBasename(version_base);
279 }
280 return true;
281}
282
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283} // namespace rtc