Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

341 lines
11KB

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