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
#include "talk/base/win32.h"
34
#include "talk/base/pathutils.h"
35
#include "talk/base/fileutils.h"
36
#include "talk/base/stringutils.h"
37
#include "talk/base/stream.h"
39
#include "talk/base/unixfilesystem.h"
40
#include "talk/base/win32filesystem.h"
48
//////////////////////////
49
// Directory Iterator //
50
//////////////////////////
52
// A DirectoryIterator is created with a given directory. It originally points
53
// to the first file in the directory, and can be advanecd with Next(). This
54
// allows you to get information about each file.
57
DirectoryIterator::DirectoryIterator()
59
: handle_(INVALID_HANDLE_VALUE) {
61
: dir_(NULL), dirent_(NULL) {
66
DirectoryIterator::~DirectoryIterator() {
68
if (handle_ != INVALID_HANDLE_VALUE)
76
// Starts traversing a directory.
77
// dir is the directory to traverse
78
// returns true if the directory exists and is valid
79
bool DirectoryIterator::Iterate(const Pathname &dir) {
80
directory_ = dir.pathname();
82
if (handle_ != INVALID_HANDLE_VALUE)
84
std::string d = dir.pathname() + '*';
85
handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
86
if (handle_ == INVALID_HANDLE_VALUE)
91
dir_ = ::opendir(directory_.c_str());
94
dirent_ = readdir(dir_);
98
if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)
104
// Advances to the next file
105
// returns true if there were more files in the directory.
106
bool DirectoryIterator::Next() {
108
return ::FindNextFile(handle_, &data_) == TRUE;
110
dirent_ = ::readdir(dir_);
114
return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;
118
// returns true if the file currently pointed to is a directory
119
bool DirectoryIterator::IsDirectory() const {
121
return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
123
return S_ISDIR(stat_.st_mode);
127
// returns the name of the file currently pointed to
128
std::string DirectoryIterator::Name() const {
130
return ToUtf8(data_.cFileName);
132
assert(dirent_ != NULL);
133
return dirent_->d_name;
137
// returns the size of the file currently pointed to
138
size_t DirectoryIterator::FileSize() const {
140
return stat_.st_size;
142
return data_.nFileSizeLow;
146
// returns the last modified time of this file
147
time_t DirectoryIterator::FileModifyTime() const {
150
FileTimeToUnixTime(data_.ftLastWriteTime, &val);
153
return stat_.st_mtime;
157
FilesystemInterface* Filesystem::default_filesystem_ = NULL;
159
FilesystemInterface *Filesystem::EnsureDefaultFilesystem() {
160
if (!default_filesystem_) {
162
default_filesystem_ = new Win32Filesystem();
164
default_filesystem_ = new UnixFilesystem();
167
return default_filesystem_;
170
bool FilesystemInterface::CopyFolder(const Pathname &old_path,
171
const Pathname &new_path) {
173
VERIFY(IsFolder(old_path));
175
new_dir.SetFolder(new_path.pathname());
177
old_dir.SetFolder(old_path.pathname());
178
if (!CreateFolder(new_dir))
180
DirectoryIterator *di = IterateDirectory();
183
if (di->Iterate(old_dir.pathname())) {
185
if (di->Name() == "." || di->Name() == "..")
189
source.SetFolder(old_dir.pathname());
190
dest.SetFolder(new_path.pathname());
191
source.SetFilename(di->Name());
192
dest.SetFilename(di->Name());
193
if (!CopyFileOrFolder(source, dest))
195
} while (di->Next());
201
bool FilesystemInterface::DeleteFolderContents(const Pathname &folder) {
203
VERIFY(IsFolder(folder));
204
DirectoryIterator *di = IterateDirectory();
207
if (di->Iterate(folder)) {
209
if (di->Name() == "." || di->Name() == "..")
212
subdir.SetFolder(folder.pathname());
213
if (di->IsDirectory()) {
214
subdir.AppendFolder(di->Name());
215
if (!DeleteFolderAndContents(subdir)) {
219
subdir.SetFilename(di->Name());
220
if (!DeleteFile(subdir)) {
224
} while (di->Next());
230
bool FilesystemInterface::CleanAppTempFolder() {
232
if (!GetAppTempFolder(&path))
236
if (!IsTemporaryPath(path)) {
240
return DeleteFolderContents(path);
243
Pathname Filesystem::GetCurrentDirectory() {
244
return EnsureDefaultFilesystem()->GetCurrentDirectory();
247
bool CreateUniqueFile(Pathname& path, bool create_empty) {
248
LOG(LS_INFO) << "Path " << path.pathname() << std::endl;
249
// If no folder is supplied, use the temporary folder
250
if (path.folder().empty()) {
251
Pathname temporary_path;
252
if (!Filesystem::GetTemporaryFolder(temporary_path, true, NULL)) {
253
printf("Get temp failed\n");
256
path.SetFolder(temporary_path.pathname());
259
// If no filename is supplied, use a temporary name
260
if (path.filename().empty()) {
261
std::string folder(path.folder());
262
std::string filename = Filesystem::TempFilename(folder, "gt");
263
path.SetPathname(filename);
265
Filesystem::DeleteFile(path.pathname());
270
// Otherwise, create a unique name based on the given filename
271
// foo.txt -> foo-N.txt
272
const std::string basename = path.basename();
273
const size_t MAX_VERSION = 100;
275
while (version < MAX_VERSION) {
276
std::string pathname = path.pathname();
278
if (!Filesystem::IsFile(pathname)) {
280
FileStream* fs = Filesystem::OpenFile(pathname, "w");
286
char version_base[MAX_PATH];
287
sprintfn(version_base, ARRAY_SIZE(version_base), "%s-%u",
288
basename.c_str(), version);
289
path.SetBasename(version_base);
294
} // namespace talk_base