ExFatVolume.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * Copyright (c) 2011-2020 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 "ExFatPartition.h"
  28. #include "ExFatFile.h"
  29. //==============================================================================
  30. /**
  31. * \class ExFatVolume
  32. * \brief exFAT volume.
  33. */
  34. class ExFatVolume : public ExFatPartition {
  35. public:
  36. ExFatVolume() {
  37. }
  38. /**
  39. * Initialize an FatVolume object.
  40. * \param[in] dev Device block driver.
  41. * \param[in] setCwv Set current working volume if true.
  42. * \param[in] part partition to initialize.
  43. * \return true for success or false for failure.
  44. */
  45. bool begin(BlockDevice* dev, bool setCwv = true, uint8_t part = 1) {
  46. if (!init(dev, part)) {
  47. return false;
  48. }
  49. if (!chdir()) {
  50. return false;
  51. }
  52. if (setCwv) {
  53. m_cwv = this;
  54. }
  55. return true;
  56. }
  57. /**
  58. * Set volume working directory to root.
  59. * \return true for success or false for failure.
  60. */
  61. bool chdir() {
  62. m_vwd.close();
  63. return m_vwd.openRoot(this);
  64. }
  65. /**
  66. * Set volume working directory.
  67. * \param[in] path Path for volume working directory.
  68. * \return true for success or false for failure.
  69. */
  70. bool chdir(const ExChar_t* path);
  71. /** \return current working volume. */
  72. static ExFatVolume* cwv() {return m_cwv;}
  73. /** Change global working volume to this volume. */
  74. void chvol() {m_cwv = this;}
  75. /**
  76. * Test for the existence of a file.
  77. *
  78. * \param[in] path Path of the file to be tested for.
  79. *
  80. * \return true if the file exists else false.
  81. */
  82. bool exists(const ExChar_t* path) {
  83. ExFatFile tmp;
  84. return tmp.open(this, path, O_RDONLY);
  85. }
  86. //----------------------------------------------------------------------------
  87. /** List the directory contents of the root directory.
  88. *
  89. * \param[in] pr Print stream for list.
  90. *
  91. * \param[in] flags The inclusive OR of
  92. *
  93. * LS_DATE - %Print file modification date
  94. *
  95. * LS_SIZE - %Print file size.
  96. *
  97. * LS_R - Recursive list of subdirectories.
  98. *
  99. * \return true for success or false for failure.
  100. */
  101. bool ls(print_t* pr, uint8_t flags = 0) {
  102. return m_vwd.ls(pr, flags);
  103. }
  104. /** List the contents of a directory.
  105. *
  106. * \param[in] pr Print stream for list.
  107. *
  108. * \param[in] path directory to list.
  109. *
  110. * \param[in] flags The inclusive OR of
  111. *
  112. * LS_DATE - %Print file modification date
  113. *
  114. * LS_SIZE - %Print file size.
  115. *
  116. * LS_R - Recursive list of subdirectories.
  117. *
  118. * \return true for success or false for failure.
  119. */
  120. bool ls(print_t* pr, const ExChar_t* path, uint8_t flags) {
  121. ExFatFile dir;
  122. return dir.open(this, path, O_RDONLY) && dir.ls(pr, flags);
  123. }
  124. /** Make a subdirectory in the volume root directory.
  125. *
  126. * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
  127. *
  128. * \param[in] pFlag Create missing parent directories if true.
  129. *
  130. * \return true for success or false for failure.
  131. */
  132. bool mkdir(const ExChar_t* path, bool pFlag = true) {
  133. ExFatFile sub;
  134. return sub.mkdir(vwd(), path, pFlag);
  135. }
  136. /** open a file
  137. *
  138. * \param[in] path location of file to be opened.
  139. * \param[in] oflag open flags.
  140. * \return a ExFile object.
  141. */
  142. ExFile open(const ExChar_t* path, oflag_t oflag = O_RDONLY) {
  143. ExFile tmpFile;
  144. tmpFile.open(this, path, oflag);
  145. return tmpFile;
  146. }
  147. /** Remove a file from the volume root directory.
  148. *
  149. * \param[in] path A path with a valid 8.3 DOS name for the file.
  150. *
  151. * \return true for success or false for failure.
  152. */
  153. bool remove(const ExChar_t* path) {
  154. ExFatFile tmp;
  155. return tmp.open(this, path, O_WRONLY) && tmp.remove();
  156. }
  157. /** Rename a file or subdirectory.
  158. *
  159. * \param[in] oldPath Path name to the file or subdirectory to be renamed.
  160. *
  161. * \param[in] newPath New path name of the file or subdirectory.
  162. *
  163. * The \a newPath object must not exist before the rename call.
  164. *
  165. * The file to be renamed must not be open. The directory entry may be
  166. * moved and file system corruption could occur if the file is accessed by
  167. * a file object that was opened before the rename() call.
  168. *
  169. * \return true for success or false for failure.
  170. */
  171. bool rename(const ExChar_t* oldPath, const ExChar_t* newPath) {
  172. ExFatFile file;
  173. return file.open(vwd(), oldPath, O_RDONLY) && file.rename(vwd(), newPath);
  174. }
  175. /** Remove a subdirectory from the volume's working directory.
  176. *
  177. * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
  178. *
  179. * The subdirectory file will be removed only if it is empty.
  180. *
  181. * \return true for success or false for failure.
  182. */
  183. bool rmdir(const ExChar_t* path) {
  184. ExFatFile sub;
  185. return sub.open(this, path, O_RDONLY) && sub.rmdir();
  186. }
  187. /** Truncate a file to a specified length. The current file position
  188. * will be at the new EOF.
  189. *
  190. * \param[in] path A path with a valid 8.3 DOS name for the file.
  191. * \param[in] length The desired length for the file.
  192. *
  193. * \return true for success or false for failure.
  194. */
  195. bool truncate(const ExChar_t* path, uint64_t length) {
  196. ExFatFile file;
  197. if (!file.open(this, path, O_WRONLY)) {
  198. return false;
  199. }
  200. return file.truncate(length);
  201. }
  202. #if ENABLE_ARDUINO_SERIAL
  203. /** List the directory contents of the root directory to Serial.
  204. *
  205. * \return true for success or false for failure.
  206. */
  207. bool ls() {
  208. return ls(&Serial);
  209. }
  210. /** List the directory contents of the volume root to Serial.
  211. *
  212. * \param[in] flags The inclusive OR of
  213. *
  214. * LS_DATE - %Print file modification date
  215. *
  216. * LS_SIZE - %Print file size.
  217. *
  218. * LS_R - Recursive list of subdirectories.
  219. *
  220. * \return true for success or false for failure.
  221. */
  222. bool ls(uint8_t flags) {
  223. return ls(&Serial, flags);
  224. }
  225. /** List the directory contents of a directory to Serial.
  226. *
  227. * \param[in] path directory to list.
  228. *
  229. * \param[in] flags The inclusive OR of
  230. *
  231. * LS_DATE - %Print file modification date
  232. *
  233. * LS_SIZE - %Print file size.
  234. *
  235. * LS_R - Recursive list of subdirectories.
  236. *
  237. * \return true for success or false for failure.
  238. */
  239. bool ls(const ExChar_t* path, uint8_t flags = 0) {
  240. return ls(&Serial, path, flags);
  241. }
  242. #endif // ENABLE_ARDUINO_SERIAL
  243. #if ENABLE_ARDUINO_STRING
  244. /**
  245. * Set volume working directory.
  246. * \param[in] path Path for volume working directory.
  247. * \return true for success or false for failure.
  248. */
  249. bool chdir(const String& path) {
  250. return chdir(path.c_str());
  251. }
  252. /** Test for the existence of a file in a directory
  253. *
  254. * \param[in] path Path of the file to be tested for.
  255. *
  256. * \return true if the file exists else false.
  257. */
  258. bool exists(const String &path) {
  259. return exists(path.c_str());
  260. }
  261. /** Make a subdirectory in the volume root directory.
  262. *
  263. * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
  264. *
  265. * \param[in] pFlag Create missing parent directories if true.
  266. *
  267. * \return true for success or false for failure.
  268. */
  269. bool mkdir(const String &path, bool pFlag = true) {
  270. return mkdir(path.c_str(), pFlag);
  271. }
  272. /** open a file
  273. *
  274. * \param[in] path location of file to be opened.
  275. * \param[in] oflag open oflag flags.
  276. * \return a ExFile object.
  277. */
  278. ExFile open(const String &path, oflag_t oflag = O_RDONLY) {
  279. return open(path.c_str(), oflag);
  280. }
  281. /** Remove a file from the volume root directory.
  282. *
  283. * \param[in] path A path with a valid name for the file.
  284. *
  285. * \return true for success or false for failure.
  286. */
  287. bool remove(const String& path) {
  288. return remove(path.c_str());
  289. }
  290. /** Rename a file or subdirectory.
  291. *
  292. * \param[in] oldPath Path name to the file or subdirectory to be renamed.
  293. *
  294. * \param[in] newPath New path name of the file or subdirectory.
  295. *
  296. * The \a newPath object must not exist before the rename call.
  297. *
  298. * The file to be renamed must not be open. The directory entry may be
  299. * moved and file system corruption could occur if the file is accessed by
  300. * a file object that was opened before the rename() call.
  301. *
  302. * \return true for success or false for failure.
  303. */
  304. bool rename(const String& oldPath, const String& newPath) {
  305. return rename(oldPath.c_str(), newPath.c_str());
  306. }
  307. /** Remove a subdirectory from the volume's working directory.
  308. *
  309. * \param[in] path A path with a valid name for the subdirectory.
  310. *
  311. * The subdirectory file will be removed only if it is empty.
  312. *
  313. * \return true for success or false for failure.
  314. */
  315. bool rmdir(const String& path) {
  316. return rmdir(path.c_str());
  317. }
  318. /** Truncate a file to a specified length. The current file position
  319. * will be at the new EOF.
  320. *
  321. * \param[in] path A path with a valid name for the file.
  322. * \param[in] length The desired length for the file.
  323. *
  324. * \return true for success or false for failure.
  325. */
  326. bool truncate(const String& path, uint64_t length) {
  327. return truncate(path.c_str(), length);
  328. }
  329. #endif // ENABLE_ARDUINO_STRING
  330. //============================================================================
  331. #if USE_EXFAT_UNICODE_NAMES
  332. // Not implemented when Unicode is selected.
  333. bool exists(const char* path);
  334. bool mkdir(const char* path, bool pFlag = true);
  335. bool remove(const char* path);
  336. bool rename(const char* oldPath, const char* newPath);
  337. bool rmdir(const char* path);
  338. #endif // USE_EXFAT_UNICODE_NAMES
  339. private:
  340. friend ExFatFile;
  341. ExFatFile* vwd() {return &m_vwd;}
  342. ExFatFile m_vwd;
  343. static ExFatVolume* m_cwv;
  344. };
  345. #endif // ExFatVolume_h