//! Stripping one save must not disturb another save that shares manifests. module exchange import os import src.history import src.snapshot import src.storage fn strip_test_repo(name string) !storage.Store { dir := os.join_path(os.temp_dir(), 'koh3_strip_${os.getpid()}_${name}') os.rmdir_all(dir) or {} os.mkdir_all(dir)! mut store := storage.create(dir)! os.write_file(os.join_path(dir, '.kohignore'), '*.log\n')! return store } fn strip_test_put(root string, rel string, text string) ! { full := os.join_path(root, rel) os.mkdir_all(os.dir(full))! os.write_file(full, text)! } fn strip_test_save(store &storage.Store, message string) !history.Save { return history.save_worktree(store, store.root, snapshot.load(store.root), history.SaveInput{ message: message })! } fn strip_test_folder(store &storage.Store, root string, name string) !string { for entry in store.manifest_entries(root)! { if entry.name == name && entry.kind == storage.folder_kind { return entry.manifest } } return error('no folder ${name}') } fn test_stripping_one_save_leaves_a_sharing_save_untouched() { mut store := strip_test_repo('isolation')! defer { store.close() os.rmdir_all(store.root) or {} } strip_test_put(store.root, 'a/keep', 'keep')! strip_test_put(store.root, 'a/secret', 'secret')! strip_test_put(store.root, 'b/x', 'x')! first := strip_test_save(store, 'first')! strip_test_put(store.root, 'a/secret', 'secret two')! second := strip_test_save(store, 'second')! first_saved := history.by_id(store, first.id) or { panic('first save is missing') } second_saved := history.by_id(store, second.id) or { panic('second save is missing') } first_root := first_saved.manifest second_root := second_saved.manifest assert first_root != second_root shared_b := strip_test_folder(store, second_root, 'b')! first_b := strip_test_folder(store, first_root, 'b')! assert first_b == shared_b strip_local(store, 'a/secret', first.id)! first_paths := history.files_of(store, first.id)! assert first_paths.map(it.path) == ['.kohignore', 'a/keep', 'b/x'] second_paths := history.files_of(store, second.id)! assert second_paths.map(it.path) == ['.kohignore', 'a/keep', 'a/secret', 'b/x'] assert store.manifest_body(second_root)!.contains('"name":"b"') assert store.manifest_body(shared_b)!.contains('"name":"x"') gone := store.has_object(storage.hash_hex('secret'.bytes()))! assert !gone } // strip removes rows, not bytes: SQLite keeps the freed pages and the WAL keeps // the frames. After a scrub the object's compressed bytes are out of the file. fn test_stripped_object_bytes_leave_the_file() { mut store := strip_test_repo('scrub')! defer { store.close() os.rmdir_all(store.root) or {} } fresh := storage.open(store.root)! secure_delete := strip_test_pragma(fresh, 'PRAGMA secure_delete') fresh.close() assert secure_delete == 1 mut secret_text := '' for i in 0 .. 12 { mut text := 'strip-canary-${i} ' for j in 0 .. 20 + i { text += 'koh-strip-${i}-${j} ' } strip_test_put(store.root, 'secrets/f${i}', text)! if i == 0 { secret_text = text } } strip_test_save(store, 'secrets')! hash := storage.hash_hex(secret_text.bytes()) blob := store.object_blob(hash)!.bytestr() strip_local(store, 'secrets', '')! assert !store.has_object(hash)! store.scrub()! raw := os.read_bytes(os.join_path(store.root, storage.file_name))!.bytestr() assert !raw.contains(blob) wal := os.join_path(store.root, '${storage.file_name}-wal') if os.exists(wal) { assert !os.read_bytes(wal)!.bytestr().contains(blob) } assert strip_test_pragma(store, 'PRAGMA freelist_count') == 0 } // strip_test_pragma reads the first column of a one-row PRAGMA. fn strip_test_pragma(store &storage.Store, query string) i64 { mut stmt := store.db.prepare(query) or { panic(err.msg()) } defer { stmt.finalize() } if !stmt.step() or { return -1 } { return -1 } return stmt.int(0) }