File.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // File.h
  3. //
  4. // Library: Foundation
  5. // Package: Filesystem
  6. // Module: File
  7. //
  8. // Definition of the File class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_File_INCLUDED
  16. #define Foundation_File_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Timestamp.h"
  19. #include <vector>
  20. #if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
  21. #if defined(_WIN32_WCE)
  22. #include "File_WINCE.h"
  23. #else
  24. #include "Poco/File_WIN32U.h"
  25. #endif
  26. #elif defined(POCO_OS_FAMILY_WINDOWS)
  27. #include "Poco/File_WIN32.h"
  28. #elif defined(POCO_VXWORKS)
  29. #include "Poco/File_VX.h"
  30. #elif defined(POCO_OS_FAMILY_UNIX)
  31. #include "Poco/File_UNIX.h"
  32. #endif
  33. namespace Poco {
  34. class Path;
  35. class Foundation_API File: private FileImpl
  36. /// The File class provides methods for working with a file.
  37. ///
  38. /// Regarding paths passed to the various methods, note that
  39. /// platform-specific limitations regarding maximum length
  40. /// of the entire path and its components apply.
  41. ///
  42. /// On Windows, if compiled with UTF-8 support (POCO_WIN32_UTF8)
  43. /// the implementation tries to work around the rather low
  44. /// 260 characters MAX_PATH limit by adding the "\\?\" prefix if
  45. /// a path is absolute and exceeds MAX_PATH characters in length.
  46. /// Note that various limitations regarding usage of the "\\?\"
  47. /// prefix apply in that case, e.g. the path must
  48. /// not contain relative components ("." and "..") and must not
  49. /// use the forward slash ("/") as directory separator.
  50. {
  51. public:
  52. typedef FileSizeImpl FileSize;
  53. File();
  54. /// Creates the file.
  55. File(const std::string& path);
  56. /// Creates the file.
  57. File(const char* path);
  58. /// Creates the file.
  59. File(const Path& path);
  60. /// Creates the file.
  61. File(const File& file);
  62. /// Copy constructor.
  63. virtual ~File();
  64. /// Destroys the file.
  65. File& operator = (const File& file);
  66. /// Assignment operator.
  67. File& operator = (const std::string& path);
  68. /// Assignment operator.
  69. File& operator = (const char* path);
  70. /// Assignment operator.
  71. File& operator = (const Path& path);
  72. /// Assignment operator.
  73. void swap(File& file);
  74. /// Swaps the file with another one.
  75. const std::string& path() const;
  76. /// Returns the path.
  77. bool exists() const;
  78. /// Returns true iff the file exists.
  79. bool canRead() const;
  80. /// Returns true iff the file is readable.
  81. bool canWrite() const;
  82. /// Returns true iff the file is writeable.
  83. bool canExecute() const;
  84. /// Returns true iff the file is executable.
  85. ///
  86. /// On Windows, the file must have
  87. /// the extension ".EXE" to be executable.
  88. /// On Unix platforms, the executable permission
  89. /// bit must be set.
  90. bool isFile() const;
  91. /// Returns true iff the file is a regular file.
  92. bool isLink() const;
  93. /// Returns true iff the file is a symbolic link.
  94. bool isDirectory() const;
  95. /// Returns true iff the file is a directory.
  96. bool isDevice() const;
  97. /// Returns true iff the file is a device.
  98. bool isHidden() const;
  99. /// Returns true if the file is hidden.
  100. ///
  101. /// On Windows platforms, the file's hidden
  102. /// attribute is set for this to be true.
  103. ///
  104. /// On Unix platforms, the file name must
  105. /// begin with a period for this to be true.
  106. Timestamp created() const;
  107. /// Returns the creation date of the file.
  108. ///
  109. /// Not all platforms or filesystems (e.g. Linux and most Unix
  110. /// platforms with the exception of FreeBSD and Mac OS X)
  111. /// maintain the creation date of a file.
  112. /// On such platforms, created() returns
  113. /// the time of the last inode modification.
  114. Timestamp getLastModified() const;
  115. /// Returns the modification date of the file.
  116. File& setLastModified(const Timestamp& ts);
  117. /// Sets the modification date of the file.
  118. FileSize getSize() const;
  119. /// Returns the size of the file in bytes.
  120. File& setSize(FileSize size);
  121. /// Sets the size of the file in bytes. Can be used
  122. /// to truncate a file.
  123. File& setWriteable(bool flag = true);
  124. /// Makes the file writeable (if flag is true), or
  125. /// non-writeable (if flag is false) by setting the
  126. /// file's flags in the filesystem accordingly.
  127. File& setReadOnly(bool flag = true);
  128. /// Makes the file non-writeable (if flag is true), or
  129. /// writeable (if flag is false) by setting the
  130. /// file's flags in the filesystem accordingly.
  131. File& setExecutable(bool flag = true);
  132. /// Makes the file executable (if flag is true), or
  133. /// non-executable (if flag is false) by setting
  134. /// the file's permission bits accordingly.
  135. ///
  136. /// Does nothing on Windows.
  137. void copyTo(const std::string& path) const;
  138. /// Copies the file (or directory) to the given path.
  139. /// The target path can be a directory.
  140. ///
  141. /// A directory is copied recursively.
  142. void moveTo(const std::string& path);
  143. /// Copies the file (or directory) to the given path and
  144. /// removes the original file. The target path can be a directory.
  145. void renameTo(const std::string& path);
  146. /// Renames the file to the new name.
  147. void remove(bool recursive = false);
  148. /// Deletes the file. If recursive is true and the
  149. /// file is a directory, recursively deletes all
  150. /// files in the directory.
  151. bool createFile();
  152. /// Creates a new, empty file in an atomic operation.
  153. /// Returns true if the file has been created and false
  154. /// if the file already exists. Throws an exception if
  155. /// an error occurs.
  156. bool createDirectory();
  157. /// Creates a directory. Returns true if the directory
  158. /// has been created and false if it already exists.
  159. /// Throws an exception if an error occurs.
  160. void createDirectories();
  161. /// Creates a directory (and all parent directories
  162. /// if necessary).
  163. void list(std::vector<std::string>& files) const;
  164. /// Fills the vector with the names of all
  165. /// files in the directory.
  166. void list(std::vector<File>& files) const;
  167. /// Fills the vector with the names of all
  168. /// files in the directory.
  169. bool operator == (const File& file) const;
  170. bool operator != (const File& file) const;
  171. bool operator < (const File& file) const;
  172. bool operator <= (const File& file) const;
  173. bool operator > (const File& file) const;
  174. bool operator >= (const File& file) const;
  175. static void handleLastError(const std::string& path);
  176. /// For internal use only. Throws an appropriate
  177. /// exception for the last file-related error.
  178. protected:
  179. void copyDirectory(const std::string& path) const;
  180. /// Copies a directory. Used internally by copyTo().
  181. };
  182. //
  183. // inlines
  184. //
  185. inline const std::string& File::path() const
  186. {
  187. return getPathImpl();
  188. }
  189. inline bool File::operator == (const File& file) const
  190. {
  191. return getPathImpl() == file.getPathImpl();
  192. }
  193. inline bool File::operator != (const File& file) const
  194. {
  195. return getPathImpl() != file.getPathImpl();
  196. }
  197. inline bool File::operator < (const File& file) const
  198. {
  199. return getPathImpl() < file.getPathImpl();
  200. }
  201. inline bool File::operator <= (const File& file) const
  202. {
  203. return getPathImpl() <= file.getPathImpl();
  204. }
  205. inline bool File::operator > (const File& file) const
  206. {
  207. return getPathImpl() > file.getPathImpl();
  208. }
  209. inline bool File::operator >= (const File& file) const
  210. {
  211. return getPathImpl() >= file.getPathImpl();
  212. }
  213. inline void swap(File& f1, File& f2)
  214. {
  215. f1.swap(f2);
  216. }
  217. } // namespace Poco
  218. #endif // Foundation_File_INCLUDED