vfs.c 16 KB

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