cmd_doctor.v

2.9 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.history1415pub fn cmd_doctor(raw []string) int {16	args := parse('doctor', raw, Options{17		values: ['backup']18		flags:  ['json']19	}) or { return usage_error(err.msg()) }20	store := open_store() or { return refuse(args.json(), err.msg()) }21	defer {22		store.close()23	}24	mut report := DoctorJson{25		problems: []string{}26	}2728	report.problems << store.integrity_check() or { return refuse(args.json(), err.msg()) }29	report.problems << store.check_objects() or { return refuse(args.json(), err.msg()) }30	report.problems << store.check_save_files() or { return refuse(args.json(), err.msg()) }3132	report.objects = int(store.object_count() or { 0 })33	report.saves = int(history.save_count(store) or { 0 })34	report.checkpoints = int(history.checkpoint_count(store) or { 0 })35	report.cached_files = int(store.cache_count() or { 0 })36	report.peek_folders_removed = sweep_peek_folders()37	store.drop_cache() or { return refuse(args.json(), err.msg()) }3839	if path := args.value_or_none('backup') {40		if os.exists(path) {41			return refuse(args.json(), '${path} already exists')42		}43		store.backup(path) or { return refuse(args.json(), err.msg()) }44		report.backup = path45	}4647	if args.json() {48		emit_json(json2.encode(report))49	} else {50		if report.problems.len == 0 {51			ok('${store.root} is sound')52		} else {53			for problem in report.problems {54				warn(problem)55			}56		}57		note('${report.objects} objects · ${report.saves} saves · ${report.checkpoints} checkpoints')58		if report.backup != '' {59			ok('backed up to ${report.backup}')60		}61	}62	if report.problems.len > 0 {63		return 164	}65	return 066}6768// peek_age is how old a peek folder must be before doctor clears it.69const peek_age = 864007071// sweep_peek_folders clears the scratch trees `koh peek` leaves in the system72// temp directory, once they are a day old.73fn sweep_peek_folders() int {74	mut removed := 075	for name in os.ls(os.temp_dir()) or { return 0 } {76		path := os.join_path(os.temp_dir(), name)77		if !name.starts_with(peek_prefix)78			|| !peek_is_old(os.file_last_mod_unix(path), time.now().unix()) {79			continue80		}81		os.rmdir_all(path) or { continue }82		removed++83	}84	return removed85}8687// peek_is_old is true once a peek is more than a day old. A younger one may88// still be open in somebody's editor.89fn peek_is_old(modified i64, now i64) bool {90	return modified < now - peek_age91}9293struct DoctorJson {94mut:95	problems             []string96	objects              int97	saves                int98	checkpoints          int99	cached_files         int100	peek_folders_removed int101	backup               string102}