bootcache.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <libc.h>
  11. #include <../boot/boot.h>
  12. uint8_t statbuf[Statsz];
  13. int
  14. cache(int fd)
  15. {
  16. int argc, i, p[2];
  17. char *argv[5], bd[32], buf[256], partition[64], *pp;
  18. if(stat("/boot/cfs", statbuf, sizeof statbuf) < 0)
  19. return fd;
  20. *partition = 0;
  21. bind("#S", "/dev", MAFTER);
  22. readfile("#e/cfs", buf, sizeof(buf));
  23. if(*buf){
  24. argc = tokenize(buf, argv, 4);
  25. for(i = 0; i < argc; i++){
  26. if(strcmp(argv[i], "off") == 0)
  27. return fd;
  28. else if(stat(argv[i], statbuf, sizeof statbuf) >= 0){
  29. strncpy(partition, argv[i], sizeof(partition)-1);
  30. partition[sizeof(partition)-1] = 0;
  31. }
  32. }
  33. }
  34. if(*partition == 0){
  35. readfile("#e/bootdisk", bd, sizeof(bd));
  36. if(*bd){
  37. if((pp = strchr(bd, ':')) != nil)
  38. *pp = 0;
  39. /* damned artificial intelligence */
  40. i = strlen(bd);
  41. if(strcmp("disk", &bd[i-4]) == 0)
  42. bd[i-4] = 0;
  43. else if(strcmp("fs", &bd[i-2]) == 0)
  44. bd[i-2] = 0;
  45. else if(strcmp("fossil", &bd[i-6]) == 0)
  46. bd[i-6] = 0;
  47. sprint(partition, "%scache", bd);
  48. if(stat(partition, statbuf, sizeof statbuf) < 0)
  49. *bd = 0;
  50. }
  51. if(*bd == 0){
  52. sprint(partition, "%scache", bootdisk);
  53. if(stat(partition, statbuf, sizeof statbuf) < 0)
  54. return fd;
  55. }
  56. }
  57. print("cfs...");
  58. if(pipe(p)<0)
  59. fatal("pipe");
  60. switch(fork()){
  61. case -1:
  62. fatal("fork");
  63. case 0:
  64. close(p[1]);
  65. dup(fd, 0);
  66. close(fd);
  67. dup(p[0], 1);
  68. close(p[0]);
  69. if(fflag)
  70. execl("/boot/cfs", "bootcfs", "-rs", "-f", partition, 0);
  71. else
  72. execl("/boot/cfs", "bootcfs", "-s", "-f", partition, 0);
  73. break;
  74. default:
  75. close(p[0]);
  76. close(fd);
  77. fd = p[1];
  78. break;
  79. }
  80. return fd;
  81. }