subdir.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * (C) Copyright 2002
  3. * Stäubli Faverges - <www.staubli.com>
  4. * Pierre AUBERT p.aubert@staubli.com
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <config.h>
  26. #include <malloc.h>
  27. #if (CONFIG_COMMANDS & CFG_CMD_FDOS)
  28. #include "dos.h"
  29. #include "fdos.h"
  30. static int cache_sect;
  31. static unsigned char cache [SZ_STD_SECTOR];
  32. #define min(x,y) ((x)<(y)?(x):(y))
  33. static int descend (Slot_t *parent,
  34. Fs_t *fs,
  35. char *path);
  36. /*-----------------------------------------------------------------------------
  37. * init_subdir --
  38. *-----------------------------------------------------------------------------
  39. */
  40. void init_subdir (void)
  41. {
  42. cache_sect = -1;
  43. }
  44. /*-----------------------------------------------------------------------------
  45. * basename --
  46. *-----------------------------------------------------------------------------
  47. */
  48. char *basename (char *name)
  49. {
  50. register char *cptr;
  51. if (!name || !*name) {
  52. return ("");
  53. }
  54. for (cptr= name; *cptr++; );
  55. while (--cptr >= name) {
  56. if (*cptr == '/') {
  57. return (cptr + 1);
  58. }
  59. }
  60. return(name);
  61. }
  62. /*-----------------------------------------------------------------------------
  63. * root_map --
  64. *-----------------------------------------------------------------------------
  65. */
  66. static int root_map (Fs_t *fs, Slot_t *file, int where, int *len)
  67. {
  68. *len = min (*len, fs -> dir_len * SZ_STD_SECTOR - where);
  69. if (*len < 0 ) {
  70. *len = 0;
  71. return (-1);
  72. }
  73. return fs -> dir_start * SZ_STD_SECTOR + where;
  74. }
  75. /*-----------------------------------------------------------------------------
  76. * normal_map --
  77. *-----------------------------------------------------------------------------
  78. */
  79. static int normal_map (Fs_t *fs, Slot_t *file, int where, int *len)
  80. {
  81. int offset;
  82. int NrClu;
  83. unsigned short RelCluNr;
  84. unsigned short CurCluNr;
  85. unsigned short NewCluNr;
  86. unsigned short AbsCluNr;
  87. int clus_size;
  88. clus_size = fs -> cluster_size * SZ_STD_SECTOR;
  89. offset = where % clus_size;
  90. *len = min (*len, file -> FileSize - where);
  91. if (*len < 0 ) {
  92. *len = 0;
  93. return (0);
  94. }
  95. if (file -> FirstAbsCluNr < 2){
  96. *len = 0;
  97. return (0);
  98. }
  99. RelCluNr = where / clus_size;
  100. if (RelCluNr >= file -> PreviousRelCluNr){
  101. CurCluNr = file -> PreviousRelCluNr;
  102. AbsCluNr = file -> PreviousAbsCluNr;
  103. } else {
  104. CurCluNr = 0;
  105. AbsCluNr = file -> FirstAbsCluNr;
  106. }
  107. NrClu = (offset + *len - 1) / clus_size;
  108. while (CurCluNr <= RelCluNr + NrClu) {
  109. if (CurCluNr == RelCluNr){
  110. /* we have reached the beginning of our zone. Save
  111. * coordinates */
  112. file -> PreviousRelCluNr = RelCluNr;
  113. file -> PreviousAbsCluNr = AbsCluNr;
  114. }
  115. NewCluNr = fat_decode (fs, AbsCluNr);
  116. if (NewCluNr == 1 || NewCluNr == 0) {
  117. PRINTF("Fat problem while decoding %d %x\n",
  118. AbsCluNr, NewCluNr);
  119. return (-1);
  120. }
  121. if (CurCluNr == RelCluNr + NrClu) {
  122. break;
  123. }
  124. if (CurCluNr < RelCluNr && NewCluNr == FAT12_END) {
  125. *len = 0;
  126. return 0;
  127. }
  128. if (CurCluNr >= RelCluNr && NewCluNr != AbsCluNr + 1)
  129. break;
  130. CurCluNr++;
  131. AbsCluNr = NewCluNr;
  132. }
  133. *len = min (*len, (1 + CurCluNr - RelCluNr) * clus_size - offset);
  134. return (((file -> PreviousAbsCluNr - 2) * fs -> cluster_size +
  135. fs -> dir_start + fs -> dir_len) *
  136. SZ_STD_SECTOR + offset);
  137. }
  138. /*-----------------------------------------------------------------------------
  139. * open_subdir -- open the subdir containing the file
  140. *-----------------------------------------------------------------------------
  141. */
  142. int open_subdir (File_t *desc)
  143. {
  144. char *pathname;
  145. char *tmp, *s, *path;
  146. char terminator;
  147. if ((pathname = (char *)malloc (MAX_PATH)) == NULL) {
  148. return (-1);
  149. }
  150. strcpy (pathname, desc -> name);
  151. /* Suppress file name */
  152. tmp = basename (pathname);
  153. *tmp = '\0';
  154. /* root directory init */
  155. desc -> subdir.FirstAbsCluNr = 0;
  156. desc -> subdir.FileSize = -1;
  157. desc -> subdir.map = root_map;
  158. desc -> subdir.dir.attr = ATTR_DIRECTORY;
  159. tmp = pathname;
  160. for (s = tmp; ; ++s) {
  161. if (*s == '/' || *s == '\0') {
  162. path = tmp;
  163. terminator = *s;
  164. *s = '\0';
  165. if (s != tmp && strcmp (path,".")) {
  166. if (descend (&desc -> subdir, desc -> fs, path) < 0) {
  167. free (pathname);
  168. return (-1);
  169. }
  170. }
  171. if (terminator == 0) {
  172. break;
  173. }
  174. tmp = s + 1;
  175. }
  176. }
  177. free (pathname);
  178. return (0);
  179. }
  180. /*-----------------------------------------------------------------------------
  181. * descend --
  182. *-----------------------------------------------------------------------------
  183. */
  184. static int descend (Slot_t *parent,
  185. Fs_t *fs,
  186. char *path)
  187. {
  188. int entry;
  189. Slot_t SubDir;
  190. if(path[0] == '\0' || strcmp (path, ".") == 0) {
  191. return (0);
  192. }
  193. entry = 0;
  194. if (vfat_lookup (parent,
  195. fs,
  196. &(SubDir.dir),
  197. &entry,
  198. 0,
  199. path,
  200. ACCEPT_DIR | SINGLE | DO_OPEN,
  201. 0,
  202. &SubDir) == 0) {
  203. *parent = SubDir;
  204. return (0);
  205. }
  206. if (strcmp(path, "..") == 0) {
  207. parent -> FileSize = -1;
  208. parent -> FirstAbsCluNr = 0;
  209. parent -> map = root_map;
  210. return (0);
  211. }
  212. return (-1);
  213. }
  214. /*-----------------------------------------------------------------------------
  215. * open_file --
  216. *-----------------------------------------------------------------------------
  217. */
  218. int open_file (Slot_t *file, Directory_t *dir)
  219. {
  220. int first;
  221. unsigned long size;
  222. first = __le16_to_cpu (dir -> start);
  223. if(first == 0 &&
  224. (dir -> attr & ATTR_DIRECTORY) != 0) {
  225. file -> FirstAbsCluNr = 0;
  226. file -> FileSize = -1;
  227. file -> map = root_map;
  228. return (0);
  229. }
  230. if ((dir -> attr & ATTR_DIRECTORY) != 0) {
  231. size = (1UL << 31) - 1;
  232. }
  233. else {
  234. size = __le32_to_cpu (dir -> size);
  235. }
  236. file -> map = normal_map;
  237. file -> FirstAbsCluNr = first;
  238. file -> PreviousRelCluNr = 0xffff;
  239. file -> FileSize = size;
  240. return (0);
  241. }
  242. /*-----------------------------------------------------------------------------
  243. * read_file --
  244. *-----------------------------------------------------------------------------
  245. */
  246. int read_file (Fs_t *fs,
  247. Slot_t *file,
  248. char *buf,
  249. int where,
  250. int len)
  251. {
  252. int pos;
  253. int read, nb, sect, offset;
  254. pos = file -> map (fs, file, where, &len);
  255. if (pos < 0) {
  256. return -1;
  257. }
  258. if (len == 0) {
  259. return (0);
  260. }
  261. /* Compute sector number */
  262. sect = pos / SZ_STD_SECTOR;
  263. offset = pos % SZ_STD_SECTOR;
  264. read = 0;
  265. if (offset) {
  266. /* Read doesn't start at the sector beginning. We need to use our */
  267. /* cache */
  268. if (sect != cache_sect) {
  269. if (dev_read (cache, sect, 1) < 0) {
  270. return (-1);
  271. }
  272. cache_sect = sect;
  273. }
  274. nb = min (len, SZ_STD_SECTOR - offset);
  275. memcpy (buf, cache + offset, nb);
  276. read += nb;
  277. len -= nb;
  278. sect += 1;
  279. }
  280. if (len > SZ_STD_SECTOR) {
  281. nb = (len - 1) / SZ_STD_SECTOR;
  282. if (dev_read (buf + read, sect, nb) < 0) {
  283. return ((read) ? read : -1);
  284. }
  285. /* update sector position */
  286. sect += nb;
  287. /* Update byte position */
  288. nb *= SZ_STD_SECTOR;
  289. read += nb;
  290. len -= nb;
  291. }
  292. if (len) {
  293. if (sect != cache_sect) {
  294. if (dev_read (cache, sect, 1) < 0) {
  295. return ((read) ? read : -1);
  296. cache_sect = -1;
  297. }
  298. cache_sect = sect;
  299. }
  300. memcpy (buf + read, cache, len);
  301. read += len;
  302. }
  303. return (read);
  304. }
  305. #endif