You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
8.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2006, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  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.
  15. *
  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.
  26. */
  27. #include <cassert>
  28. #ifdef WIN32
  29. #include "talk/base/win32.h"
  30. #endif
  31. #include "talk/base/pathutils.h"
  32. #include "talk/base/fileutils.h"
  33. #include "talk/base/stringutils.h"
  34. #include "talk/base/stream.h"
  35. #include "talk/base/unixfilesystem.h"
  36. #include "talk/base/win32filesystem.h"
  37. #ifndef WIN32
  38. #define MAX_PATH 260
  39. #endif
  40. namespace talk_base {
  41. //////////////////////////
  42. // Directory Iterator //
  43. //////////////////////////
  44. // A DirectoryIterator is created with a given directory. It originally points
  45. // to the first file in the directory, and can be advanecd with Next(). This
  46. // allows you to get information about each file.
  47. // Constructor
  48. DirectoryIterator::DirectoryIterator()
  49. #ifdef _WIN32
  50. : handle_(INVALID_HANDLE_VALUE) {
  51. #else
  52. : dir_(NULL), dirent_(NULL) {
  53. #endif
  54. }
  55. // Destructor
  56. DirectoryIterator::~DirectoryIterator() {
  57. #ifdef WIN32
  58. if (handle_ != INVALID_HANDLE_VALUE)
  59. ::FindClose(handle_);
  60. #else
  61. if (dir_)
  62. closedir(dir_);
  63. #endif
  64. }
  65. // Starts traversing a directory.
  66. // dir is the directory to traverse
  67. // returns true if the directory exists and is valid
  68. bool DirectoryIterator::Iterate(const Pathname &dir) {
  69. directory_ = dir.pathname();
  70. #ifdef WIN32
  71. if (handle_ != INVALID_HANDLE_VALUE)
  72. ::FindClose(handle_);
  73. std::string d = dir.pathname() + '*';
  74. handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
  75. if (handle_ == INVALID_HANDLE_VALUE)
  76. return false;
  77. #else
  78. if (dir_ != NULL)
  79. closedir(dir_);
  80. dir_ = ::opendir(directory_.c_str());
  81. if (dir_ == NULL)
  82. return false;
  83. dirent_ = readdir(dir_);
  84. if (dirent_ == NULL)
  85. return false;
  86. if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)
  87. return false;
  88. #endif
  89. return true;
  90. }
  91. // Advances to the next file
  92. // returns true if there were more files in the directory.
  93. bool DirectoryIterator::Next() {
  94. #ifdef WIN32
  95. return ::FindNextFile(handle_, &data_) == TRUE;
  96. #else
  97. dirent_ = ::readdir(dir_);
  98. if (dirent_ == NULL)
  99. return false;
  100. return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;
  101. #endif
  102. }
  103. // returns true if the file currently pointed to is a directory
  104. bool DirectoryIterator::IsDirectory() const {
  105. #ifdef WIN32
  106. return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
  107. #else
  108. return S_ISDIR(stat_.st_mode);
  109. #endif
  110. }
  111. // returns the name of the file currently pointed to
  112. std::string DirectoryIterator::Name() const {
  113. #ifdef WIN32
  114. return ToUtf8(data_.cFileName);
  115. #else
  116. assert(dirent_ != NULL);
  117. return dirent_->d_name;
  118. #endif
  119. }
  120. // returns the size of the file currently pointed to
  121. size_t DirectoryIterator::FileSize() const {
  122. #ifndef WIN32
  123. return stat_.st_size;
  124. #else
  125. return data_.nFileSizeLow;
  126. #endif
  127. }
  128. // returns the last modified time of this file
  129. time_t DirectoryIterator::FileModifyTime() const {
  130. #ifdef WIN32
  131. time_t val;
  132. FileTimeToUnixTime(data_.ftLastWriteTime, &val);
  133. return val;
  134. #else
  135. return stat_.st_mtime;
  136. #endif
  137. }
  138. FilesystemInterface* Filesystem::default_filesystem_ = NULL;
  139. FilesystemInterface *Filesystem::EnsureDefaultFilesystem() {
  140. if (!default_filesystem_) {
  141. #ifdef WIN32
  142. default_filesystem_ = new Win32Filesystem();
  143. #else
  144. default_filesystem_ = new UnixFilesystem();
  145. #endif
  146. }
  147. return default_filesystem_;
  148. }
  149. bool FilesystemInterface::CopyFolder(const Pathname &old_path,
  150. const Pathname &new_path) {
  151. bool success = true;
  152. VERIFY(IsFolder(old_path));
  153. Pathname new_dir;
  154. new_dir.SetFolder(new_path.pathname());
  155. Pathname old_dir;
  156. old_dir.SetFolder(old_path.pathname());
  157. if (!CreateFolder(new_dir))
  158. return false;
  159. DirectoryIterator *di = IterateDirectory();
  160. if (!di)
  161. return false;
  162. if (di->Iterate(old_dir.pathname())) {
  163. do {
  164. if (di->Name() == "." || di->Name() == "..")
  165. continue;
  166. Pathname source;
  167. Pathname dest;
  168. source.SetFolder(old_dir.pathname());
  169. dest.SetFolder(new_path.pathname());
  170. source.SetFilename(di->Name());
  171. dest.SetFilename(di->Name());
  172. if (!CopyFileOrFolder(source, dest))
  173. success = false;
  174. } while (di->Next());
  175. }
  176. delete di;
  177. return success;
  178. }
  179. bool FilesystemInterface::DeleteFolderContents(const Pathname &folder) {
  180. bool success = true;
  181. VERIFY(IsFolder(folder));
  182. DirectoryIterator *di = IterateDirectory();
  183. if (!di)
  184. return false;
  185. if (di->Iterate(folder)) {
  186. do {
  187. if (di->Name() == "." || di->Name() == "..")
  188. continue;
  189. Pathname subdir;
  190. subdir.SetFolder(folder.pathname());
  191. if (di->IsDirectory()) {
  192. subdir.AppendFolder(di->Name());
  193. if (!DeleteFolderAndContents(subdir)) {
  194. success = false;
  195. }
  196. } else {
  197. subdir.SetFilename(di->Name());
  198. if (!DeleteFile(subdir)) {
  199. success = false;
  200. }
  201. }
  202. } while (di->Next());
  203. }
  204. delete di;
  205. return success;
  206. }
  207. bool FilesystemInterface::CleanAppTempFolder() {
  208. Pathname path;
  209. if (!GetAppTempFolder(&path))
  210. return false;
  211. if (IsAbsent(path))
  212. return true;
  213. if (!IsTemporaryPath(path)) {
  214. ASSERT(false);
  215. return false;
  216. }
  217. return DeleteFolderContents(path);
  218. }
  219. Pathname Filesystem::GetCurrentDirectory() {
  220. return EnsureDefaultFilesystem()->GetCurrentDirectory();
  221. }
  222. bool CreateUniqueFile(Pathname& path, bool create_empty) {
  223. LOG(LS_INFO) << "Path " << path.pathname() << std::endl;
  224. // If no folder is supplied, use the temporary folder
  225. if (path.folder().empty()) {
  226. Pathname temporary_path;
  227. if (!Filesystem::GetTemporaryFolder(temporary_path, true, NULL)) {
  228. printf("Get temp failed\n");
  229. return false;
  230. }
  231. path.SetFolder(temporary_path.pathname());
  232. }
  233. // If no filename is supplied, use a temporary name
  234. if (path.filename().empty()) {
  235. std::string folder(path.folder());
  236. std::string filename = Filesystem::TempFilename(folder, "gt");
  237. path.SetPathname(filename);
  238. if (!create_empty) {
  239. Filesystem::DeleteFile(path.pathname());
  240. }
  241. return true;
  242. }
  243. // Otherwise, create a unique name based on the given filename
  244. // foo.txt -> foo-N.txt
  245. const std::string basename = path.basename();
  246. const size_t MAX_VERSION = 100;
  247. size_t version = 0;
  248. while (version < MAX_VERSION) {
  249. std::string pathname = path.pathname();
  250. if (!Filesystem::IsFile(pathname)) {
  251. if (create_empty) {
  252. FileStream* fs = Filesystem::OpenFile(pathname, "w");
  253. delete fs;
  254. }
  255. return true;
  256. }
  257. version += 1;
  258. char version_base[MAX_PATH];
  259. sprintfn(version_base, ARRAY_SIZE(version_base), "%s-%u",
  260. basename.c_str(), version);
  261. path.SetBasename(version_base);
  262. }
  263. return true;
  264. }
  265. } // namespace talk_base