fs.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "io.h"
  15. #include "fs.h"
  16. /*
  17. * grab next element from a path, return the pointer to unprocessed portion of
  18. * path.
  19. */
  20. char *
  21. nextelem(char *path, char *elem)
  22. {
  23. int i;
  24. while(*path == '/')
  25. path++;
  26. if(*path==0 || *path==' ')
  27. return 0;
  28. for(i=0; *path!='\0' && *path!='/' && *path!=' '; i++){
  29. if(i==NAMELEN){
  30. print("name component too long\n");
  31. return 0;
  32. }
  33. *elem++ = *path++;
  34. }
  35. *elem = '\0';
  36. return path;
  37. }
  38. int
  39. fswalk(Fs *fs, char *path, File *f)
  40. {
  41. char element[NAMELEN];
  42. *f = fs->root;
  43. if(BADPTR(fs->walk))
  44. panic("fswalk bad pointer fs->walk");
  45. f->path = path;
  46. while(path = nextelem(path, element)){
  47. switch(fs->walk(f, element)){
  48. case -1:
  49. return -1;
  50. case 0:
  51. return 0;
  52. }
  53. }
  54. return 1;
  55. }
  56. /*
  57. * boot
  58. */
  59. int
  60. fsboot(Fs *fs, char *path, Boot *b)
  61. {
  62. File file;
  63. int32_t n;
  64. static char buf[8192];
  65. switch(fswalk(fs, path, &file)){
  66. case -1:
  67. print("error walking to %s\n", path);
  68. return -1;
  69. case 0:
  70. print("%s not found\n", path);
  71. return -1;
  72. case 1:
  73. print("found %s\n", path);
  74. break;
  75. }
  76. while((n = fsread(&file, buf, sizeof buf)) > 0) {
  77. if(bootpass(b, buf, n) != MORE)
  78. break;
  79. }
  80. bootpass(b, nil, 0); /* tries boot */
  81. return -1;
  82. }
  83. int
  84. fsread(File *file, void *a, int32_t n)
  85. {
  86. if(BADPTR(file->fs))
  87. panic("bad pointer file->fs in fsread");
  88. if(BADPTR(file->fs->read))
  89. panic("bad pointer file->fs->read in fsread");
  90. return file->fs->read(file, a, n);
  91. }