vfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /* Virtual filesystem: mount every device with a valid FAT filesystem under `/`,
  2. * and the command pack (ramfs) at /ramfs. File operations go through the
  3. * fs_ops vtable so sys_fs.c and the shell stay filesystem-agnostic. */
  4. #include "vfs.h"
  5. #include "fatdetect.h"
  6. #include "io.h"
  7. #include "ramfs.h"
  8. #include "share.h"
  9. vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
  10. unsigned int vfs_mount_count;
  11. static void put_dec(unsigned int n)
  12. {
  13. char buf[12];
  14. unsigned int i = sizeof(buf) - 1;
  15. buf[i] = '\0';
  16. if (n == 0) {
  17. io_putchar('0');
  18. return;
  19. }
  20. while (n && i > 0) {
  21. buf[--i] = (char)('0' + (n % 10));
  22. n /= 10;
  23. }
  24. io_putstr(buf + i);
  25. }
  26. /* Print a size given in 512-byte sectors. Uses shifts (not bytes) so a
  27. * >4 GB filesystem/disk does not overflow 32-bit arithmetic. */
  28. static void put_size_sectors(unsigned int sectors)
  29. {
  30. if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
  31. unsigned int gb = sectors >> 21;
  32. unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
  33. put_dec(gb);
  34. if (tenths) {
  35. io_putchar('.');
  36. put_dec(tenths);
  37. }
  38. io_putstr(" GB");
  39. } else if (sectors >= 2048u) { /* >= 1 MiB */
  40. unsigned int mb = sectors >> 11;
  41. unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
  42. put_dec(mb);
  43. if (tenths) {
  44. io_putchar('.');
  45. put_dec(tenths);
  46. }
  47. io_putstr(" MB");
  48. } else {
  49. put_dec(sectors / 2u);
  50. io_putstr(" KB");
  51. }
  52. }
  53. static const char *fat_name(fat_type_t t)
  54. {
  55. switch (t) {
  56. case FAT12: return "FAT12";
  57. case FAT16: return "FAT16";
  58. case FAT32: return "FAT32";
  59. default: return "FAT";
  60. }
  61. }
  62. static void mem_copy(void *dst, const void *src, unsigned int n)
  63. {
  64. unsigned char *d = (unsigned char *)dst;
  65. const unsigned char *s = (const unsigned char *)src;
  66. while (n--)
  67. *d++ = *s++;
  68. }
  69. static void copy_bounded(char *dst, const char *src, unsigned int size)
  70. {
  71. unsigned int i = 0;
  72. while (i + 1 < size && src[i]) {
  73. dst[i] = src[i];
  74. i++;
  75. }
  76. dst[i] = '\0';
  77. }
  78. /* ---- FAT implementation ---- */
  79. /* One load buffer for the shell: only one command is loaded at a time and the
  80. * ELF loader has copied the segments out before we return. */
  81. static unsigned char fat_load_buf[VFS_FILE_MAX];
  82. static int fat_op_read_file(struct vfs_mount *m, const char *path,
  83. void *buf, unsigned int max)
  84. {
  85. unsigned int dir;
  86. char name[12];
  87. int r = fat_resolve_path(&m->fs, path, &dir, name);
  88. if (r == -2)
  89. return VFS_EIO;
  90. if (r != 0)
  91. return VFS_ENOENT;
  92. r = fat_read_file_in(&m->fs, dir, name, buf, max);
  93. if (r == -2)
  94. return VFS_EIO;
  95. if (r < 0)
  96. return VFS_ENOENT;
  97. return r;
  98. }
  99. static int fat_op_stat(struct vfs_mount *m, const char *path,
  100. unsigned int *size, int *is_dir)
  101. {
  102. int r = fat_stat(&m->fs, path, size, is_dir);
  103. if (r == -2)
  104. return VFS_EIO;
  105. if (r != 0)
  106. return VFS_ENOENT;
  107. return 0;
  108. }
  109. static int fat_op_load_file(struct vfs_mount *m, const char *path,
  110. struct vfs_file *out)
  111. {
  112. unsigned int size = 0;
  113. int is_dir = 0, r;
  114. r = fat_op_stat(m, path, &size, &is_dir);
  115. if (r != 0)
  116. return r;
  117. if (is_dir || size == 0 || size > sizeof(fat_load_buf))
  118. return VFS_EINVAL;
  119. r = fat_op_read_file(m, path, fat_load_buf, sizeof(fat_load_buf));
  120. if (r < 0)
  121. return r;
  122. out->data = fat_load_buf;
  123. out->size = (unsigned int)r;
  124. return 0;
  125. }
  126. static void fat_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  127. {
  128. (void)m;
  129. (void)f;
  130. }
  131. static int fat_op_write_file(struct vfs_mount *m, const char *path,
  132. const void *buf, unsigned int len)
  133. {
  134. unsigned int dir;
  135. char name[12];
  136. int r = fat_resolve_path(&m->fs, path, &dir, name);
  137. if (r == -2)
  138. return VFS_EIO;
  139. if (r != 0)
  140. return VFS_EINVAL;
  141. r = fat_write_file_in(&m->fs, dir, name, buf, len);
  142. if (r == -2)
  143. return VFS_EIO;
  144. if (r != 0)
  145. return VFS_EINVAL;
  146. return 0;
  147. }
  148. static int fat_op_unlink(struct vfs_mount *m, const char *path)
  149. {
  150. unsigned int dir;
  151. char name[12];
  152. int r = fat_resolve_path(&m->fs, path, &dir, name);
  153. if (r == -2)
  154. return VFS_EIO;
  155. if (r != 0)
  156. return VFS_ENOENT;
  157. r = fat_delete_file_in(&m->fs, dir, name);
  158. if (r == -2)
  159. return VFS_EIO;
  160. if (r != 0)
  161. return VFS_ENOENT;
  162. return 0;
  163. }
  164. static int fat_op_mkdir(struct vfs_mount *m, const char *path)
  165. {
  166. unsigned int dir;
  167. char name[12];
  168. int r = fat_resolve_path(&m->fs, path, &dir, name);
  169. if (r == -2)
  170. return VFS_EIO;
  171. if (r != 0)
  172. return VFS_EINVAL;
  173. r = fat_mkdir(&m->fs, dir, name);
  174. if (r == -2)
  175. return VFS_EIO;
  176. if (r != 0)
  177. return VFS_EINVAL; /* the FAT layer printed its own reason */
  178. return 0;
  179. }
  180. static int fat_op_rmdir(struct vfs_mount *m, const char *path)
  181. {
  182. unsigned int dir;
  183. char name[12];
  184. int r = fat_resolve_path(&m->fs, path, &dir, name);
  185. if (r == -2)
  186. return VFS_EIO;
  187. if (r != 0)
  188. return VFS_EINVAL;
  189. r = fat_rmdir(&m->fs, dir, name);
  190. if (r == -2)
  191. return VFS_EIO;
  192. if (r != 0)
  193. return VFS_EINVAL;
  194. return 0;
  195. }
  196. /* Index-based listing: fat_list_dir walks from the start each call and we
  197. * count past `index` entries. Directories are small. */
  198. static unsigned int fat_list_index;
  199. static struct vfs_dirent *fat_list_out;
  200. static int fat_list_done;
  201. static unsigned int fat_list_seen;
  202. static void fat_list_cb(const char *name, int is_dir, unsigned int size)
  203. {
  204. unsigned int i;
  205. if (fat_list_done || fat_list_seen < fat_list_index) {
  206. fat_list_seen++;
  207. return;
  208. }
  209. for (i = 0; i < 11; i++)
  210. fat_list_out->name[i] = name[i] ? name[i] : ' ';
  211. fat_list_out->name[11] = '\0';
  212. fat_list_out->size = size;
  213. fat_list_out->is_dir = (unsigned int)is_dir;
  214. fat_list_out->raw_83 = 1;
  215. fat_list_done = 1;
  216. fat_list_seen++;
  217. }
  218. static int fat_op_list_dir(struct vfs_mount *m, const char *path,
  219. unsigned int index, struct vfs_dirent *out)
  220. {
  221. unsigned int dir;
  222. int r = fat_resolve_dir(&m->fs, path, &dir);
  223. if (r == -2)
  224. return VFS_EIO;
  225. if (r != 0)
  226. return VFS_ENOENT; /* not found (or not a directory) */
  227. fat_list_index = index;
  228. fat_list_seen = 0;
  229. fat_list_out = out;
  230. fat_list_done = 0;
  231. r = fat_list_dir(&m->fs, dir, fat_list_cb);
  232. if (r == -2)
  233. return VFS_EIO;
  234. return fat_list_done ? 1 : 0;
  235. }
  236. static const struct fs_ops fat_ops = {
  237. .raw_83 = 1,
  238. .read_file = fat_op_read_file,
  239. .load_file = fat_op_load_file,
  240. .release_file = fat_op_release_file,
  241. .stat = fat_op_stat,
  242. .write_file = fat_op_write_file,
  243. .unlink = fat_op_unlink,
  244. .mkdir = fat_op_mkdir,
  245. .rmdir = fat_op_rmdir,
  246. .list_dir = fat_op_list_dir,
  247. };
  248. /* ---- ramfs implementation ---- */
  249. static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
  250. void *buf, unsigned int max)
  251. {
  252. const void *data;
  253. unsigned int size = 0, n;
  254. (void)m;
  255. data = ramfs_lookup(path, &size);
  256. if (!data)
  257. return VFS_ENOENT;
  258. n = size > max ? max : size;
  259. mem_copy(buf, data, n);
  260. return (int)n;
  261. }
  262. static int ramfs_op_load_file(struct vfs_mount *m, const char *path,
  263. struct vfs_file *out)
  264. {
  265. const void *data;
  266. unsigned int size = 0;
  267. (void)m;
  268. data = ramfs_lookup(path, &size);
  269. if (!data)
  270. return VFS_ENOENT;
  271. out->data = data;
  272. out->size = size;
  273. return 0;
  274. }
  275. static void ramfs_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  276. {
  277. (void)m;
  278. (void)f;
  279. }
  280. static int ramfs_op_stat(struct vfs_mount *m, const char *path,
  281. unsigned int *size, int *is_dir)
  282. {
  283. const void *data;
  284. unsigned int sz = 0;
  285. struct ramfs_dirent e;
  286. (void)m;
  287. if (path[0] == '\0') {
  288. if (size)
  289. *size = 0;
  290. if (is_dir)
  291. *is_dir = 1;
  292. return 0;
  293. }
  294. data = ramfs_lookup(path, &sz);
  295. if (data) {
  296. if (size)
  297. *size = sz;
  298. if (is_dir)
  299. *is_dir = 0;
  300. return 0;
  301. }
  302. if (ramfs_list(path, 0, &e) == 1) {
  303. if (size)
  304. *size = 0;
  305. if (is_dir)
  306. *is_dir = 1;
  307. return 0;
  308. }
  309. return VFS_ENOENT;
  310. }
  311. static int ramfs_op_ro(struct vfs_mount *m, const char *path)
  312. {
  313. (void)m;
  314. (void)path;
  315. return VFS_EROFS;
  316. }
  317. static int ramfs_op_write_file(struct vfs_mount *m, const char *path,
  318. const void *buf, unsigned int len)
  319. {
  320. (void)buf;
  321. (void)len;
  322. return ramfs_op_ro(m, path);
  323. }
  324. static int ramfs_op_unlink(struct vfs_mount *m, const char *path)
  325. {
  326. return ramfs_op_ro(m, path);
  327. }
  328. static int ramfs_op_mkdir(struct vfs_mount *m, const char *path)
  329. {
  330. return ramfs_op_ro(m, path);
  331. }
  332. static int ramfs_op_rmdir(struct vfs_mount *m, const char *path)
  333. {
  334. return ramfs_op_ro(m, path);
  335. }
  336. static int ramfs_op_list_dir(struct vfs_mount *m, const char *path,
  337. unsigned int index, struct vfs_dirent *out)
  338. {
  339. struct ramfs_dirent e;
  340. (void)m;
  341. if (ramfs_list(path, index, &e) != 1) {
  342. if (index == 0 && path[0] != '\0') {
  343. unsigned int size = 0;
  344. /* Nothing matched: report a missing path instead of an
  345. * empty directory (the pack has no empty directories --
  346. * one exists exactly as a prefix of file paths), and a
  347. * file as "not a directory". */
  348. if (ramfs_lookup(path, &size))
  349. return VFS_EINVAL;
  350. return VFS_ENOENT;
  351. }
  352. return 0; /* end of directory */
  353. }
  354. copy_bounded(out->name, e.name, sizeof(out->name));
  355. out->size = e.size;
  356. out->is_dir = e.is_dir;
  357. out->raw_83 = 0;
  358. return 1;
  359. }
  360. static const struct fs_ops ramfs_ops = {
  361. .raw_83 = 0,
  362. .read_file = ramfs_op_read_file,
  363. .load_file = ramfs_op_load_file,
  364. .release_file = ramfs_op_release_file,
  365. .stat = ramfs_op_stat,
  366. .write_file = ramfs_op_write_file,
  367. .unlink = ramfs_op_unlink,
  368. .mkdir = ramfs_op_mkdir,
  369. .rmdir = ramfs_op_rmdir,
  370. .list_dir = ramfs_op_list_dir,
  371. };
  372. /* ---- mounts ---- */
  373. static vfs_mount_t *mount_slot(const char *name)
  374. {
  375. unsigned int i;
  376. if (vfs_mount_count >= VFS_MAX_MOUNTS)
  377. return 0;
  378. {
  379. vfs_mount_t *m = &vfs_mounts[vfs_mount_count];
  380. for (i = 0; name[i] && i < sizeof(m->name) - 1; i++)
  381. m->name[i] = name[i];
  382. m->name[i] = '\0';
  383. m->mounted = 1;
  384. return m;
  385. }
  386. }
  387. static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
  388. {
  389. fat_fs_t fs;
  390. vfs_mount_t *m;
  391. if (fat_mount(dev, part_lba, &fs) != 0)
  392. return;
  393. m = mount_slot(name);
  394. if (!m)
  395. return;
  396. m->ops = &fat_ops;
  397. m->fs = fs;
  398. m->ro = 0;
  399. m->total_sectors = fs.total_sectors;
  400. copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
  401. copy_bounded(m->label, fs.label, sizeof(m->label));
  402. vfs_mount_count++;
  403. }
  404. void vfs_mount_ramfs(void)
  405. {
  406. vfs_mount_t *m;
  407. if (ramfs_count() == 0)
  408. return;
  409. m = mount_slot("ramfs");
  410. if (!m)
  411. return;
  412. m->ops = &ramfs_ops;
  413. m->ro = 1;
  414. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  415. copy_bounded(m->type, "ramfs", sizeof(m->type));
  416. m->label[0] = '\0';
  417. vfs_mount_count++;
  418. }
  419. void vfs_refresh_ramfs(void)
  420. {
  421. vfs_mount_t *m = vfs_find("ramfs");
  422. if (m)
  423. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  424. }
  425. /* Writable RAM store for network uploads. Unlike the pack ramfs it is not a
  426. * block device and its contents survive vfs_mount_all() refreshes. */
  427. void vfs_mount_share(void)
  428. {
  429. vfs_mount_t *m;
  430. if (share_init() != 0)
  431. return;
  432. m = mount_slot("share");
  433. if (!m)
  434. return;
  435. m->ops = &share_ops;
  436. m->ro = 0;
  437. m->total_sectors = SHARE_MAX_BYTES / 512u;
  438. copy_bounded(m->type, "share", sizeof(m->type));
  439. m->label[0] = '\0';
  440. vfs_mount_count++;
  441. }
  442. static void part_name(char *out, const char *base, unsigned int idx)
  443. {
  444. unsigned int i = 0;
  445. while (base[i] && i < 13) {
  446. out[i] = base[i];
  447. i++;
  448. }
  449. out[i++] = 'p';
  450. out[i++] = (char)('0' + idx + 1);
  451. out[i] = '\0';
  452. }
  453. static void mount_device(blk_dev_t *d, unsigned char *buf)
  454. {
  455. mbr_part_t parts[4];
  456. int n, j;
  457. int any_fat = 0;
  458. if (d->kind != BLK_FLOPPY && !d->present)
  459. return;
  460. if (d->kind == BLK_FLOPPY) {
  461. mount_one(d, 0, d->name);
  462. return;
  463. }
  464. /* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
  465. if (blk_read(d, 0, buf, 1) != 0)
  466. return;
  467. n = mbr_parse(buf, parts);
  468. for (j = 0; j < n; j++) {
  469. char pname[16];
  470. if (!parts[j].is_fat || parts[j].start_lba == 0 ||
  471. parts[j].sectors == 0)
  472. continue;
  473. any_fat = 1;
  474. part_name(pname, d->name, parts[j].index);
  475. mount_one(d, parts[j].start_lba, pname);
  476. }
  477. if (!any_fat)
  478. mount_one(d, 0, d->name);
  479. }
  480. /* Mount names for a device are the device itself or "<device>pN". */
  481. static int mount_belongs_to(const char *mount, const char *dev)
  482. {
  483. unsigned int i = 0;
  484. while (dev[i] && mount[i] == dev[i])
  485. i++;
  486. if (dev[i] != '\0')
  487. return 0;
  488. return mount[i] == '\0' || mount[i] == 'p';
  489. }
  490. static void unmount_device(const char *dev)
  491. {
  492. unsigned int i, j;
  493. for (i = 0; i < vfs_mount_count; ) {
  494. if (mount_belongs_to(vfs_mounts[i].name, dev)) {
  495. for (j = i + 1; j < vfs_mount_count; j++)
  496. vfs_mounts[j - 1] = vfs_mounts[j];
  497. vfs_mount_count--;
  498. } else {
  499. i++;
  500. }
  501. }
  502. }
  503. void vfs_mount_all(void)
  504. {
  505. static unsigned char buf[512];
  506. unsigned int i;
  507. vfs_mount_count = 0;
  508. blk_scan();
  509. for (i = 0; i < blk_dev_count; i++)
  510. mount_device(&blk_devs[i], buf);
  511. /* The ramfs is not a block device; keep it mounted across a refresh
  512. * (the `storage` command calls vfs_mount_all()). /share is likewise
  513. * independent of the block devices. */
  514. vfs_mount_ramfs();
  515. vfs_mount_share();
  516. }
  517. static int device_is_mounted(const char *dev)
  518. {
  519. unsigned int i;
  520. for (i = 0; i < vfs_mount_count; i++)
  521. if (mount_belongs_to(vfs_mounts[i].name, dev))
  522. return 1;
  523. return 0;
  524. }
  525. void vfs_remount_device(blk_dev_t *d)
  526. {
  527. unsigned char buf[512];
  528. if (!d)
  529. return;
  530. unmount_device(d->name);
  531. mount_device(d, buf);
  532. if (!device_is_mounted(d->name)) {
  533. io_putstr("vfs: remount of ");
  534. io_putstr(d->name);
  535. io_putstr(" found no filesystem\n");
  536. }
  537. }
  538. vfs_mount_t *vfs_find(const char *name)
  539. {
  540. unsigned int i;
  541. for (i = 0; i < vfs_mount_count; i++) {
  542. unsigned int j = 0;
  543. while (vfs_mounts[i].name[j] && name[j] &&
  544. vfs_mounts[i].name[j] == name[j])
  545. j++;
  546. if (vfs_mounts[i].name[j] == '\0' && name[j] == '\0')
  547. return &vfs_mounts[i];
  548. }
  549. return 0;
  550. }
  551. void vfs_list_root(void)
  552. {
  553. unsigned int i;
  554. for (i = 0; i < vfs_mount_count; i++) {
  555. vfs_mount_t *m = &vfs_mounts[i];
  556. io_putstr(m->name);
  557. io_putstr(" ");
  558. io_putstr(m->type);
  559. io_putchar(' ');
  560. put_size_sectors(m->total_sectors);
  561. if (m->label[0]) {
  562. io_putstr(" \"");
  563. io_putstr(m->label);
  564. io_putchar('"');
  565. }
  566. if (m->ro)
  567. io_putstr(" (ro)");
  568. io_putchar('\n');
  569. }
  570. }
  571. int vfs_resolve(const char *path, struct vfs_mount **m_out,
  572. char *rel_path, unsigned int rel_size)
  573. {
  574. unsigned int i = 0, j = 0, o = 0;
  575. char name[16];
  576. vfs_mount_t *m;
  577. if (!path || path[0] != '/' || rel_size == 0)
  578. return -1;
  579. i = 1;
  580. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  581. name[j++] = path[i++];
  582. name[j] = '\0';
  583. if (j == 0)
  584. return -1; /* path is just "/" */
  585. m = vfs_find(name);
  586. if (!m)
  587. return -1;
  588. /* Copy the remainder as a normalized relative path: skip slashes, drop
  589. * "." components and never leave a trailing one. */
  590. while (path[i]) {
  591. unsigned int clen = 0;
  592. const char *comp;
  593. while (path[i] == '/')
  594. i++;
  595. if (!path[i])
  596. break;
  597. comp = path + i;
  598. while (path[i] && path[i] != '/') {
  599. i++;
  600. clen++;
  601. }
  602. if (clen == 1 && comp[0] == '.')
  603. continue;
  604. if (o + clen + 2 > rel_size)
  605. return -1;
  606. if (o > 0)
  607. rel_path[o++] = '/';
  608. {
  609. unsigned int k;
  610. for (k = 0; k < clen; k++)
  611. rel_path[o++] = comp[k];
  612. }
  613. }
  614. rel_path[o] = '\0';
  615. *m_out = m;
  616. return 0;
  617. }
  618. int vfs_load_file(const char *path, struct vfs_file *out,
  619. struct vfs_mount **m_out)
  620. {
  621. struct vfs_mount *m;
  622. char rel[VFS_PATH_MAX];
  623. int r;
  624. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  625. return VFS_ENOENT;
  626. r = m->ops->load_file(m, rel, out);
  627. if (r == 0 && m_out)
  628. *m_out = m;
  629. return r;
  630. }
  631. void vfs_release_file(struct vfs_mount *m, struct vfs_file *f)
  632. {
  633. if (m && m->ops->release_file)
  634. m->ops->release_file(m, f);
  635. }
  636. int vfs_stat(const char *path, unsigned int *size, int *is_dir)
  637. {
  638. struct vfs_mount *m;
  639. char rel[VFS_PATH_MAX];
  640. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  641. return VFS_ENOENT;
  642. return m->ops->stat(m, rel, size, is_dir);
  643. }
  644. int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out)
  645. {
  646. struct vfs_mount *m;
  647. char rel[VFS_PATH_MAX];
  648. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  649. return VFS_ENOENT;
  650. return m->ops->list_dir(m, rel, index, out);
  651. }
  652. int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size)
  653. {
  654. char buf[VFS_PATH_MAX * 2 + 2];
  655. unsigned int bi = 0;
  656. unsigned int len;
  657. const char *p;
  658. /* Build the combined (un-normalized) path. */
  659. if (arg[0] == '/') {
  660. p = arg;
  661. } else {
  662. p = cwd;
  663. while (*p && bi < sizeof(buf) - 1)
  664. buf[bi++] = *p++;
  665. if (bi > 0 && buf[bi - 1] != '/') {
  666. if (bi < sizeof(buf) - 1)
  667. buf[bi++] = '/';
  668. }
  669. p = arg;
  670. }
  671. while (*p && bi < sizeof(buf) - 1)
  672. buf[bi++] = *p++;
  673. buf[bi] = '\0';
  674. if (out_size < 2)
  675. return -1;
  676. /* Normalize into out (always begins with "/"). */
  677. out[0] = '/';
  678. out[1] = '\0';
  679. len = 1;
  680. p = buf;
  681. while (*p) {
  682. unsigned int clen = 0;
  683. const char *comp;
  684. while (*p == '/')
  685. p++;
  686. if (!*p)
  687. break;
  688. comp = p;
  689. while (*p && *p != '/') {
  690. p++;
  691. clen++;
  692. }
  693. if (clen == 1 && comp[0] == '.')
  694. continue;
  695. if (clen == 2 && comp[0] == '.' && comp[1] == '.') {
  696. /* Pop the previous component (may leave "/"). */
  697. while (len > 1 && out[len - 1] != '/')
  698. len--;
  699. if (len > 1)
  700. len--;
  701. out[len] = '\0';
  702. continue;
  703. }
  704. /* Append "/component". */
  705. if (len + 1 + clen + 1 > out_size)
  706. return -1;
  707. if (len > 1)
  708. out[len++] = '/';
  709. {
  710. unsigned int k;
  711. for (k = 0; k < clen; k++)
  712. out[len++] = comp[k];
  713. }
  714. out[len] = '\0';
  715. }
  716. return 0;
  717. }
  718. int vfs_device_known(const char *path)
  719. {
  720. unsigned int i = 0, j = 0;
  721. char name[16];
  722. if (!path || path[0] != '/')
  723. return 0;
  724. i = 1;
  725. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  726. name[j++] = path[i++];
  727. name[j] = '\0';
  728. if (j == 0)
  729. return 0;
  730. return blk_find(name) != 0;
  731. }