FatVolume.h 11 KB

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