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.

457 lines
16 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. #ifndef TALK_BASE_FILEUTILS_H_
  28. #define TALK_BASE_FILEUTILS_H_
  29. #include <string>
  30. #ifdef WIN32
  31. #include "talk/base/win32.h"
  32. #else
  33. #include <sys/types.h>
  34. #include <dirent.h>
  35. #include <sys/stat.h>
  36. #include <unistd.h>
  37. #endif
  38. #include "talk/base/basictypes.h"
  39. #include "talk/base/common.h"
  40. #include "talk/base/scoped_ptr.h"
  41. namespace talk_base {
  42. class FileStream;
  43. class Pathname;
  44. //////////////////////////
  45. // Directory Iterator //
  46. //////////////////////////
  47. // A DirectoryIterator is created with a given directory. It originally points
  48. // to the first file in the directory, and can be advanecd with Next(). This
  49. // allows you to get information about each file.
  50. class DirectoryIterator {
  51. friend class Filesystem;
  52. public:
  53. // Constructor
  54. DirectoryIterator();
  55. // Destructor
  56. virtual ~DirectoryIterator();
  57. // Starts traversing a directory
  58. // dir is the directory to traverse
  59. // returns true if the directory exists and is valid
  60. // The iterator will point to the first entry in the directory
  61. virtual bool Iterate(const Pathname &path);
  62. // Advances to the next file
  63. // returns true if there were more files in the directory.
  64. virtual bool Next();
  65. // returns true if the file currently pointed to is a directory
  66. virtual bool IsDirectory() const;
  67. // returns the name of the file currently pointed to
  68. virtual std::string Name() const;
  69. // returns the size of the file currently pointed to
  70. virtual size_t FileSize() const;
  71. // returns the last modified time of the file currently pointed to
  72. virtual time_t FileModifyTime() const;
  73. // checks whether current file is a special directory file "." or ".."
  74. bool IsDots() const {
  75. std::string filename(Name());
  76. return (filename.compare(".") == 0) || (filename.compare("..") == 0);
  77. }
  78. private:
  79. std::string directory_;
  80. #ifdef WIN32
  81. WIN32_FIND_DATA data_;
  82. HANDLE handle_;
  83. #else
  84. DIR *dir_;
  85. struct dirent *dirent_;
  86. struct stat stat_;
  87. #endif
  88. };
  89. enum FileTimeType { FTT_CREATED, FTT_MODIFIED, FTT_ACCESSED };
  90. class FilesystemInterface {
  91. public:
  92. virtual ~FilesystemInterface() {}
  93. // Returns a DirectoryIterator for a given pathname.
  94. // TODO: Do fancy abstracted stuff
  95. virtual DirectoryIterator *IterateDirectory() {
  96. return new DirectoryIterator();
  97. }
  98. // Opens a file. Returns an open StreamInterface if function succeeds.
  99. // Otherwise, returns NULL.
  100. // TODO: Add an error param to indicate failure reason, similar to
  101. // FileStream::Open
  102. virtual FileStream *OpenFile(const Pathname &filename,
  103. const std::string &mode) = 0;
  104. // Atomically creates an empty file accessible only to the current user if one
  105. // does not already exist at the given path, otherwise fails. This is the only
  106. // secure way to create a file in a shared temp directory (e.g., C:\Temp on
  107. // Windows or /tmp on Linux).
  108. // Note that if it is essential that a file be successfully created then the
  109. // app must generate random names and retry on failure, or else it will be
  110. // vulnerable to a trivial DoS.
  111. virtual bool CreatePrivateFile(const Pathname &filename) = 0;
  112. // This will attempt to delete the path located at filename.
  113. // It ASSERTS and returns false if the path points to a folder or a
  114. // non-existent file.
  115. virtual bool DeleteFile(const Pathname &filename) = 0;
  116. // This will attempt to delete the empty folder located at 'folder'
  117. // It ASSERTS and returns false if the path points to a file or a non-existent
  118. // folder. It fails normally if the folder is not empty or can otherwise
  119. // not be deleted.
  120. virtual bool DeleteEmptyFolder(const Pathname &folder) = 0;
  121. // This will call IterateDirectory, to get a directory iterator, and then
  122. // call DeleteFolderAndContents and DeleteFile on every path contained in this
  123. // folder. If the folder is empty, this returns true.
  124. virtual bool DeleteFolderContents(const Pathname &folder);
  125. // This deletes the contents of a folder, recursively, and then deletes
  126. // the folder itself.
  127. virtual bool DeleteFolderAndContents(const Pathname &folder) {
  128. return DeleteFolderContents(folder) && DeleteEmptyFolder(folder);
  129. }
  130. // This will delete whatever is located at path, be it a file or a folder.
  131. // If it is a folder, it will delete it recursively by calling
  132. // DeleteFolderAndContents
  133. bool DeleteFileOrFolder(const Pathname &path) {
  134. if (IsFolder(path))
  135. return DeleteFolderAndContents(path);
  136. else
  137. return DeleteFile(path);
  138. }
  139. // Creates a directory. This will call itself recursively to create /foo/bar
  140. // even if /foo does not exist. Returns true if the function succeeds.
  141. virtual bool CreateFolder(const Pathname &pathname) = 0;
  142. // This moves a file from old_path to new_path, where "old_path" is a
  143. // plain file. This ASSERTs and returns false if old_path points to a
  144. // directory, and returns true if the function succeeds.
  145. // If the new path is on a different volume than the old path, this function
  146. // will attempt to copy and, if that succeeds, delete the old path.
  147. virtual bool MoveFolder(const Pathname &old_path,
  148. const Pathname &new_path) = 0;
  149. // This moves a directory from old_path to new_path, where "old_path" is a
  150. // directory. This ASSERTs and returns false if old_path points to a plain
  151. // file, and returns true if the function succeeds.
  152. // If the new path is on a different volume, this function will attempt to
  153. // copy and if that succeeds, delete the old path.
  154. virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path) = 0;
  155. // This attempts to move whatever is located at old_path to new_path,
  156. // be it a file or folder.
  157. bool MoveFileOrFolder(const Pathname &old_path, const Pathname &new_path) {
  158. if (IsFile(old_path)) {
  159. return MoveFile(old_path, new_path);
  160. } else {
  161. return MoveFolder(old_path, new_path);
  162. }
  163. }
  164. // This copies a file from old_path to new_path. This method ASSERTs and
  165. // returns false if old_path is a folder, and returns true if the copy
  166. // succeeds.
  167. virtual bool CopyFile(const Pathname &old_path, const Pathname &new_path) = 0;
  168. // This copies a folder from old_path to new_path.
  169. bool CopyFolder(const Pathname &old_path, const Pathname &new_path);
  170. bool CopyFileOrFolder(const Pathname &old_path, const Pathname &new_path) {
  171. if (IsFile(old_path))
  172. return CopyFile(old_path, new_path);
  173. else
  174. return CopyFolder(old_path, new_path);
  175. }
  176. // Returns true if pathname refers to a directory
  177. virtual bool IsFolder(const Pathname& pathname) = 0;
  178. // Returns true if pathname refers to a file
  179. virtual bool IsFile(const Pathname& pathname) = 0;
  180. // Returns true if pathname refers to no filesystem object, every parent
  181. // directory either exists, or is also absent.
  182. virtual bool IsAbsent(const Pathname& pathname) = 0;
  183. // Returns true if pathname represents a temporary location on the system.
  184. virtual bool IsTemporaryPath(const Pathname& pathname) = 0;
  185. // A folder appropriate for storing temporary files (Contents are
  186. // automatically deleted when the program exits)
  187. virtual bool GetTemporaryFolder(Pathname &path, bool create,
  188. const std::string *append) = 0;
  189. virtual std::string TempFilename(const Pathname &dir,
  190. const std::string &prefix) = 0;
  191. // Determines the size of the file indicated by path.
  192. virtual bool GetFileSize(const Pathname& path, size_t* size) = 0;
  193. // Determines a timestamp associated with the file indicated by path.
  194. virtual bool GetFileTime(const Pathname& path, FileTimeType which,
  195. time_t* time) = 0;
  196. // Returns the path to the running application.
  197. // Note: This is not guaranteed to work on all platforms. Be aware of the
  198. // limitations before using it, and robustly handle failure.
  199. virtual bool GetAppPathname(Pathname* path) = 0;
  200. // Get a folder that is unique to the current application, which is suitable
  201. // for sharing data between executions of the app. If the per_user arg is
  202. // true, the folder is also specific to the current user.
  203. virtual bool GetAppDataFolder(Pathname* path, bool per_user) = 0;
  204. // Get a temporary folder that is unique to the current user and application.
  205. // TODO: Re-evaluate the goals of this function. We probably just need any
  206. // directory that won't collide with another existing directory, and which
  207. // will be cleaned up when the program exits.
  208. virtual bool GetAppTempFolder(Pathname* path) = 0;
  209. // Delete the contents of the folder returned by GetAppTempFolder
  210. bool CleanAppTempFolder();
  211. virtual bool GetDiskFreeSpace(const Pathname& path, int64 *freebytes) = 0;
  212. // Returns the absolute path of the current directory.
  213. virtual Pathname GetCurrentDirectory() = 0;
  214. // Note: These might go into some shared config section later, but they're
  215. // used by some methods in this interface, so we're leaving them here for now.
  216. void SetOrganizationName(const std::string& organization) {
  217. organization_name_ = organization;
  218. }
  219. void GetOrganizationName(std::string* organization) {
  220. ASSERT(NULL != organization);
  221. *organization = organization_name_;
  222. }
  223. void SetApplicationName(const std::string& application) {
  224. application_name_ = application;
  225. }
  226. void GetApplicationName(std::string* application) {
  227. ASSERT(NULL != application);
  228. *application = application_name_;
  229. }
  230. protected:
  231. std::string organization_name_;
  232. std::string application_name_;
  233. };
  234. class Filesystem {
  235. public:
  236. static FilesystemInterface *default_filesystem() {
  237. ASSERT(default_filesystem_ != NULL);
  238. return default_filesystem_;
  239. }
  240. static void set_default_filesystem(FilesystemInterface *filesystem) {
  241. default_filesystem_ = filesystem;
  242. }
  243. static FilesystemInterface *swap_default_filesystem(
  244. FilesystemInterface *filesystem) {
  245. FilesystemInterface *cur = default_filesystem_;
  246. default_filesystem_ = filesystem;
  247. return cur;
  248. }
  249. static DirectoryIterator *IterateDirectory() {
  250. return EnsureDefaultFilesystem()->IterateDirectory();
  251. }
  252. static bool CreateFolder(const Pathname &pathname) {
  253. return EnsureDefaultFilesystem()->CreateFolder(pathname);
  254. }
  255. static FileStream *OpenFile(const Pathname &filename,
  256. const std::string &mode) {
  257. return EnsureDefaultFilesystem()->OpenFile(filename, mode);
  258. }
  259. static bool CreatePrivateFile(const Pathname &filename) {
  260. return EnsureDefaultFilesystem()->CreatePrivateFile(filename);
  261. }
  262. static bool DeleteFile(const Pathname &filename) {
  263. return EnsureDefaultFilesystem()->DeleteFile(filename);
  264. }
  265. static bool DeleteEmptyFolder(const Pathname &folder) {
  266. return EnsureDefaultFilesystem()->DeleteEmptyFolder(folder);
  267. }
  268. static bool DeleteFolderContents(const Pathname &folder) {
  269. return EnsureDefaultFilesystem()->DeleteFolderContents(folder);
  270. }
  271. static bool DeleteFolderAndContents(const Pathname &folder) {
  272. return EnsureDefaultFilesystem()->DeleteFolderAndContents(folder);
  273. }
  274. static bool MoveFolder(const Pathname &old_path, const Pathname &new_path) {
  275. return EnsureDefaultFilesystem()->MoveFolder(old_path, new_path);
  276. }
  277. static bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
  278. return EnsureDefaultFilesystem()->MoveFile(old_path, new_path);
  279. }
  280. static bool CopyFolder(const Pathname &old_path, const Pathname &new_path) {
  281. return EnsureDefaultFilesystem()->CopyFolder(old_path, new_path);
  282. }
  283. static bool CopyFile(const Pathname &old_path, const Pathname &new_path) {
  284. return EnsureDefaultFilesystem()->CopyFile(old_path, new_path);
  285. }
  286. static bool IsFolder(const Pathname& pathname) {
  287. return EnsureDefaultFilesystem()->IsFolder(pathname);
  288. }
  289. static bool IsFile(const Pathname &pathname) {
  290. return EnsureDefaultFilesystem()->IsFile(pathname);
  291. }
  292. static bool IsAbsent(const Pathname &pathname) {
  293. return EnsureDefaultFilesystem()->IsAbsent(pathname);
  294. }
  295. static bool IsTemporaryPath(const Pathname& pathname) {
  296. return EnsureDefaultFilesystem()->IsTemporaryPath(pathname);
  297. }
  298. static bool GetTemporaryFolder(Pathname &path, bool create,
  299. const std::string *append) {
  300. return EnsureDefaultFilesystem()->GetTemporaryFolder(path, create, append);
  301. }
  302. static std::string TempFilename(const Pathname &dir,
  303. const std::string &prefix) {
  304. return EnsureDefaultFilesystem()->TempFilename(dir, prefix);
  305. }
  306. static bool GetFileSize(const Pathname& path, size_t* size) {
  307. return EnsureDefaultFilesystem()->GetFileSize(path, size);
  308. }
  309. static bool GetFileTime(const Pathname& path, FileTimeType which,
  310. time_t* time) {
  311. return EnsureDefaultFilesystem()->GetFileTime(path, which, time);
  312. }
  313. static bool GetAppPathname(Pathname* path) {
  314. return EnsureDefaultFilesystem()->GetAppPathname(path);
  315. }
  316. static bool GetAppDataFolder(Pathname* path, bool per_user) {
  317. return EnsureDefaultFilesystem()->GetAppDataFolder(path, per_user);
  318. }
  319. static bool GetAppTempFolder(Pathname* path) {
  320. return EnsureDefaultFilesystem()->GetAppTempFolder(path);
  321. }
  322. static bool CleanAppTempFolder() {
  323. return EnsureDefaultFilesystem()->CleanAppTempFolder();
  324. }
  325. static bool GetDiskFreeSpace(const Pathname& path, int64 *freebytes) {
  326. return EnsureDefaultFilesystem()->GetDiskFreeSpace(path, freebytes);
  327. }
  328. // Definition has to be in the .cc file due to returning forward-declared
  329. // Pathname by value.
  330. static Pathname GetCurrentDirectory();
  331. static void SetOrganizationName(const std::string& organization) {
  332. EnsureDefaultFilesystem()->SetOrganizationName(organization);
  333. }
  334. static void GetOrganizationName(std::string* organization) {
  335. EnsureDefaultFilesystem()->GetOrganizationName(organization);
  336. }
  337. static void SetApplicationName(const std::string& application) {
  338. EnsureDefaultFilesystem()->SetApplicationName(application);
  339. }
  340. static void GetApplicationName(std::string* application) {
  341. EnsureDefaultFilesystem()->GetApplicationName(application);
  342. }
  343. private:
  344. static FilesystemInterface* default_filesystem_;
  345. static FilesystemInterface *EnsureDefaultFilesystem();
  346. DISALLOW_IMPLICIT_CONSTRUCTORS(Filesystem);
  347. };
  348. class FilesystemScope{
  349. public:
  350. explicit FilesystemScope(FilesystemInterface *new_fs) {
  351. old_fs_ = Filesystem::swap_default_filesystem(new_fs);
  352. }
  353. ~FilesystemScope() {
  354. Filesystem::set_default_filesystem(old_fs_);
  355. }
  356. private:
  357. FilesystemInterface* old_fs_;
  358. DISALLOW_IMPLICIT_CONSTRUCTORS(FilesystemScope);
  359. };
  360. // Generates a unique filename based on the input path. If no path component
  361. // is specified, it uses the temporary directory. If a filename is provided,
  362. // up to 100 variations of form basename-N.extension are tried. When
  363. // create_empty is true, an empty file of this name is created (which
  364. // decreases the chance of a temporary filename collision with another
  365. // process).
  366. bool CreateUniqueFile(Pathname& path, bool create_empty);
  367. } // namespace talk_base
  368. #endif // TALK_BASE_FILEUTILS_H_