subdir.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include "dos.h"
  28. #include "fdos.h"
  29. static int cache_sect;
  30. static unsigned char cache [SZ_STD_SECTOR];
  31. #define min(x,y) ((x)<(y)?(x):(y))
  32. static int descend (Slot_t *parent,
  33. Fs_t *fs,
  34. char *path);
  35. /*-----------------------------------------------------------------------------
  36. * init_subdir --
  37. *-----------------------------------------------------------------------------
  38. */
  39. void init_subdir (void)
  40. {
  41. cache_sect = -1;
  42. }
  43. /*-----------------------------------------------------------------------------
  44. * basename --
  45. *-----------------------------------------------------------------------------
  46. */
  47. char *basename (char *name)
  48. {
  49. register char *cptr;
  50. if (!name || !*name) {
  51. return ("");
  52. }
  53. for (cptr= name; *cptr++; );
  54. while (--cptr >= name) {
  55. if (*cptr == '/') {
  56. return (cptr + 1);
  57. }
  58. }
  59. return(name);
  60. }
  61. /*-----------------------------------------------------------------------------
  62. * root_map --
  63. *-----------------------------------------------------------------------------
  64. */
  65. static int root_map (Fs_t *fs, Slot_t *file, int where, int *len)
  66. {
  67. *len = min (*len, fs -> dir_len * SZ_STD_SECTOR - where);
  68. if (*len < 0 ) {
  69. *len = 0;
  70. return (-1);
  71. }
  72. return fs -> dir_start * SZ_STD_SECTOR + where;
  73. }
  74. /*-----------------------------------------------------------------------------
  75. * normal_map --
  76. *-----------------------------------------------------------------------------
  77. */
  78. static int normal_map (Fs_t *fs, Slot_t *file, int where, int *len)
  79. {
  80. int offset;
  81. int NrClu;
  82. unsigned short RelCluNr;
  83. unsigned short CurCluNr;
  84. unsigned short NewCluNr;
  85. unsigned short AbsCluNr;
  86. int clus_size;
  87. clus_size = fs -> cluster_size * SZ_STD_SECTOR;
  88. offset = where % clus_size;
  89. *len = min (*len, file -> FileSize - where);
  90. if (*len < 0 ) {
  91. *len = 0;
  92. return (0);
  93. }
  94. if (file -> FirstAbsCluNr < 2){
  95. *len = 0;
  96. return (0);
  97. }
  98. RelCluNr = where / clus_size;
  99. if (RelCluNr >= file -> PreviousRelCluNr){
  100. CurCluNr = file -> PreviousRelCluNr;
  101. AbsCluNr = file -> PreviousAbsCluNr;
  102. } else {
  103. CurCluNr = 0;
  104. AbsCluNr = file -> FirstAbsCluNr;
  105. }
  106. NrClu = (offset + *len - 1) / clus_size;
  107. while (CurCluNr <= RelCluNr + NrClu) {
  108. if (CurCluNr == RelCluNr){
  109. /* we have reached the beginning of our zone. Save
  110. * coordinates */
  111. file -> PreviousRelCluNr = RelCluNr;
  112. file -> PreviousAbsCluNr = AbsCluNr;
  113. }
  114. NewCluNr = fat_decode (fs, AbsCluNr);
  115. if (NewCluNr == 1 || NewCluNr == 0) {
  116. PRINTF("Fat problem while decoding %d %x\n",
  117. AbsCluNr, NewCluNr);
  118. return (-1);
  119. }
  120. if (CurCluNr == RelCluNr + NrClu) {
  121. break;
  122. }
  123. if (CurCluNr < RelCluNr && NewCluNr == FAT12_END) {
  124. *len = 0;
  125. return 0;
  126. }
  127. if (CurCluNr >= RelCluNr && NewCluNr != AbsCluNr + 1)
  128. break;
  129. CurCluNr++;
  130. AbsCluNr = NewCluNr;
  131. }
  132. *len = min (*len, (1 + CurCluNr - RelCluNr) * clus_size - offset);
  133. return (((file -> PreviousAbsCluNr - 2) * fs -> cluster_size +
  134. fs -> dir_start + fs -> dir_len) *
  135. SZ_STD_SECTOR + offset);
  136. }
  137. /*-----------------------------------------------------------------------------
  138. * open_subdir -- open the subdir containing the file
  139. *-----------------------------------------------------------------------------
  140. */
  141. int open_subdir (File_t *desc)
  142. {
  143. char *pathname;
  144. char *tmp, *s, *path;
  145. char terminator;
  146. if ((pathname = (char *)malloc (MAX_PATH)) == NULL) {
  147. return (-1);
  148. }
  149. strcpy (pathname, desc -> name);
  150. /* Suppress file name */
  151. tmp = basename (pathname);
  152. *tmp = '\0';
  153. /* root directory init */
  154. desc -> subdir.FirstAbsCluNr = 0;
  155. desc -> subdir.FileSize = -1;
  156. desc -> subdir.map = root_map;
  157. desc -> subdir.dir.attr = ATTR_DIRECTORY;
  158. tmp = pathname;
  159. for (s = tmp; ; ++s) {
  160. if (*s == '/' || *s == '\0') {
  161. path = tmp;
  162. terminator = *s;
  163. *s = '\0';
  164. if (s != tmp && strcmp (path,".")) {
  165. if (descend (&desc -> subdir, desc -> fs, path) < 0) {
  166. free (pathname);
  167. return (-1);
  168. }
  169. }
  170. if (terminator == 0) {
  171. break;
  172. }
  173. tmp = s + 1;
  174. }
  175. }
  176. free (pathname);
  177. return (0);
  178. }
  179. /*-----------------------------------------------------------------------------
  180. * descend --
  181. *-----------------------------------------------------------------------------
  182. */
  183. static int descend (Slot_t *parent,
  184. Fs_t *fs,
  185. char *path)
  186. {
  187. int entry;
  188. Slot_t SubDir;
  189. if(path[0] == '\0' || strcmp (path, ".") == 0) {
  190. return (0);
  191. }
  192. entry = 0;
  193. if (vfat_lookup (parent,
  194. fs,
  195. &(SubDir.dir),
  196. &entry,
  197. 0,
  198. path,
  199. ACCEPT_DIR | SINGLE | DO_OPEN,
  200. 0,
  201. &SubDir) == 0) {
  202. *parent = SubDir;
  203. return (0);
  204. }
  205. if (strcmp(path, "..") == 0) {
  206. parent -> FileSize = -1;
  207. parent -> FirstAbsCluNr = 0;
  208. parent -> map = root_map;
  209. return (0);
  210. }
  211. return (-1);
  212. }
  213. /*-----------------------------------------------------------------------------
  214. * open_file --
  215. *-----------------------------------------------------------------------------
  216. */
  217. int open_file (Slot_t *file, Directory_t *dir)
  218. {
  219. int first;
  220. unsigned long size;
  221. first = __le16_to_cpu (dir -> start);
  222. if(first == 0 &&
  223. (dir -> attr & ATTR_DIRECTORY) != 0) {
  224. file -> FirstAbsCluNr = 0;
  225. file -> FileSize = -1;
  226. file -> map = root_map;
  227. return (0);
  228. }
  229. if ((dir -> attr & ATTR_DIRECTORY) != 0) {
  230. size = (1UL << 31) - 1;
  231. }
  232. else {
  233. size = __le32_to_cpu (dir -> size);
  234. }
  235. file -> map = normal_map;
  236. file -> FirstAbsCluNr = first;
  237. file -> PreviousRelCluNr = 0xffff;
  238. file -> FileSize = size;
  239. return (0);
  240. }
  241. /*-----------------------------------------------------------------------------
  242. * read_file --
  243. *-----------------------------------------------------------------------------
  244. */
  245. int read_file (Fs_t *fs,
  246. Slot_t *file,
  247. char *buf,
  248. int where,
  249. int len)
  250. {
  251. int pos;
  252. int read, nb, sect, offset;
  253. pos = file -> map (fs, file, where, &len);
  254. if (pos < 0) {
  255. return -1;
  256. }
  257. if (len == 0) {
  258. return (0);
  259. }
  260. /* Compute sector number */
  261. sect = pos / SZ_STD_SECTOR;
  262. offset = pos % SZ_STD_SECTOR;
  263. read = 0;
  264. if (offset) {
  265. /* Read doesn't start at the sector beginning. We need to use our */
  266. /* cache */
  267. if (sect != cache_sect) {
  268. if (dev_read (cache, sect, 1) < 0) {
  269. return (-1);
  270. }
  271. cache_sect = sect;
  272. }
  273. nb = min (len, SZ_STD_SECTOR - offset);
  274. memcpy (buf, cache + offset, nb);
  275. read += nb;
  276. len -= nb;
  277. sect += 1;
  278. }
  279. if (len > SZ_STD_SECTOR) {
  280. nb = (len - 1) / SZ_STD_SECTOR;
  281. if (dev_read (buf + read, sect, nb) < 0) {
  282. return ((read) ? read : -1);
  283. }
  284. /* update sector position */
  285. sect += nb;
  286. /* Update byte position */
  287. nb *= SZ_STD_SECTOR;
  288. read += nb;
  289. len -= nb;
  290. }
  291. if (len) {
  292. if (sect != cache_sect) {
  293. if (dev_read (cache, sect, 1) < 0) {
  294. return ((read) ? read : -1);
  295. cache_sect = -1;
  296. }
  297. cache_sect = sect;
  298. }
  299. memcpy (buf + read, cache, len);
  300. read += len;
  301. }
  302. return (read);
  303. }