ExFatVolume.h 11 KB

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