henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
kwiberg | 22487b2 | 2016-09-13 01:17:10 -0700 | [diff] [blame] | 11 | #include "webrtc/base/fileutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 13 | #include "webrtc/base/arraysize.h" |
kwiberg | 22487b2 | 2016-09-13 01:17:10 -0700 | [diff] [blame] | 14 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include "webrtc/base/pathutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | #include "webrtc/base/stringutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
| 28 | namespace 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 |
| 39 | DirectoryIterator::DirectoryIterator() |
| 40 | #ifdef WEBRTC_WIN |
| 41 | : handle_(INVALID_HANDLE_VALUE) { |
| 42 | #else |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 43 | : dir_(nullptr), |
| 44 | dirent_(nullptr){ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | #endif |
| 46 | } |
| 47 | |
| 48 | // Destructor |
| 49 | DirectoryIterator::~DirectoryIterator() { |
| 50 | #if defined(WEBRTC_WIN) |
| 51 | if (handle_ != INVALID_HANDLE_VALUE) |
| 52 | ::FindClose(handle_); |
| 53 | #else |
| 54 | if (dir_) |
| 55 | closedir(dir_); |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | // Starts traversing a directory. |
| 60 | // dir is the directory to traverse |
| 61 | // returns true if the directory exists and is valid |
| 62 | bool DirectoryIterator::Iterate(const Pathname &dir) { |
| 63 | directory_ = dir.pathname(); |
| 64 | #if defined(WEBRTC_WIN) |
| 65 | if (handle_ != INVALID_HANDLE_VALUE) |
| 66 | ::FindClose(handle_); |
| 67 | std::string d = dir.pathname() + '*'; |
| 68 | handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_); |
| 69 | if (handle_ == INVALID_HANDLE_VALUE) |
| 70 | return false; |
| 71 | #else |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 72 | if (dir_ != nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | closedir(dir_); |
| 74 | dir_ = ::opendir(directory_.c_str()); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 75 | if (dir_ == nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | return false; |
| 77 | dirent_ = readdir(dir_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 78 | if (dirent_ == nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | return false; |
| 80 | |
| 81 | if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0) |
| 82 | return false; |
| 83 | #endif |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | // Advances to the next file |
| 88 | // returns true if there were more files in the directory. |
| 89 | bool DirectoryIterator::Next() { |
| 90 | #if defined(WEBRTC_WIN) |
| 91 | return ::FindNextFile(handle_, &data_) == TRUE; |
| 92 | #else |
| 93 | dirent_ = ::readdir(dir_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 94 | if (dirent_ == nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | return false; |
| 96 | |
| 97 | return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0; |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | // returns true if the file currently pointed to is a directory |
| 102 | bool DirectoryIterator::IsDirectory() const { |
| 103 | #if defined(WEBRTC_WIN) |
| 104 | return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE; |
| 105 | #else |
| 106 | return S_ISDIR(stat_.st_mode); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | // returns the name of the file currently pointed to |
| 111 | std::string DirectoryIterator::Name() const { |
| 112 | #if defined(WEBRTC_WIN) |
| 113 | return ToUtf8(data_.cFileName); |
| 114 | #else |
kwiberg | 22487b2 | 2016-09-13 01:17:10 -0700 | [diff] [blame] | 115 | RTC_DCHECK(dirent_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | return dirent_->d_name; |
| 117 | #endif |
| 118 | } |
| 119 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 120 | FilesystemInterface* Filesystem::default_filesystem_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 121 | |
| 122 | FilesystemInterface *Filesystem::EnsureDefaultFilesystem() { |
| 123 | if (!default_filesystem_) { |
| 124 | #if defined(WEBRTC_WIN) |
| 125 | default_filesystem_ = new Win32Filesystem(); |
| 126 | #else |
| 127 | default_filesystem_ = new UnixFilesystem(); |
| 128 | #endif |
| 129 | } |
| 130 | return default_filesystem_; |
| 131 | } |
| 132 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 133 | DirectoryIterator* FilesystemInterface::IterateDirectory() { |
| 134 | return new DirectoryIterator(); |
| 135 | } |
| 136 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | bool FilesystemInterface::DeleteFolderContents(const Pathname &folder) { |
| 138 | bool success = true; |
nisse | 989ec09 | 2017-01-10 03:44:41 -0800 | [diff] [blame] | 139 | RTC_CHECK(IsFolder(folder)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 140 | DirectoryIterator *di = IterateDirectory(); |
| 141 | if (!di) |
| 142 | return false; |
| 143 | if (di->Iterate(folder)) { |
| 144 | do { |
| 145 | if (di->Name() == "." || di->Name() == "..") |
| 146 | continue; |
| 147 | Pathname subdir; |
| 148 | subdir.SetFolder(folder.pathname()); |
| 149 | if (di->IsDirectory()) { |
| 150 | subdir.AppendFolder(di->Name()); |
| 151 | if (!DeleteFolderAndContents(subdir)) { |
| 152 | success = false; |
| 153 | } |
| 154 | } else { |
| 155 | subdir.SetFilename(di->Name()); |
| 156 | if (!DeleteFile(subdir)) { |
| 157 | success = false; |
| 158 | } |
| 159 | } |
| 160 | } while (di->Next()); |
| 161 | } |
| 162 | delete di; |
| 163 | return success; |
| 164 | } |
| 165 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 166 | bool FilesystemInterface::DeleteFolderAndContents(const Pathname& folder) { |
| 167 | return DeleteFolderContents(folder) && DeleteEmptyFolder(folder); |
| 168 | } |
| 169 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 170 | } // namespace rtc |