File.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. enum LinkType
  54. /// Type of link for linkTo().
  55. {
  56. LINK_HARD = 0, /// hard link
  57. LINK_SYMBOLIC = 1 /// symbolic link
  58. };
  59. File();
  60. /// Creates the file.
  61. File(const std::string& path);
  62. /// Creates the file.
  63. File(const char* path);
  64. /// Creates the file.
  65. File(const Path& path);
  66. /// Creates the file.
  67. File(const File& file);
  68. /// Copy constructor.
  69. virtual ~File();
  70. /// Destroys the file.
  71. File& operator = (const File& file);
  72. /// Assignment operator.
  73. File& operator = (const std::string& path);
  74. /// Assignment operator.
  75. File& operator = (const char* path);
  76. /// Assignment operator.
  77. File& operator = (const Path& path);
  78. /// Assignment operator.
  79. void swap(File& file);
  80. /// Swaps the file with another one.
  81. const std::string& path() const;
  82. /// Returns the path.
  83. bool exists() const;
  84. /// Returns true iff the file exists.
  85. bool canRead() const;
  86. /// Returns true iff the file is readable.
  87. bool canWrite() const;
  88. /// Returns true iff the file is writeable.
  89. bool canExecute() const;
  90. /// Returns true iff the file is executable.
  91. ///
  92. /// On Windows, the file must have
  93. /// the extension ".EXE" to be executable.
  94. /// On Unix platforms, the executable permission
  95. /// bit must be set.
  96. bool isFile() const;
  97. /// Returns true iff the file is a regular file.
  98. bool isLink() const;
  99. /// Returns true iff the file is a symbolic link.
  100. bool isDirectory() const;
  101. /// Returns true iff the file is a directory.
  102. bool isDevice() const;
  103. /// Returns true iff the file is a device.
  104. bool isHidden() const;
  105. /// Returns true if the file is hidden.
  106. ///
  107. /// On Windows platforms, the file's hidden
  108. /// attribute is set for this to be true.
  109. ///
  110. /// On Unix platforms, the file name must
  111. /// begin with a period for this to be true.
  112. Timestamp created() const;
  113. /// Returns the creation date of the file.
  114. ///
  115. /// Not all platforms or filesystems (e.g. Linux and most Unix
  116. /// platforms with the exception of FreeBSD and Mac OS X)
  117. /// maintain the creation date of a file.
  118. /// On such platforms, created() returns
  119. /// the time of the last inode modification.
  120. Timestamp getLastModified() const;
  121. /// Returns the modification date of the file.
  122. File& setLastModified(const Timestamp& ts);
  123. /// Sets the modification date of the file.
  124. FileSize getSize() const;
  125. /// Returns the size of the file in bytes.
  126. File& setSize(FileSize size);
  127. /// Sets the size of the file in bytes. Can be used
  128. /// to truncate a file.
  129. File& setWriteable(bool flag = true);
  130. /// Makes the file writeable (if flag is true), or
  131. /// non-writeable (if flag is false) by setting the
  132. /// file's flags in the filesystem accordingly.
  133. File& setReadOnly(bool flag = true);
  134. /// Makes the file non-writeable (if flag is true), or
  135. /// writeable (if flag is false) by setting the
  136. /// file's flags in the filesystem accordingly.
  137. File& setExecutable(bool flag = true);
  138. /// Makes the file executable (if flag is true), or
  139. /// non-executable (if flag is false) by setting
  140. /// the file's permission bits accordingly.
  141. ///
  142. /// Does nothing on Windows.
  143. void copyTo(const std::string& path) const;
  144. /// Copies the file (or directory) to the given path.
  145. /// The target path can be a directory.
  146. ///
  147. /// A directory is copied recursively.
  148. void moveTo(const std::string& path);
  149. /// Copies the file (or directory) to the given path and
  150. /// removes the original file. The target path can be a directory.
  151. void renameTo(const std::string& path);
  152. /// Renames the file to the new name.
  153. void linkTo(const std::string& path, LinkType type = LINK_SYMBOLIC) const;
  154. /// Creates a link (symbolic or hard, depending on type argument)
  155. /// at the given path to the file or directory.
  156. ///
  157. /// May not be supported on all platforms.
  158. /// Furthermore, some operating systems do not allow creating
  159. /// hard links to directories.
  160. void remove(bool recursive = false);
  161. /// Deletes the file. If recursive is true and the
  162. /// file is a directory, recursively deletes all
  163. /// files in the directory.
  164. bool createFile();
  165. /// Creates a new, empty file in an atomic operation.
  166. /// Returns true if the file has been created and false
  167. /// if the file already exists. Throws an exception if
  168. /// an error occurs.
  169. bool createDirectory();
  170. /// Creates a directory. Returns true if the directory
  171. /// has been created and false if it already exists.
  172. /// Throws an exception if an error occurs.
  173. void createDirectories();
  174. /// Creates a directory (and all parent directories
  175. /// if necessary).
  176. void list(std::vector<std::string>& files) const;
  177. /// Fills the vector with the names of all
  178. /// files in the directory.
  179. void list(std::vector<File>& files) const;
  180. /// Fills the vector with the names of all
  181. /// files in the directory.
  182. FileSize totalSpace() const;
  183. /// Returns the total size in bytes of the partition containing this path.
  184. FileSize usableSpace() const;
  185. /// Returns the number of usable free bytes on the partition containing this path.
  186. FileSize freeSpace() const;
  187. /// Returns the number of free bytes on the partition containing this path.
  188. bool operator == (const File& file) const;
  189. bool operator != (const File& file) const;
  190. bool operator < (const File& file) const;
  191. bool operator <= (const File& file) const;
  192. bool operator > (const File& file) const;
  193. bool operator >= (const File& file) const;
  194. static void handleLastError(const std::string& path);
  195. /// For internal use only. Throws an appropriate
  196. /// exception for the last file-related error.
  197. protected:
  198. void copyDirectory(const std::string& path) const;
  199. /// Copies a directory. Used internally by copyTo().
  200. };
  201. //
  202. // inlines
  203. //
  204. inline const std::string& File::path() const
  205. {
  206. return getPathImpl();
  207. }
  208. inline bool File::operator == (const File& file) const
  209. {
  210. return getPathImpl() == file.getPathImpl();
  211. }
  212. inline bool File::operator != (const File& file) const
  213. {
  214. return getPathImpl() != file.getPathImpl();
  215. }
  216. inline bool File::operator < (const File& file) const
  217. {
  218. return getPathImpl() < file.getPathImpl();
  219. }
  220. inline bool File::operator <= (const File& file) const
  221. {
  222. return getPathImpl() <= file.getPathImpl();
  223. }
  224. inline bool File::operator > (const File& file) const
  225. {
  226. return getPathImpl() > file.getPathImpl();
  227. }
  228. inline bool File::operator >= (const File& file) const
  229. {
  230. return getPathImpl() >= file.getPathImpl();
  231. }
  232. inline void swap(File& f1, File& f2)
  233. {
  234. f1.swap(f2);
  235. }
  236. } // namespace Poco
  237. #endif // Foundation_File_INCLUDED