3 * Copyright 2004--2006, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // TODO(grunell): Remove io.h includes when Chromium has started
32 // to use AEC in each source. http://crbug.com/264611.
34 #include "talk/base/win32.h"
37 #include "talk/base/pathutils.h"
38 #include "talk/base/fileutils.h"
39 #include "talk/base/stringutils.h"
40 #include "talk/base/stream.h"
43 #include "talk/base/win32filesystem.h"
45 #include "talk/base/unixfilesystem.h"
54 //////////////////////////
55 // Directory Iterator //
56 //////////////////////////
58 // A DirectoryIterator is created with a given directory. It originally points
59 // to the first file in the directory, and can be advanecd with Next(). This
60 // allows you to get information about each file.
63 DirectoryIterator::DirectoryIterator()
65 : handle_(INVALID_HANDLE_VALUE) {
67 : dir_(NULL), dirent_(NULL) {
72 DirectoryIterator::~DirectoryIterator() {
74 if (handle_ != INVALID_HANDLE_VALUE)
82 // Starts traversing a directory.
83 // dir is the directory to traverse
84 // returns true if the directory exists and is valid
85 bool DirectoryIterator::Iterate(const Pathname &dir) {
86 directory_ = dir.pathname();
88 if (handle_ != INVALID_HANDLE_VALUE)
90 std::string d = dir.pathname() + '*';
91 handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
92 if (handle_ == INVALID_HANDLE_VALUE)
97 dir_ = ::opendir(directory_.c_str());
100 dirent_ = readdir(dir_);
104 if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)
110 // Advances to the next file
111 // returns true if there were more files in the directory.
112 bool DirectoryIterator::Next() {
114 return ::FindNextFile(handle_, &data_) == TRUE;
116 dirent_ = ::readdir(dir_);
120 return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;
124 // returns true if the file currently pointed to is a directory
125 bool DirectoryIterator::IsDirectory() const {
127 return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
129 return S_ISDIR(stat_.st_mode);
133 // returns the name of the file currently pointed to
134 std::string DirectoryIterator::Name() const {
136 return ToUtf8(data_.cFileName);
138 assert(dirent_ != NULL);
139 return dirent_->d_name;
143 // returns the size of the file currently pointed to
144 size_t DirectoryIterator::FileSize() const {
146 return stat_.st_size;
148 return data_.nFileSizeLow;
152 // returns the last modified time of this file
153 time_t DirectoryIterator::FileModifyTime() const {
156 FileTimeToUnixTime(data_.ftLastWriteTime, &val);
159 return stat_.st_mtime;
163 FilesystemInterface* Filesystem::default_filesystem_ = NULL;
165 FilesystemInterface *Filesystem::EnsureDefaultFilesystem() {
166 if (!default_filesystem_) {
168 default_filesystem_ = new Win32Filesystem();
170 default_filesystem_ = new UnixFilesystem();
173 return default_filesystem_;
176 bool FilesystemInterface::CopyFolder(const Pathname &old_path,
177 const Pathname &new_path) {
179 VERIFY(IsFolder(old_path));
181 new_dir.SetFolder(new_path.pathname());
183 old_dir.SetFolder(old_path.pathname());
184 if (!CreateFolder(new_dir))
186 DirectoryIterator *di = IterateDirectory();
189 if (di->Iterate(old_dir.pathname())) {
191 if (di->Name() == "." || di->Name() == "..")
195 source.SetFolder(old_dir.pathname());
196 dest.SetFolder(new_path.pathname());
197 source.SetFilename(di->Name());
198 dest.SetFilename(di->Name());
199 if (!CopyFileOrFolder(source, dest))
201 } while (di->Next());
207 bool FilesystemInterface::DeleteFolderContents(const Pathname &folder) {
209 VERIFY(IsFolder(folder));
210 DirectoryIterator *di = IterateDirectory();
213 if (di->Iterate(folder)) {
215 if (di->Name() == "." || di->Name() == "..")
218 subdir.SetFolder(folder.pathname());
219 if (di->IsDirectory()) {
220 subdir.AppendFolder(di->Name());
221 if (!DeleteFolderAndContents(subdir)) {
225 subdir.SetFilename(di->Name());
226 if (!DeleteFile(subdir)) {
230 } while (di->Next());
236 bool FilesystemInterface::CleanAppTempFolder() {
238 if (!GetAppTempFolder(&path))
242 if (!IsTemporaryPath(path)) {
246 return DeleteFolderContents(path);
249 Pathname Filesystem::GetCurrentDirectory() {
250 return EnsureDefaultFilesystem()->GetCurrentDirectory();
253 bool CreateUniqueFile(Pathname& path, bool create_empty) {
254 LOG(LS_INFO) << "Path " << path.pathname() << std::endl;
255 // If no folder is supplied, use the temporary folder
256 if (path.folder().empty()) {
257 Pathname temporary_path;
258 if (!Filesystem::GetTemporaryFolder(temporary_path, true, NULL)) {
259 printf("Get temp failed\n");
262 path.SetFolder(temporary_path.pathname());
265 // If no filename is supplied, use a temporary name
266 if (path.filename().empty()) {
267 std::string folder(path.folder());
268 std::string filename = Filesystem::TempFilename(folder, "gt");
269 path.SetPathname(filename);
271 Filesystem::DeleteFile(path.pathname());
276 // Otherwise, create a unique name based on the given filename
277 // foo.txt -> foo-N.txt
278 const std::string basename = path.basename();
279 const size_t MAX_VERSION = 100;
281 while (version < MAX_VERSION) {
282 std::string pathname = path.pathname();
284 if (!Filesystem::IsFile(pathname)) {
286 FileStream* fs = Filesystem::OpenFile(pathname, "w");
292 char version_base[MAX_PATH];
293 sprintfn(version_base, ARRAY_SIZE(version_base), "%s-%u",
294 basename.c_str(), version);
295 path.SetBasename(version_base);
300 // Taken from Chromium's base/platform_file_*.cc.
301 // TODO(grunell): Remove when Chromium has started to use AEC in each source.
302 // http://crbug.com/264611.
303 FILE* FdopenPlatformFileForWriting(PlatformFile file) {
305 if (file == kInvalidPlatformFileValue)
307 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0);
310 return _fdopen(fd, "w");
312 return fdopen(file, "w");
316 bool ClosePlatformFile(PlatformFile file) {
318 return CloseHandle(file) != 0;
324 } // namespace talk_base