mntn.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <u.h>
  2. #include <libc.h>
  3. const char *contents = "This is a string of bytes in a file";
  4. const char *changedcontents = "This is a xxxxxx of bytes in a file";
  5. const char *changedcontents2 = "This is a yyyyyy of bytes in a file";
  6. void
  7. failonbad(int rc, const char* reason)
  8. {
  9. if(rc < 0){
  10. fprint(2,"FAIL %r - %s\n", reason);
  11. exits("FAIL");
  12. }
  13. }
  14. void
  15. writetest()
  16. {
  17. int rc;
  18. char buffer[1000];
  19. int fd = create("/n/ram/stuff", ORDWR, 0666);
  20. failonbad(fd, "File open in ramdisk");
  21. rc = write(fd, contents, strlen(contents));
  22. if(rc != strlen(contents)){
  23. failonbad(-1, "Incorrect length written");
  24. }
  25. // Check contents
  26. rc = seek(fd, 0, 0);
  27. failonbad(rc, "seek failed");
  28. rc = read(fd, buffer, strlen(contents));
  29. if(rc != strlen(contents)){
  30. failonbad(-1, "Incorrect length read");
  31. }
  32. if(strncmp(buffer, contents, strlen(contents))){
  33. failonbad(-1, "compare failed");
  34. }
  35. // update the string and verify it
  36. rc = seek(fd, 10, 0);
  37. failonbad(rc, "seek failed");
  38. rc = write(fd, "xxxxxx", 6);
  39. if (rc != 6){
  40. failonbad(-1, "Incorrect update length written");
  41. }
  42. rc = seek(fd, 0, 0);
  43. failonbad(rc, "seek failed");
  44. rc = read(fd, buffer, strlen(contents));
  45. if(rc != strlen(contents)){
  46. failonbad(-1, "Incorrect update length read");
  47. }
  48. if(strncmp(buffer, changedcontents, strlen(contents))){
  49. failonbad(-1, "changed compare failed");
  50. }
  51. rc = seek(fd, 10, 0);
  52. failonbad(rc, "seek failed");
  53. write(fd, "yyy", 3);
  54. write(fd, "yyy", 3);
  55. rc = seek(fd, 0, 0);
  56. failonbad(rc, "seek failed");
  57. rc = read(fd, buffer, strlen(contents));
  58. if(rc != strlen(contents)){
  59. failonbad(-1, "Incorrect update length read");
  60. }
  61. if(strncmp(buffer, changedcontents2, strlen(contents))){
  62. failonbad(-1, "changed compare failed");
  63. }
  64. rc = close(fd);
  65. failonbad(rc, "File close in ramdisk");
  66. }
  67. void
  68. main(int argc, char *argv[])
  69. {
  70. int rc, fd;
  71. Dir* opstat;
  72. switch(fork()){
  73. case -1:
  74. fprint(2, "fork fail\n");
  75. exits("FAIL");
  76. case 0:
  77. execl("/bin/ramfs", "ramfs", "-S", "ramfs", nil);
  78. fprint(2, "execl fail\n");
  79. exits("execl");
  80. default:
  81. do{
  82. sleep(10);
  83. opstat = dirstat("/srv/ramfs");
  84. }while (opstat == nil);
  85. free(opstat);
  86. fd = open("/srv/ramfs", ORDWR);
  87. rc = mount(fd, -1, "/n/ram", MREPL|MCREATE, "", (int)'N');
  88. if(rc < 0){
  89. failonbad(-1, "mount failed");
  90. exits("FAIL");
  91. }
  92. writetest();
  93. break;
  94. }
  95. print("PASS\n");
  96. exits("PASS");
  97. }