SdBaseFile.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* Arduino SdFat Library
  2. * Copyright (C) 2009 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat 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 Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include "Marlin.h"
  21. #ifdef SDSUPPORT
  22. #ifndef SdBaseFile_h
  23. #define SdBaseFile_h
  24. /**
  25. * \file
  26. * \brief SdBaseFile class
  27. */
  28. #include "Marlin.h"
  29. #include "SdFatConfig.h"
  30. #include "SdVolume.h"
  31. //------------------------------------------------------------------------------
  32. /**
  33. * \struct filepos_t
  34. * \brief internal type for istream
  35. * do not use in user apps
  36. */
  37. struct filepos_t {
  38. /** stream position */
  39. uint32_t position;
  40. /** cluster for position */
  41. uint32_t cluster;
  42. filepos_t() : position(0), cluster(0) {}
  43. };
  44. // use the gnu style oflag in open()
  45. /** open() oflag for reading */
  46. uint8_t const O_READ = 0X01;
  47. /** open() oflag - same as O_IN */
  48. uint8_t const O_RDONLY = O_READ;
  49. /** open() oflag for write */
  50. uint8_t const O_WRITE = 0X02;
  51. /** open() oflag - same as O_WRITE */
  52. uint8_t const O_WRONLY = O_WRITE;
  53. /** open() oflag for reading and writing */
  54. uint8_t const O_RDWR = (O_READ | O_WRITE);
  55. /** open() oflag mask for access modes */
  56. uint8_t const O_ACCMODE = (O_READ | O_WRITE);
  57. /** The file offset shall be set to the end of the file prior to each write. */
  58. uint8_t const O_APPEND = 0X04;
  59. /** synchronous writes - call sync() after each write */
  60. uint8_t const O_SYNC = 0X08;
  61. /** truncate the file to zero length */
  62. uint8_t const O_TRUNC = 0X10;
  63. /** set the initial position at the end of the file */
  64. uint8_t const O_AT_END = 0X20;
  65. /** create the file if nonexistent */
  66. uint8_t const O_CREAT = 0X40;
  67. /** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
  68. uint8_t const O_EXCL = 0X80;
  69. // SdBaseFile class static and const definitions
  70. // flags for ls()
  71. /** ls() flag to print modify date */
  72. uint8_t const LS_DATE = 1;
  73. /** ls() flag to print file size */
  74. uint8_t const LS_SIZE = 2;
  75. /** ls() flag for recursive list of subdirectories */
  76. uint8_t const LS_R = 4;
  77. // flags for timestamp
  78. /** set the file's last access date */
  79. uint8_t const T_ACCESS = 1;
  80. /** set the file's creation date and time */
  81. uint8_t const T_CREATE = 2;
  82. /** Set the file's write date and time */
  83. uint8_t const T_WRITE = 4;
  84. // values for type_
  85. /** This file has not been opened. */
  86. uint8_t const FAT_FILE_TYPE_CLOSED = 0;
  87. /** A normal file */
  88. uint8_t const FAT_FILE_TYPE_NORMAL = 1;
  89. /** A FAT12 or FAT16 root directory */
  90. uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2;
  91. /** A FAT32 root directory */
  92. uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
  93. /** A subdirectory file*/
  94. uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
  95. /** Test value for directory type */
  96. uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED;
  97. /** date field for FAT directory entry
  98. * \param[in] year [1980,2107]
  99. * \param[in] month [1,12]
  100. * \param[in] day [1,31]
  101. *
  102. * \return Packed date for dir_t entry.
  103. */
  104. static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
  105. return (year - 1980) << 9 | month << 5 | day;
  106. }
  107. /** year part of FAT directory date field
  108. * \param[in] fatDate Date in packed dir format.
  109. *
  110. * \return Extracted year [1980,2107]
  111. */
  112. static inline uint16_t FAT_YEAR(uint16_t fatDate) {
  113. return 1980 + (fatDate >> 9);
  114. }
  115. /** month part of FAT directory date field
  116. * \param[in] fatDate Date in packed dir format.
  117. *
  118. * \return Extracted month [1,12]
  119. */
  120. static inline uint8_t FAT_MONTH(uint16_t fatDate) {
  121. return (fatDate >> 5) & 0XF;
  122. }
  123. /** day part of FAT directory date field
  124. * \param[in] fatDate Date in packed dir format.
  125. *
  126. * \return Extracted day [1,31]
  127. */
  128. static inline uint8_t FAT_DAY(uint16_t fatDate) {
  129. return fatDate & 0X1F;
  130. }
  131. /** time field for FAT directory entry
  132. * \param[in] hour [0,23]
  133. * \param[in] minute [0,59]
  134. * \param[in] second [0,59]
  135. *
  136. * \return Packed time for dir_t entry.
  137. */
  138. static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
  139. return hour << 11 | minute << 5 | second >> 1;
  140. }
  141. /** hour part of FAT directory time field
  142. * \param[in] fatTime Time in packed dir format.
  143. *
  144. * \return Extracted hour [0,23]
  145. */
  146. static inline uint8_t FAT_HOUR(uint16_t fatTime) {
  147. return fatTime >> 11;
  148. }
  149. /** minute part of FAT directory time field
  150. * \param[in] fatTime Time in packed dir format.
  151. *
  152. * \return Extracted minute [0,59]
  153. */
  154. static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
  155. return(fatTime >> 5) & 0X3F;
  156. }
  157. /** second part of FAT directory time field
  158. * Note second/2 is stored in packed time.
  159. *
  160. * \param[in] fatTime Time in packed dir format.
  161. *
  162. * \return Extracted second [0,58]
  163. */
  164. static inline uint8_t FAT_SECOND(uint16_t fatTime) {
  165. return 2*(fatTime & 0X1F);
  166. }
  167. /** Default date for file timestamps is 1 Jan 2000 */
  168. uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
  169. /** Default time for file timestamp is 1 am */
  170. uint16_t const FAT_DEFAULT_TIME = (1 << 11);
  171. //------------------------------------------------------------------------------
  172. /**
  173. * \class SdBaseFile
  174. * \brief Base class for SdFile with Print and C++ streams.
  175. */
  176. class SdBaseFile {
  177. public:
  178. /** Create an instance. */
  179. SdBaseFile() : writeError(false), type_(FAT_FILE_TYPE_CLOSED) {}
  180. SdBaseFile(const char* path, uint8_t oflag);
  181. ~SdBaseFile() {if(isOpen()) close();}
  182. /**
  183. * writeError is set to true if an error occurs during a write().
  184. * Set writeError to false before calling print() and/or write() and check
  185. * for true after calls to print() and/or write().
  186. */
  187. bool writeError;
  188. //----------------------------------------------------------------------------
  189. // helpers for stream classes
  190. /** get position for streams
  191. * \param[out] pos struct to receive position
  192. */
  193. void getpos(filepos_t* pos);
  194. /** set position for streams
  195. * \param[out] pos struct with value for new position
  196. */
  197. void setpos(filepos_t* pos);
  198. //----------------------------------------------------------------------------
  199. bool close();
  200. bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
  201. bool createContiguous(SdBaseFile* dirFile,
  202. const char* path, uint32_t size);
  203. /** \return The current cluster number for a file or directory. */
  204. uint32_t curCluster() const {return curCluster_;}
  205. /** \return The current position for a file or directory. */
  206. uint32_t curPosition() const {return curPosition_;}
  207. /** \return Current working directory */
  208. static SdBaseFile* cwd() {return cwd_;}
  209. /** Set the date/time callback function
  210. *
  211. * \param[in] dateTime The user's call back function. The callback
  212. * function is of the form:
  213. *
  214. * \code
  215. * void dateTime(uint16_t* date, uint16_t* time) {
  216. * uint16_t year;
  217. * uint8_t month, day, hour, minute, second;
  218. *
  219. * // User gets date and time from GPS or real-time clock here
  220. *
  221. * // return date using FAT_DATE macro to format fields
  222. * *date = FAT_DATE(year, month, day);
  223. *
  224. * // return time using FAT_TIME macro to format fields
  225. * *time = FAT_TIME(hour, minute, second);
  226. * }
  227. * \endcode
  228. *
  229. * Sets the function that is called when a file is created or when
  230. * a file's directory entry is modified by sync(). All timestamps,
  231. * access, creation, and modify, are set when a file is created.
  232. * sync() maintains the last access date and last modify date/time.
  233. *
  234. * See the timestamp() function.
  235. */
  236. static void dateTimeCallback(
  237. void (*dateTime)(uint16_t* date, uint16_t* time)) {
  238. dateTime_ = dateTime;
  239. }
  240. /** Cancel the date/time callback function. */
  241. static void dateTimeCallbackCancel() {dateTime_ = 0;}
  242. bool dirEntry(dir_t* dir);
  243. static void dirName(const dir_t& dir, char* name);
  244. bool exists(const char* name);
  245. int16_t fgets(char* str, int16_t num, char* delim = 0);
  246. /** \return The total number of bytes in a file or directory. */
  247. uint32_t fileSize() const {return fileSize_;}
  248. /** \return The first cluster number for a file or directory. */
  249. uint32_t firstCluster() const {return firstCluster_;}
  250. bool getFilename(char* name);
  251. /** \return True if this is a directory else false. */
  252. bool isDir() const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
  253. /** \return True if this is a normal file else false. */
  254. bool isFile() const {return type_ == FAT_FILE_TYPE_NORMAL;}
  255. /** \return True if this is an open file/directory else false. */
  256. bool isOpen() const {return type_ != FAT_FILE_TYPE_CLOSED;}
  257. /** \return True if this is a subdirectory else false. */
  258. bool isSubDir() const {return type_ == FAT_FILE_TYPE_SUBDIR;}
  259. /** \return True if this is the root directory. */
  260. bool isRoot() const {
  261. return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32;
  262. }
  263. void ls( uint8_t flags = 0, uint8_t indent = 0);
  264. bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
  265. // alias for backward compactability
  266. bool makeDir(SdBaseFile* dir, const char* path) {
  267. return mkdir(dir, path, false);
  268. }
  269. bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
  270. bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
  271. bool open(const char* path, uint8_t oflag = O_READ);
  272. bool openNext(SdBaseFile* dirFile, uint8_t oflag);
  273. bool openRoot(SdVolume* vol);
  274. int peek();
  275. static void printFatDate(uint16_t fatDate);
  276. static void printFatTime( uint16_t fatTime);
  277. bool printName();
  278. protected:
  279. int16_t read();
  280. int16_t read(void* buf, uint16_t nbyte);
  281. public:
  282. int8_t readDir(dir_t* dir, char* longFilename);
  283. static bool remove(SdBaseFile* dirFile, const char* path);
  284. bool remove();
  285. /** Set the file's current position to zero. */
  286. void rewind() {seekSet(0);}
  287. bool rename(SdBaseFile* dirFile, const char* newPath);
  288. bool rmdir();
  289. // for backward compatibility
  290. bool rmDir() {return rmdir();}
  291. bool rmRfStar();
  292. /** Set the files position to current position + \a pos. See seekSet().
  293. * \param[in] offset The new position in bytes from the current position.
  294. * \return true for success or false for failure.
  295. */
  296. bool seekCur(int32_t offset) {
  297. return seekSet(curPosition_ + offset);
  298. }
  299. /** Set the files position to end-of-file + \a offset. See seekSet().
  300. * \param[in] offset The new position in bytes from end-of-file.
  301. * \return true for success or false for failure.
  302. */
  303. bool seekEnd(int32_t offset = 0) {return seekSet(fileSize_ + offset);}
  304. bool seekSet(uint32_t pos);
  305. bool sync();
  306. bool timestamp(SdBaseFile* file);
  307. bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
  308. uint8_t hour, uint8_t minute, uint8_t second);
  309. /** Type of file. You should use isFile() or isDir() instead of type()
  310. * if possible.
  311. *
  312. * \return The file or directory type.
  313. */
  314. uint8_t type() const {return type_;}
  315. bool truncate(uint32_t size);
  316. /** \return SdVolume that contains this file. */
  317. SdVolume* volume() const {return vol_;}
  318. int16_t write(const void* buf, uint16_t nbyte);
  319. //------------------------------------------------------------------------------
  320. protected:
  321. // allow SdFat to set cwd_
  322. friend class SdFat;
  323. // global pointer to cwd dir
  324. static SdBaseFile* cwd_;
  325. // data time callback function
  326. static void (*dateTime_)(uint16_t* date, uint16_t* time);
  327. // bits defined in flags_
  328. // should be 0X0F
  329. static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
  330. // sync of directory entry required
  331. static uint8_t const F_FILE_DIR_DIRTY = 0X80;
  332. // private data
  333. uint8_t flags_; // See above for definition of flags_ bits
  334. uint8_t fstate_; // error and eof indicator
  335. uint8_t type_; // type of file see above for values
  336. uint32_t curCluster_; // cluster for current file position
  337. uint32_t curPosition_; // current file position in bytes from beginning
  338. uint32_t dirBlock_; // block for this files directory entry
  339. uint8_t dirIndex_; // index of directory entry in dirBlock
  340. uint32_t fileSize_; // file size in bytes
  341. uint32_t firstCluster_; // first cluster of file
  342. SdVolume* vol_; // volume where file is located
  343. /** experimental don't use */
  344. bool openParent(SdBaseFile* dir);
  345. // private functions
  346. bool addCluster();
  347. bool addDirCluster();
  348. dir_t* cacheDirEntry(uint8_t action);
  349. int8_t lsPrintNext( uint8_t flags, uint8_t indent);
  350. static bool make83Name(const char* str, uint8_t* name, const char** ptr);
  351. bool mkdir(SdBaseFile* parent, const uint8_t dname[11]);
  352. bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
  353. bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
  354. dir_t* readDirCache();
  355. //------------------------------------------------------------------------------
  356. // to be deleted
  357. static void printDirName( const dir_t& dir,
  358. uint8_t width, bool printSlash);
  359. //------------------------------------------------------------------------------
  360. // Deprecated functions - suppress cpplint warnings with NOLINT comment
  361. #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
  362. public:
  363. /** \deprecated Use:
  364. * bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
  365. * \param[out] bgnBlock the first block address for the file.
  366. * \param[out] endBlock the last block address for the file.
  367. * \return true for success or false for failure.
  368. */
  369. bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT
  370. return contiguousRange(&bgnBlock, &endBlock);
  371. }
  372. /** \deprecated Use:
  373. * bool createContiguous(SdBaseFile* dirFile,
  374. * const char* path, uint32_t size)
  375. * \param[in] dirFile The directory where the file will be created.
  376. * \param[in] path A path with a valid DOS 8.3 file name.
  377. * \param[in] size The desired file size.
  378. * \return true for success or false for failure.
  379. */
  380. bool createContiguous(SdBaseFile& dirFile, // NOLINT
  381. const char* path, uint32_t size) {
  382. return createContiguous(&dirFile, path, size);
  383. }
  384. /** \deprecated Use:
  385. * static void dateTimeCallback(
  386. * void (*dateTime)(uint16_t* date, uint16_t* time));
  387. * \param[in] dateTime The user's call back function.
  388. */
  389. static void dateTimeCallback(
  390. void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
  391. oldDateTime_ = dateTime;
  392. dateTime_ = dateTime ? oldToNew : 0;
  393. }
  394. /** \deprecated Use: bool dirEntry(dir_t* dir);
  395. * \param[out] dir Location for return of the file's directory entry.
  396. * \return true for success or false for failure.
  397. */
  398. bool dirEntry(dir_t& dir) {return dirEntry(&dir);} // NOLINT
  399. /** \deprecated Use:
  400. * bool mkdir(SdBaseFile* dir, const char* path);
  401. * \param[in] dir An open SdFat instance for the directory that will contain
  402. * the new directory.
  403. * \param[in] path A path with a valid 8.3 DOS name for the new directory.
  404. * \return true for success or false for failure.
  405. */
  406. bool mkdir(SdBaseFile& dir, const char* path) { // NOLINT
  407. return mkdir(&dir, path);
  408. }
  409. /** \deprecated Use:
  410. * bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
  411. * \param[in] dirFile An open SdFat instance for the directory containing the
  412. * file to be opened.
  413. * \param[in] path A path with a valid 8.3 DOS name for the file.
  414. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  415. * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
  416. * \return true for success or false for failure.
  417. */
  418. bool open(SdBaseFile& dirFile, // NOLINT
  419. const char* path, uint8_t oflag) {
  420. return open(&dirFile, path, oflag);
  421. }
  422. /** \deprecated Do not use in new apps
  423. * \param[in] dirFile An open SdFat instance for the directory containing the
  424. * file to be opened.
  425. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  426. * \return true for success or false for failure.
  427. */
  428. bool open(SdBaseFile& dirFile, const char* path) { // NOLINT
  429. return open(dirFile, path, O_RDWR);
  430. }
  431. /** \deprecated Use:
  432. * bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
  433. * \param[in] dirFile An open SdFat instance for the directory.
  434. * \param[in] index The \a index of the directory entry for the file to be
  435. * opened. The value for \a index is (directory file position)/32.
  436. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  437. * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
  438. * \return true for success or false for failure.
  439. */
  440. bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT
  441. return open(&dirFile, index, oflag);
  442. }
  443. /** \deprecated Use: bool openRoot(SdVolume* vol);
  444. * \param[in] vol The FAT volume containing the root directory to be opened.
  445. * \return true for success or false for failure.
  446. */
  447. bool openRoot(SdVolume& vol) {return openRoot(&vol);} // NOLINT
  448. /** \deprecated Use: int8_t readDir(dir_t* dir);
  449. * \param[out] dir The dir_t struct that will receive the data.
  450. * \return bytes read for success zero for eof or -1 for failure.
  451. */
  452. int8_t readDir(dir_t& dir, char* longFilename) {return readDir(&dir, longFilename);} // NOLINT
  453. /** \deprecated Use:
  454. * static uint8_t remove(SdBaseFile* dirFile, const char* path);
  455. * \param[in] dirFile The directory that contains the file.
  456. * \param[in] path The name of the file to be removed.
  457. * \return true for success or false for failure.
  458. */
  459. static bool remove(SdBaseFile& dirFile, const char* path) { // NOLINT
  460. return remove(&dirFile, path);
  461. }
  462. //------------------------------------------------------------------------------
  463. // rest are private
  464. private:
  465. static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
  466. static void oldToNew(uint16_t* date, uint16_t* time) {
  467. uint16_t d;
  468. uint16_t t;
  469. oldDateTime_(d, t);
  470. *date = d;
  471. *time = t;
  472. }
  473. #endif // ALLOW_DEPRECATED_FUNCTIONS
  474. };
  475. #endif // SdBaseFile_h
  476. #endif