GdbmDbmTest.vala 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /**
  2. * GdbmDbmTest - Unit tests for GdbmDbm storage
  3. */
  4. using Implexus.Storage;
  5. public static int main(string[] args) {
  6. int passed = 0;
  7. int failed = 0;
  8. // Test 1: Basic set and get
  9. if (test_basic_set_get()) {
  10. passed++;
  11. stdout.puts("PASS: test_basic_set_get\n");
  12. } else {
  13. failed++;
  14. stdout.puts("FAIL: test_basic_set_get\n");
  15. }
  16. // Test 2: Get non-existent key
  17. if (test_get_nonexistent()) {
  18. passed++;
  19. stdout.puts("PASS: test_get_nonexistent\n");
  20. } else {
  21. failed++;
  22. stdout.puts("FAIL: test_get_nonexistent\n");
  23. }
  24. // Test 3: Delete key
  25. if (test_delete_key()) {
  26. passed++;
  27. stdout.puts("PASS: test_delete_key\n");
  28. } else {
  29. failed++;
  30. stdout.puts("FAIL: test_delete_key\n");
  31. }
  32. // Test 4: Has key
  33. if (test_has_key()) {
  34. passed++;
  35. stdout.puts("PASS: test_has_key\n");
  36. } else {
  37. failed++;
  38. stdout.puts("FAIL: test_has_key\n");
  39. }
  40. // Test 5: Keys iteration
  41. if (test_keys_iteration()) {
  42. passed++;
  43. stdout.puts("PASS: test_keys_iteration\n");
  44. } else {
  45. failed++;
  46. stdout.puts("FAIL: test_keys_iteration\n");
  47. }
  48. // Test 6: Transaction commit
  49. if (test_transaction_commit()) {
  50. passed++;
  51. stdout.puts("PASS: test_transaction_commit\n");
  52. } else {
  53. failed++;
  54. stdout.puts("FAIL: test_transaction_commit\n");
  55. }
  56. // Test 7: Transaction rollback
  57. if (test_transaction_rollback()) {
  58. passed++;
  59. stdout.puts("PASS: test_transaction_rollback\n");
  60. } else {
  61. failed++;
  62. stdout.puts("FAIL: test_transaction_rollback\n");
  63. }
  64. // Test 8: Transaction with delete
  65. if (test_transaction_delete()) {
  66. passed++;
  67. stdout.puts("PASS: test_transaction_delete\n");
  68. } else {
  69. failed++;
  70. stdout.puts("FAIL: test_transaction_delete\n");
  71. }
  72. // Test 9: Overwrite value
  73. if (test_overwrite_value()) {
  74. passed++;
  75. stdout.puts("PASS: test_overwrite_value\n");
  76. } else {
  77. failed++;
  78. stdout.puts("FAIL: test_overwrite_value\n");
  79. }
  80. // Test 10: Special key names
  81. if (test_special_key_names()) {
  82. passed++;
  83. stdout.puts("PASS: test_special_key_names\n");
  84. } else {
  85. failed++;
  86. stdout.puts("FAIL: test_special_key_names\n");
  87. }
  88. // Test 11: Binary data
  89. if (test_binary_data()) {
  90. passed++;
  91. stdout.puts("PASS: test_binary_data\n");
  92. } else {
  93. failed++;
  94. stdout.puts("FAIL: test_binary_data\n");
  95. }
  96. // Test 12: Empty key
  97. if (test_empty_key()) {
  98. passed++;
  99. stdout.puts("PASS: test_empty_key\n");
  100. } else {
  101. failed++;
  102. stdout.puts("FAIL: test_empty_key\n");
  103. }
  104. // Test 13: Large value
  105. if (test_large_value()) {
  106. passed++;
  107. stdout.puts("PASS: test_large_value\n");
  108. } else {
  109. failed++;
  110. stdout.puts("FAIL: test_large_value\n");
  111. }
  112. // Test 14: Multiple operations
  113. if (test_multiple_operations()) {
  114. passed++;
  115. stdout.puts("PASS: test_multiple_operations\n");
  116. } else {
  117. failed++;
  118. stdout.puts("FAIL: test_multiple_operations\n");
  119. }
  120. // Test 15: Persistence
  121. if (test_persistence()) {
  122. passed++;
  123. stdout.puts("PASS: test_persistence\n");
  124. } else {
  125. failed++;
  126. stdout.puts("FAIL: test_persistence\n");
  127. }
  128. // Test 16: Open and close
  129. if (test_open_close()) {
  130. passed++;
  131. stdout.puts("PASS: test_open_close\n");
  132. } else {
  133. failed++;
  134. stdout.puts("FAIL: test_open_close\n");
  135. }
  136. stdout.printf("\nResults: %d passed, %d failed\n", passed, failed);
  137. return failed > 0 ? 1 : 0;
  138. }
  139. // Helper to create temporary database path
  140. string create_temp_db_path() {
  141. string temp_dir = DirUtils.mkdtemp("implexus_gdbm_test_XXXXXX");
  142. return Path.build_filename(temp_dir, "test.db");
  143. }
  144. // Helper to get directory from db path
  145. string get_dir_from_path(string db_path) {
  146. return Path.get_dirname(db_path);
  147. }
  148. // Helper to create BinaryData from string (includes null terminator)
  149. Invercargill.BinaryData string_to_binary(string str) {
  150. // Include null terminator for proper string reconstruction when reading back
  151. uint8[] data = new uint8[str.length + 1];
  152. Memory.copy(data, str, str.length);
  153. data[str.length] = 0; // null terminator
  154. return new Invercargill.DataStructures.ByteBuffer.from_byte_array(data);
  155. }
  156. // Helper to convert BinaryData to string
  157. string binary_to_string(Invercargill.BinaryData data) {
  158. var bytes = data.to_bytes();
  159. return (string) bytes.get_data();
  160. }
  161. // Test 1: Basic set and get
  162. bool test_basic_set_get() {
  163. string db_path = create_temp_db_path();
  164. string temp_dir = get_dir_from_path(db_path);
  165. try {
  166. var dbm = new GdbmDbm();
  167. dbm.open(db_path, false);
  168. dbm.set("test_key", string_to_binary("test_value"));
  169. var value = dbm.get("test_key");
  170. if (value == null) {
  171. dbm.close();
  172. cleanup_dir(temp_dir);
  173. return false;
  174. }
  175. if (binary_to_string((!) value) != "test_value") {
  176. dbm.close();
  177. cleanup_dir(temp_dir);
  178. return false;
  179. }
  180. dbm.close();
  181. cleanup_dir(temp_dir);
  182. return true;
  183. } catch (Error e) {
  184. cleanup_dir(temp_dir);
  185. return false;
  186. }
  187. }
  188. // Test 2: Get non-existent key
  189. bool test_get_nonexistent() {
  190. string db_path = create_temp_db_path();
  191. string temp_dir = get_dir_from_path(db_path);
  192. try {
  193. var dbm = new GdbmDbm();
  194. dbm.open(db_path, false);
  195. var value = dbm.get("nonexistent");
  196. dbm.close();
  197. cleanup_dir(temp_dir);
  198. return value == null;
  199. } catch (Error e) {
  200. cleanup_dir(temp_dir);
  201. return false;
  202. }
  203. }
  204. // Test 3: Delete key
  205. bool test_delete_key() {
  206. string db_path = create_temp_db_path();
  207. string temp_dir = get_dir_from_path(db_path);
  208. try {
  209. var dbm = new GdbmDbm();
  210. dbm.open(db_path, false);
  211. dbm.set("key1", string_to_binary("value1"));
  212. if (!dbm.has_key("key1")) {
  213. dbm.close();
  214. cleanup_dir(temp_dir);
  215. return false;
  216. }
  217. dbm.delete("key1");
  218. if (dbm.has_key("key1")) {
  219. dbm.close();
  220. cleanup_dir(temp_dir);
  221. return false;
  222. }
  223. dbm.close();
  224. cleanup_dir(temp_dir);
  225. return true;
  226. } catch (Error e) {
  227. cleanup_dir(temp_dir);
  228. return false;
  229. }
  230. }
  231. // Test 4: Has key
  232. bool test_has_key() {
  233. string db_path = create_temp_db_path();
  234. string temp_dir = get_dir_from_path(db_path);
  235. try {
  236. var dbm = new GdbmDbm();
  237. dbm.open(db_path, false);
  238. if (dbm.has_key("key1")) {
  239. dbm.close();
  240. cleanup_dir(temp_dir);
  241. return false; // Should not exist
  242. }
  243. dbm.set("key1", string_to_binary("value1"));
  244. if (!dbm.has_key("key1")) {
  245. dbm.close();
  246. cleanup_dir(temp_dir);
  247. return false; // Should exist
  248. }
  249. if (dbm.has_key("key2")) {
  250. dbm.close();
  251. cleanup_dir(temp_dir);
  252. return false; // Should not exist
  253. }
  254. dbm.close();
  255. cleanup_dir(temp_dir);
  256. return true;
  257. } catch (Error e) {
  258. cleanup_dir(temp_dir);
  259. return false;
  260. }
  261. }
  262. // Test 5: Keys iteration
  263. bool test_keys_iteration() {
  264. string db_path = create_temp_db_path();
  265. string temp_dir = get_dir_from_path(db_path);
  266. try {
  267. var dbm = new GdbmDbm();
  268. dbm.open(db_path, false);
  269. dbm.set("key1", string_to_binary("value1"));
  270. dbm.set("key2", string_to_binary("value2"));
  271. dbm.set("key3", string_to_binary("value3"));
  272. var keys = dbm.keys;
  273. if (keys.count() != 3) {
  274. dbm.close();
  275. cleanup_dir(temp_dir);
  276. return false;
  277. }
  278. // Check all keys are present
  279. var key_set = new Invercargill.DataStructures.HashSet<string>();
  280. foreach (var key in keys) {
  281. key_set.add(key);
  282. }
  283. if (!key_set.has("key1")) {
  284. dbm.close();
  285. cleanup_dir(temp_dir);
  286. return false;
  287. }
  288. if (!key_set.has("key2")) {
  289. dbm.close();
  290. cleanup_dir(temp_dir);
  291. return false;
  292. }
  293. if (!key_set.has("key3")) {
  294. dbm.close();
  295. cleanup_dir(temp_dir);
  296. return false;
  297. }
  298. dbm.close();
  299. cleanup_dir(temp_dir);
  300. return true;
  301. } catch (Error e) {
  302. cleanup_dir(temp_dir);
  303. return false;
  304. }
  305. }
  306. // Test 6: Transaction commit
  307. bool test_transaction_commit() {
  308. string db_path = create_temp_db_path();
  309. string temp_dir = get_dir_from_path(db_path);
  310. try {
  311. var dbm = new GdbmDbm();
  312. dbm.open(db_path, false);
  313. dbm.set("key1", string_to_binary("value1"));
  314. dbm.begin_transaction();
  315. dbm.set("key2", string_to_binary("value2"));
  316. dbm.set("key3", string_to_binary("value3"));
  317. // Before commit, values should be in transaction buffer
  318. var value2 = dbm.get("key2");
  319. if (value2 == null) {
  320. dbm.close();
  321. cleanup_dir(temp_dir);
  322. return false;
  323. }
  324. dbm.commit_transaction();
  325. // After commit, values should be persisted
  326. if (!dbm.has_key("key1")) {
  327. dbm.close();
  328. cleanup_dir(temp_dir);
  329. return false;
  330. }
  331. if (!dbm.has_key("key2")) {
  332. dbm.close();
  333. cleanup_dir(temp_dir);
  334. return false;
  335. }
  336. if (!dbm.has_key("key3")) {
  337. dbm.close();
  338. cleanup_dir(temp_dir);
  339. return false;
  340. }
  341. dbm.close();
  342. cleanup_dir(temp_dir);
  343. return true;
  344. } catch (Error e) {
  345. cleanup_dir(temp_dir);
  346. return false;
  347. }
  348. }
  349. // Test 7: Transaction rollback
  350. bool test_transaction_rollback() {
  351. string db_path = create_temp_db_path();
  352. string temp_dir = get_dir_from_path(db_path);
  353. try {
  354. var dbm = new GdbmDbm();
  355. dbm.open(db_path, false);
  356. dbm.set("key1", string_to_binary("value1"));
  357. dbm.begin_transaction();
  358. dbm.set("key2", string_to_binary("value2"));
  359. // Value should be visible in transaction
  360. if (!dbm.has_key("key2")) {
  361. dbm.close();
  362. cleanup_dir(temp_dir);
  363. return false;
  364. }
  365. dbm.rollback_transaction();
  366. // After rollback, key2 should not exist
  367. if (dbm.has_key("key2")) {
  368. dbm.close();
  369. cleanup_dir(temp_dir);
  370. return false;
  371. }
  372. // key1 should still exist
  373. if (!dbm.has_key("key1")) {
  374. dbm.close();
  375. cleanup_dir(temp_dir);
  376. return false;
  377. }
  378. dbm.close();
  379. cleanup_dir(temp_dir);
  380. return true;
  381. } catch (Error e) {
  382. cleanup_dir(temp_dir);
  383. return false;
  384. }
  385. }
  386. // Test 8: Transaction with delete
  387. bool test_transaction_delete() {
  388. string db_path = create_temp_db_path();
  389. string temp_dir = get_dir_from_path(db_path);
  390. try {
  391. var dbm = new GdbmDbm();
  392. dbm.open(db_path, false);
  393. dbm.set("key1", string_to_binary("value1"));
  394. dbm.set("key2", string_to_binary("value2"));
  395. dbm.begin_transaction();
  396. dbm.delete("key1");
  397. // In transaction, key1 should not be visible
  398. if (dbm.has_key("key1")) {
  399. dbm.close();
  400. cleanup_dir(temp_dir);
  401. return false;
  402. }
  403. dbm.commit_transaction();
  404. // After commit, key1 should be deleted
  405. if (dbm.has_key("key1")) {
  406. dbm.close();
  407. cleanup_dir(temp_dir);
  408. return false;
  409. }
  410. // key2 should still exist
  411. if (!dbm.has_key("key2")) {
  412. dbm.close();
  413. cleanup_dir(temp_dir);
  414. return false;
  415. }
  416. dbm.close();
  417. cleanup_dir(temp_dir);
  418. return true;
  419. } catch (Error e) {
  420. cleanup_dir(temp_dir);
  421. return false;
  422. }
  423. }
  424. // Test 9: Overwrite value
  425. bool test_overwrite_value() {
  426. string db_path = create_temp_db_path();
  427. string temp_dir = get_dir_from_path(db_path);
  428. try {
  429. var dbm = new GdbmDbm();
  430. dbm.open(db_path, false);
  431. dbm.set("key1", string_to_binary("value1"));
  432. var value = dbm.get("key1");
  433. if (value == null || binary_to_string((!) value) != "value1") {
  434. dbm.close();
  435. cleanup_dir(temp_dir);
  436. return false;
  437. }
  438. dbm.set("key1", string_to_binary("value2"));
  439. value = dbm.get("key1");
  440. if (value == null || binary_to_string((!) value) != "value2") {
  441. dbm.close();
  442. cleanup_dir(temp_dir);
  443. return false;
  444. }
  445. dbm.close();
  446. cleanup_dir(temp_dir);
  447. return true;
  448. } catch (Error e) {
  449. cleanup_dir(temp_dir);
  450. return false;
  451. }
  452. }
  453. // Test 10: Special key names
  454. bool test_special_key_names() {
  455. string db_path = create_temp_db_path();
  456. string temp_dir = get_dir_from_path(db_path);
  457. try {
  458. var dbm = new GdbmDbm();
  459. dbm.open(db_path, false);
  460. string[] special_keys = {
  461. "key/with/slashes",
  462. "key:with:colons",
  463. "key.with.dots",
  464. "key with spaces",
  465. "key\nwith\nnewlines",
  466. "日本語キー"
  467. };
  468. foreach (var key in special_keys) {
  469. dbm.set(key, string_to_binary(@"value for $key"));
  470. var value = dbm.get(key);
  471. if (value == null) {
  472. dbm.close();
  473. cleanup_dir(temp_dir);
  474. return false;
  475. }
  476. if (binary_to_string((!) value) != @"value for $key") {
  477. dbm.close();
  478. cleanup_dir(temp_dir);
  479. return false;
  480. }
  481. }
  482. dbm.close();
  483. cleanup_dir(temp_dir);
  484. return true;
  485. } catch (Error e) {
  486. cleanup_dir(temp_dir);
  487. return false;
  488. }
  489. }
  490. // Test 11: Binary data
  491. bool test_binary_data() {
  492. string db_path = create_temp_db_path();
  493. string temp_dir = get_dir_from_path(db_path);
  494. try {
  495. var dbm = new GdbmDbm();
  496. dbm.open(db_path, false);
  497. // Create binary data with all byte values
  498. var data = new uint8[256];
  499. for (int i = 0; i < 256; i++) {
  500. data[i] = (uint8) i;
  501. }
  502. var binary = new Invercargill.DataStructures.ByteBuffer.from_byte_array(data);
  503. dbm.set("binary_key", binary);
  504. var value = dbm.get("binary_key");
  505. if (value == null) {
  506. dbm.close();
  507. cleanup_dir(temp_dir);
  508. return false;
  509. }
  510. var read_data = ((!) value).to_bytes();
  511. if (read_data.length != 256) {
  512. dbm.close();
  513. cleanup_dir(temp_dir);
  514. return false;
  515. }
  516. for (int i = 0; i < 256; i++) {
  517. if (read_data.get_data()[i] != (uint8) i) {
  518. dbm.close();
  519. cleanup_dir(temp_dir);
  520. return false;
  521. }
  522. }
  523. dbm.close();
  524. cleanup_dir(temp_dir);
  525. return true;
  526. } catch (Error e) {
  527. cleanup_dir(temp_dir);
  528. return false;
  529. }
  530. }
  531. // Test 12: Empty key
  532. bool test_empty_key() {
  533. string db_path = create_temp_db_path();
  534. string temp_dir = get_dir_from_path(db_path);
  535. try {
  536. var dbm = new GdbmDbm();
  537. dbm.open(db_path, false);
  538. dbm.set("", string_to_binary("empty_key_value"));
  539. var value = dbm.get("");
  540. if (value == null) {
  541. dbm.close();
  542. cleanup_dir(temp_dir);
  543. return false;
  544. }
  545. if (binary_to_string((!) value) != "empty_key_value") {
  546. dbm.close();
  547. cleanup_dir(temp_dir);
  548. return false;
  549. }
  550. dbm.close();
  551. cleanup_dir(temp_dir);
  552. return true;
  553. } catch (Error e) {
  554. cleanup_dir(temp_dir);
  555. return false;
  556. }
  557. }
  558. // Test 13: Large value
  559. bool test_large_value() {
  560. string db_path = create_temp_db_path();
  561. string temp_dir = get_dir_from_path(db_path);
  562. try {
  563. var dbm = new GdbmDbm();
  564. dbm.open(db_path, false);
  565. // Create a 1MB value
  566. var data = new uint8[1024 * 1024];
  567. for (int i = 0; i < data.length; i++) {
  568. data[i] = (uint8) (i % 256);
  569. }
  570. var binary = new Invercargill.DataStructures.ByteBuffer.from_byte_array(data);
  571. dbm.set("large_key", binary);
  572. var value = dbm.get("large_key");
  573. if (value == null) {
  574. dbm.close();
  575. cleanup_dir(temp_dir);
  576. return false;
  577. }
  578. var read_data = ((!) value).to_bytes();
  579. if (read_data.length != data.length) {
  580. dbm.close();
  581. cleanup_dir(temp_dir);
  582. return false;
  583. }
  584. for (int i = 0; i < data.length; i++) {
  585. if (read_data.get_data()[i] != data[i]) {
  586. dbm.close();
  587. cleanup_dir(temp_dir);
  588. return false;
  589. }
  590. }
  591. dbm.close();
  592. cleanup_dir(temp_dir);
  593. return true;
  594. } catch (Error e) {
  595. cleanup_dir(temp_dir);
  596. return false;
  597. }
  598. }
  599. // Test 14: Multiple operations
  600. bool test_multiple_operations() {
  601. string db_path = create_temp_db_path();
  602. string temp_dir = get_dir_from_path(db_path);
  603. try {
  604. var dbm = new GdbmDbm();
  605. dbm.open(db_path, false);
  606. // Set multiple keys
  607. for (int i = 0; i < 100; i++) {
  608. dbm.set(@"key$i", string_to_binary(@"value$i"));
  609. }
  610. // Verify all keys
  611. for (int i = 0; i < 100; i++) {
  612. var value = dbm.get(@"key$i");
  613. if (value == null) {
  614. dbm.close();
  615. cleanup_dir(temp_dir);
  616. return false;
  617. }
  618. if (binary_to_string((!) value) != @"value$i") {
  619. dbm.close();
  620. cleanup_dir(temp_dir);
  621. return false;
  622. }
  623. }
  624. // Delete half the keys
  625. for (int i = 0; i < 50; i++) {
  626. dbm.delete(@"key$i");
  627. }
  628. // Verify deleted keys are gone
  629. for (int i = 0; i < 50; i++) {
  630. if (dbm.has_key(@"key$i")) {
  631. dbm.close();
  632. cleanup_dir(temp_dir);
  633. return false;
  634. }
  635. }
  636. // Verify remaining keys still exist
  637. for (int i = 50; i < 100; i++) {
  638. if (!dbm.has_key(@"key$i")) {
  639. dbm.close();
  640. cleanup_dir(temp_dir);
  641. return false;
  642. }
  643. }
  644. dbm.close();
  645. cleanup_dir(temp_dir);
  646. return true;
  647. } catch (Error e) {
  648. cleanup_dir(temp_dir);
  649. return false;
  650. }
  651. }
  652. // Test 15: Persistence
  653. bool test_persistence() {
  654. string db_path = create_temp_db_path();
  655. string temp_dir = get_dir_from_path(db_path);
  656. try {
  657. // Create and write
  658. var dbm1 = new GdbmDbm();
  659. dbm1.open(db_path, false);
  660. dbm1.set("persistent_key", string_to_binary("persistent_value"));
  661. dbm1.close();
  662. // Create new instance with same path
  663. var dbm2 = new GdbmDbm();
  664. dbm2.open(db_path, false);
  665. // Data should persist
  666. var value = dbm2.get("persistent_key");
  667. if (value == null) {
  668. dbm2.close();
  669. cleanup_dir(temp_dir);
  670. return false;
  671. }
  672. if (binary_to_string((!) value) != "persistent_value") {
  673. dbm2.close();
  674. cleanup_dir(temp_dir);
  675. return false;
  676. }
  677. dbm2.close();
  678. cleanup_dir(temp_dir);
  679. return true;
  680. } catch (Error e) {
  681. cleanup_dir(temp_dir);
  682. return false;
  683. }
  684. }
  685. // Test 16: Open and close
  686. bool test_open_close() {
  687. string db_path = create_temp_db_path();
  688. string temp_dir = get_dir_from_path(db_path);
  689. try {
  690. var dbm = new GdbmDbm();
  691. if (dbm.is_open) {
  692. cleanup_dir(temp_dir);
  693. return false;
  694. }
  695. dbm.open(db_path, false);
  696. if (!dbm.is_open) {
  697. cleanup_dir(temp_dir);
  698. return false;
  699. }
  700. dbm.close();
  701. if (dbm.is_open) {
  702. cleanup_dir(temp_dir);
  703. return false;
  704. }
  705. cleanup_dir(temp_dir);
  706. return true;
  707. } catch (Error e) {
  708. cleanup_dir(temp_dir);
  709. return false;
  710. }
  711. }
  712. // Cleanup helper
  713. void cleanup_dir(string path) {
  714. // Remove all files in directory and the directory itself
  715. try {
  716. Dir dir = Dir.open(path, 0);
  717. string? name;
  718. while ((name = dir.read_name()) != null) {
  719. string file_path = Path.build_filename(path, name);
  720. if (FileUtils.test(file_path, FileTest.IS_DIR)) {
  721. cleanup_dir(file_path);
  722. } else {
  723. FileUtils.unlink(file_path);
  724. }
  725. }
  726. } catch (FileError e) {
  727. // Ignore errors
  728. }
  729. DirUtils.remove(path);
  730. }