Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

248 linhas
8.4KB

  1. /* FatLib Library
  2. * Copyright (C) 2013 by William Greiman
  3. *
  4. * This file is part of the FatLib Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the FatLib Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef FatFileSystem_h
  21. #define FatFileSystem_h
  22. #include "FatVolume.h"
  23. #include "FatFile.h"
  24. /**
  25. * \file
  26. * \brief FatFileSystem class
  27. */
  28. //------------------------------------------------------------------------------
  29. /** FatFileSystem version YYYYMMDD */
  30. #define FAT_LIB_VERSION 20141115
  31. //------------------------------------------------------------------------------
  32. /**
  33. * \class FatFileSystem
  34. * \brief Integration class for the FatLib library.
  35. */
  36. class FatFileSystem : protected FatVolume {
  37. protected:
  38. /**
  39. * Initialize an FatFileSystem object.
  40. * \param[in] d Volume Working Directory.
  41. * \return The value one, true, is returned for success and
  42. * the value zero, false, is returned for failure.
  43. */
  44. bool begin(FatFile* d) {
  45. m_vwd = d;
  46. m_vwd->close();
  47. return init() && vwd()->openRoot(this) && FatFile::setCwd(vwd());
  48. }
  49. public:
  50. /** Change a volume's working directory to root
  51. *
  52. * Changes the volume's working directory to the SD's root directory.
  53. * Optionally set the current working directory to the volume's
  54. * working directory.
  55. *
  56. * \param[in] set_cwd Set the current working directory to this volume's
  57. * working directory if true.
  58. *
  59. * \return The value one, true, is returned for success and
  60. * the value zero, false, is returned for failure.
  61. */
  62. bool chdir(bool set_cwd = false) {
  63. vwd()->close();
  64. return vwd()->openRoot(this) && (set_cwd ? FatFile::setCwd(vwd()) : true);
  65. }
  66. /** Change a volume's working directory
  67. *
  68. * Changes the volume working directory to the \a path subdirectory.
  69. * Optionally set the current working directory to the volume's
  70. * working directory.
  71. *
  72. * Example: If the volume's working directory is "/DIR", chdir("SUB")
  73. * will change the volume's working directory from "/DIR" to "/DIR/SUB".
  74. *
  75. * If path is "/", the volume's working directory will be changed to the
  76. * root directory
  77. *
  78. * \param[in] path The name of the subdirectory.
  79. *
  80. * \param[in] set_cwd Set the current working directory to this volume's
  81. * working directory if true.
  82. *
  83. * \return The value one, true, is returned for success and
  84. * the value zero, false, is returned for failure.
  85. */
  86. //----------------------------------------------------------------------------
  87. bool chdir(const char *path, bool set_cwd = false) {
  88. FatFile dir;
  89. if (path[0] == '/' && path[1] == '\0') return chdir(set_cwd);
  90. if (!dir.open(vwd(), path, O_READ)) goto fail;
  91. if (!dir.isDir()) goto fail;
  92. *m_vwd = dir;
  93. if (set_cwd) FatFile::setCwd(vwd());
  94. return true;
  95. fail:
  96. return false;
  97. }
  98. //----------------------------------------------------------------------------
  99. /** Set the current working directory to a volume's working directory.
  100. *
  101. * This is useful with multiple SD cards.
  102. *
  103. * The current working directory is changed to this
  104. * volume's working directory.
  105. *
  106. * This is like the Windows/DOS \<drive letter>: command.
  107. */
  108. void chvol() {
  109. FatFile::setCwd(vwd());
  110. }
  111. //----------------------------------------------------------------------------
  112. /**
  113. * Test for the existence of a file.
  114. *
  115. * \param[in] path Path of the file to be tested for.
  116. *
  117. * \return true if the file exists else false.
  118. */
  119. bool exists(const char* path) {
  120. return vwd()->exists(path);
  121. }
  122. //----------------------------------------------------------------------------
  123. /** List the directory contents of the volume working directory.
  124. *
  125. * \param[in] pr Print stream for list.
  126. *
  127. * \param[in] flags The inclusive OR of
  128. *
  129. * LS_DATE - %Print file modification date
  130. *
  131. * LS_SIZE - %Print file size.
  132. *
  133. * LS_R - Recursive list of subdirectories.
  134. */
  135. void ls(print_t* pr, uint8_t flags) {
  136. vwd()->ls(pr, flags);
  137. }
  138. //----------------------------------------------------------------------------
  139. /** List the directory contents of a directory.
  140. *
  141. * \param[in] pr Print stream for list.
  142. *
  143. * \param[in] path directory to list.
  144. *
  145. * \param[in] flags The inclusive OR of
  146. *
  147. * LS_DATE - %Print file modification date
  148. *
  149. * LS_SIZE - %Print file size.
  150. *
  151. * LS_R - Recursive list of subdirectories.
  152. */
  153. void ls(print_t* pr, const char* path, uint8_t flags) {
  154. FatFile dir;
  155. dir.open(vwd(), path, O_READ);
  156. dir.ls(pr, flags);
  157. }
  158. //----------------------------------------------------------------------------
  159. /** Make a subdirectory in the volume working directory.
  160. *
  161. * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
  162. *
  163. * \param[in] pFlag Create missing parent directories if true.
  164. *
  165. * \return The value one, true, is returned for success and
  166. * the value zero, false, is returned for failure.
  167. */
  168. bool mkdir(const char* path, bool pFlag = true) {
  169. FatFile sub;
  170. return sub.mkdir(vwd(), path, pFlag);
  171. }
  172. //----------------------------------------------------------------------------
  173. /** Remove a file from the volume working directory.
  174. *
  175. * \param[in] path A path with a valid 8.3 DOS name for the file.
  176. *
  177. * \return The value one, true, is returned for success and
  178. * the value zero, false, is returned for failure.
  179. */
  180. bool remove(const char* path) {
  181. return FatFile::remove(vwd(), path);
  182. }
  183. //----------------------------------------------------------------------------
  184. /** Rename a file or subdirectory.
  185. *
  186. * \param[in] oldPath Path name to the file or subdirectory to be renamed.
  187. *
  188. * \param[in] newPath New path name of the file or subdirectory.
  189. *
  190. * The \a newPath object must not exist before the rename call.
  191. *
  192. * The file to be renamed must not be open. The directory entry may be
  193. * moved and file system corruption could occur if the file is accessed by
  194. * a file object that was opened before the rename() call.
  195. *
  196. * \return The value one, true, is returned for success and
  197. * the value zero, false, is returned for failure.
  198. */
  199. bool rename(const char *oldPath, const char *newPath) {
  200. FatFile file;
  201. if (!file.open(vwd(), oldPath, O_READ)) return false;
  202. return file.rename(vwd(), newPath);
  203. }
  204. //----------------------------------------------------------------------------
  205. /** Remove a subdirectory from the volume's working directory.
  206. *
  207. * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
  208. *
  209. * The subdirectory file will be removed only if it is empty.
  210. *
  211. * \return The value one, true, is returned for success and
  212. * the value zero, false, is returned for failure.
  213. */
  214. bool rmdir(const char* path) {
  215. FatFile sub;
  216. if (!sub.open(vwd(), path, O_READ)) return false;
  217. return sub.rmdir();
  218. }
  219. //----------------------------------------------------------------------------
  220. /** Truncate a file to a specified length. The current file position
  221. * will be maintained if it is less than or equal to \a length otherwise
  222. * it will be set to end of file.
  223. *
  224. * \param[in] path A path with a valid 8.3 DOS name for the file.
  225. * \param[in] length The desired length for the file.
  226. *
  227. * \return The value one, true, is returned for success and
  228. * the value zero, false, is returned for failure.
  229. * Reasons for failure include file is read only, file is a directory,
  230. * \a length is greater than the current file size or an I/O error occurs.
  231. */
  232. bool truncate(const char* path, uint32_t length) {
  233. FatFile file;
  234. if (!file.open(vwd(), path, O_WRITE)) return false;
  235. return file.truncate(length);
  236. }
  237. /** \return a pointer to the FatVolume object. */
  238. FatVolume* vol() {return this;}
  239. /** \return a pointer to the volume working directory. */
  240. FatFile* vwd() {return m_vwd;}
  241. private:
  242. FatFile* m_vwd;
  243. };
  244. #endif // FatFileSystem_h