fs.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "fs.h"
  8. /*
  9. * grab next element from a path, return the pointer to unprocessed portion of
  10. * path.
  11. */
  12. char *
  13. nextelem(char *path, char *elem)
  14. {
  15. int i;
  16. while(*path == '/')
  17. path++;
  18. if(*path==0 || *path==' ')
  19. return 0;
  20. for(i=0; *path!='\0' && *path!='/' && *path!=' '; i++){
  21. if(i==NAMELEN){
  22. print("name component too long\n");
  23. return 0;
  24. }
  25. *elem++ = *path++;
  26. }
  27. *elem = '\0';
  28. return path;
  29. }
  30. int
  31. fswalk(Fs *fs, char *path, File *f)
  32. {
  33. char element[NAMELEN];
  34. *f = fs->root;
  35. if(BADPTR(fs->walk))
  36. panic("fswalk bad pointer fs->walk");
  37. f->path = path;
  38. while(path = nextelem(path, element)){
  39. switch(fs->walk(f, element)){
  40. case -1:
  41. return -1;
  42. case 0:
  43. return 0;
  44. }
  45. }
  46. return 1;
  47. }
  48. /*
  49. * boot
  50. */
  51. int
  52. fsboot(Fs *fs, char *path, Boot *b)
  53. {
  54. File file;
  55. long n;
  56. static char buf[8192];
  57. switch(fswalk(fs, path, &file)){
  58. case -1:
  59. print("error walking to %s\n", path);
  60. return -1;
  61. case 0:
  62. print("%s not found\n", path);
  63. return -1;
  64. case 1:
  65. print("found %s\n", path);
  66. break;
  67. }
  68. while((n = fsread(&file, buf, sizeof buf)) > 0) {
  69. if(bootpass(b, buf, n) != MORE)
  70. break;
  71. }
  72. bootpass(b, nil, 0); /* tries boot */
  73. return -1;
  74. }
  75. int
  76. fsread(File *file, void *a, long n)
  77. {
  78. if(BADPTR(file->fs))
  79. panic("bad pointer file->fs in fsread");
  80. if(BADPTR(file->fs->read))
  81. panic("bad pointer file->fs->read in fsread");
  82. return file->fs->read(file, a, n);
  83. }