test_orphans.toml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. [[case]] # orphan test
  2. in = "lfs.c"
  3. code = '''
  4. lfs_format(&lfs, &cfg) => 0;
  5. lfs_mount(&lfs, &cfg) => 0;
  6. lfs_mkdir(&lfs, "parent") => 0;
  7. lfs_mkdir(&lfs, "parent/orphan") => 0;
  8. lfs_mkdir(&lfs, "parent/child") => 0;
  9. lfs_remove(&lfs, "parent/orphan") => 0;
  10. lfs_unmount(&lfs) => 0;
  11. // corrupt the child's most recent commit, this should be the update
  12. // to the linked-list entry, which should orphan the orphan. Note this
  13. // makes a lot of assumptions about the remove operation.
  14. lfs_mount(&lfs, &cfg) => 0;
  15. lfs_dir_open(&lfs, &dir, "parent/child") => 0;
  16. lfs_block_t block = dir.m.pair[0];
  17. lfs_dir_close(&lfs, &dir) => 0;
  18. lfs_unmount(&lfs) => 0;
  19. uint8_t bbuffer[LFS_BLOCK_SIZE];
  20. cfg.read(&cfg, block, 0, bbuffer, LFS_BLOCK_SIZE) => 0;
  21. int off = LFS_BLOCK_SIZE-1;
  22. while (off >= 0 && bbuffer[off] == LFS_ERASE_VALUE) {
  23. off -= 1;
  24. }
  25. memset(&bbuffer[off-3], LFS_BLOCK_SIZE, 3);
  26. cfg.erase(&cfg, block) => 0;
  27. cfg.prog(&cfg, block, 0, bbuffer, LFS_BLOCK_SIZE) => 0;
  28. cfg.sync(&cfg) => 0;
  29. lfs_mount(&lfs, &cfg) => 0;
  30. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  31. lfs_stat(&lfs, "parent/child", &info) => 0;
  32. lfs_fs_size(&lfs) => 8;
  33. lfs_unmount(&lfs) => 0;
  34. lfs_mount(&lfs, &cfg) => 0;
  35. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  36. lfs_stat(&lfs, "parent/child", &info) => 0;
  37. lfs_fs_size(&lfs) => 8;
  38. // this mkdir should both create a dir and deorphan, so size
  39. // should be unchanged
  40. lfs_mkdir(&lfs, "parent/otherchild") => 0;
  41. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  42. lfs_stat(&lfs, "parent/child", &info) => 0;
  43. lfs_stat(&lfs, "parent/otherchild", &info) => 0;
  44. lfs_fs_size(&lfs) => 8;
  45. lfs_unmount(&lfs) => 0;
  46. lfs_mount(&lfs, &cfg) => 0;
  47. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  48. lfs_stat(&lfs, "parent/child", &info) => 0;
  49. lfs_stat(&lfs, "parent/otherchild", &info) => 0;
  50. lfs_fs_size(&lfs) => 8;
  51. lfs_unmount(&lfs) => 0;
  52. '''
  53. [[case]] # reentrant testing for orphans, basically just spam mkdir/remove
  54. reentrant = true
  55. define = [
  56. {FILES=6, DEPTH=1, CYCLES=50},
  57. {FILES=26, DEPTH=1, CYCLES=50},
  58. {FILES=3, DEPTH=3, CYCLES=50},
  59. ]
  60. code = '''
  61. err = lfs_mount(&lfs, &cfg);
  62. if (err) {
  63. lfs_format(&lfs, &cfg) => 0;
  64. lfs_mount(&lfs, &cfg) => 0;
  65. }
  66. srand(1);
  67. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  68. for (int i = 0; i < CYCLES; i++) {
  69. // create random path
  70. char full_path[256];
  71. for (int d = 0; d < DEPTH; d++) {
  72. sprintf(&full_path[2*d], "/%c", alpha[rand() % FILES]);
  73. }
  74. // if it does not exist, we create it, else we destroy
  75. int res = lfs_stat(&lfs, full_path, &info);
  76. if (res == LFS_ERR_NOENT) {
  77. // create each directory in turn, ignore if dir already exists
  78. for (int d = 0; d < DEPTH; d++) {
  79. strcpy(path, full_path);
  80. path[2*d+2] = '\0';
  81. err = lfs_mkdir(&lfs, path);
  82. assert(!err || err == LFS_ERR_EXIST);
  83. }
  84. for (int d = 0; d < DEPTH; d++) {
  85. strcpy(path, full_path);
  86. path[2*d+2] = '\0';
  87. lfs_stat(&lfs, path, &info) => 0;
  88. assert(strcmp(info.name, &path[2*d+1]) == 0);
  89. assert(info.type == LFS_TYPE_DIR);
  90. }
  91. } else {
  92. // is valid dir?
  93. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  94. assert(info.type == LFS_TYPE_DIR);
  95. // try to delete path in reverse order, ignore if dir is not empty
  96. for (int d = DEPTH-1; d >= 0; d--) {
  97. strcpy(path, full_path);
  98. path[2*d+2] = '\0';
  99. err = lfs_remove(&lfs, path);
  100. assert(!err || err == LFS_ERR_NOTEMPTY);
  101. }
  102. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  103. }
  104. }
  105. lfs_unmount(&lfs) => 0;
  106. '''