2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
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.
13 #include "webrtc/base/pathutils.h"
14 #include "webrtc/base/fileutils.h"
15 #include "webrtc/base/stringutils.h"
16 #include "webrtc/base/stream.h"
18 #if defined(WEBRTC_WIN)
19 #include "webrtc/base/win32filesystem.h"
21 #include "webrtc/base/unixfilesystem.h"
24 #if !defined(WEBRTC_WIN)
30 //////////////////////////
31 // Directory Iterator //
32 //////////////////////////
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.
39 DirectoryIterator::DirectoryIterator()
41 : handle_(INVALID_HANDLE_VALUE) {
43 : dir_(NULL), dirent_(NULL) {
48 DirectoryIterator::~DirectoryIterator() {
49 #if defined(WEBRTC_WIN)
50 if (handle_ != INVALID_HANDLE_VALUE)
58 // Starts traversing a directory.
59 // dir is the directory to traverse
60 // returns true if the directory exists and is valid
61 bool DirectoryIterator::Iterate(const Pathname &dir) {
62 directory_ = dir.pathname();
63 #if defined(WEBRTC_WIN)
64 if (handle_ != INVALID_HANDLE_VALUE)
66 std::string d = dir.pathname() + '*';
67 handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
68 if (handle_ == INVALID_HANDLE_VALUE)
73 dir_ = ::opendir(directory_.c_str());
76 dirent_ = readdir(dir_);
80 if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)
86 // Advances to the next file
87 // returns true if there were more files in the directory.
88 bool DirectoryIterator::Next() {
89 #if defined(WEBRTC_WIN)
90 return ::FindNextFile(handle_, &data_) == TRUE;
92 dirent_ = ::readdir(dir_);
96 return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;
100 // returns true if the file currently pointed to is a directory
101 bool DirectoryIterator::IsDirectory() const {
102 #if defined(WEBRTC_WIN)
103 return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
105 return S_ISDIR(stat_.st_mode);
109 // returns the name of the file currently pointed to
110 std::string DirectoryIterator::Name() const {
111 #if defined(WEBRTC_WIN)
112 return ToUtf8(data_.cFileName);
114 assert(dirent_ != NULL);
115 return dirent_->d_name;
119 // returns the size of the file currently pointed to
120 size_t DirectoryIterator::FileSize() const {
121 #if !defined(WEBRTC_WIN)
122 return stat_.st_size;
124 return data_.nFileSizeLow;
128 bool DirectoryIterator::OlderThan(int seconds) const {
129 time_t file_modify_time;
130 #if defined(WEBRTC_WIN)
131 FileTimeToUnixTime(data_.ftLastWriteTime, &file_modify_time);
133 file_modify_time = stat_.st_mtime;
135 return TimeDiff(time(NULL), file_modify_time) >= seconds;
138 FilesystemInterface* Filesystem::default_filesystem_ = NULL;
140 FilesystemInterface *Filesystem::EnsureDefaultFilesystem() {
141 if (!default_filesystem_) {
142 #if defined(WEBRTC_WIN)
143 default_filesystem_ = new Win32Filesystem();
145 default_filesystem_ = new UnixFilesystem();
148 return default_filesystem_;
151 bool FilesystemInterface::CopyFolder(const Pathname &old_path,
152 const Pathname &new_path) {
154 VERIFY(IsFolder(old_path));
156 new_dir.SetFolder(new_path.pathname());
158 old_dir.SetFolder(old_path.pathname());
159 if (!CreateFolder(new_dir))
161 DirectoryIterator *di = IterateDirectory();
164 if (di->Iterate(old_dir.pathname())) {
166 if (di->Name() == "." || di->Name() == "..")
170 source.SetFolder(old_dir.pathname());
171 dest.SetFolder(new_path.pathname());
172 source.SetFilename(di->Name());
173 dest.SetFilename(di->Name());
174 if (!CopyFileOrFolder(source, dest))
176 } while (di->Next());
182 bool FilesystemInterface::DeleteFolderContents(const Pathname &folder) {
184 VERIFY(IsFolder(folder));
185 DirectoryIterator *di = IterateDirectory();
188 if (di->Iterate(folder)) {
190 if (di->Name() == "." || di->Name() == "..")
193 subdir.SetFolder(folder.pathname());
194 if (di->IsDirectory()) {
195 subdir.AppendFolder(di->Name());
196 if (!DeleteFolderAndContents(subdir)) {
200 subdir.SetFilename(di->Name());
201 if (!DeleteFile(subdir)) {
205 } while (di->Next());
211 bool FilesystemInterface::CleanAppTempFolder() {
213 if (!GetAppTempFolder(&path))
217 if (!IsTemporaryPath(path)) {
221 return DeleteFolderContents(path);
224 Pathname Filesystem::GetCurrentDirectory() {
225 return EnsureDefaultFilesystem()->GetCurrentDirectory();
228 bool CreateUniqueFile(Pathname& path, bool create_empty) {
229 LOG(LS_INFO) << "Path " << path.pathname() << std::endl;
230 // If no folder is supplied, use the temporary folder
231 if (path.folder().empty()) {
232 Pathname temporary_path;
233 if (!Filesystem::GetTemporaryFolder(temporary_path, true, NULL)) {
234 printf("Get temp failed\n");
237 path.SetFolder(temporary_path.pathname());
240 // If no filename is supplied, use a temporary name
241 if (path.filename().empty()) {
242 std::string folder(path.folder());
243 std::string filename = Filesystem::TempFilename(folder, "gt");
244 path.SetPathname(filename);
246 Filesystem::DeleteFile(path.pathname());
251 // Otherwise, create a unique name based on the given filename
252 // foo.txt -> foo-N.txt
253 const std::string basename = path.basename();
254 const size_t MAX_VERSION = 100;
256 while (version < MAX_VERSION) {
257 std::string pathname = path.pathname();
259 if (!Filesystem::IsFile(pathname)) {
261 FileStream* fs = Filesystem::OpenFile(pathname, "w");
267 char version_base[MAX_PATH];
268 sprintfn(version_base, ARRAY_SIZE(version_base), "%s-%u",
269 basename.c_str(), version);
270 path.SetBasename(version_base);