bootcache.c 1.5 KB

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