cmd_doctor.v

3.6 KB · raw

1//! `koh doctor` — integrity check and cleanup.2//!3//! One source of truth means there is nothing to rebuild; what is left to check4//! is that the file is sound and that every object a save names is really5//! there. Doctor also tidies the things that are safe to lose: the file cache6//! and old peek folders.78module cli910import json211import os12import time13import src.fmt14import src.history15import src.snapshot16import src.storage1718pub fn cmd_doctor(raw []string) int {19	args := parse('doctor', raw, Options{20		values: ['backup']21		flags:  ['json']22	}) or { return usage_error(err.msg()) }23	store := open_store() or { return refuse(args.json(), err.msg()) }24	defer {25		store.close()26	}27	mut report := DoctorJson{28		problems: []string{}29	}3031	working('checking ${fmt.plural(int(store.object_count() or { 0 }), 'object')} and every save')32	report.problems << store.integrity_check() or { return refuse(args.json(), err.msg()) }33	report.problems << store.check_objects() or { return refuse(args.json(), err.msg()) }34	report.problems << store.check_save_files() or { return refuse(args.json(), err.msg()) }35	report.problems << unsafe_save_paths(store) or { return refuse(args.json(), err.msg()) }3637	report.objects = int(store.object_count() or { 0 })38	report.saves = int(history.save_count(store) or { 0 })39	report.checkpoints = int(history.checkpoint_count(store) or { 0 })40	report.cached_files = int(store.cache_count() or { 0 })41	report.peek_folders_removed = sweep_peek_folders()42	store.drop_cache() or { return refuse(args.json(), err.msg()) }4344	if path := args.value_or_none('backup') {45		if os.exists(path) {46			return refuse(args.json(), '${path} already exists')47		}48		store.backup(path) or { return refuse(args.json(), err.msg()) }49		report.backup = path50	}5152	if args.json() {53		emit_json(json2.encode(report))54	} else {55		if report.problems.len == 0 {56			ok('${store.root} is sound')57		} else {58			for problem in report.problems {59				warn(problem)60			}61		}62		note('${fmt.plural(report.objects, 'object')} · ${fmt.plural(report.saves, 'save')} · ${fmt.plural(report.checkpoints, 'checkpoint')}')63		if report.backup != '' {64			ok('backed up to ${report.backup}')65		}66	}67	if report.problems.len > 0 {68		return 169	}70	return 071}7273fn unsafe_save_paths(store &storage.Store) ![]string {74	mut problems := []string{}75	for save in history.line(store)! {76		for file in history.files_of(store, save.id)! {77			if !snapshot.legal_path(file.path) {78				problems << 'save [${save.id}] has unsafe path ${snapshot.escape_path(file.path)} — koh strip ${shell_quote(file.path)}'79			}80		}81	}82	return problems83}8485fn shell_quote(text string) string {86	return "'${text.replace("'", "'\\''")}'"87}8889// peek_age is how old a peek folder must be before doctor clears it.90const peek_age = 864009192// sweep_peek_folders clears the scratch trees `koh peek` leaves in the system93// temp directory, once they are a day old.94fn sweep_peek_folders() int {95	mut removed := 096	for name in os.ls(os.temp_dir()) or { return 0 } {97		path := os.join_path(os.temp_dir(), name)98		if !name.starts_with(peek_prefix)99			|| !peek_is_old(os.file_last_mod_unix(path), time.now().unix()) {100			continue101		}102		os.rmdir_all(path) or { continue }103		removed++104	}105	return removed106}107108// peek_is_old is true once a peek is more than a day old. A younger one may109// still be open in somebody's editor.110fn peek_is_old(modified i64, now i64) bool {111	return modified < now - peek_age112}113114struct DoctorJson {115mut:116	problems             []string117	objects              int118	saves                int119	checkpoints          int120	cached_files         int121	peek_folders_removed int122	backup               string123}