phatdir.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. /*
  2. * Copyright (C) 2005-2006 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. /*!
  33. * \file fs/phatdir.c
  34. * \brief PHAT File System.
  35. *
  36. * \verbatim
  37. *
  38. * $Log$
  39. * Revision 1.14 2009/02/13 14:52:05 haraldkipp
  40. * Include memdebug.h for heap management debugging support.
  41. *
  42. * Revision 1.13 2009/01/17 11:26:46 haraldkipp
  43. * Getting rid of two remaining BSD types in favor of stdint.
  44. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  45. *
  46. * Revision 1.12 2008/08/11 06:59:42 haraldkipp
  47. * BSD types replaced by stdint types (feature request #1282721).
  48. *
  49. * Revision 1.11 2006/10/08 16:42:56 haraldkipp
  50. * Not optimal, but simple and reliable exclusive access implemented.
  51. * Fixes bug #1486539. Furthermore, bug #1567790, which had been rejected,
  52. * had been reported correctly and is now fixed.
  53. *
  54. * Revision 1.10 2006/10/05 17:23:14 haraldkipp
  55. * Fixes bug #1571269. Thanks to Dirk Boecker for reporting this.
  56. *
  57. * Revision 1.9 2006/09/08 16:48:28 haraldkipp
  58. * Directory entries honor daylight saving time. Thanks to Dirk Boecker for
  59. * this fix.
  60. *
  61. * Revision 1.8 2006/07/05 16:00:30 haraldkipp
  62. * Two bugs fixed. Renaming a file's path (including directory change) failed.
  63. * Thanks to Dirk Boecker, who reported this.
  64. * The second bug was a memory hole, which had been discovered by
  65. * Ralf Spettel.
  66. *
  67. * Revision 1.7 2006/06/28 17:24:29 haraldkipp
  68. * Bugfix. Directories with long filename entries may not expand when
  69. * crossing cluster boundaries.
  70. *
  71. * Revision 1.6 2006/06/18 16:37:10 haraldkipp
  72. * No need to set errno after GetParentPath() returns an error.
  73. * No need to set errno after malloc failed.
  74. * New local helper function PhatOpenParentPath() reduces code size.
  75. * Support for long filenames (VFAT) added.
  76. * New function PhatDirReleaseChain() simplifies code.
  77. * Static function PhatDirOpenParentPath() replaced by global PhatDirOpenParent().
  78. * Added const attribute to path parameter of PhatDirOpen().
  79. * Now PhatDirEntryUpdate() clears the dirty flag for the root directory.
  80. * Calls to PhatFilePosSet() with position zero replaced by calls to PhatFilePosRewind().
  81. *
  82. * Revision 1.5 2006/04/07 12:52:24 haraldkipp
  83. * Memory hole fixed. Flag for normal / directory files now set correctly.
  84. *
  85. * Revision 1.4 2006/02/23 15:45:21 haraldkipp
  86. * PHAT file system now supports configurable number of sector buffers.
  87. * This dramatically increased write rates of no-name cards.
  88. * AVR compile errors corrected.
  89. *
  90. * Revision 1.3 2006/01/25 18:47:42 haraldkipp
  91. * Fixes wrong implementation of readdir() and simplifies the code.
  92. * Thanks to Jesper Hansen.
  93. *
  94. * Revision 1.2 2006/01/22 17:40:51 haraldkipp
  95. * Now mkdir() fails, if the directory exists already.
  96. * Now rmdir() returns an error when trying to delete subdirectories, which
  97. * are not empty.
  98. * Now PhatDirEntryStatus() sets correct errno value, if out of memory.
  99. *
  100. * Revision 1.1 2006/01/05 16:31:32 haraldkipp
  101. * First check-in.
  102. *
  103. *
  104. * \endverbatim
  105. */
  106. #include <fs/fs.h>
  107. #include <fs/phatfs.h>
  108. #include <fs/phatvol.h>
  109. #include <fs/phatio.h>
  110. #include <fs/phatutil.h>
  111. #include <fs/phatdir.h>
  112. #include <stdlib.h>
  113. #include <string.h>
  114. #include <ctype.h>
  115. #include <fcntl.h>
  116. #include <errno.h>
  117. #include <time.h>
  118. #include <memdebug.h>
  119. #if defined(NUTDEBUG)
  120. #include <stdio.h>
  121. #include <fs/phatdbg.h>
  122. #endif
  123. /*!
  124. * \addtogroup xgPhatDir
  125. */
  126. /*@{*/
  127. /*! \brief Maximum number of directory entries. */
  128. #define PHAT_MAXDIRENT 65536
  129. /*!
  130. * \brief Create the short name's checksum.
  131. *
  132. * The checksum is calculated by rotating the previous sum by one
  133. * bit and adding the next character.
  134. */
  135. static INLINE uint8_t GenShortNameChecksum(char *sfn)
  136. {
  137. uint8_t rc = 0;
  138. int i;
  139. for (i = 0; i < 11; i++) {
  140. rc = ((rc >> 1) | ((rc & 1) << 7)) + (uint8_t)(sfn[i]);
  141. }
  142. return rc;
  143. }
  144. /*!
  145. * \brief Create a short unique file name from a given long file name.
  146. *
  147. * \param ndp Descriptor of the directory, which will be used for the
  148. * short name.
  149. * \param lfn Long file name.
  150. * \param sfn Points to the buffer that will receive the short name in
  151. * FCB format. Eleven characters will be stored in this buffer.
  152. *
  153. * \return 0 on success, -1 otherwise.
  154. */
  155. static int GenShortName(NUTFILE * ndp, const char *lfn, char *sfn)
  156. {
  157. int rc = -1;
  158. int i;
  159. int got;
  160. PHATDIRENT *entry;
  161. char *xp;
  162. /* Fill the buffer with spaces. */
  163. memset(sfn, ' ', 11);
  164. /* Get the position of the last dot in the long name. */
  165. xp = strrchr(lfn, '.');
  166. /* Now collect up to 8 characters for the basename and 3 characters
  167. * for the extension. */
  168. for (i = 0; i < 11 && *lfn; lfn++) {
  169. if (*lfn == '.') {
  170. /* We reached the first dot. The basename ends here. */
  171. if (lfn == xp) {
  172. /* This is also the last dot. Start copying the extension. */
  173. lfn++;
  174. if (*lfn) {
  175. sfn[8] = toupper((unsigned char)*lfn);
  176. }
  177. i = 9;
  178. }
  179. }
  180. else if (i == 8) {
  181. /* First 8 characters collected. */
  182. if (xp == NULL) {
  183. /* No dot, no extension. */
  184. break;
  185. }
  186. lfn = xp + 1;
  187. if (*lfn) {
  188. sfn[i++] = toupper((unsigned char)*lfn);
  189. }
  190. }
  191. else if (*lfn != ' ') {
  192. if (strrchr("+,;=[]", *lfn)) {
  193. sfn[i++] = '_';
  194. }
  195. else {
  196. sfn[i++] = toupper((unsigned char)*lfn);
  197. }
  198. }
  199. }
  200. /*
  201. * Select a unique short name by verifying existing entries in the
  202. * specified directory.
  203. */
  204. if ((entry = malloc(sizeof(PHATDIRENT))) != NULL) {
  205. xp = sfn;
  206. for (i = 0; i < 6 && *xp != ' '; i++) {
  207. xp++;
  208. }
  209. /*
  210. * We try up to 99 unique short names only, but this should be
  211. * sufficient for our tiny system - hopefully.
  212. */
  213. for (i = 1; i <= 99 && rc; i++) {
  214. if (i == 10) {
  215. xp--;
  216. }
  217. *xp = '~';
  218. if (i > 9) {
  219. *(xp + 1) = '0' + i / 10;
  220. *(xp + 2) = '0' + i % 10;
  221. }
  222. else {
  223. *(xp + 1) = '0' + i;
  224. }
  225. PhatFilePosRewind((PHATFILE *)ndp->nf_fcb);
  226. for (;;) {
  227. /* Read next entry. */
  228. if ((got = PhatFileRead(ndp, entry, sizeof(PHATDIRENT))) != sizeof(PHATDIRENT)) {
  229. if (got) {
  230. /* Read error, stop searching. */
  231. i = 99;
  232. }
  233. else {
  234. /* End of directory reached, entry is unique. */
  235. rc = 0;
  236. }
  237. break;
  238. }
  239. if ((entry->dent_attr & PHAT_FATTR_VOLID) == 0) {
  240. if (entry->dent_name[0] == 0) {
  241. /* End of directory reached, entry is unique. */
  242. rc = 0;
  243. break;
  244. }
  245. if (memcmp(sfn, (char *)entry->dent_name, 11) == 0) {
  246. /* Entry exists, try next one. */
  247. break;
  248. }
  249. }
  250. }
  251. }
  252. free(entry);
  253. }
  254. return rc;
  255. }
  256. /*!
  257. * \brief Allocate a directory entry.
  258. *
  259. * \param ndp Parent directory of the new entry.
  260. * \param fname Name of the new entry.
  261. * \param entry Information structure of the new entry is stored here.
  262. *
  263. * \return 0 on success, -1 otherwise.
  264. */
  265. static int PhatDirEntryAlloc(NUTFILE * ndp, const char *fname, PHATDIRENT * entry)
  266. {
  267. int rc;
  268. int pos = 0;
  269. int got;
  270. uint8_t sect;
  271. uint8_t *temp;
  272. NUTDEVICE *dev = ndp->nf_dev;
  273. PHATFILE *dfcb = ndp->nf_fcb;
  274. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  275. PHATDIRENT *dent;
  276. PHATXDIRENT *xdent;
  277. int nwant = 1;
  278. int ngot = 0;
  279. int npos = 0;
  280. int i;
  281. int ci;
  282. uint8_t cks = 0;
  283. /* Convert the dotted name to a space filled one. */
  284. if ((rc = MakePhatName(fname, entry->dent_name)) == 1) {
  285. /* Reject name with wildcards. */
  286. errno = EINVAL;
  287. return -1;
  288. }
  289. if (rc == 0) {
  290. char vname[16];
  291. MakeVisibleName(entry->dent_name, vname);
  292. if (strcmp(fname, vname)) {
  293. rc = 1;
  294. }
  295. }
  296. if (rc) {
  297. if (GenShortName(ndp, fname, (char *)entry->dent_name)) {
  298. errno = EINVAL;
  299. return -1;
  300. }
  301. nwant = (strlen(fname) + 12) / 13 + 1;
  302. cks = GenShortNameChecksum((char *)entry->dent_name);
  303. }
  304. /* Allocate a temporary entry buffer. */
  305. if ((dent = malloc(sizeof(PHATDIRENT))) == NULL) {
  306. return -1;
  307. }
  308. /*
  309. * Find free entries, starting from the beginning.
  310. */
  311. PhatFilePosRewind(dfcb);
  312. rc = -1;
  313. for (;;) {
  314. /* Memorize the current position and try to read the next entry. */
  315. npos = dfcb->f_pos;
  316. if ((got = PhatFileRead(ndp, dent, sizeof(PHATDIRENT))) < 0) {
  317. /* Read failed. */
  318. break;
  319. }
  320. if (got != sizeof(PHATDIRENT)) {
  321. /*
  322. * End of directory reached. Try to expand it.
  323. */
  324. if (IsFixedRootDir(ndp) || /* */
  325. dfcb->f_pos >= PHAT_MAXDIRENT * sizeof(PHATDIRENT)) {
  326. /* Failed. Either PHAT12/16 root dir or max. size reached. */
  327. errno = EFBIG;
  328. break;
  329. }
  330. /* Fill a new cluster with zeros. */
  331. if ((temp = malloc(vol->vol_sectsz)) == NULL) {
  332. break;
  333. }
  334. memset(temp, 0, vol->vol_sectsz);
  335. for (sect = vol->vol_clustsz; sect; sect--) {
  336. if (PhatFileWrite(ndp, temp, vol->vol_sectsz) < 0) {
  337. /* Write failed. */
  338. break;
  339. }
  340. }
  341. free(temp);
  342. /* End of directory reached and expanded by a new cluster. */
  343. if (sect == 0) {
  344. if (ngot == 0) {
  345. pos = npos;
  346. }
  347. rc = 0;
  348. }
  349. break;
  350. }
  351. if (dent->dent_name[0] == PHAT_REM_DIRENT || dent->dent_name[0] == 0) {
  352. /* Empty entry found. */
  353. if (ngot == 0) {
  354. pos = npos;
  355. }
  356. ngot++;
  357. if (ngot >= nwant) {
  358. rc = 0;
  359. break;
  360. }
  361. }
  362. else {
  363. ngot = 0;
  364. }
  365. }
  366. free(dent);
  367. if (rc == 0) {
  368. /* Return to the memorized position and write the new entry. */
  369. PhatFilePosSet(ndp, pos);
  370. if ((xdent = malloc(sizeof(PHATXDIRENT))) == NULL) {
  371. return -1;
  372. }
  373. for (ngot = nwant - 1; ngot; ngot--) {
  374. memset(xdent, 0xFF, sizeof(PHATXDIRENT));
  375. xdent->xdent_seq = ngot;
  376. if (ngot == nwant - 1) {
  377. xdent->xdent_seq |= 0x40;
  378. }
  379. xdent->xdent_attr = PHAT_FATTR_LFN;
  380. xdent->xdent_cks = cks;
  381. xdent->xdent_clust = 0;
  382. xdent->xdent_rsvd = 0;
  383. /* Simplified unicode conversion, ignores double byte characters. */
  384. ci = (ngot - 1) * 13;
  385. for (i = 0; i < 13; i++) {
  386. if (ci + i <= strlen(fname)) {
  387. if (i < 5) {
  388. xdent->xdent_uname_1_5[i] = fname[ci + i];
  389. }
  390. else if (i < 11) {
  391. xdent->xdent_uname_6_11[i - 5] = fname[ci + i];
  392. }
  393. else {
  394. xdent->xdent_uname_12_13[i - 11] = fname[ci + i];
  395. }
  396. }
  397. }
  398. if (PhatFileWrite(ndp, xdent, sizeof(PHATXDIRENT)) < 0) {
  399. /* Write error. */
  400. rc = -1;
  401. break;
  402. }
  403. }
  404. if (PhatFileWrite(ndp, entry, sizeof(PHATDIRENT)) < 0) {
  405. /* Write error. */
  406. rc = -1;
  407. }
  408. free(xdent);
  409. }
  410. return rc;
  411. }
  412. /*!
  413. * \brief Release a directory entry.
  414. *
  415. * In the PHAT file system released entries will be marked with the
  416. * special character 0xE5 as the first character in the file name.
  417. *
  418. * \param ndp The parent directory of the entry that should be removed.
  419. * \param pos Byte position of that entry.
  420. * \param lfncnt Number of long file entries.
  421. *
  422. * \return 0 on success, -1 otherwsie.
  423. */
  424. static int PhatDirEntryRelease(NUTFILE * ndp, uint32_t pos, int lfncnt)
  425. {
  426. uint8_t ch = PHAT_REM_DIRENT;
  427. int i;
  428. for (i = lfncnt; i; i--) {
  429. PhatFilePosSet(ndp, pos - i * sizeof(PHATXDIRENT));
  430. if (PhatFileWrite(ndp, &ch, 1) < 0) {
  431. return -1;
  432. }
  433. }
  434. PhatFilePosSet(ndp, pos);
  435. if (PhatFileWrite(ndp, &ch, 1) < 0) {
  436. return -1;
  437. }
  438. return 0;
  439. }
  440. /*!
  441. * \brief Create a new directory entry.
  442. *
  443. * \param ndp Handle to the parent directory.
  444. * \param name Name of the new entry.
  445. * \param acc Attributes of the new entry.
  446. * \param dirent Information structure of the new entry.
  447. *
  448. * \return 0 on success. Otherwise -1 is returned.
  449. */
  450. int PhatDirEntryCreate(NUTFILE * ndp, const char *name, int acc, PHATDIRENT * dirent)
  451. {
  452. dirent->dent_attr = (uint8_t) acc;
  453. GetDosTimeStamp(&dirent->dent_ctime, &dirent->dent_cdate);
  454. dirent->dent_adate = dirent->dent_cdate;
  455. dirent->dent_mtime = dirent->dent_ctime;
  456. dirent->dent_mdate = dirent->dent_cdate;
  457. if (PhatDirEntryAlloc(ndp, name, dirent)) {
  458. return -1;
  459. }
  460. #ifdef NUTDEBUG
  461. PhatDbgFileInfo(stdout, "New Dir Entry", ndp->nf_fcb);
  462. #endif
  463. return 0;
  464. }
  465. /*!
  466. * \brief Update directory entry of an opened file or directory.
  467. *
  468. * \param nfp File of which the directory entry will be updated.
  469. *
  470. * \return 0 on success. Otherwise -1 is returned.
  471. */
  472. int PhatDirEntryUpdate(NUTFILE * nfp)
  473. {
  474. int sbn;
  475. NUTDEVICE *dev = nfp->nf_dev;
  476. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  477. PHATFILE *fcb = nfp->nf_fcb;
  478. if (fcb->f_de_dirty) {
  479. /*
  480. * The file may specify the root directory, in which case
  481. * the updated is skipped.
  482. */
  483. if (fcb->f_de_sect) {
  484. #ifdef NUTDEBUG
  485. PhatDbgDirEntry(stdout, "PhatDirEntryUpdate", &fcb->f_dirent);
  486. #endif
  487. if ((sbn = PhatSectorLoad(dev, fcb->f_de_sect)) < 0) {
  488. return -1;
  489. }
  490. memcpy(vol->vol_buf[sbn].sect_data + fcb->f_de_offs, &fcb->f_dirent, sizeof(PHATDIRENT));
  491. vol->vol_buf[sbn].sect_dirty = 1;
  492. PhatSectorBufferRelease(dev, sbn);
  493. }
  494. fcb->f_de_dirty = 0;
  495. }
  496. return 0;
  497. }
  498. /*!
  499. * \brief Read the next directory entry.
  500. *
  501. * \param ndp Specifies the directory to read.
  502. * \param srch Pointer to a structure which receives the result.
  503. *
  504. * \return 0 on success. Otherwise -1 is returned.
  505. */
  506. static int PhatDirEntryRead(NUTFILE * ndp, PHATFIND * srch)
  507. {
  508. PHATDIRENT *entry = &srch->phfind_ent;
  509. PHATFILE *fcb = ndp->nf_fcb;
  510. PHATXDIRENT *xentry;
  511. char *lfn = NULL;
  512. int lfnpos = 0;
  513. int nxtseq = 0;
  514. int lfncnt = 0;
  515. int i;
  516. for (;;) {
  517. /* Read next entry. */
  518. if (PhatFileRead(ndp, entry, sizeof(PHATDIRENT)) != sizeof(PHATDIRENT)) {
  519. break;
  520. }
  521. /* Skip removed entries. */
  522. if (entry->dent_name[0] == PHAT_REM_DIRENT) {
  523. if (lfn) {
  524. free(lfn);
  525. lfn = NULL;
  526. }
  527. }
  528. /* Process long filename entries. */
  529. else if (entry->dent_attr == PHAT_FATTR_LFN) {
  530. xentry = (PHATXDIRENT *)entry;
  531. lfnpos = (xentry->xdent_seq & 0x3F);
  532. /* Make sure we are in sequence. */
  533. if ((nxtseq == 0 && (xentry->xdent_seq & 0x40) != 0) || nxtseq == lfnpos) {
  534. nxtseq = --lfnpos;
  535. lfnpos *= 13;
  536. if (lfnpos + 13 > PHAT_MAX_NAMELEN) {
  537. errno = EINVAL;
  538. break;
  539. }
  540. if (xentry->xdent_seq & 0x40) {
  541. if (lfn == NULL) {
  542. lfn = malloc(PHAT_MAX_NAMELEN + 1);
  543. }
  544. lfn[lfnpos + 13] = 0;
  545. lfncnt = 0;
  546. }
  547. lfncnt++;
  548. /* Simplified unicode conversion, ignores double byte characters. */
  549. for (i = 0; i < 5; i++) {
  550. lfn[lfnpos + i] = (char)xentry->xdent_uname_1_5[i];
  551. }
  552. for (i = 0; i < 6; i++) {
  553. lfn[lfnpos + 5 + i] = (char)xentry->xdent_uname_6_11[i];
  554. }
  555. for (i = 0; i < 2; i++) {
  556. lfn[lfnpos + 11 + i] = (char)xentry->xdent_uname_12_13[i];
  557. }
  558. }
  559. }
  560. /* Skip volume IDs. */
  561. else if ((entry->dent_attr & PHAT_FATTR_VOLID) == 0) {
  562. /* Stop searching if last entry reached. */
  563. if (entry->dent_name[0] == 0) {
  564. break;
  565. }
  566. /* Valid entry found. Return success. */
  567. srch->phfind_pos = fcb->f_pos - sizeof(PHATDIRENT);
  568. if (lfn && lfnpos == 0) {
  569. strcpy(srch->phfind_name, lfn);
  570. srch->phfind_xcnt = lfncnt;
  571. }
  572. else {
  573. MakeVisibleName(entry->dent_name, srch->phfind_name);
  574. srch->phfind_xcnt = 0;
  575. }
  576. if (lfn) {
  577. free(lfn);
  578. }
  579. nxtseq = 0;
  580. lfncnt = 0;
  581. return 0;
  582. }
  583. else if (lfn) {
  584. free(lfn);
  585. lfn = NULL;
  586. nxtseq = 0;
  587. lfncnt = 0;
  588. }
  589. }
  590. if (lfn) {
  591. free(lfn);
  592. }
  593. return -1;
  594. }
  595. /*!
  596. * \brief Find a directory entry with a specified name.
  597. *
  598. * \param ndp Pointer to a previously opened directory.
  599. * \param spec Name of the entry to retrieve.
  600. * \param attmsk Attribute mask. Entries with attributes not specified
  601. * in this mask will be ignored.
  602. * \param srch Optional pointer to a structure which receives the
  603. * search result.
  604. *
  605. * \return 0 if an entry was found, otherwise -1 is returned.
  606. */
  607. int PhatDirEntryFind(NUTFILE * ndp, const char *spec, uint32_t attmsk, PHATFIND * srch)
  608. {
  609. int rc;
  610. PHATFIND *temps;
  611. /* Allocate a temporary structure to store the search result. */
  612. if ((temps = malloc(sizeof(PHATFIND))) == NULL) {
  613. return -1;
  614. }
  615. /*
  616. * Loop until the specified entry was found or until we reach the
  617. * end of the directory.
  618. */
  619. PhatFilePosRewind((PHATFILE *)ndp->nf_fcb);
  620. while ((rc = PhatDirEntryRead(ndp, temps)) == 0) {
  621. if ((temps->phfind_ent.dent_attr | attmsk) == attmsk) {
  622. if (strcasecmp(temps->phfind_name, spec) == 0) {
  623. /* Specified entry found. */
  624. if (srch) {
  625. *srch = *temps;
  626. }
  627. break;
  628. }
  629. }
  630. }
  631. free(temps);
  632. return rc;
  633. }
  634. /*!
  635. * \brief Open parent directory of a given path.
  636. *
  637. * \param dev Specifies the file system device.
  638. * \param path Full path.
  639. * \param basename Points to a pointer which will be set to the last component
  640. * within the full path.
  641. *
  642. * \return Pointer to a NUTFILE structure if successful or NUTFILE_EOF otherwise.
  643. */
  644. NUTFILE *PhatDirOpenParent(NUTDEVICE * dev, const char *path, const char **basename)
  645. {
  646. NUTFILE *rc = NUTFILE_EOF;
  647. char *parent;
  648. if ((parent = GetParentPath(path, basename)) != NULL) {
  649. rc = PhatDirOpen(dev, parent);
  650. free(parent);
  651. }
  652. return rc;
  653. }
  654. /*!
  655. * \brief Rename file.
  656. *
  657. * \param dev Specifies the file system device.
  658. * \param old_path Old name and path of the file.
  659. * \param new_path New name and path of the file.
  660. *
  661. * \return 0 on success, -1 otherwise.
  662. */
  663. int PhatDirRenameEntry(NUTDEVICE * dev, const char *old_path, const char *new_path)
  664. {
  665. int rc = -1;
  666. const char *fname;
  667. NUTFILE *old_ndp;
  668. NUTFILE *new_ndp;
  669. PHATFIND *srch;
  670. /*
  671. * Open directory of the old file.
  672. */
  673. if ((old_ndp = PhatDirOpenParent(dev, old_path, &fname)) == NUTFILE_EOF) {
  674. return -1;
  675. }
  676. if ((srch = malloc(sizeof(PHATFIND))) != NULL) {
  677. if ((rc = PhatDirEntryFind(old_ndp, fname, PHAT_FATTR_FILEMASK, srch)) == 0) {
  678. rc = -1;
  679. if ((new_ndp = PhatDirOpenParent(dev, new_path, &fname)) != NUTFILE_EOF) {
  680. if (PhatDirEntryFind(new_ndp, fname, PHAT_FATTR_FILEMASK, NULL) == 0) {
  681. errno = EEXIST;
  682. } else {
  683. if ((rc = PhatDirEntryAlloc(new_ndp, fname, &srch->phfind_ent)) == 0) {
  684. rc = PhatDirEntryRelease(old_ndp, srch->phfind_pos, srch->phfind_xcnt);
  685. }
  686. }
  687. PhatFileClose(new_ndp);
  688. }
  689. }
  690. else {
  691. errno = ENOENT;
  692. }
  693. free(srch);
  694. }
  695. PhatFileClose(old_ndp);
  696. return rc;
  697. }
  698. /*!
  699. * \brief Release a cluster chain of a specified directory entry.
  700. *
  701. * \param dev Specifies the file system device.
  702. * \param dent Directory entry.
  703. *
  704. * \return 0 on success or -1 on failure.
  705. */
  706. int PhatDirReleaseChain(NUTDEVICE * dev, PHATDIRENT *dent)
  707. {
  708. int rc = 0;
  709. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  710. uint32_t clust;
  711. /* Do not remove clusters of files with RDONLY attribute. */
  712. if (dent->dent_attr & PHAT_FATTR_RDONLY) {
  713. errno = EACCES;
  714. return -1;
  715. }
  716. /* Get the first cluster of this file. The directory entry stores this
  717. value in two positions. */
  718. clust = dent->dent_clusthi;
  719. clust <<= 16;
  720. clust |= dent->dent_clust;
  721. /* The data area starts at cluster 2. With empty files the first cluster
  722. is set to zero. A value of one is suspicious and will be ignored. */
  723. if (clust >= 2) {
  724. /* Call the format specific release routine. */
  725. if (vol->vol_type == 32) {
  726. rc = Phat32ReleaseChain(dev, clust);
  727. } else if (vol->vol_type == 16) {
  728. rc = Phat16ReleaseChain(dev, clust);
  729. } else {
  730. rc = Phat12ReleaseChain(dev, clust);
  731. }
  732. }
  733. return rc;
  734. }
  735. /*!
  736. * \brief Remove a directory entry.
  737. *
  738. * Allocated clusters are released.
  739. *
  740. * \param dev Specifies the file system device.
  741. * \param path Name of the entry to remove.
  742. * \param flags Attributes of the entry to remove.
  743. *
  744. * \return 0 if successful. Otherwise returns an error code.
  745. */
  746. int PhatDirDelEntry(NUTDEVICE * dev, const char *path, uint32_t flags)
  747. {
  748. int rc = -1;
  749. PHATFIND *srch;
  750. NUTFILE *ndp;
  751. const char *fname;
  752. /* Open the parent directory. */
  753. if ((ndp = PhatDirOpenParent(dev, path, &fname)) != NUTFILE_EOF) {
  754. if ((srch = malloc(sizeof(PHATFIND))) != NULL) {
  755. /* Find the specified file name. */
  756. if (PhatDirEntryFind(ndp, fname, flags, srch) == 0) {
  757. if (PhatDirReleaseChain(dev, &srch->phfind_ent) == 0) {
  758. rc = PhatDirEntryRelease(ndp, srch->phfind_pos, srch->phfind_xcnt);
  759. }
  760. }
  761. else {
  762. errno = ENOENT;
  763. }
  764. free(srch);
  765. }
  766. PhatFileClose(ndp);
  767. }
  768. return rc;
  769. }
  770. /*!
  771. * \brief Open a directory.
  772. *
  773. * \param dev Specifies the file system device.
  774. * \param dpath Full absolute pathname of the directory to open.
  775. *
  776. * \return Pointer to a NUTFILE structure if successful or NUTFILE_EOF otherwise.
  777. */
  778. NUTFILE *PhatDirOpen(NUTDEVICE * dev, const char *dpath)
  779. {
  780. NUTFILE *ndp;
  781. PHATFILE *dfcb;
  782. PHATFIND *srch;
  783. char *comp;
  784. char *cp;
  785. int sz;
  786. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  787. /* Make sure the volume is mounted. */
  788. if (vol == NULL) {
  789. errno = ENODEV;
  790. return NUTFILE_EOF;
  791. }
  792. /* Allocate the structure to return. */
  793. if ((ndp = malloc(sizeof(NUTFILE))) == NULL) {
  794. return NUTFILE_EOF;
  795. }
  796. if ((ndp->nf_fcb = malloc(sizeof(PHATFILE))) == NULL) {
  797. free(ndp);
  798. return NUTFILE_EOF;
  799. }
  800. memset(ndp->nf_fcb, 0, sizeof(PHATFILE));
  801. ndp->nf_dev = dev;
  802. dfcb = ndp->nf_fcb;
  803. dfcb->f_dirent.dent_attr = PHAT_FATTR_DIR;
  804. dfcb->f_mode = _O_RDONLY;
  805. /* We start at the root directory. */
  806. dfcb->f_clust = vol->vol_root_clust;
  807. dfcb->f_dirent.dent_clusthi = (uint16_t) (vol->vol_root_clust >> 16);
  808. dfcb->f_dirent.dent_clust = (uint16_t) vol->vol_root_clust;
  809. if (*dpath == '/') {
  810. dpath++;
  811. }
  812. if (*dpath) {
  813. /*
  814. * We are looking for a subdirectory.
  815. */
  816. if ((comp = malloc(PHAT_MAX_NAMELEN + 1)) == NULL) {
  817. free(dfcb);
  818. free(ndp);
  819. return NUTFILE_EOF;
  820. }
  821. if ((srch = malloc(sizeof(PHATFIND))) == NULL) {
  822. free(comp);
  823. free(dfcb);
  824. free(ndp);
  825. return NUTFILE_EOF;
  826. }
  827. /*
  828. * Walk down the path.
  829. */
  830. while (*dpath) {
  831. /* Fetch the next component from the path. */
  832. cp = comp;
  833. sz = 0;
  834. while (*dpath) {
  835. if (*dpath == '/') {
  836. dpath++;
  837. break;
  838. }
  839. if (++sz > PHAT_MAX_NAMELEN) {
  840. break;
  841. }
  842. *cp++ = *dpath++;
  843. }
  844. *cp = 0;
  845. /* Search component's entry in the current directory. */
  846. if (sz > PHAT_MAX_NAMELEN || PhatDirEntryFind(ndp, comp, PHAT_FATTR_FILEMASK, srch)) {
  847. errno = ENOENT;
  848. free(dfcb);
  849. free(ndp);
  850. ndp = NUTFILE_EOF;
  851. break;
  852. }
  853. /*
  854. * Next component found. Mimic the open by updating the existing
  855. * file control block structure.
  856. */
  857. dfcb->f_de_sect = PhatClusterSector(ndp, dfcb->f_clust) + dfcb->f_clust_pos;
  858. dfcb->f_de_offs = dfcb->f_sect_pos - 32;
  859. /* Set the cluster of our directory entry. */
  860. dfcb->f_pde_clusthi = dfcb->f_dirent.dent_clusthi;
  861. dfcb->f_pde_clust = dfcb->f_dirent.dent_clust;
  862. dfcb->f_dirent = srch->phfind_ent;
  863. #ifdef NUTDEBUG
  864. PhatDbgFileInfo(stdout, "Component", dfcb);
  865. #endif
  866. /*
  867. * Handle root directory.
  868. */
  869. if (dfcb->f_dirent.dent_attr & PHAT_FATTR_DIR) {
  870. if (dfcb->f_dirent.dent_clust == 0 && dfcb->f_dirent.dent_clusthi == 0) {
  871. if (vol->vol_type != 32) {
  872. dfcb->f_de_sect = 0;
  873. }
  874. dfcb->f_dirent.dent_clusthi = (uint16_t) (vol->vol_root_clust >> 16);
  875. dfcb->f_dirent.dent_clust = (uint16_t) vol->vol_root_clust;
  876. }
  877. }
  878. /*
  879. * Reset position.
  880. */
  881. PhatFilePosRewind(dfcb);
  882. dfcb->f_clust_prv = 0;
  883. dfcb->f_mode = _O_RDONLY;
  884. }
  885. free(srch);
  886. free(comp);
  887. }
  888. return ndp;
  889. }
  890. /*!
  891. * \brief Read the next directory entry.
  892. *
  893. * \param dir Pointer to the internal directory information structure.
  894. *
  895. * \return 0 on success, -1 otherwise.
  896. */
  897. int PhatDirRead(DIR * dir)
  898. {
  899. PHATFIND *srch;
  900. struct dirent *ent;
  901. if ((srch = malloc(sizeof(PHATFIND))) == NULL) {
  902. return -1;
  903. }
  904. if (PhatDirEntryRead(dir->dd_fd, srch)) {
  905. free(srch);
  906. return -1;
  907. }
  908. #ifdef NUTDEBUG
  909. PhatDbgDirEntry(stdout, "Read entry", &srch->phfind_ent);
  910. #endif
  911. ent = (struct dirent *) dir->dd_buf;
  912. memset(dir->dd_buf, 0, sizeof(struct dirent));
  913. ent->d_namlen = (uint8_t) strlen(srch->phfind_name);
  914. strcpy(ent->d_name, srch->phfind_name);
  915. if (srch->phfind_ent.dent_attr & PHAT_FATTR_DIR) {
  916. ent->d_type = 1;
  917. }
  918. free(srch);
  919. return 0;
  920. }
  921. /*!
  922. * \brief Create a new subdirectory.
  923. *
  924. * One cluster is allocated, initialized to zero and two directory
  925. * entries are created, '.' and '..'.
  926. *
  927. * \param dev Specifies the file system device.
  928. * \param path Full path to the directory.
  929. *
  930. * \return 0 on success. Otherwise -1 is returned.
  931. */
  932. int PhatDirCreate(NUTDEVICE * dev, char *path)
  933. {
  934. NUTFILE *ndp;
  935. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  936. PHATFILE *dfcb;
  937. PHATDIRENT *entry;
  938. uint8_t *buf;
  939. uint32_t sect;
  940. uint32_t clust;
  941. /*
  942. * Create the new directory like a normal file with a special attribute.
  943. */
  944. if ((ndp = PhatFileOpen(dev, path, _O_CREAT | _O_RDWR | _O_EXCL, PHAT_FATTR_DIR)) == NUTFILE_EOF) {
  945. return -1;
  946. }
  947. dfcb = ndp->nf_fcb;
  948. /*
  949. * Allocate a first cluster and initialize it with zeros.
  950. */
  951. if ((clust = AllocFirstCluster(ndp)) < 2) {
  952. PhatFileClose(ndp);
  953. return -1;
  954. }
  955. dfcb->f_clust_prv = clust;
  956. dfcb->f_clust = clust;
  957. if ((buf = malloc(vol->vol_sectsz)) == NULL) {
  958. PhatFileClose(ndp);
  959. return -1;
  960. }
  961. memset(buf, 0, vol->vol_sectsz);
  962. for (sect = vol->vol_clustsz; sect; sect--) {
  963. if (PhatFileWrite(ndp, buf, vol->vol_sectsz) < 0) {
  964. /* Write failed. */
  965. free(buf);
  966. PhatFileClose(ndp);
  967. return -1;
  968. }
  969. }
  970. free(buf);
  971. /*
  972. * Write the dot entry.
  973. */
  974. entry = malloc(sizeof(PHATDIRENT));
  975. *entry = dfcb->f_dirent;
  976. memset(entry->dent_name, ' ', sizeof(entry->dent_name));
  977. entry->dent_name[0] = '.';
  978. PhatFilePosRewind(dfcb);
  979. if (PhatFileWrite(ndp, entry, sizeof(PHATDIRENT)) != sizeof(PHATDIRENT)) {
  980. PhatFileClose(ndp);
  981. free(entry);
  982. return -1;
  983. }
  984. /*
  985. * Write the double dot entry. If it points to the root cluster,
  986. * then the cluster number in the directory entry must be zero.
  987. */
  988. if ((uint16_t) vol->vol_root_clust == dfcb->f_pde_clust && /* */
  989. vol->vol_root_clust >> 16 == dfcb->f_pde_clusthi) {
  990. entry->dent_clust = 0;
  991. entry->dent_clusthi = 0;
  992. } else {
  993. entry->dent_clust = dfcb->f_pde_clust;
  994. entry->dent_clusthi = dfcb->f_pde_clusthi;
  995. }
  996. entry->dent_name[1] = '.';
  997. if (PhatFileWrite(ndp, entry, sizeof(PHATDIRENT)) != sizeof(PHATDIRENT)) {
  998. PhatFileClose(ndp);
  999. free(entry);
  1000. return -1;
  1001. }
  1002. free(entry);
  1003. return PhatFileClose(ndp);
  1004. }
  1005. /*!
  1006. * \brief Remove a specified subdirectory.
  1007. *
  1008. * \param dev Specifies the file system device.
  1009. * \param path Full path to the directory.
  1010. *
  1011. * \return 0 on success. Otherwise -1 is returned.
  1012. */
  1013. int PhatDirRemove(NUTDEVICE * dev, char *path)
  1014. {
  1015. int rc = -1;
  1016. PHATDIRENT *entry;
  1017. NUTFILE *ndp;
  1018. /* Never remove the root directory */
  1019. if (path[0] == '/' && path[1] == 0) {
  1020. errno = EBUSY;
  1021. return -1;
  1022. }
  1023. if ((entry = malloc(sizeof(PHATDIRENT))) == NULL) {
  1024. return -1;
  1025. }
  1026. /*
  1027. * Make sure, that the directory we want to remove is empty. The dot
  1028. * and double dot entries are ignored.
  1029. */
  1030. if ((ndp = PhatFileOpen(dev, path, _O_RDONLY, 0)) != NUTFILE_EOF) {
  1031. rc = 0;
  1032. for (;;) {
  1033. rc = PhatFileRead(ndp, entry, sizeof(PHATDIRENT));
  1034. if (rc < 0) {
  1035. break;
  1036. }
  1037. /* Check for end of directory. */
  1038. if (rc < sizeof(PHATDIRENT) || entry->dent_name[0] == 0) {
  1039. rc = 0;
  1040. break;
  1041. }
  1042. /* Skip removed entries. */
  1043. if (entry->dent_name[0] == PHAT_REM_DIRENT) {
  1044. continue;
  1045. }
  1046. /* Ignore entries which are not files. */
  1047. if ((entry->dent_attr | PHAT_FATTR_FILEMASK) != PHAT_FATTR_FILEMASK) {
  1048. continue;
  1049. }
  1050. /* Ignore dot and double dot entries. */
  1051. if (entry->dent_name[0] == '.' && /* */
  1052. (entry->dent_name[1] == '.' || entry->dent_name[1] == ' ')) {
  1053. if (memcmp(" ", &entry->dent_name[2], 9) == 0) {
  1054. continue;
  1055. }
  1056. }
  1057. errno = ENOTEMPTY;
  1058. rc = -1;
  1059. break;
  1060. }
  1061. PhatFileClose(ndp);
  1062. }
  1063. free(entry);
  1064. /* If the empty check was successful, then remove the entry. */
  1065. if (rc == 0) {
  1066. rc = PhatDirDelEntry(dev, path, PHAT_FATTR_DIR);
  1067. }
  1068. return rc;
  1069. }
  1070. /*!
  1071. * \brief Retrieve status of a specified file.
  1072. *
  1073. * \param dev File system device.
  1074. * \param path Path name to the file to query.
  1075. * \param stp Pointer to a structure which receives the result.
  1076. *
  1077. * return 0 on success, -1 otherwise.
  1078. */
  1079. int PhatDirEntryStatus(NUTDEVICE * dev, const char *path, struct stat *stp)
  1080. {
  1081. int rc;
  1082. const char *fname;
  1083. NUTFILE *ndp;
  1084. PHATFIND *srch;
  1085. unsigned int val;
  1086. /* Open parent directory. */
  1087. if ((ndp = PhatDirOpenParent(dev, path, &fname)) == NUTFILE_EOF) {
  1088. return -1;
  1089. }
  1090. if ((srch = malloc(sizeof(PHATFIND))) == NULL) {
  1091. PhatFileClose(ndp);
  1092. return -1;
  1093. }
  1094. if ((rc = PhatDirEntryFind(ndp, fname, PHAT_FATTR_FILEMASK, srch)) == 0) {
  1095. struct _tm t;
  1096. memset(&t, 0, sizeof(struct _tm));
  1097. val = srch->phfind_ent.dent_mtime;
  1098. t.tm_sec = (val & 0x1F) << 1;
  1099. t.tm_min = (val >> 5) & 0x3F;
  1100. t.tm_hour = (val >> 11) & 0x1F;
  1101. val = srch->phfind_ent.dent_mdate;
  1102. t.tm_mday = val & 0x1F;
  1103. t.tm_mon = ((val >> 5) & 0x0F);
  1104. if (t.tm_mon) {
  1105. t.tm_mon--;
  1106. }
  1107. t.tm_year = ((val >> 9) & 0x7F) + 80;
  1108. t.tm_isdst = _daylight;
  1109. stp->st_mtime = mktime(&t);
  1110. stp->st_ino = 0;
  1111. stp->st_mode = (srch->phfind_ent.dent_attr & PHAT_FATTR_DIR) != 0;
  1112. stp->st_nlink = 0;
  1113. stp->st_size = srch->phfind_ent.dent_fsize;
  1114. }
  1115. free(srch);
  1116. PhatFileClose(ndp);
  1117. return rc;
  1118. }
  1119. /*@}*/