My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SdBaseFile.cpp 53KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. #define SERIAL MSerial
  21. #include "SdBaseFile.h"
  22. //------------------------------------------------------------------------------
  23. // pointer to cwd directory
  24. SdBaseFile* SdBaseFile::cwd_ = 0;
  25. // callback function for date/time
  26. void (*SdBaseFile::dateTime_)(uint16_t* date, uint16_t* time) = 0;
  27. //------------------------------------------------------------------------------
  28. // add a cluster to a file
  29. bool SdBaseFile::addCluster() {
  30. if (!vol_->allocContiguous(1, &curCluster_)) goto fail;
  31. // if first cluster of file link to directory entry
  32. if (firstCluster_ == 0) {
  33. firstCluster_ = curCluster_;
  34. flags_ |= F_FILE_DIR_DIRTY;
  35. }
  36. return true;
  37. fail:
  38. return false;
  39. }
  40. //------------------------------------------------------------------------------
  41. // Add a cluster to a directory file and zero the cluster.
  42. // return with first block of cluster in the cache
  43. bool SdBaseFile::addDirCluster() {
  44. uint32_t block;
  45. // max folder size
  46. if (fileSize_/sizeof(dir_t) >= 0XFFFF) goto fail;
  47. if (!addCluster()) goto fail;
  48. if (!vol_->cacheFlush()) goto fail;
  49. block = vol_->clusterStartBlock(curCluster_);
  50. // set cache to first block of cluster
  51. vol_->cacheSetBlockNumber(block, true);
  52. // zero first block of cluster
  53. memset(vol_->cacheBuffer_.data, 0, 512);
  54. // zero rest of cluster
  55. for (uint8_t i = 1; i < vol_->blocksPerCluster_; i++) {
  56. if (!vol_->writeBlock(block + i, vol_->cacheBuffer_.data)) goto fail;
  57. }
  58. // Increase directory file size by cluster size
  59. fileSize_ += 512UL << vol_->clusterSizeShift_;
  60. return true;
  61. fail:
  62. return false;
  63. }
  64. //------------------------------------------------------------------------------
  65. // cache a file's directory entry
  66. // return pointer to cached entry or null for failure
  67. dir_t* SdBaseFile::cacheDirEntry(uint8_t action) {
  68. if (!vol_->cacheRawBlock(dirBlock_, action)) goto fail;
  69. return vol_->cache()->dir + dirIndex_;
  70. fail:
  71. return 0;
  72. }
  73. //------------------------------------------------------------------------------
  74. /** Close a file and force cached data and directory information
  75. * to be written to the storage device.
  76. *
  77. * \return The value one, true, is returned for success and
  78. * the value zero, false, is returned for failure.
  79. * Reasons for failure include no file is open or an I/O error.
  80. */
  81. bool SdBaseFile::close() {
  82. bool rtn = sync();
  83. type_ = FAT_FILE_TYPE_CLOSED;
  84. return rtn;
  85. }
  86. //------------------------------------------------------------------------------
  87. /** Check for contiguous file and return its raw block range.
  88. *
  89. * \param[out] bgnBlock the first block address for the file.
  90. * \param[out] endBlock the last block address for the file.
  91. *
  92. * \return The value one, true, is returned for success and
  93. * the value zero, false, is returned for failure.
  94. * Reasons for failure include file is not contiguous, file has zero length
  95. * or an I/O error occurred.
  96. */
  97. bool SdBaseFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock) {
  98. // error if no blocks
  99. if (firstCluster_ == 0) goto fail;
  100. for (uint32_t c = firstCluster_; ; c++) {
  101. uint32_t next;
  102. if (!vol_->fatGet(c, &next)) goto fail;
  103. // check for contiguous
  104. if (next != (c + 1)) {
  105. // error if not end of chain
  106. if (!vol_->isEOC(next)) goto fail;
  107. *bgnBlock = vol_->clusterStartBlock(firstCluster_);
  108. *endBlock = vol_->clusterStartBlock(c)
  109. + vol_->blocksPerCluster_ - 1;
  110. return true;
  111. }
  112. }
  113. fail:
  114. return false;
  115. }
  116. //------------------------------------------------------------------------------
  117. /** Create and open a new contiguous file of a specified size.
  118. *
  119. * \note This function only supports short DOS 8.3 names.
  120. * See open() for more information.
  121. *
  122. * \param[in] dirFile The directory where the file will be created.
  123. * \param[in] path A path with a valid DOS 8.3 file name.
  124. * \param[in] size The desired file size.
  125. *
  126. * \return The value one, true, is returned for success and
  127. * the value zero, false, is returned for failure.
  128. * Reasons for failure include \a path contains
  129. * an invalid DOS 8.3 file name, the FAT volume has not been initialized,
  130. * a file is already open, the file already exists, the root
  131. * directory is full or an I/O error.
  132. *
  133. */
  134. bool SdBaseFile::createContiguous(SdBaseFile* dirFile,
  135. const char* path, uint32_t size) {
  136. uint32_t count;
  137. // don't allow zero length file
  138. if (size == 0) goto fail;
  139. if (!open(dirFile, path, O_CREAT | O_EXCL | O_RDWR)) goto fail;
  140. // calculate number of clusters needed
  141. count = ((size - 1) >> (vol_->clusterSizeShift_ + 9)) + 1;
  142. // allocate clusters
  143. if (!vol_->allocContiguous(count, &firstCluster_)) {
  144. remove();
  145. goto fail;
  146. }
  147. fileSize_ = size;
  148. // insure sync() will update dir entry
  149. flags_ |= F_FILE_DIR_DIRTY;
  150. return sync();
  151. fail:
  152. return false;
  153. }
  154. //------------------------------------------------------------------------------
  155. /** Return a file's directory entry.
  156. *
  157. * \param[out] dir Location for return of the file's directory entry.
  158. *
  159. * \return The value one, true, is returned for success and
  160. * the value zero, false, is returned for failure.
  161. */
  162. bool SdBaseFile::dirEntry(dir_t* dir) {
  163. dir_t* p;
  164. // make sure fields on SD are correct
  165. if (!sync()) goto fail;
  166. // read entry
  167. p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
  168. if (!p) goto fail;
  169. // copy to caller's struct
  170. memcpy(dir, p, sizeof(dir_t));
  171. return true;
  172. fail:
  173. return false;
  174. }
  175. //------------------------------------------------------------------------------
  176. /** Format the name field of \a dir into the 13 byte array
  177. * \a name in standard 8.3 short name format.
  178. *
  179. * \param[in] dir The directory structure containing the name.
  180. * \param[out] name A 13 byte char array for the formatted name.
  181. */
  182. void SdBaseFile::dirName(const dir_t& dir, char* name) {
  183. uint8_t j = 0;
  184. for (uint8_t i = 0; i < 11; i++) {
  185. if (dir.name[i] == ' ')continue;
  186. if (i == 8) name[j++] = '.';
  187. name[j++] = dir.name[i];
  188. }
  189. name[j] = 0;
  190. }
  191. //------------------------------------------------------------------------------
  192. /** Test for the existence of a file in a directory
  193. *
  194. * \param[in] name Name of the file to be tested for.
  195. *
  196. * The calling instance must be an open directory file.
  197. *
  198. * dirFile.exists("TOFIND.TXT") searches for "TOFIND.TXT" in the directory
  199. * dirFile.
  200. *
  201. * \return true if the file exists else false.
  202. */
  203. bool SdBaseFile::exists(const char* name) {
  204. SdBaseFile file;
  205. return file.open(this, name, O_READ);
  206. }
  207. //------------------------------------------------------------------------------
  208. /**
  209. * Get a string from a file.
  210. *
  211. * fgets() reads bytes from a file into the array pointed to by \a str, until
  212. * \a num - 1 bytes are read, or a delimiter is read and transferred to \a str,
  213. * or end-of-file is encountered. The string is then terminated
  214. * with a null byte.
  215. *
  216. * fgets() deletes CR, '\\r', from the string. This insures only a '\\n'
  217. * terminates the string for Windows text files which use CRLF for newline.
  218. *
  219. * \param[out] str Pointer to the array where the string is stored.
  220. * \param[in] num Maximum number of characters to be read
  221. * (including the final null byte). Usually the length
  222. * of the array \a str is used.
  223. * \param[in] delim Optional set of delimiters. The default is "\n".
  224. *
  225. * \return For success fgets() returns the length of the string in \a str.
  226. * If no data is read, fgets() returns zero for EOF or -1 if an error occurred.
  227. **/
  228. int16_t SdBaseFile::fgets(char* str, int16_t num, char* delim) {
  229. char ch;
  230. int16_t n = 0;
  231. int16_t r = -1;
  232. while ((n + 1) < num && (r = read(&ch, 1)) == 1) {
  233. // delete CR
  234. if (ch == '\r') continue;
  235. str[n++] = ch;
  236. if (!delim) {
  237. if (ch == '\n') break;
  238. } else {
  239. if (strchr(delim, ch)) break;
  240. }
  241. }
  242. if (r < 0) {
  243. // read error
  244. return -1;
  245. }
  246. str[n] = '\0';
  247. return n;
  248. }
  249. //------------------------------------------------------------------------------
  250. /** Get a file's name
  251. *
  252. * \param[out] name An array of 13 characters for the file's name.
  253. *
  254. * \return The value one, true, is returned for success and
  255. * the value zero, false, is returned for failure.
  256. */
  257. bool SdBaseFile::getFilename(char* name) {
  258. if (!isOpen()) return false;
  259. if (isRoot()) {
  260. name[0] = '/';
  261. name[1] = '\0';
  262. return true;
  263. }
  264. // cache entry
  265. dir_t* p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
  266. if (!p) return false;
  267. // format name
  268. dirName(*p, name);
  269. return true;
  270. }
  271. //------------------------------------------------------------------------------
  272. void SdBaseFile::getpos(fpos_t* pos) {
  273. pos->position = curPosition_;
  274. pos->cluster = curCluster_;
  275. }
  276. //------------------------------------------------------------------------------
  277. /** List directory contents.
  278. *
  279. * \param[in] pr Print stream for list.
  280. *
  281. * \param[in] flags The inclusive OR of
  282. *
  283. * LS_DATE - %Print file modification date
  284. *
  285. * LS_SIZE - %Print file size.
  286. *
  287. * LS_R - Recursive list of subdirectories.
  288. *
  289. * \param[in] indent Amount of space before file name. Used for recursive
  290. * list to indicate subdirectory level.
  291. */
  292. void SdBaseFile::ls(uint8_t flags, uint8_t indent) {
  293. rewind();
  294. int8_t status;
  295. while ((status = lsPrintNext( flags, indent))) {
  296. if (status > 1 && (flags & LS_R)) {
  297. uint16_t index = curPosition()/32 - 1;
  298. SdBaseFile s;
  299. if (s.open(this, index, O_READ)) s.ls( flags, indent + 2);
  300. seekSet(32 * (index + 1));
  301. }
  302. }
  303. }
  304. //------------------------------------------------------------------------------
  305. // saves 32 bytes on stack for ls recursion
  306. // return 0 - EOF, 1 - normal file, or 2 - directory
  307. int8_t SdBaseFile::lsPrintNext( uint8_t flags, uint8_t indent) {
  308. dir_t dir;
  309. uint8_t w = 0;
  310. while (1) {
  311. if (read(&dir, sizeof(dir)) != sizeof(dir)) return 0;
  312. if (dir.name[0] == DIR_NAME_FREE) return 0;
  313. // skip deleted entry and entries for . and ..
  314. if (dir.name[0] != DIR_NAME_DELETED && dir.name[0] != '.'
  315. && DIR_IS_FILE_OR_SUBDIR(&dir)) break;
  316. }
  317. // indent for dir level
  318. for (uint8_t i = 0; i < indent; i++) MSerial.write(' ');
  319. // print name
  320. for (uint8_t i = 0; i < 11; i++) {
  321. if (dir.name[i] == ' ')continue;
  322. if (i == 8) {
  323. MSerial.write('.');
  324. w++;
  325. }
  326. MSerial.write(dir.name[i]);
  327. w++;
  328. }
  329. if (DIR_IS_SUBDIR(&dir)) {
  330. MSerial.write('/');
  331. w++;
  332. }
  333. if (flags & (LS_DATE | LS_SIZE)) {
  334. while (w++ < 14) MSerial.write(' ');
  335. }
  336. // print modify date/time if requested
  337. if (flags & LS_DATE) {
  338. MSerial.write(' ');
  339. printFatDate( dir.lastWriteDate);
  340. MSerial.write(' ');
  341. printFatTime( dir.lastWriteTime);
  342. }
  343. // print size if requested
  344. if (!DIR_IS_SUBDIR(&dir) && (flags & LS_SIZE)) {
  345. MSerial.write(' ');
  346. MSerial.print(dir.fileSize);
  347. }
  348. MSerial.println();
  349. return DIR_IS_FILE(&dir) ? 1 : 2;
  350. }
  351. //------------------------------------------------------------------------------
  352. // format directory name field from a 8.3 name string
  353. bool SdBaseFile::make83Name(const char* str, uint8_t* name, const char** ptr) {
  354. uint8_t c;
  355. uint8_t n = 7; // max index for part before dot
  356. uint8_t i = 0;
  357. // blank fill name and extension
  358. while (i < 11) name[i++] = ' ';
  359. i = 0;
  360. while (*str != '\0' && *str != '/') {
  361. c = *str++;
  362. if (c == '.') {
  363. if (n == 10) goto fail; // only one dot allowed
  364. n = 10; // max index for full 8.3 name
  365. i = 8; // place for extension
  366. } else {
  367. // illegal FAT characters
  368. PGM_P p = PSTR("|<>^+=?/[];,*\"\\");
  369. uint8_t b;
  370. while ((b = pgm_read_byte(p++))) if (b == c) goto fail;
  371. // check size and only allow ASCII printable characters
  372. if (i > n || c < 0X21 || c > 0X7E)goto fail;
  373. // only upper case allowed in 8.3 names - convert lower to upper
  374. name[i++] = c < 'a' || c > 'z' ? c : c + ('A' - 'a');
  375. }
  376. }
  377. *ptr = str;
  378. // must have a file name, extension is optional
  379. return name[0] != ' ';
  380. fail:
  381. return false;
  382. }
  383. //------------------------------------------------------------------------------
  384. /** Make a new directory.
  385. *
  386. * \param[in] parent An open SdFat instance for the directory that will contain
  387. * the new directory.
  388. *
  389. * \param[in] path A path with a valid 8.3 DOS name for the new directory.
  390. *
  391. * \param[in] pFlag Create missing parent directories if true.
  392. *
  393. * \return The value one, true, is returned for success and
  394. * the value zero, false, is returned for failure.
  395. * Reasons for failure include this file is already open, \a parent is not a
  396. * directory, \a path is invalid or already exists in \a parent.
  397. */
  398. bool SdBaseFile::mkdir(SdBaseFile* parent, const char* path, bool pFlag) {
  399. uint8_t dname[11];
  400. SdBaseFile dir1, dir2;
  401. SdBaseFile* sub = &dir1;
  402. SdBaseFile* start = parent;
  403. if (!parent || isOpen()) goto fail;
  404. if (*path == '/') {
  405. while (*path == '/') path++;
  406. if (!parent->isRoot()) {
  407. if (!dir2.openRoot(parent->vol_)) goto fail;
  408. parent = &dir2;
  409. }
  410. }
  411. while (1) {
  412. if (!make83Name(path, dname, &path)) goto fail;
  413. while (*path == '/') path++;
  414. if (!*path) break;
  415. if (!sub->open(parent, dname, O_READ)) {
  416. if (!pFlag || !sub->mkdir(parent, dname)) {
  417. goto fail;
  418. }
  419. }
  420. if (parent != start) parent->close();
  421. parent = sub;
  422. sub = parent != &dir1 ? &dir1 : &dir2;
  423. }
  424. return mkdir(parent, dname);
  425. fail:
  426. return false;
  427. }
  428. //------------------------------------------------------------------------------
  429. bool SdBaseFile::mkdir(SdBaseFile* parent, const uint8_t dname[11]) {
  430. uint32_t block;
  431. dir_t d;
  432. dir_t* p;
  433. if (!parent->isDir()) goto fail;
  434. // create a normal file
  435. if (!open(parent, dname, O_CREAT | O_EXCL | O_RDWR)) goto fail;
  436. // convert file to directory
  437. flags_ = O_READ;
  438. type_ = FAT_FILE_TYPE_SUBDIR;
  439. // allocate and zero first cluster
  440. if (!addDirCluster())goto fail;
  441. // force entry to SD
  442. if (!sync()) goto fail;
  443. // cache entry - should already be in cache due to sync() call
  444. p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  445. if (!p) goto fail;
  446. // change directory entry attribute
  447. p->attributes = DIR_ATT_DIRECTORY;
  448. // make entry for '.'
  449. memcpy(&d, p, sizeof(d));
  450. d.name[0] = '.';
  451. for (uint8_t i = 1; i < 11; i++) d.name[i] = ' ';
  452. // cache block for '.' and '..'
  453. block = vol_->clusterStartBlock(firstCluster_);
  454. if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail;
  455. // copy '.' to block
  456. memcpy(&vol_->cache()->dir[0], &d, sizeof(d));
  457. // make entry for '..'
  458. d.name[1] = '.';
  459. if (parent->isRoot()) {
  460. d.firstClusterLow = 0;
  461. d.firstClusterHigh = 0;
  462. } else {
  463. d.firstClusterLow = parent->firstCluster_ & 0XFFFF;
  464. d.firstClusterHigh = parent->firstCluster_ >> 16;
  465. }
  466. // copy '..' to block
  467. memcpy(&vol_->cache()->dir[1], &d, sizeof(d));
  468. // write first block
  469. return vol_->cacheFlush();
  470. fail:
  471. return false;
  472. }
  473. //------------------------------------------------------------------------------
  474. /** Open a file in the current working directory.
  475. *
  476. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  477. *
  478. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  479. * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
  480. *
  481. * \return The value one, true, is returned for success and
  482. * the value zero, false, is returned for failure.
  483. */
  484. bool SdBaseFile::open(const char* path, uint8_t oflag) {
  485. return open(cwd_, path, oflag);
  486. }
  487. //------------------------------------------------------------------------------
  488. /** Open a file or directory by name.
  489. *
  490. * \param[in] dirFile An open SdFat instance for the directory containing the
  491. * file to be opened.
  492. *
  493. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  494. *
  495. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  496. * OR of flags from the following list
  497. *
  498. * O_READ - Open for reading.
  499. *
  500. * O_RDONLY - Same as O_READ.
  501. *
  502. * O_WRITE - Open for writing.
  503. *
  504. * O_WRONLY - Same as O_WRITE.
  505. *
  506. * O_RDWR - Open for reading and writing.
  507. *
  508. * O_APPEND - If set, the file offset shall be set to the end of the
  509. * file prior to each write.
  510. *
  511. * O_AT_END - Set the initial position at the end of the file.
  512. *
  513. * O_CREAT - If the file exists, this flag has no effect except as noted
  514. * under O_EXCL below. Otherwise, the file shall be created
  515. *
  516. * O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.
  517. *
  518. * O_SYNC - Call sync() after each write. This flag should not be used with
  519. * write(uint8_t), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class.
  520. * These functions do character at a time writes so sync() will be called
  521. * after each byte.
  522. *
  523. * O_TRUNC - If the file exists and is a regular file, and the file is
  524. * successfully opened and is not read only, its length shall be truncated to 0.
  525. *
  526. * WARNING: A given file must not be opened by more than one SdBaseFile object
  527. * of file corruption may occur.
  528. *
  529. * \note Directory files must be opened read only. Write and truncation is
  530. * not allowed for directory files.
  531. *
  532. * \return The value one, true, is returned for success and
  533. * the value zero, false, is returned for failure.
  534. * Reasons for failure include this file is already open, \a dirFile is not
  535. * a directory, \a path is invalid, the file does not exist
  536. * or can't be opened in the access mode specified by oflag.
  537. */
  538. bool SdBaseFile::open(SdBaseFile* dirFile, const char* path, uint8_t oflag) {
  539. uint8_t dname[11];
  540. SdBaseFile dir1, dir2;
  541. SdBaseFile *parent = dirFile;
  542. SdBaseFile *sub = &dir1;
  543. if (!dirFile) goto fail;
  544. // error if already open
  545. if (isOpen()) goto fail;
  546. if (*path == '/') {
  547. while (*path == '/') path++;
  548. if (!dirFile->isRoot()) {
  549. if (!dir2.openRoot(dirFile->vol_)) goto fail;
  550. parent = &dir2;
  551. }
  552. }
  553. while (1) {
  554. if (!make83Name(path, dname, &path)) goto fail;
  555. while (*path == '/') path++;
  556. if (!*path) break;
  557. if (!sub->open(parent, dname, O_READ)) goto fail;
  558. if (parent != dirFile) parent->close();
  559. parent = sub;
  560. sub = parent != &dir1 ? &dir1 : &dir2;
  561. }
  562. return open(parent, dname, oflag);
  563. fail:
  564. return false;
  565. }
  566. //------------------------------------------------------------------------------
  567. // open with filename in dname
  568. bool SdBaseFile::open(SdBaseFile* dirFile,
  569. const uint8_t dname[11], uint8_t oflag) {
  570. bool emptyFound = false;
  571. bool fileFound = false;
  572. uint8_t index;
  573. dir_t* p;
  574. vol_ = dirFile->vol_;
  575. dirFile->rewind();
  576. // search for file
  577. while (dirFile->curPosition_ < dirFile->fileSize_) {
  578. index = 0XF & (dirFile->curPosition_ >> 5);
  579. p = dirFile->readDirCache();
  580. if (!p) goto fail;
  581. if (p->name[0] == DIR_NAME_FREE || p->name[0] == DIR_NAME_DELETED) {
  582. // remember first empty slot
  583. if (!emptyFound) {
  584. dirBlock_ = dirFile->vol_->cacheBlockNumber();
  585. dirIndex_ = index;
  586. emptyFound = true;
  587. }
  588. // done if no entries follow
  589. if (p->name[0] == DIR_NAME_FREE) break;
  590. } else if (!memcmp(dname, p->name, 11)) {
  591. fileFound = true;
  592. break;
  593. }
  594. }
  595. if (fileFound) {
  596. // don't open existing file if O_EXCL
  597. if (oflag & O_EXCL) goto fail;
  598. } else {
  599. // don't create unless O_CREAT and O_WRITE
  600. if (!(oflag & O_CREAT) || !(oflag & O_WRITE)) goto fail;
  601. if (emptyFound) {
  602. index = dirIndex_;
  603. p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  604. if (!p) goto fail;
  605. } else {
  606. if (dirFile->type_ == FAT_FILE_TYPE_ROOT_FIXED) goto fail;
  607. // add and zero cluster for dirFile - first cluster is in cache for write
  608. if (!dirFile->addDirCluster()) goto fail;
  609. // use first entry in cluster
  610. p = dirFile->vol_->cache()->dir;
  611. index = 0;
  612. }
  613. // initialize as empty file
  614. memset(p, 0, sizeof(dir_t));
  615. memcpy(p->name, dname, 11);
  616. // set timestamps
  617. if (dateTime_) {
  618. // call user date/time function
  619. dateTime_(&p->creationDate, &p->creationTime);
  620. } else {
  621. // use default date/time
  622. p->creationDate = FAT_DEFAULT_DATE;
  623. p->creationTime = FAT_DEFAULT_TIME;
  624. }
  625. p->lastAccessDate = p->creationDate;
  626. p->lastWriteDate = p->creationDate;
  627. p->lastWriteTime = p->creationTime;
  628. // write entry to SD
  629. if (!dirFile->vol_->cacheFlush()) goto fail;
  630. }
  631. // open entry in cache
  632. return openCachedEntry(index, oflag);
  633. fail:
  634. return false;
  635. }
  636. //------------------------------------------------------------------------------
  637. /** Open a file by index.
  638. *
  639. * \param[in] dirFile An open SdFat instance for the directory.
  640. *
  641. * \param[in] index The \a index of the directory entry for the file to be
  642. * opened. The value for \a index is (directory file position)/32.
  643. *
  644. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  645. * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
  646. *
  647. * See open() by path for definition of flags.
  648. * \return true for success or false for failure.
  649. */
  650. bool SdBaseFile::open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag) {
  651. dir_t* p;
  652. vol_ = dirFile->vol_;
  653. // error if already open
  654. if (isOpen() || !dirFile) goto fail;
  655. // don't open existing file if O_EXCL - user call error
  656. if (oflag & O_EXCL) goto fail;
  657. // seek to location of entry
  658. if (!dirFile->seekSet(32 * index)) goto fail;
  659. // read entry into cache
  660. p = dirFile->readDirCache();
  661. if (!p) goto fail;
  662. // error if empty slot or '.' or '..'
  663. if (p->name[0] == DIR_NAME_FREE ||
  664. p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') {
  665. goto fail;
  666. }
  667. // open cached entry
  668. return openCachedEntry(index & 0XF, oflag);
  669. fail:
  670. return false;
  671. }
  672. //------------------------------------------------------------------------------
  673. // open a cached directory entry. Assumes vol_ is initialized
  674. bool SdBaseFile::openCachedEntry(uint8_t dirIndex, uint8_t oflag) {
  675. // location of entry in cache
  676. dir_t* p = &vol_->cache()->dir[dirIndex];
  677. // write or truncate is an error for a directory or read-only file
  678. if (p->attributes & (DIR_ATT_READ_ONLY | DIR_ATT_DIRECTORY)) {
  679. if (oflag & (O_WRITE | O_TRUNC)) goto fail;
  680. }
  681. // remember location of directory entry on SD
  682. dirBlock_ = vol_->cacheBlockNumber();
  683. dirIndex_ = dirIndex;
  684. // copy first cluster number for directory fields
  685. firstCluster_ = (uint32_t)p->firstClusterHigh << 16;
  686. firstCluster_ |= p->firstClusterLow;
  687. // make sure it is a normal file or subdirectory
  688. if (DIR_IS_FILE(p)) {
  689. fileSize_ = p->fileSize;
  690. type_ = FAT_FILE_TYPE_NORMAL;
  691. } else if (DIR_IS_SUBDIR(p)) {
  692. if (!vol_->chainSize(firstCluster_, &fileSize_)) goto fail;
  693. type_ = FAT_FILE_TYPE_SUBDIR;
  694. } else {
  695. goto fail;
  696. }
  697. // save open flags for read/write
  698. flags_ = oflag & F_OFLAG;
  699. // set to start of file
  700. curCluster_ = 0;
  701. curPosition_ = 0;
  702. if ((oflag & O_TRUNC) && !truncate(0)) return false;
  703. return oflag & O_AT_END ? seekEnd(0) : true;
  704. fail:
  705. type_ = FAT_FILE_TYPE_CLOSED;
  706. return false;
  707. }
  708. //------------------------------------------------------------------------------
  709. /** Open the next file or subdirectory in a directory.
  710. *
  711. * \param[in] dirFile An open SdFat instance for the directory containing the
  712. * file to be opened.
  713. *
  714. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  715. * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
  716. *
  717. * See open() by path for definition of flags.
  718. * \return true for success or false for failure.
  719. */
  720. bool SdBaseFile::openNext(SdBaseFile* dirFile, uint8_t oflag) {
  721. dir_t* p;
  722. uint8_t index;
  723. if (!dirFile) goto fail;
  724. // error if already open
  725. if (isOpen()) goto fail;
  726. vol_ = dirFile->vol_;
  727. while (1) {
  728. index = 0XF & (dirFile->curPosition_ >> 5);
  729. // read entry into cache
  730. p = dirFile->readDirCache();
  731. if (!p) goto fail;
  732. // done if last entry
  733. if (p->name[0] == DIR_NAME_FREE) goto fail;
  734. // skip empty slot or '.' or '..'
  735. if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') {
  736. continue;
  737. }
  738. // must be file or dir
  739. if (DIR_IS_FILE_OR_SUBDIR(p)) {
  740. return openCachedEntry(index, oflag);
  741. }
  742. }
  743. fail:
  744. return false;
  745. }
  746. //------------------------------------------------------------------------------
  747. /** Open a directory's parent directory.
  748. *
  749. * \param[in] dir Parent of this directory will be opened. Must not be root.
  750. *
  751. * \return The value one, true, is returned for success and
  752. * the value zero, false, is returned for failure.
  753. */
  754. bool SdBaseFile::openParent(SdBaseFile* dir) {
  755. dir_t entry;
  756. dir_t* p;
  757. SdBaseFile file;
  758. uint32_t c;
  759. uint32_t cluster;
  760. uint32_t lbn;
  761. // error if already open or dir is root or dir is not a directory
  762. if (isOpen() || !dir || dir->isRoot() || !dir->isDir()) goto fail;
  763. vol_ = dir->vol_;
  764. // position to '..'
  765. if (!dir->seekSet(32)) goto fail;
  766. // read '..' entry
  767. if (dir->read(&entry, sizeof(entry)) != 32) goto fail;
  768. // verify it is '..'
  769. if (entry.name[0] != '.' || entry.name[1] != '.') goto fail;
  770. // start cluster for '..'
  771. cluster = entry.firstClusterLow;
  772. cluster |= (uint32_t)entry.firstClusterHigh << 16;
  773. if (cluster == 0) return openRoot(vol_);
  774. // start block for '..'
  775. lbn = vol_->clusterStartBlock(cluster);
  776. // first block of parent dir
  777. if (!vol_->cacheRawBlock(lbn, SdVolume::CACHE_FOR_READ)) {
  778. goto fail;
  779. }
  780. p = &vol_->cacheBuffer_.dir[1];
  781. // verify name for '../..'
  782. if (p->name[0] != '.' || p->name[1] != '.') goto fail;
  783. // '..' is pointer to first cluster of parent. open '../..' to find parent
  784. if (p->firstClusterHigh == 0 && p->firstClusterLow == 0) {
  785. if (!file.openRoot(dir->volume())) goto fail;
  786. } else {
  787. if (!file.openCachedEntry(1, O_READ)) goto fail;
  788. }
  789. // search for parent in '../..'
  790. do {
  791. if (file.readDir(&entry) != 32) goto fail;
  792. c = entry.firstClusterLow;
  793. c |= (uint32_t)entry.firstClusterHigh << 16;
  794. } while (c != cluster);
  795. // open parent
  796. return open(&file, file.curPosition()/32 - 1, O_READ);
  797. fail:
  798. return false;
  799. }
  800. //------------------------------------------------------------------------------
  801. /** Open a volume's root directory.
  802. *
  803. * \param[in] vol The FAT volume containing the root directory to be opened.
  804. *
  805. * \return The value one, true, is returned for success and
  806. * the value zero, false, is returned for failure.
  807. * Reasons for failure include the file is already open, the FAT volume has
  808. * not been initialized or it a FAT12 volume.
  809. */
  810. bool SdBaseFile::openRoot(SdVolume* vol) {
  811. // error if file is already open
  812. if (isOpen()) goto fail;
  813. if (vol->fatType() == 16 || (FAT12_SUPPORT && vol->fatType() == 12)) {
  814. type_ = FAT_FILE_TYPE_ROOT_FIXED;
  815. firstCluster_ = 0;
  816. fileSize_ = 32 * vol->rootDirEntryCount();
  817. } else if (vol->fatType() == 32) {
  818. type_ = FAT_FILE_TYPE_ROOT32;
  819. firstCluster_ = vol->rootDirStart();
  820. if (!vol->chainSize(firstCluster_, &fileSize_)) goto fail;
  821. } else {
  822. // volume is not initialized, invalid, or FAT12 without support
  823. return false;
  824. }
  825. vol_ = vol;
  826. // read only
  827. flags_ = O_READ;
  828. // set to start of file
  829. curCluster_ = 0;
  830. curPosition_ = 0;
  831. // root has no directory entry
  832. dirBlock_ = 0;
  833. dirIndex_ = 0;
  834. return true;
  835. fail:
  836. return false;
  837. }
  838. //------------------------------------------------------------------------------
  839. /** Return the next available byte without consuming it.
  840. *
  841. * \return The byte if no error and not at eof else -1;
  842. */
  843. int SdBaseFile::peek() {
  844. fpos_t pos;
  845. getpos(&pos);
  846. int c = read();
  847. if (c >= 0) setpos(&pos);
  848. return c;
  849. }
  850. //------------------------------------------------------------------------------
  851. /** %Print the name field of a directory entry in 8.3 format.
  852. * \param[in] pr Print stream for output.
  853. * \param[in] dir The directory structure containing the name.
  854. * \param[in] width Blank fill name if length is less than \a width.
  855. * \param[in] printSlash Print '/' after directory names if true.
  856. */
  857. void SdBaseFile::printDirName(const dir_t& dir,
  858. uint8_t width, bool printSlash) {
  859. uint8_t w = 0;
  860. for (uint8_t i = 0; i < 11; i++) {
  861. if (dir.name[i] == ' ')continue;
  862. if (i == 8) {
  863. MSerial.write('.');
  864. w++;
  865. }
  866. MSerial.write(dir.name[i]);
  867. w++;
  868. }
  869. if (DIR_IS_SUBDIR(&dir) && printSlash) {
  870. MSerial.write('/');
  871. w++;
  872. }
  873. while (w < width) {
  874. MSerial.write(' ');
  875. w++;
  876. }
  877. }
  878. //------------------------------------------------------------------------------
  879. // print uint8_t with width 2
  880. static void print2u( uint8_t v) {
  881. if (v < 10) MSerial.write('0');
  882. MSerial.print(v, DEC);
  883. }
  884. //------------------------------------------------------------------------------
  885. /** %Print a directory date field to Serial.
  886. *
  887. * Format is yyyy-mm-dd.
  888. *
  889. * \param[in] fatDate The date field from a directory entry.
  890. */
  891. //------------------------------------------------------------------------------
  892. /** %Print a directory date field.
  893. *
  894. * Format is yyyy-mm-dd.
  895. *
  896. * \param[in] pr Print stream for output.
  897. * \param[in] fatDate The date field from a directory entry.
  898. */
  899. void SdBaseFile::printFatDate(uint16_t fatDate) {
  900. MSerial.print(FAT_YEAR(fatDate));
  901. MSerial.write('-');
  902. print2u( FAT_MONTH(fatDate));
  903. MSerial.write('-');
  904. print2u( FAT_DAY(fatDate));
  905. }
  906. //------------------------------------------------------------------------------
  907. /** %Print a directory time field.
  908. *
  909. * Format is hh:mm:ss.
  910. *
  911. * \param[in] pr Print stream for output.
  912. * \param[in] fatTime The time field from a directory entry.
  913. */
  914. void SdBaseFile::printFatTime( uint16_t fatTime) {
  915. print2u( FAT_HOUR(fatTime));
  916. MSerial.write(':');
  917. print2u( FAT_MINUTE(fatTime));
  918. MSerial.write(':');
  919. print2u( FAT_SECOND(fatTime));
  920. }
  921. //------------------------------------------------------------------------------
  922. /** Print a file's name to Serial
  923. *
  924. * \return The value one, true, is returned for success and
  925. * the value zero, false, is returned for failure.
  926. */
  927. bool SdBaseFile::printName() {
  928. char name[13];
  929. if (!getFilename(name)) return false;
  930. MSerial.print(name);
  931. return true;
  932. }
  933. //------------------------------------------------------------------------------
  934. /** Read the next byte from a file.
  935. *
  936. * \return For success read returns the next byte in the file as an int.
  937. * If an error occurs or end of file is reached -1 is returned.
  938. */
  939. int16_t SdBaseFile::read() {
  940. uint8_t b;
  941. return read(&b, 1) == 1 ? b : -1;
  942. }
  943. //------------------------------------------------------------------------------
  944. /** Read data from a file starting at the current position.
  945. *
  946. * \param[out] buf Pointer to the location that will receive the data.
  947. *
  948. * \param[in] nbyte Maximum number of bytes to read.
  949. *
  950. * \return For success read() returns the number of bytes read.
  951. * A value less than \a nbyte, including zero, will be returned
  952. * if end of file is reached.
  953. * If an error occurs, read() returns -1. Possible errors include
  954. * read() called before a file has been opened, corrupt file system
  955. * or an I/O error occurred.
  956. */
  957. int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
  958. uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
  959. uint16_t offset;
  960. uint16_t toRead;
  961. uint32_t block; // raw device block number
  962. // error if not open or write only
  963. if (!isOpen() || !(flags_ & O_READ)) goto fail;
  964. // max bytes left in file
  965. if (nbyte >= (fileSize_ - curPosition_)) {
  966. nbyte = fileSize_ - curPosition_;
  967. }
  968. // amount left to read
  969. toRead = nbyte;
  970. while (toRead > 0) {
  971. offset = curPosition_ & 0X1FF; // offset in block
  972. if (type_ == FAT_FILE_TYPE_ROOT_FIXED) {
  973. block = vol_->rootDirStart() + (curPosition_ >> 9);
  974. } else {
  975. uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
  976. if (offset == 0 && blockOfCluster == 0) {
  977. // start of new cluster
  978. if (curPosition_ == 0) {
  979. // use first cluster in file
  980. curCluster_ = firstCluster_;
  981. } else {
  982. // get next cluster from FAT
  983. if (!vol_->fatGet(curCluster_, &curCluster_)) goto fail;
  984. }
  985. }
  986. block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
  987. }
  988. uint16_t n = toRead;
  989. // amount to be read from current block
  990. if (n > (512 - offset)) n = 512 - offset;
  991. // no buffering needed if n == 512
  992. if (n == 512 && block != vol_->cacheBlockNumber()) {
  993. if (!vol_->readBlock(block, dst)) goto fail;
  994. } else {
  995. // read block to cache and copy data to caller
  996. if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto fail;
  997. uint8_t* src = vol_->cache()->data + offset;
  998. memcpy(dst, src, n);
  999. }
  1000. dst += n;
  1001. curPosition_ += n;
  1002. toRead -= n;
  1003. }
  1004. return nbyte;
  1005. fail:
  1006. return -1;
  1007. }
  1008. //------------------------------------------------------------------------------
  1009. /** Read the next directory entry from a directory file.
  1010. *
  1011. * \param[out] dir The dir_t struct that will receive the data.
  1012. *
  1013. * \return For success readDir() returns the number of bytes read.
  1014. * A value of zero will be returned if end of file is reached.
  1015. * If an error occurs, readDir() returns -1. Possible errors include
  1016. * readDir() called before a directory has been opened, this is not
  1017. * a directory file or an I/O error occurred.
  1018. */
  1019. int8_t SdBaseFile::readDir(dir_t* dir) {
  1020. int16_t n;
  1021. // if not a directory file or miss-positioned return an error
  1022. if (!isDir() || (0X1F & curPosition_)) return -1;
  1023. while (1) {
  1024. n = read(dir, sizeof(dir_t));
  1025. if (n != sizeof(dir_t)) return n == 0 ? 0 : -1;
  1026. // last entry if DIR_NAME_FREE
  1027. if (dir->name[0] == DIR_NAME_FREE) return 0;
  1028. // skip empty entries and entry for . and ..
  1029. if (dir->name[0] == DIR_NAME_DELETED || dir->name[0] == '.') continue;
  1030. // return if normal file or subdirectory
  1031. if (DIR_IS_FILE_OR_SUBDIR(dir)) return n;
  1032. }
  1033. }
  1034. //------------------------------------------------------------------------------
  1035. // Read next directory entry into the cache
  1036. // Assumes file is correctly positioned
  1037. dir_t* SdBaseFile::readDirCache() {
  1038. uint8_t i;
  1039. // error if not directory
  1040. if (!isDir()) goto fail;
  1041. // index of entry in cache
  1042. i = (curPosition_ >> 5) & 0XF;
  1043. // use read to locate and cache block
  1044. if (read() < 0) goto fail;
  1045. // advance to next entry
  1046. curPosition_ += 31;
  1047. // return pointer to entry
  1048. return vol_->cache()->dir + i;
  1049. fail:
  1050. return 0;
  1051. }
  1052. //------------------------------------------------------------------------------
  1053. /** Remove a file.
  1054. *
  1055. * The directory entry and all data for the file are deleted.
  1056. *
  1057. * \note This function should not be used to delete the 8.3 version of a
  1058. * file that has a long name. For example if a file has the long name
  1059. * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
  1060. *
  1061. * \return The value one, true, is returned for success and
  1062. * the value zero, false, is returned for failure.
  1063. * Reasons for failure include the file read-only, is a directory,
  1064. * or an I/O error occurred.
  1065. */
  1066. bool SdBaseFile::remove() {
  1067. dir_t* d;
  1068. // free any clusters - will fail if read-only or directory
  1069. if (!truncate(0)) goto fail;
  1070. // cache directory entry
  1071. d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  1072. if (!d) goto fail;
  1073. // mark entry deleted
  1074. d->name[0] = DIR_NAME_DELETED;
  1075. // set this file closed
  1076. type_ = FAT_FILE_TYPE_CLOSED;
  1077. // write entry to SD
  1078. return vol_->cacheFlush();
  1079. return true;
  1080. fail:
  1081. return false;
  1082. }
  1083. //------------------------------------------------------------------------------
  1084. /** Remove a file.
  1085. *
  1086. * The directory entry and all data for the file are deleted.
  1087. *
  1088. * \param[in] dirFile The directory that contains the file.
  1089. * \param[in] path Path for the file to be removed.
  1090. *
  1091. * \note This function should not be used to delete the 8.3 version of a
  1092. * file that has a long name. For example if a file has the long name
  1093. * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
  1094. *
  1095. * \return The value one, true, is returned for success and
  1096. * the value zero, false, is returned for failure.
  1097. * Reasons for failure include the file is a directory, is read only,
  1098. * \a dirFile is not a directory, \a path is not found
  1099. * or an I/O error occurred.
  1100. */
  1101. bool SdBaseFile::remove(SdBaseFile* dirFile, const char* path) {
  1102. SdBaseFile file;
  1103. if (!file.open(dirFile, path, O_WRITE)) goto fail;
  1104. return file.remove();
  1105. fail:
  1106. // can't set iostate - static function
  1107. return false;
  1108. }
  1109. //------------------------------------------------------------------------------
  1110. /** Rename a file or subdirectory.
  1111. *
  1112. * \param[in] dirFile Directory for the new path.
  1113. * \param[in] newPath New path name for the file/directory.
  1114. *
  1115. * \return The value one, true, is returned for success and
  1116. * the value zero, false, is returned for failure.
  1117. * Reasons for failure include \a dirFile is not open or is not a directory
  1118. * file, newPath is invalid or already exists, or an I/O error occurs.
  1119. */
  1120. bool SdBaseFile::rename(SdBaseFile* dirFile, const char* newPath) {
  1121. dir_t entry;
  1122. uint32_t dirCluster = 0;
  1123. SdBaseFile file;
  1124. dir_t* d;
  1125. // must be an open file or subdirectory
  1126. if (!(isFile() || isSubDir())) goto fail;
  1127. // can't move file
  1128. if (vol_ != dirFile->vol_) goto fail;
  1129. // sync() and cache directory entry
  1130. sync();
  1131. d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  1132. if (!d) goto fail;
  1133. // save directory entry
  1134. memcpy(&entry, d, sizeof(entry));
  1135. // mark entry deleted
  1136. d->name[0] = DIR_NAME_DELETED;
  1137. // make directory entry for new path
  1138. if (isFile()) {
  1139. if (!file.open(dirFile, newPath, O_CREAT | O_EXCL | O_WRITE)) {
  1140. goto restore;
  1141. }
  1142. } else {
  1143. // don't create missing path prefix components
  1144. if (!file.mkdir(dirFile, newPath, false)) {
  1145. goto restore;
  1146. }
  1147. // save cluster containing new dot dot
  1148. dirCluster = file.firstCluster_;
  1149. }
  1150. // change to new directory entry
  1151. dirBlock_ = file.dirBlock_;
  1152. dirIndex_ = file.dirIndex_;
  1153. // mark closed to avoid possible destructor close call
  1154. file.type_ = FAT_FILE_TYPE_CLOSED;
  1155. // cache new directory entry
  1156. d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  1157. if (!d) goto fail;
  1158. // copy all but name field to new directory entry
  1159. memcpy(&d->attributes, &entry.attributes, sizeof(entry) - sizeof(d->name));
  1160. // update dot dot if directory
  1161. if (dirCluster) {
  1162. // get new dot dot
  1163. uint32_t block = vol_->clusterStartBlock(dirCluster);
  1164. if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto fail;
  1165. memcpy(&entry, &vol_->cache()->dir[1], sizeof(entry));
  1166. // free unused cluster
  1167. if (!vol_->freeChain(dirCluster)) goto fail;
  1168. // store new dot dot
  1169. block = vol_->clusterStartBlock(firstCluster_);
  1170. if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail;
  1171. memcpy(&vol_->cache()->dir[1], &entry, sizeof(entry));
  1172. }
  1173. return vol_->cacheFlush();
  1174. restore:
  1175. d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  1176. if (!d) goto fail;
  1177. // restore entry
  1178. d->name[0] = entry.name[0];
  1179. vol_->cacheFlush();
  1180. fail:
  1181. return false;
  1182. }
  1183. //------------------------------------------------------------------------------
  1184. /** Remove a directory file.
  1185. *
  1186. * The directory file will be removed only if it is empty and is not the
  1187. * root directory. rmdir() follows DOS and Windows and ignores the
  1188. * read-only attribute for the directory.
  1189. *
  1190. * \note This function should not be used to delete the 8.3 version of a
  1191. * directory that has a long name. For example if a directory has the
  1192. * long name "New folder" you should not delete the 8.3 name "NEWFOL~1".
  1193. *
  1194. * \return The value one, true, is returned for success and
  1195. * the value zero, false, is returned for failure.
  1196. * Reasons for failure include the file is not a directory, is the root
  1197. * directory, is not empty, or an I/O error occurred.
  1198. */
  1199. bool SdBaseFile::rmdir() {
  1200. // must be open subdirectory
  1201. if (!isSubDir()) goto fail;
  1202. rewind();
  1203. // make sure directory is empty
  1204. while (curPosition_ < fileSize_) {
  1205. dir_t* p = readDirCache();
  1206. if (!p) goto fail;
  1207. // done if past last used entry
  1208. if (p->name[0] == DIR_NAME_FREE) break;
  1209. // skip empty slot, '.' or '..'
  1210. if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
  1211. // error not empty
  1212. if (DIR_IS_FILE_OR_SUBDIR(p)) goto fail;
  1213. }
  1214. // convert empty directory to normal file for remove
  1215. type_ = FAT_FILE_TYPE_NORMAL;
  1216. flags_ |= O_WRITE;
  1217. return remove();
  1218. fail:
  1219. return false;
  1220. }
  1221. //------------------------------------------------------------------------------
  1222. /** Recursively delete a directory and all contained files.
  1223. *
  1224. * This is like the Unix/Linux 'rm -rf *' if called with the root directory
  1225. * hence the name.
  1226. *
  1227. * Warning - This will remove all contents of the directory including
  1228. * subdirectories. The directory will then be removed if it is not root.
  1229. * The read-only attribute for files will be ignored.
  1230. *
  1231. * \note This function should not be used to delete the 8.3 version of
  1232. * a directory that has a long name. See remove() and rmdir().
  1233. *
  1234. * \return The value one, true, is returned for success and
  1235. * the value zero, false, is returned for failure.
  1236. */
  1237. bool SdBaseFile::rmRfStar() {
  1238. uint16_t index;
  1239. SdBaseFile f;
  1240. rewind();
  1241. while (curPosition_ < fileSize_) {
  1242. // remember position
  1243. index = curPosition_/32;
  1244. dir_t* p = readDirCache();
  1245. if (!p) goto fail;
  1246. // done if past last entry
  1247. if (p->name[0] == DIR_NAME_FREE) break;
  1248. // skip empty slot or '.' or '..'
  1249. if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
  1250. // skip if part of long file name or volume label in root
  1251. if (!DIR_IS_FILE_OR_SUBDIR(p)) continue;
  1252. if (!f.open(this, index, O_READ)) goto fail;
  1253. if (f.isSubDir()) {
  1254. // recursively delete
  1255. if (!f.rmRfStar()) goto fail;
  1256. } else {
  1257. // ignore read-only
  1258. f.flags_ |= O_WRITE;
  1259. if (!f.remove()) goto fail;
  1260. }
  1261. // position to next entry if required
  1262. if (curPosition_ != (32*(index + 1))) {
  1263. if (!seekSet(32*(index + 1))) goto fail;
  1264. }
  1265. }
  1266. // don't try to delete root
  1267. if (!isRoot()) {
  1268. if (!rmdir()) goto fail;
  1269. }
  1270. return true;
  1271. fail:
  1272. return false;
  1273. }
  1274. //------------------------------------------------------------------------------
  1275. /** Create a file object and open it in the current working directory.
  1276. *
  1277. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  1278. *
  1279. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  1280. * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
  1281. */
  1282. SdBaseFile::SdBaseFile(const char* path, uint8_t oflag) {
  1283. type_ = FAT_FILE_TYPE_CLOSED;
  1284. writeError = false;
  1285. open(path, oflag);
  1286. }
  1287. //------------------------------------------------------------------------------
  1288. /** Sets a file's position.
  1289. *
  1290. * \param[in] pos The new position in bytes from the beginning of the file.
  1291. *
  1292. * \return The value one, true, is returned for success and
  1293. * the value zero, false, is returned for failure.
  1294. */
  1295. bool SdBaseFile::seekSet(uint32_t pos) {
  1296. uint32_t nCur;
  1297. uint32_t nNew;
  1298. // error if file not open or seek past end of file
  1299. if (!isOpen() || pos > fileSize_) goto fail;
  1300. if (type_ == FAT_FILE_TYPE_ROOT_FIXED) {
  1301. curPosition_ = pos;
  1302. goto done;
  1303. }
  1304. if (pos == 0) {
  1305. // set position to start of file
  1306. curCluster_ = 0;
  1307. curPosition_ = 0;
  1308. goto done;
  1309. }
  1310. // calculate cluster index for cur and new position
  1311. nCur = (curPosition_ - 1) >> (vol_->clusterSizeShift_ + 9);
  1312. nNew = (pos - 1) >> (vol_->clusterSizeShift_ + 9);
  1313. if (nNew < nCur || curPosition_ == 0) {
  1314. // must follow chain from first cluster
  1315. curCluster_ = firstCluster_;
  1316. } else {
  1317. // advance from curPosition
  1318. nNew -= nCur;
  1319. }
  1320. while (nNew--) {
  1321. if (!vol_->fatGet(curCluster_, &curCluster_)) goto fail;
  1322. }
  1323. curPosition_ = pos;
  1324. done:
  1325. return true;
  1326. fail:
  1327. return false;
  1328. }
  1329. //------------------------------------------------------------------------------
  1330. void SdBaseFile::setpos(fpos_t* pos) {
  1331. curPosition_ = pos->position;
  1332. curCluster_ = pos->cluster;
  1333. }
  1334. //------------------------------------------------------------------------------
  1335. /** The sync() call causes all modified data and directory fields
  1336. * to be written to the storage device.
  1337. *
  1338. * \return The value one, true, is returned for success and
  1339. * the value zero, false, is returned for failure.
  1340. * Reasons for failure include a call to sync() before a file has been
  1341. * opened or an I/O error.
  1342. */
  1343. bool SdBaseFile::sync() {
  1344. // only allow open files and directories
  1345. if (!isOpen()) goto fail;
  1346. if (flags_ & F_FILE_DIR_DIRTY) {
  1347. dir_t* d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  1348. // check for deleted by another open file object
  1349. if (!d || d->name[0] == DIR_NAME_DELETED) goto fail;
  1350. // do not set filesize for dir files
  1351. if (!isDir()) d->fileSize = fileSize_;
  1352. // update first cluster fields
  1353. d->firstClusterLow = firstCluster_ & 0XFFFF;
  1354. d->firstClusterHigh = firstCluster_ >> 16;
  1355. // set modify time if user supplied a callback date/time function
  1356. if (dateTime_) {
  1357. dateTime_(&d->lastWriteDate, &d->lastWriteTime);
  1358. d->lastAccessDate = d->lastWriteDate;
  1359. }
  1360. // clear directory dirty
  1361. flags_ &= ~F_FILE_DIR_DIRTY;
  1362. }
  1363. return vol_->cacheFlush();
  1364. fail:
  1365. writeError = true;
  1366. return false;
  1367. }
  1368. //------------------------------------------------------------------------------
  1369. /** Copy a file's timestamps
  1370. *
  1371. * \param[in] file File to copy timestamps from.
  1372. *
  1373. * \note
  1374. * Modify and access timestamps may be overwritten if a date time callback
  1375. * function has been set by dateTimeCallback().
  1376. *
  1377. * \return The value one, true, is returned for success and
  1378. * the value zero, false, is returned for failure.
  1379. */
  1380. bool SdBaseFile::timestamp(SdBaseFile* file) {
  1381. dir_t* d;
  1382. dir_t dir;
  1383. // get timestamps
  1384. if (!file->dirEntry(&dir)) goto fail;
  1385. // update directory fields
  1386. if (!sync()) goto fail;
  1387. d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  1388. if (!d) goto fail;
  1389. // copy timestamps
  1390. d->lastAccessDate = dir.lastAccessDate;
  1391. d->creationDate = dir.creationDate;
  1392. d->creationTime = dir.creationTime;
  1393. d->creationTimeTenths = dir.creationTimeTenths;
  1394. d->lastWriteDate = dir.lastWriteDate;
  1395. d->lastWriteTime = dir.lastWriteTime;
  1396. // write back entry
  1397. return vol_->cacheFlush();
  1398. fail:
  1399. return false;
  1400. }
  1401. //------------------------------------------------------------------------------
  1402. /** Set a file's timestamps in its directory entry.
  1403. *
  1404. * \param[in] flags Values for \a flags are constructed by a bitwise-inclusive
  1405. * OR of flags from the following list
  1406. *
  1407. * T_ACCESS - Set the file's last access date.
  1408. *
  1409. * T_CREATE - Set the file's creation date and time.
  1410. *
  1411. * T_WRITE - Set the file's last write/modification date and time.
  1412. *
  1413. * \param[in] year Valid range 1980 - 2107 inclusive.
  1414. *
  1415. * \param[in] month Valid range 1 - 12 inclusive.
  1416. *
  1417. * \param[in] day Valid range 1 - 31 inclusive.
  1418. *
  1419. * \param[in] hour Valid range 0 - 23 inclusive.
  1420. *
  1421. * \param[in] minute Valid range 0 - 59 inclusive.
  1422. *
  1423. * \param[in] second Valid range 0 - 59 inclusive
  1424. *
  1425. * \note It is possible to set an invalid date since there is no check for
  1426. * the number of days in a month.
  1427. *
  1428. * \note
  1429. * Modify and access timestamps may be overwritten if a date time callback
  1430. * function has been set by dateTimeCallback().
  1431. *
  1432. * \return The value one, true, is returned for success and
  1433. * the value zero, false, is returned for failure.
  1434. */
  1435. bool SdBaseFile::timestamp(uint8_t flags, uint16_t year, uint8_t month,
  1436. uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
  1437. uint16_t dirDate;
  1438. uint16_t dirTime;
  1439. dir_t* d;
  1440. if (!isOpen()
  1441. || year < 1980
  1442. || year > 2107
  1443. || month < 1
  1444. || month > 12
  1445. || day < 1
  1446. || day > 31
  1447. || hour > 23
  1448. || minute > 59
  1449. || second > 59) {
  1450. goto fail;
  1451. }
  1452. // update directory entry
  1453. if (!sync()) goto fail;
  1454. d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  1455. if (!d) goto fail;
  1456. dirDate = FAT_DATE(year, month, day);
  1457. dirTime = FAT_TIME(hour, minute, second);
  1458. if (flags & T_ACCESS) {
  1459. d->lastAccessDate = dirDate;
  1460. }
  1461. if (flags & T_CREATE) {
  1462. d->creationDate = dirDate;
  1463. d->creationTime = dirTime;
  1464. // seems to be units of 1/100 second not 1/10 as Microsoft states
  1465. d->creationTimeTenths = second & 1 ? 100 : 0;
  1466. }
  1467. if (flags & T_WRITE) {
  1468. d->lastWriteDate = dirDate;
  1469. d->lastWriteTime = dirTime;
  1470. }
  1471. return vol_->cacheFlush();
  1472. fail:
  1473. return false;
  1474. }
  1475. //------------------------------------------------------------------------------
  1476. /** Truncate a file to a specified length. The current file position
  1477. * will be maintained if it is less than or equal to \a length otherwise
  1478. * it will be set to end of file.
  1479. *
  1480. * \param[in] length The desired length for the file.
  1481. *
  1482. * \return The value one, true, is returned for success and
  1483. * the value zero, false, is returned for failure.
  1484. * Reasons for failure include file is read only, file is a directory,
  1485. * \a length is greater than the current file size or an I/O error occurs.
  1486. */
  1487. bool SdBaseFile::truncate(uint32_t length) {
  1488. uint32_t newPos;
  1489. // error if not a normal file or read-only
  1490. if (!isFile() || !(flags_ & O_WRITE)) goto fail;
  1491. // error if length is greater than current size
  1492. if (length > fileSize_) goto fail;
  1493. // fileSize and length are zero - nothing to do
  1494. if (fileSize_ == 0) return true;
  1495. // remember position for seek after truncation
  1496. newPos = curPosition_ > length ? length : curPosition_;
  1497. // position to last cluster in truncated file
  1498. if (!seekSet(length)) goto fail;
  1499. if (length == 0) {
  1500. // free all clusters
  1501. if (!vol_->freeChain(firstCluster_)) goto fail;
  1502. firstCluster_ = 0;
  1503. } else {
  1504. uint32_t toFree;
  1505. if (!vol_->fatGet(curCluster_, &toFree)) goto fail;
  1506. if (!vol_->isEOC(toFree)) {
  1507. // free extra clusters
  1508. if (!vol_->freeChain(toFree)) goto fail;
  1509. // current cluster is end of chain
  1510. if (!vol_->fatPutEOC(curCluster_)) goto fail;
  1511. }
  1512. }
  1513. fileSize_ = length;
  1514. // need to update directory entry
  1515. flags_ |= F_FILE_DIR_DIRTY;
  1516. if (!sync()) goto fail;
  1517. // set file to correct position
  1518. return seekSet(newPos);
  1519. fail:
  1520. return false;
  1521. }
  1522. //------------------------------------------------------------------------------
  1523. /** Write data to an open file.
  1524. *
  1525. * \note Data is moved to the cache but may not be written to the
  1526. * storage device until sync() is called.
  1527. *
  1528. * \param[in] buf Pointer to the location of the data to be written.
  1529. *
  1530. * \param[in] nbyte Number of bytes to write.
  1531. *
  1532. * \return For success write() returns the number of bytes written, always
  1533. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  1534. * include write() is called before a file has been opened, write is called
  1535. * for a read-only file, device is full, a corrupt file system or an I/O error.
  1536. *
  1537. */
  1538. int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) {
  1539. // convert void* to uint8_t* - must be before goto statements
  1540. const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
  1541. // number of bytes left to write - must be before goto statements
  1542. uint16_t nToWrite = nbyte;
  1543. // error if not a normal file or is read-only
  1544. if (!isFile() || !(flags_ & O_WRITE)) goto fail;
  1545. // seek to end of file if append flag
  1546. if ((flags_ & O_APPEND) && curPosition_ != fileSize_) {
  1547. if (!seekEnd()) goto fail;
  1548. }
  1549. while (nToWrite > 0) {
  1550. uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
  1551. uint16_t blockOffset = curPosition_ & 0X1FF;
  1552. if (blockOfCluster == 0 && blockOffset == 0) {
  1553. // start of new cluster
  1554. if (curCluster_ == 0) {
  1555. if (firstCluster_ == 0) {
  1556. // allocate first cluster of file
  1557. if (!addCluster()) goto fail;
  1558. } else {
  1559. curCluster_ = firstCluster_;
  1560. }
  1561. } else {
  1562. uint32_t next;
  1563. if (!vol_->fatGet(curCluster_, &next)) goto fail;
  1564. if (vol_->isEOC(next)) {
  1565. // add cluster if at end of chain
  1566. if (!addCluster()) goto fail;
  1567. } else {
  1568. curCluster_ = next;
  1569. }
  1570. }
  1571. }
  1572. // max space in block
  1573. uint16_t n = 512 - blockOffset;
  1574. // lesser of space and amount to write
  1575. if (n > nToWrite) n = nToWrite;
  1576. // block for data write
  1577. uint32_t block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
  1578. if (n == 512) {
  1579. // full block - don't need to use cache
  1580. if (vol_->cacheBlockNumber() == block) {
  1581. // invalidate cache if block is in cache
  1582. vol_->cacheSetBlockNumber(0XFFFFFFFF, false);
  1583. }
  1584. if (!vol_->writeBlock(block, src)) goto fail;
  1585. } else {
  1586. if (blockOffset == 0 && curPosition_ >= fileSize_) {
  1587. // start of new block don't need to read into cache
  1588. if (!vol_->cacheFlush()) goto fail;
  1589. // set cache dirty and SD address of block
  1590. vol_->cacheSetBlockNumber(block, true);
  1591. } else {
  1592. // rewrite part of block
  1593. if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail;
  1594. }
  1595. uint8_t* dst = vol_->cache()->data + blockOffset;
  1596. memcpy(dst, src, n);
  1597. }
  1598. curPosition_ += n;
  1599. src += n;
  1600. nToWrite -= n;
  1601. }
  1602. if (curPosition_ > fileSize_) {
  1603. // update fileSize and insure sync will update dir entry
  1604. fileSize_ = curPosition_;
  1605. flags_ |= F_FILE_DIR_DIRTY;
  1606. } else if (dateTime_ && nbyte) {
  1607. // insure sync will update modified date and time
  1608. flags_ |= F_FILE_DIR_DIRTY;
  1609. }
  1610. if (flags_ & O_SYNC) {
  1611. if (!sync()) goto fail;
  1612. }
  1613. return nbyte;
  1614. fail:
  1615. // return for write error
  1616. writeError = true;
  1617. return -1;
  1618. }
  1619. //------------------------------------------------------------------------------
  1620. // suppress cpplint warnings with NOLINT comment
  1621. #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
  1622. void (*SdBaseFile::oldDateTime_)(uint16_t& date, uint16_t& time) = 0; // NOLINT
  1623. #endif // ALLOW_DEPRECATED_FUNCTIONS