bootcache.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. sprint(partition, "%scache", bd);
  37. if(stat(partition, statbuf, sizeof statbuf) < 0)
  38. *bd = 0;
  39. }
  40. if(*bd == 0){
  41. sprint(partition, "%scache", bootdisk);
  42. if(stat(partition, statbuf, sizeof statbuf) < 0)
  43. return fd;
  44. }
  45. }
  46. print("cfs...");
  47. if(pipe(p)<0)
  48. fatal("pipe");
  49. switch(fork()){
  50. case -1:
  51. fatal("fork");
  52. case 0:
  53. close(p[1]);
  54. dup(fd, 0);
  55. close(fd);
  56. dup(p[0], 1);
  57. close(p[0]);
  58. if(fflag)
  59. execl("/boot/cfs", "bootcfs", "-rs", "-f", partition, 0);
  60. else
  61. execl("/boot/cfs", "bootcfs", "-s", "-f", partition, 0);
  62. break;
  63. default:
  64. close(p[0]);
  65. close(fd);
  66. fd = p[1];
  67. break;
  68. }
  69. return fd;
  70. }