test_orphans.toml 4.1 KB

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