bootcache.c 1.6 KB

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