File.h 6.7 KB

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