ExFatVolume.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * Copyright (c) 2011-2021 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef ExFatVolume_h
  26. #define ExFatVolume_h
  27. #include "ExFatFile.h"
  28. //==============================================================================
  29. /**
  30. * \class ExFatVolume
  31. * \brief exFAT volume.
  32. */
  33. class ExFatVolume : public ExFatPartition {
  34. public:
  35. ExFatVolume() {}
  36. /**
  37. * Initialize an FatVolume object.
  38. * \param[in] dev Device block driver.
  39. * \param[in] setCwv Set current working volume if true.
  40. * \param[in] part partition to initialize.
  41. * \return true for success or false for failure.
  42. */
  43. bool begin(FsBlockDevice* dev, bool setCwv = true, uint8_t part = 1) {
  44. if (!init(dev, part)) {
  45. return false;
  46. }
  47. if (!chdir()) {
  48. return false;
  49. }
  50. if (setCwv || !m_cwv) {
  51. m_cwv = this;
  52. }
  53. return true;
  54. }
  55. /**
  56. * Set volume working directory to root.
  57. * \return true for success or false for failure.
  58. */
  59. bool chdir() {
  60. m_vwd.close();
  61. return m_vwd.openRoot(this);
  62. }
  63. /**
  64. * Set volume working directory.
  65. * \param[in] path Path for volume working directory.
  66. * \return true for success or false for failure.
  67. */
  68. bool chdir(const char* path);
  69. /** Change global working volume to this volume. */
  70. void chvol() {m_cwv = this;}
  71. /**
  72. * Test for the existence of a file.
  73. *
  74. * \param[in] path Path of the file to be tested for.
  75. *
  76. * \return true if the file exists else false.
  77. */
  78. bool exists(const char* path) {
  79. ExFatFile tmp;
  80. return tmp.open(this, path, O_RDONLY);
  81. }
  82. //----------------------------------------------------------------------------
  83. /** List the directory contents of the root directory.
  84. *
  85. * \param[in] pr Print stream for list.
  86. *
  87. * \param[in] flags The inclusive OR of
  88. *
  89. * LS_DATE - %Print file modification date
  90. *
  91. * LS_SIZE - %Print file size.
  92. *
  93. * LS_R - Recursive list of subdirectories.
  94. *
  95. * \return true for success or false for failure.
  96. */
  97. bool ls(print_t* pr, uint8_t flags = 0) {
  98. return m_vwd.ls(pr, flags);
  99. }
  100. /** List the contents of a directory.
  101. *
  102. * \param[in] pr Print stream for list.
  103. *
  104. * \param[in] path directory to list.
  105. *
  106. * \param[in] flags The inclusive OR of
  107. *
  108. * LS_DATE - %Print file modification date
  109. *
  110. * LS_SIZE - %Print file size.
  111. *
  112. * LS_R - Recursive list of subdirectories.
  113. *
  114. * \return true for success or false for failure.
  115. */
  116. bool ls(print_t* pr, const char* path, uint8_t flags) {
  117. ExFatFile dir;
  118. return dir.open(this, path, O_RDONLY) && dir.ls(pr, flags);
  119. }
  120. /** Make a subdirectory in the volume root directory.
  121. *
  122. * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
  123. *
  124. * \param[in] pFlag Create missing parent directories if true.
  125. *
  126. * \return true for success or false for failure.
  127. */
  128. bool mkdir(const char* path, bool pFlag = true) {
  129. ExFatFile sub;
  130. return sub.mkdir(vwd(), path, pFlag);
  131. }
  132. /** open a file
  133. *
  134. * \param[in] path location of file to be opened.
  135. * \param[in] oflag open flags.
  136. * \return a ExFile object.
  137. */
  138. ExFile open(const char* path, oflag_t oflag = O_RDONLY) {
  139. ExFile tmpFile;
  140. tmpFile.open(this, path, oflag);
  141. return tmpFile;
  142. }
  143. /** Remove a file from the volume root directory.
  144. *
  145. * \param[in] path A path with a valid 8.3 DOS name for the file.
  146. *
  147. * \return true for success or false for failure.
  148. */
  149. bool remove(const char* path) {
  150. ExFatFile tmp;
  151. return tmp.open(this, path, O_WRONLY) && tmp.remove();
  152. }
  153. /** Rename a file or subdirectory.
  154. *
  155. * \param[in] oldPath Path name to the file or subdirectory to be renamed.
  156. *
  157. * \param[in] newPath New path name of the file or subdirectory.
  158. *
  159. * The \a newPath object must not exist before the rename call.
  160. *
  161. * The file to be renamed must not be open. The directory entry may be
  162. * moved and file system corruption could occur if the file is accessed by
  163. * a file object that was opened before the rename() call.
  164. *
  165. * \return true for success or false for failure.
  166. */
  167. bool rename(const char* oldPath, const char* newPath) {
  168. ExFatFile file;
  169. return file.open(vwd(), oldPath, O_RDONLY) && file.rename(vwd(), newPath);
  170. }
  171. /** Remove a subdirectory from the volume's working directory.
  172. *
  173. * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
  174. *
  175. * The subdirectory file will be removed only if it is empty.
  176. *
  177. * \return true for success or false for failure.
  178. */
  179. bool rmdir(const char* path) {
  180. ExFatFile sub;
  181. return sub.open(this, path, O_RDONLY) && sub.rmdir();
  182. }
  183. /** Truncate a file to a specified length. The current file position
  184. * will be at the new EOF.
  185. *
  186. * \param[in] path A path with a valid 8.3 DOS name for the file.
  187. * \param[in] length The desired length for the file.
  188. *
  189. * \return true for success or false for failure.
  190. */
  191. bool truncate(const char* path, uint64_t length) {
  192. ExFatFile file;
  193. if (!file.open(this, path, O_WRONLY)) {
  194. return false;
  195. }
  196. return file.truncate(length);
  197. }
  198. #if ENABLE_ARDUINO_SERIAL
  199. /** List the directory contents of the root directory to Serial.
  200. *
  201. * \return true for success or false for failure.
  202. */
  203. bool ls() {
  204. return ls(&Serial);
  205. }
  206. /** List the directory contents of the volume root to Serial.
  207. *
  208. * \param[in] flags The inclusive OR of
  209. *
  210. * LS_DATE - %Print file modification date
  211. *
  212. * LS_SIZE - %Print file size.
  213. *
  214. * LS_R - Recursive list of subdirectories.
  215. *
  216. * \return true for success or false for failure.
  217. */
  218. bool ls(uint8_t flags) {
  219. return ls(&Serial, flags);
  220. }
  221. /** List the directory contents of a directory to Serial.
  222. *
  223. * \param[in] path directory to list.
  224. *
  225. * \param[in] flags The inclusive OR of
  226. *
  227. * LS_DATE - %Print file modification date
  228. *
  229. * LS_SIZE - %Print file size.
  230. *
  231. * LS_R - Recursive list of subdirectories.
  232. *
  233. * \return true for success or false for failure.
  234. */
  235. bool ls(const char* path, uint8_t flags = 0) {
  236. return ls(&Serial, path, flags);
  237. }
  238. #endif // ENABLE_ARDUINO_SERIAL
  239. #if ENABLE_ARDUINO_STRING
  240. /**
  241. * Set volume working directory.
  242. * \param[in] path Path for volume working directory.
  243. * \return true for success or false for failure.
  244. */
  245. bool chdir(const String& path) {
  246. return chdir(path.c_str());
  247. }
  248. /** Test for the existence of a file in a directory
  249. *
  250. * \param[in] path Path of the file to be tested for.
  251. *
  252. * \return true if the file exists else false.
  253. */
  254. bool exists(const String &path) {
  255. return exists(path.c_str());
  256. }
  257. /** Make a subdirectory in the volume root directory.
  258. *
  259. * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
  260. *
  261. * \param[in] pFlag Create missing parent directories if true.
  262. *
  263. * \return true for success or false for failure.
  264. */
  265. bool mkdir(const String &path, bool pFlag = true) {
  266. return mkdir(path.c_str(), pFlag);
  267. }
  268. /** open a file
  269. *
  270. * \param[in] path location of file to be opened.
  271. * \param[in] oflag open oflag flags.
  272. * \return a ExFile object.
  273. */
  274. ExFile open(const String &path, oflag_t oflag = O_RDONLY) {
  275. return open(path.c_str(), oflag);
  276. }
  277. /** Remove a file from the volume root directory.
  278. *
  279. * \param[in] path A path with a valid name for the file.
  280. *
  281. * \return true for success or false for failure.
  282. */
  283. bool remove(const String& path) {
  284. return remove(path.c_str());
  285. }
  286. /** Rename a file or subdirectory.
  287. *
  288. * \param[in] oldPath Path name to the file or subdirectory to be renamed.
  289. *
  290. * \param[in] newPath New path name of the file or subdirectory.
  291. *
  292. * The \a newPath object must not exist before the rename call.
  293. *
  294. * The file to be renamed must not be open. The directory entry may be
  295. * moved and file system corruption could occur if the file is accessed by
  296. * a file object that was opened before the rename() call.
  297. *
  298. * \return true for success or false for failure.
  299. */
  300. bool rename(const String& oldPath, const String& newPath) {
  301. return rename(oldPath.c_str(), newPath.c_str());
  302. }
  303. /** Remove a subdirectory from the volume's working directory.
  304. *
  305. * \param[in] path A path with a valid name for the subdirectory.
  306. *
  307. * The subdirectory file will be removed only if it is empty.
  308. *
  309. * \return true for success or false for failure.
  310. */
  311. bool rmdir(const String& path) {
  312. return rmdir(path.c_str());
  313. }
  314. /** Truncate a file to a specified length. The current file position
  315. * will be at the new EOF.
  316. *
  317. * \param[in] path A path with a valid name for the file.
  318. * \param[in] length The desired length for the file.
  319. *
  320. * \return true for success or false for failure.
  321. */
  322. bool truncate(const String& path, uint64_t length) {
  323. return truncate(path.c_str(), length);
  324. }
  325. #endif // ENABLE_ARDUINO_STRING
  326. private:
  327. friend ExFatFile;
  328. static ExFatVolume* cwv() {return m_cwv;}
  329. ExFatFile* vwd() {return &m_vwd;}
  330. static ExFatVolume* m_cwv;
  331. ExFatFile m_vwd;
  332. };
  333. #endif // ExFatVolume_h