Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

941 lines
32KB

  1. /* FatLib Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the FatLib Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the FatLib Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef FatFile_h
  21. #define FatFile_h
  22. /**
  23. * \file
  24. * \brief FatFile class
  25. */
  26. // #include <ctype.h>
  27. #include <string.h>
  28. #include <stddef.h>
  29. #include <limits.h>
  30. #include "FatLibConfig.h"
  31. #include "FatApiConstants.h"
  32. #include "FatStructs.h"
  33. #include "FatVolume.h"
  34. class FatFileSystem;
  35. //------------------------------------------------------------------------------
  36. // Stuff to store strings in AVR flash.
  37. #ifdef __AVR__
  38. #include <avr/pgmspace.h>
  39. #else // __AVR__
  40. #ifndef PGM_P
  41. /** pointer to flash for ARM */
  42. #define PGM_P const char*
  43. #endif // PGM_P
  44. #ifndef PSTR
  45. /** store literal string in flash for ARM */
  46. #define PSTR(x) (x)
  47. #endif // PSTR
  48. #ifndef pgm_read_byte
  49. /** read 8-bits from flash for ARM */
  50. #define pgm_read_byte(addr) (*(const unsigned char*)(addr))
  51. #endif // pgm_read_byte
  52. #ifndef pgm_read_word
  53. /** read 16-bits from flash for ARM */
  54. #define pgm_read_word(addr) (*(const uint16_t*)(addr))
  55. #endif // pgm_read_word
  56. #ifndef PROGMEM
  57. /** store in flash for ARM */
  58. #define PROGMEM const
  59. #endif // PROGMEM
  60. #endif // __AVR__
  61. //------------------------------------------------------------------------------
  62. /**
  63. * \struct FatPos_t
  64. * \brief Internal type for file position - do not use in user apps.
  65. */
  66. struct FatPos_t {
  67. /** stream position */
  68. uint32_t position;
  69. /** cluster for position */
  70. uint32_t cluster;
  71. FatPos_t() : position(0), cluster(0) {}
  72. };
  73. //------------------------------------------------------------------------------
  74. /** Expression for path name separator. */
  75. #define isDirSeparator(c) ((c) == '/')
  76. //------------------------------------------------------------------------------
  77. /**
  78. * \struct fname_t
  79. * \brief Internal type for Short File Name - do not use in user apps.
  80. */
  81. struct fname_t {
  82. /** Flags for base and extension character case and LFN. */
  83. uint8_t flags;
  84. /** length of Long File Name */
  85. size_t len;
  86. /** Long File Name start. */
  87. const char* lfn;
  88. /** position for sequence number */
  89. uint8_t seqPos;
  90. /** Short File Name */
  91. uint8_t sfn[11];
  92. };
  93. /** Derived from a LFN with loss or conversion of characters. */
  94. const uint8_t FNAME_FLAG_LOST_CHARS = 0X01;
  95. /** Base-name or extension has mixed case. */
  96. const uint8_t FNAME_FLAG_MIXED_CASE = 0X02;
  97. /** LFN entries are required for file name. */
  98. const uint8_t FNAME_FLAG_NEED_LFN =
  99. FNAME_FLAG_LOST_CHARS | FNAME_FLAG_MIXED_CASE;
  100. /** Filename base-name is all lower case */
  101. const uint8_t FNAME_FLAG_LC_BASE = DIR_NT_LC_BASE;
  102. /** Filename extension is all lower case. */
  103. const uint8_t FNAME_FLAG_LC_EXT = DIR_NT_LC_EXT;
  104. //==============================================================================
  105. /**
  106. * \class FatFile
  107. * \brief Basic file class.
  108. */
  109. class FatFile {
  110. public:
  111. /** Create an instance. */
  112. FatFile() : m_attr(FILE_ATTR_CLOSED), m_error(0) {}
  113. /** Create a file object and open it in the current working directory.
  114. *
  115. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  116. *
  117. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  118. * OR of open flags. see FatFile::open(FatFile*, const char*, uint8_t).
  119. */
  120. FatFile(const char* path, uint8_t oflag) {
  121. m_attr = FILE_ATTR_CLOSED;
  122. m_error = 0;
  123. open(path, oflag);
  124. }
  125. #if DESTRUCTOR_CLOSES_FILE
  126. ~FatFile() {
  127. if (isOpen()) {
  128. close();
  129. }
  130. }
  131. #endif // DESTRUCTOR_CLOSES_FILE
  132. /** \return value of writeError */
  133. bool getWriteError() {
  134. return m_error & WRITE_ERROR;
  135. }
  136. /** Set writeError to zero */
  137. void clearWriteError() {
  138. m_error &= ~WRITE_ERROR;
  139. }
  140. /** Clear all error bits. */
  141. void clearError() {
  142. m_error = 0;
  143. }
  144. /** \return All error bits. */
  145. uint8_t getError() {
  146. return m_error;
  147. }
  148. /** get position for streams
  149. * \param[out] pos struct to receive position
  150. */
  151. void getpos(FatPos_t* pos);
  152. /** set position for streams
  153. * \param[out] pos struct with value for new position
  154. */
  155. void setpos(FatPos_t* pos);
  156. /** \return The number of bytes available from the current position
  157. * to EOF for normal files. Zero is returned for directory files.
  158. */
  159. uint32_t available() {
  160. return isFile() ? fileSize() - curPosition() : 0;
  161. }
  162. /** Close a file and force cached data and directory information
  163. * to be written to the storage device.
  164. *
  165. * \return The value true is returned for success and
  166. * the value false is returned for failure.
  167. */
  168. bool close();
  169. /** Check for contiguous file and return its raw block range.
  170. *
  171. * \param[out] bgnBlock the first block address for the file.
  172. * \param[out] endBlock the last block address for the file.
  173. *
  174. * \return The value true is returned for success and
  175. * the value false is returned for failure.
  176. */
  177. bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
  178. /** Create and open a new contiguous file of a specified size.
  179. *
  180. * \note This function only supports short DOS 8.3 names.
  181. * See open() for more information.
  182. *
  183. * \param[in] dirFile The directory where the file will be created.
  184. * \param[in] path A path with a valid DOS 8.3 file name.
  185. * \param[in] size The desired file size.
  186. *
  187. * \return The value true is returned for success and
  188. * the value false, is returned for failure.
  189. */
  190. bool createContiguous(FatFile* dirFile,
  191. const char* path, uint32_t size);
  192. /** \return The current cluster number for a file or directory. */
  193. uint32_t curCluster() const {
  194. return m_curCluster;
  195. }
  196. /** \return The current position for a file or directory. */
  197. uint32_t curPosition() const {
  198. return m_curPosition;
  199. }
  200. /** \return Current working directory */
  201. static FatFile* cwd() {
  202. return m_cwd;
  203. }
  204. /** Set the date/time callback function
  205. *
  206. * \param[in] dateTime The user's call back function. The callback
  207. * function is of the form:
  208. *
  209. * \code
  210. * void dateTime(uint16_t* date, uint16_t* time) {
  211. * uint16_t year;
  212. * uint8_t month, day, hour, minute, second;
  213. *
  214. * // User gets date and time from GPS or real-time clock here
  215. *
  216. * // return date using FAT_DATE macro to format fields
  217. * *date = FAT_DATE(year, month, day);
  218. *
  219. * // return time using FAT_TIME macro to format fields
  220. * *time = FAT_TIME(hour, minute, second);
  221. * }
  222. * \endcode
  223. *
  224. * Sets the function that is called when a file is created or when
  225. * a file's directory entry is modified by sync(). All timestamps,
  226. * access, creation, and modify, are set when a file is created.
  227. * sync() maintains the last access date and last modify date/time.
  228. *
  229. * See the timestamp() function.
  230. */
  231. static void dateTimeCallback(
  232. void (*dateTime)(uint16_t* date, uint16_t* time)) {
  233. m_dateTime = dateTime;
  234. }
  235. /** Cancel the date/time callback function. */
  236. static void dateTimeCallbackCancel() {
  237. m_dateTime = 0;
  238. }
  239. /** Return a file's directory entry.
  240. *
  241. * \param[out] dir Location for return of the file's directory entry.
  242. *
  243. * \return The value true is returned for success and
  244. * the value false is returned for failure.
  245. */
  246. bool dirEntry(dir_t* dir);
  247. /**
  248. * \return The index of this file in it's directory.
  249. */
  250. uint16_t dirIndex() {
  251. return m_dirIndex;
  252. }
  253. /** Format the name field of \a dir into the 13 byte array
  254. * \a name in standard 8.3 short name format.
  255. *
  256. * \param[in] dir The directory structure containing the name.
  257. * \param[out] name A 13 byte char array for the formatted name.
  258. * \return length of the name.
  259. */
  260. static uint8_t dirName(const dir_t* dir, char* name);
  261. /** \return The number of bytes allocated to a directory or zero
  262. * if an error occurs.
  263. */
  264. uint32_t dirSize();
  265. /** Dump file in Hex
  266. * \param[in] pr Print stream for list.
  267. * \param[in] pos Start position in file.
  268. * \param[in] n number of locations to dump.
  269. */
  270. void dmpFile(print_t* pr, uint32_t pos, size_t n);
  271. /** Test for the existence of a file in a directory
  272. *
  273. * \param[in] path Path of the file to be tested for.
  274. *
  275. * The calling instance must be an open directory file.
  276. *
  277. * dirFile.exists("TOFIND.TXT") searches for "TOFIND.TXT" in the directory
  278. * dirFile.
  279. *
  280. * \return true if the file exists else false.
  281. */
  282. bool exists(const char* path) {
  283. FatFile file;
  284. return file.open(this, path, O_READ);
  285. }
  286. /**
  287. * Get a string from a file.
  288. *
  289. * fgets() reads bytes from a file into the array pointed to by \a str, until
  290. * \a num - 1 bytes are read, or a delimiter is read and transferred to \a str,
  291. * or end-of-file is encountered. The string is then terminated
  292. * with a null byte.
  293. *
  294. * fgets() deletes CR, '\\r', from the string. This insures only a '\\n'
  295. * terminates the string for Windows text files which use CRLF for newline.
  296. *
  297. * \param[out] str Pointer to the array where the string is stored.
  298. * \param[in] num Maximum number of characters to be read
  299. * (including the final null byte). Usually the length
  300. * of the array \a str is used.
  301. * \param[in] delim Optional set of delimiters. The default is "\n".
  302. *
  303. * \return For success fgets() returns the length of the string in \a str.
  304. * If no data is read, fgets() returns zero for EOF or -1 if an error occurred.
  305. */
  306. int16_t fgets(char* str, int16_t num, char* delim = 0);
  307. /** \return The total number of bytes in a file. */
  308. uint32_t fileSize() const {
  309. return m_fileSize;
  310. }
  311. /** \return The first cluster number for a file or directory. */
  312. uint32_t firstCluster() const {
  313. return m_firstCluster;
  314. }
  315. /**
  316. * Get a file's name followed by a zero byte.
  317. *
  318. * \param[out] name An array of characters for the file's name.
  319. * \param[in] size The size of the array in bytes. The array
  320. * must be at least 13 bytes long. The file name will be
  321. * truncated if it is too long.
  322. * \return The value true, is returned for success and
  323. * the value false, is returned for failure.
  324. */
  325. bool getFilename(char* name, size_t size);
  326. /**
  327. * Get a file's Short File Name followed by a zero byte.
  328. *
  329. * \param[out] name An array of characters for the file's name.
  330. * The array must be at least 13 bytes long.
  331. * \return The value true, is returned for success and
  332. * the value false, is returned for failure.
  333. */
  334. bool getSFN(char* name);
  335. /** \return True if this is a directory else false. */
  336. bool isDir() const {
  337. return m_attr & FILE_ATTR_DIR;
  338. }
  339. /** \return True if this is a normal file else false. */
  340. bool isFile() const {
  341. return m_attr & FILE_ATTR_FILE;
  342. }
  343. /** \return True if this is a hidden file else false. */
  344. bool isHidden() const {
  345. return m_attr & FILE_ATTR_HIDDEN;
  346. }
  347. /** \return true if this file has a Long File Name. */
  348. bool isLFN() const {
  349. return m_lfnOrd;
  350. }
  351. /** \return True if this is an open file/directory else false. */
  352. bool isOpen() const {
  353. return m_attr;
  354. }
  355. /** \return True if this is the root directory. */
  356. bool isRoot() const {
  357. return m_attr & FILE_ATTR_ROOT;
  358. }
  359. /** \return True if this is the FAT32 root directory. */
  360. bool isRoot32() const {
  361. return m_attr & FILE_ATTR_ROOT32;
  362. }
  363. /** \return True if this is the FAT12 of FAT16 root directory. */
  364. bool isRootFixed() const {
  365. return m_attr & FILE_ATTR_ROOT_FIXED;
  366. }
  367. /** \return True if file is read-only */
  368. bool isReadOnly() const {
  369. return m_attr & FILE_ATTR_READ_ONLY;
  370. }
  371. /** \return True if this is a subdirectory else false. */
  372. bool isSubDir() const {
  373. return m_attr & FILE_ATTR_SUBDIR;
  374. }
  375. /** \return True if this is a system file else false. */
  376. bool isSystem() const {
  377. return m_attr & FILE_ATTR_SYSTEM;
  378. }
  379. /** Check for a legal 8.3 character.
  380. * \param[in] c Character to be checked.
  381. * \return true for a legal 8.3 character else false.
  382. */
  383. static bool legal83Char(uint8_t c) {
  384. if (c == '"' || c == '|') {
  385. return false;
  386. }
  387. // *+,./
  388. if (0X2A <= c && c <= 0X2F && c != 0X2D) {
  389. return false;
  390. }
  391. // :;<=>?
  392. if (0X3A <= c && c <= 0X3F) {
  393. return false;
  394. }
  395. // [\]
  396. if (0X5B <= c && c <= 0X5D) {
  397. return false;
  398. }
  399. return 0X20 < c && c < 0X7F;
  400. }
  401. /** List directory contents.
  402. *
  403. * \param[in] pr Print stream for list.
  404. *
  405. * \param[in] flags The inclusive OR of
  406. *
  407. * LS_DATE - %Print file modification date
  408. *
  409. * LS_SIZE - %Print file size.
  410. *
  411. * LS_R - Recursive list of subdirectories.
  412. *
  413. * \param[in] indent Amount of space before file name. Used for recursive
  414. * list to indicate subdirectory level.
  415. */
  416. void ls(print_t* pr, uint8_t flags = 0, uint8_t indent = 0);
  417. /** Make a new directory.
  418. *
  419. * \param[in] dir An open FatFile instance for the directory that will
  420. * contain the new directory.
  421. *
  422. * \param[in] path A path with a valid 8.3 DOS name for the new directory.
  423. *
  424. * \param[in] pFlag Create missing parent directories if true.
  425. *
  426. * \return The value true is returned for success and
  427. * the value false is returned for failure.
  428. */
  429. bool mkdir(FatFile* dir, const char* path, bool pFlag = true);
  430. /** Open a file in the volume working directory of a FatFileSystem.
  431. *
  432. * \param[in] fs File System where the file is located.
  433. *
  434. * \param[in] path with a valid 8.3 DOS name for a file to be opened.
  435. *
  436. * \param[in] oflag bitwise-inclusive OR of open mode flags.
  437. * See see FatFile::open(FatFile*, const char*, uint8_t).
  438. *
  439. * \return The value true is returned for success and
  440. * the value false is returned for failure.
  441. */
  442. bool open(FatFileSystem* fs, const char* path, uint8_t oflag);
  443. /** Open a file by index.
  444. *
  445. * \param[in] dirFile An open FatFile instance for the directory.
  446. *
  447. * \param[in] index The \a index of the directory entry for the file to be
  448. * opened. The value for \a index is (directory file position)/32.
  449. *
  450. * \param[in] oflag bitwise-inclusive OR of open mode flags.
  451. * See see FatFile::open(FatFile*, const char*, uint8_t).
  452. *
  453. * See open() by path for definition of flags.
  454. * \return true for success or false for failure.
  455. */
  456. bool open(FatFile* dirFile, uint16_t index, uint8_t oflag);
  457. /** Open a file or directory by name.
  458. *
  459. * \param[in] dirFile An open FatFile instance for the directory containing
  460. * the file to be opened.
  461. *
  462. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  463. *
  464. * \param[in] oflag Values for \a oflag are constructed by a
  465. * bitwise-inclusive OR of flags from the following list
  466. *
  467. * O_READ - Open for reading.
  468. *
  469. * O_RDONLY - Same as O_READ.
  470. *
  471. * O_WRITE - Open for writing.
  472. *
  473. * O_WRONLY - Same as O_WRITE.
  474. *
  475. * O_RDWR - Open for reading and writing.
  476. *
  477. * O_APPEND - If set, the file offset shall be set to the end of the
  478. * file prior to each write.
  479. *
  480. * O_AT_END - Set the initial position at the end of the file.
  481. *
  482. * O_CREAT - If the file exists, this flag has no effect except as noted
  483. * under O_EXCL below. Otherwise, the file shall be created
  484. *
  485. * O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.
  486. *
  487. * O_SYNC - Call sync() after each write. This flag should not be used with
  488. * write(uint8_t) or any functions do character at a time writes since sync()
  489. * will be called after each byte.
  490. *
  491. * O_TRUNC - If the file exists and is a regular file, and the file is
  492. * successfully opened and is not read only, its length shall be truncated to 0.
  493. *
  494. * WARNING: A given file must not be opened by more than one FatFile object
  495. * or file corruption may occur.
  496. *
  497. * \note Directory files must be opened read only. Write and truncation is
  498. * not allowed for directory files.
  499. *
  500. * \return The value true is returned for success and
  501. * the value false is returned for failure.
  502. */
  503. bool open(FatFile* dirFile, const char* path, uint8_t oflag);
  504. /** Open a file in the current working directory.
  505. *
  506. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  507. *
  508. * \param[in] oflag bitwise-inclusive OR of open mode flags.
  509. * See see FatFile::open(FatFile*, const char*, uint8_t).
  510. *
  511. * \return The value true is returned for success and
  512. * the value false is returned for failure.
  513. */
  514. bool open(const char* path, uint8_t oflag = O_READ) {
  515. return open(m_cwd, path, oflag);
  516. }
  517. /** Open the next file or subdirectory in a directory.
  518. *
  519. * \param[in] dirFile An open FatFile instance for the directory
  520. * containing the file to be opened.
  521. *
  522. * \param[in] oflag bitwise-inclusive OR of open mode flags.
  523. * See see FatFile::open(FatFile*, const char*, uint8_t).
  524. *
  525. * \return true for success or false for failure.
  526. */
  527. bool openNext(FatFile* dirFile, uint8_t oflag = O_READ);
  528. /** Open a volume's root directory.
  529. *
  530. * \param[in] vol The FAT volume containing the root directory to be opened.
  531. *
  532. * \return The value true is returned for success and
  533. * the value false is returned for failure.
  534. */
  535. bool openRoot(FatVolume* vol);
  536. /** Return the next available byte without consuming it.
  537. *
  538. * \return The byte if no error and not at eof else -1;
  539. */
  540. int peek();
  541. /** Print a file's creation date and time
  542. *
  543. * \param[in] pr Print stream for output.
  544. *
  545. * \return The value true is returned for success and
  546. * the value false is returned for failure.
  547. */
  548. bool printCreateDateTime(print_t* pr);
  549. /** %Print a directory date field.
  550. *
  551. * Format is yyyy-mm-dd.
  552. *
  553. * \param[in] pr Print stream for output.
  554. * \param[in] fatDate The date field from a directory entry.
  555. */
  556. static void printFatDate(print_t* pr, uint16_t fatDate);
  557. /** %Print a directory time field.
  558. *
  559. * Format is hh:mm:ss.
  560. *
  561. * \param[in] pr Print stream for output.
  562. * \param[in] fatTime The time field from a directory entry.
  563. */
  564. static void printFatTime(print_t* pr, uint16_t fatTime);
  565. /** Print a number followed by a field terminator.
  566. * \param[in] value The number to be printed.
  567. * \param[in] term The field terminator. Use '\\n' for CR LF.
  568. * \param[in] prec Number of digits after decimal point.
  569. * \return The number of bytes written or -1 if an error occurs.
  570. */
  571. int printField(float value, char term, uint8_t prec = 2);
  572. /** Print a number followed by a field terminator.
  573. * \param[in] value The number to be printed.
  574. * \param[in] term The field terminator. Use '\\n' for CR LF.
  575. * \return The number of bytes written or -1 if an error occurs.
  576. */
  577. int printField(int16_t value, char term);
  578. /** Print a number followed by a field terminator.
  579. * \param[in] value The number to be printed.
  580. * \param[in] term The field terminator. Use '\\n' for CR LF.
  581. * \return The number of bytes written or -1 if an error occurs.
  582. */
  583. int printField(uint16_t value, char term);
  584. /** Print a number followed by a field terminator.
  585. * \param[in] value The number to be printed.
  586. * \param[in] term The field terminator. Use '\\n' for CR LF.
  587. * \return The number of bytes written or -1 if an error occurs.
  588. */
  589. int printField(int32_t value, char term);
  590. /** Print a number followed by a field terminator.
  591. * \param[in] value The number to be printed.
  592. * \param[in] term The field terminator. Use '\\n' for CR LF.
  593. * \return The number of bytes written or -1 if an error occurs.
  594. */
  595. int printField(uint32_t value, char term);
  596. /** Print a file's modify date and time
  597. *
  598. * \param[in] pr Print stream for output.
  599. *
  600. * \return The value true is returned for success and
  601. * the value false is returned for failure.
  602. */
  603. bool printModifyDateTime(print_t* pr);
  604. /** Print a file's name
  605. *
  606. * \param[in] pr Print stream for output.
  607. *
  608. * \return The value true is returned for success and
  609. * the value false is returned for failure.
  610. */
  611. size_t printName(print_t* pr);
  612. /** Print a file's size.
  613. *
  614. * \param[in] pr Print stream for output.
  615. *
  616. * \return The number of characters printed is returned
  617. * for success and zero is returned for failure.
  618. */
  619. size_t printFileSize(print_t* pr);
  620. /** Print a file's Short File Name.
  621. *
  622. * \param[in] pr Print stream for output.
  623. *
  624. * \return The number of characters printed is returned
  625. * for success and zero is returned for failure.
  626. */
  627. size_t printSFN(print_t* pr);
  628. /** Read the next byte from a file.
  629. *
  630. * \return For success read returns the next byte in the file as an int.
  631. * If an error occurs or end of file is reached -1 is returned.
  632. */
  633. int read() {
  634. uint8_t b;
  635. return read(&b, 1) == 1 ? b : -1;
  636. }
  637. /** Read data from a file starting at the current position.
  638. *
  639. * \param[out] buf Pointer to the location that will receive the data.
  640. *
  641. * \param[in] nbyte Maximum number of bytes to read.
  642. *
  643. * \return For success read() returns the number of bytes read.
  644. * A value less than \a nbyte, including zero, will be returned
  645. * if end of file is reached.
  646. * If an error occurs, read() returns -1. Possible errors include
  647. * read() called before a file has been opened, corrupt file system
  648. * or an I/O error occurred.
  649. */
  650. int read(void* buf, size_t nbyte);
  651. /** Read the next directory entry from a directory file.
  652. *
  653. * \param[out] dir The dir_t struct that will receive the data.
  654. *
  655. * \return For success readDir() returns the number of bytes read.
  656. * A value of zero will be returned if end of file is reached.
  657. * If an error occurs, readDir() returns -1. Possible errors include
  658. * readDir() called before a directory has been opened, this is not
  659. * a directory file or an I/O error occurred.
  660. */
  661. int8_t readDir(dir_t* dir);
  662. /** Remove a file.
  663. *
  664. * The directory entry and all data for the file are deleted.
  665. *
  666. * \note This function should not be used to delete the 8.3 version of a
  667. * file that has a long name. For example if a file has the long name
  668. * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
  669. *
  670. * \return The value true is returned for success and
  671. * the value false is returned for failure.
  672. */
  673. bool remove();
  674. /** Remove a file.
  675. *
  676. * The directory entry and all data for the file are deleted.
  677. *
  678. * \param[in] dirFile The directory that contains the file.
  679. * \param[in] path Path for the file to be removed.
  680. *
  681. * \note This function should not be used to delete the 8.3 version of a
  682. * file that has a long name. For example if a file has the long name
  683. * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
  684. *
  685. * \return The value true is returned for success and
  686. * the value false is returned for failure.
  687. */
  688. static bool remove(FatFile* dirFile, const char* path);
  689. /** Set the file's current position to zero. */
  690. void rewind() {
  691. seekSet(0);
  692. }
  693. /** Rename a file or subdirectory.
  694. *
  695. * \param[in] dirFile Directory for the new path.
  696. * \param[in] newPath New path name for the file/directory.
  697. *
  698. * \return The value true is returned for success and
  699. * the value false is returned for failure.
  700. */
  701. bool rename(FatFile* dirFile, const char* newPath);
  702. /** Remove a directory file.
  703. *
  704. * The directory file will be removed only if it is empty and is not the
  705. * root directory. rmdir() follows DOS and Windows and ignores the
  706. * read-only attribute for the directory.
  707. *
  708. * \note This function should not be used to delete the 8.3 version of a
  709. * directory that has a long name. For example if a directory has the
  710. * long name "New folder" you should not delete the 8.3 name "NEWFOL~1".
  711. *
  712. * \return The value true is returned for success and
  713. * the value false is returned for failure.
  714. */
  715. bool rmdir();
  716. /** Recursively delete a directory and all contained files.
  717. *
  718. * This is like the Unix/Linux 'rm -rf *' if called with the root directory
  719. * hence the name.
  720. *
  721. * Warning - This will remove all contents of the directory including
  722. * subdirectories. The directory will then be removed if it is not root.
  723. * The read-only attribute for files will be ignored.
  724. *
  725. * \note This function should not be used to delete the 8.3 version of
  726. * a directory that has a long name. See remove() and rmdir().
  727. *
  728. * \return The value true is returned for success and
  729. * the value false is returned for failure.
  730. */
  731. bool rmRfStar();
  732. /** Set the files position to current position + \a pos. See seekSet().
  733. * \param[in] offset The new position in bytes from the current position.
  734. * \return true for success or false for failure.
  735. */
  736. bool seekCur(int32_t offset) {
  737. return seekSet(m_curPosition + offset);
  738. }
  739. /** Set the files position to end-of-file + \a offset. See seekSet().
  740. * Can't be used for directory files since file size is not defined.
  741. * \param[in] offset The new position in bytes from end-of-file.
  742. * \return true for success or false for failure.
  743. */
  744. bool seekEnd(int32_t offset = 0) {
  745. return isFile() ? seekSet(m_fileSize + offset) : false;
  746. }
  747. /** Sets a file's position.
  748. *
  749. * \param[in] pos The new position in bytes from the beginning of the file.
  750. *
  751. * \return The value true is returned for success and
  752. * the value false is returned for failure.
  753. */
  754. bool seekSet(uint32_t pos);
  755. /** Set the current working directory.
  756. *
  757. * \param[in] dir New current working directory.
  758. *
  759. * \return true for success else false.
  760. */
  761. static bool setCwd(FatFile* dir) {
  762. if (!dir->isDir()) {
  763. return false;
  764. }
  765. m_cwd = dir;
  766. return true;
  767. }
  768. /** The sync() call causes all modified data and directory fields
  769. * to be written to the storage device.
  770. *
  771. * \return The value true is returned for success and
  772. * the value false is returned for failure.
  773. */
  774. bool sync();
  775. /** Copy a file's timestamps
  776. *
  777. * \param[in] file File to copy timestamps from.
  778. *
  779. * \note
  780. * Modify and access timestamps may be overwritten if a date time callback
  781. * function has been set by dateTimeCallback().
  782. *
  783. * \return The value true is returned for success and
  784. * the value false is returned for failure.
  785. */
  786. bool timestamp(FatFile* file);
  787. /** Set a file's timestamps in its directory entry.
  788. *
  789. * \param[in] flags Values for \a flags are constructed by a bitwise-inclusive
  790. * OR of flags from the following list
  791. *
  792. * T_ACCESS - Set the file's last access date.
  793. *
  794. * T_CREATE - Set the file's creation date and time.
  795. *
  796. * T_WRITE - Set the file's last write/modification date and time.
  797. *
  798. * \param[in] year Valid range 1980 - 2107 inclusive.
  799. *
  800. * \param[in] month Valid range 1 - 12 inclusive.
  801. *
  802. * \param[in] day Valid range 1 - 31 inclusive.
  803. *
  804. * \param[in] hour Valid range 0 - 23 inclusive.
  805. *
  806. * \param[in] minute Valid range 0 - 59 inclusive.
  807. *
  808. * \param[in] second Valid range 0 - 59 inclusive
  809. *
  810. * \note It is possible to set an invalid date since there is no check for
  811. * the number of days in a month.
  812. *
  813. * \note
  814. * Modify and access timestamps may be overwritten if a date time callback
  815. * function has been set by dateTimeCallback().
  816. *
  817. * \return The value true is returned for success and
  818. * the value false is returned for failure.
  819. */
  820. bool timestamp(uint8_t flags, uint16_t year, uint8_t month, uint8_t day,
  821. uint8_t hour, uint8_t minute, uint8_t second);
  822. /** Type of file. You should use isFile() or isDir() instead of fileType()
  823. * if possible.
  824. *
  825. * \return The file or directory type.
  826. */
  827. uint8_t fileAttr() const {
  828. return m_attr;
  829. }
  830. /** Truncate a file to a specified length. The current file position
  831. * will be maintained if it is less than or equal to \a length otherwise
  832. * it will be set to end of file.
  833. *
  834. * \param[in] length The desired length for the file.
  835. *
  836. * \return The value true is returned for success and
  837. * the value false is returned for failure.
  838. */
  839. bool truncate(uint32_t length);
  840. /** \return FatVolume that contains this file. */
  841. FatVolume* volume() const {
  842. return m_vol;
  843. }
  844. /** Write a single byte.
  845. * \param[in] b The byte to be written.
  846. * \return +1 for success or -1 for failure.
  847. */
  848. int write(uint8_t b) {
  849. return write(&b, 1);
  850. }
  851. /** Write data to an open file.
  852. *
  853. * \note Data is moved to the cache but may not be written to the
  854. * storage device until sync() is called.
  855. *
  856. * \param[in] buf Pointer to the location of the data to be written.
  857. *
  858. * \param[in] nbyte Number of bytes to write.
  859. *
  860. * \return For success write() returns the number of bytes written, always
  861. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  862. * include write() is called before a file has been opened, write is called
  863. * for a read-only file, device is full, a corrupt file system or an I/O error.
  864. *
  865. */
  866. int write(const void* buf, size_t nbyte);
  867. //------------------------------------------------------------------------------
  868. private:
  869. /** This file has not been opened. */
  870. static const uint8_t FILE_ATTR_CLOSED = 0;
  871. /** File is read-only. */
  872. static const uint8_t FILE_ATTR_READ_ONLY = DIR_ATT_READ_ONLY;
  873. /** File should be hidden in directory listings. */
  874. static const uint8_t FILE_ATTR_HIDDEN = DIR_ATT_HIDDEN;
  875. /** Entry is for a system file. */
  876. static const uint8_t FILE_ATTR_SYSTEM = DIR_ATT_SYSTEM;
  877. /** Entry for normal data file */
  878. static const uint8_t FILE_ATTR_FILE = 0X08;
  879. /** Entry is for a subdirectory */
  880. static const uint8_t FILE_ATTR_SUBDIR = DIR_ATT_DIRECTORY;
  881. /** A FAT12 or FAT16 root directory */
  882. static const uint8_t FILE_ATTR_ROOT_FIXED = 0X20;
  883. /** A FAT32 root directory */
  884. static const uint8_t FILE_ATTR_ROOT32 = 0X40;
  885. /** Entry is for root. */
  886. static const uint8_t FILE_ATTR_ROOT = FILE_ATTR_ROOT_FIXED | FILE_ATTR_ROOT32;
  887. /** Directory type bits */
  888. static const uint8_t FILE_ATTR_DIR = FILE_ATTR_SUBDIR | FILE_ATTR_ROOT;
  889. /** Attributes to copy from directory entry */
  890. static const uint8_t FILE_ATTR_COPY = DIR_ATT_READ_ONLY | DIR_ATT_HIDDEN |
  891. DIR_ATT_SYSTEM | DIR_ATT_DIRECTORY;
  892. /** experimental don't use */
  893. bool openParent(FatFile* dir);
  894. // private functions
  895. bool addCluster();
  896. bool addDirCluster();
  897. dir_t* cacheDirEntry(uint8_t action);
  898. static uint8_t lfnChecksum(uint8_t* name);
  899. bool lfnUniqueSfn(fname_t* fname);
  900. bool openCluster(FatFile* file);
  901. static bool parsePathName(const char* str, fname_t* fname, const char** ptr);
  902. bool mkdir(FatFile* parent, fname_t* fname);
  903. bool open(FatFile* dirFile, fname_t* fname, uint8_t oflag);
  904. bool openCachedEntry(FatFile* dirFile, uint16_t cacheIndex, uint8_t oflag,
  905. uint8_t lfnOrd);
  906. bool readLBN(uint32_t* lbn);
  907. dir_t* readDirCache(bool skipReadOk = false);
  908. bool setDirSize();
  909. // bits defined in m_flags
  910. // should be 0X0F
  911. static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
  912. // sync of directory entry required
  913. static uint8_t const F_FILE_DIR_DIRTY = 0X80;
  914. // global pointer to cwd dir
  915. static FatFile* m_cwd;
  916. // data time callback function
  917. static void (*m_dateTime)(uint16_t* date, uint16_t* time);
  918. // private data
  919. static const uint8_t WRITE_ERROR = 0X1;
  920. static const uint8_t READ_ERROR = 0X2;
  921. uint8_t m_attr; // File attributes
  922. uint8_t m_error; // Error bits.
  923. uint8_t m_flags; // See above for definition of m_flags bits
  924. uint8_t m_lfnOrd;
  925. uint16_t m_dirIndex; // index of directory entry in dir file
  926. FatVolume* m_vol; // volume where file is located
  927. uint32_t m_dirCluster;
  928. uint32_t m_curCluster; // cluster for current file position
  929. uint32_t m_curPosition; // current file position
  930. uint32_t m_dirBlock; // block for this files directory entry
  931. uint32_t m_fileSize; // file size in bytes
  932. uint32_t m_firstCluster; // first cluster of file
  933. };
  934. #endif // FatFile_h