doctor_checks.v

8.8 KB · raw

1//! Doctor's local history, event and working-tree checks.2module cli34import os5import src.build6import src.fmt7import src.events8import src.history9import src.snapshot10import src.storage1112fn doctor_history(saves []history.Save, head string) []string {13	mut problems := []string{}14	mut known := map[string]history.Save{}15	for save in saves {16		known[save.id] = save17	}18	if head == '' && saves.len > 0 { problems << 'history has saves but no head' }19	if head != '' && head !in known { problems << 'head ${head} names a missing save' }20	mut done := map[string]bool{}21	for save in saves {22		if save.parent != '' && save.parent !in known {23			problems << 'save ${save.id} has missing parent ${save.parent}'24		}25		if save.restores != '' && save.restores !in known {26			problems << 'save ${save.id} restores missing save ${save.restores}'27		}28		mut cursor := save.id29		mut seen := map[string]bool{}30		for cursor != '' && cursor in known && cursor !in done {31			if cursor in seen {32				problems << 'history has a parent cycle at ${cursor}'33				break34			}35			seen[cursor] = true36			cursor = known[cursor].parent37		}38		for id in seen.keys() {39			done[id] = true40		}41	}42	return problems43}4445// Saves that history names (as parent, restored save or head) but does not hold.46fn missing_saves(saves []history.Save, head string) []string {47	mut known := map[string]bool{}48	for save in saves {49		known[save.id] = true50	}51	mut missing := []string{}52	for save in saves {53		for id in [save.parent, save.restores] {54			if id != '' && id !in known && id !in missing {55				missing << id56			}57		}58	}59	if head != '' && head !in known && head !in missing {60		missing << head61	}62	return missing63}6465fn doctor_files(id string, files []snapshot.FileEntry, event_list []events.Event,66	mut report DoctorJson) {67	mut paths := map[string]bool{}68	for file in files {69		paths[file.path] = true70	}71	for file in files {72		if !snapshot.legal_path(file.path) {73			report.problems << 'save [${id}] has unsafe path ${snapshot.escape_path(file.path)} — koh strip ${shell_quote(file.path)}'74		}75		mut parent := os.dir(file.path)76		for parent != '.' && parent != '' && parent != '/' {77			if parent in paths {78				report.problems << 'save ${id}: ${snapshot.escape_path(parent)} is both a file and a directory'79				break80			}81			parent = os.dir(parent)82		}83		for event in event_list {84			if event.kind == events.kind_strip && event.path != ''85				&& (event.save_id == '' || event.save_id == id)86				&& (file.path == event.path || file.path.starts_with('${event.path.trim_right('/')}/')) {87				report.problems << 'save ${id} still contains stripped path ${snapshot.escape_path(file.path)}'88				report.strip_residue = true89			}90		}91	}92}9394fn doctor_events(event_list []events.Event, saves []history.Save, mut report DoctorJson) {95	ids := saves.map(it.id)96	mut flags := map[string]events.Event{}97	for event in event_list {98		if event.created_at < 0 { report.problems << 'event ${event.id} has a negative timestamp' }99		if event.kind !in [events.kind_flag, events.kind_clear, events.kind_strip] {100			report.problems << 'event ${event.id} has unknown kind ${event.kind}'101		} else if event.kind != events.kind_strip && event.save_id == '' {102			report.problems << 'event ${event.id} has no save target'103		} else if event.kind == events.kind_strip && event.path == '' {104			report.problems << 'strip event ${event.id} has no path'105		}106		// Sync may retain an event for an off-line save this receiver never fetched.107		if event.save_id != '' && event.save_id !in ids {108			report.warnings << 'event ${event.id} targets unavailable save ${event.save_id}; nothing to do'109		}110		if event.kind in [events.kind_flag, events.kind_clear] {111			old := flags[event.save_id] or { events.Event{} }112			if event.created_at > old.created_at || (event.created_at == old.created_at && event.id > old.id) {113				flags[event.save_id] = event114			}115		}116	}117	report.events = event_list.len118	report.flagged_saves = flags.values().filter(it.kind == events.kind_flag).len119	if head_flag := flags[report.head_id] {120		report.head_flagged = head_flag.kind == events.kind_flag121		if report.head_flagged {122			report.warnings << 'head is flagged: ${head_flag.reason}; koh flag --clear removes the flag'123		}124	}125}126127fn doctor_local(store &storage.Store, mut report DoctorJson) ! {128	stats := store.doctor_stats()!129	report.metadata_available = true130	report.objects = int(stats.objects)131	report.saves = int(stats.saves)132	report.checkpoints = int(stats.checkpoints)133	report.cached_files = int(stats.cached_files)134	report.file_entries = stats.file_entries135	report.object_bytes = stats.object_bytes136	report.packed_bytes = stats.packed_bytes137	report.unreferenced_objects = stats.unused138	report.source = stats.state['source']139	report.remote_repo = stats.state['remote_repo']140	report.last_offered_head = stats.state['remote_head']141	report.head_id = stats.state[storage.head_key]142	report.restore_unfinished = 'restoring' in stats.state143	report.pending_strips = stats.pending144	report.check('references and metadata', stats.problems)145	saves := history.all_saves(store)!146	report.check('history links and cycles', doctor_history(saves, report.head_id))147	report.missing_saves = missing_saves(saves, report.head_id)148	mut known := map[string]history.Save{}149	for save in saves {150		known[save.id] = save151	}152	mut seen := map[string]bool{}153	mut cursor := report.head_id154	for cursor != '' && cursor in known && cursor !in seen {155		seen[cursor] = true156		cursor = known[cursor].parent157	}158	report.line_saves = seen.len159	report.off_line_saves = saves.len - seen.len160	if head := known[report.head_id] {161		report.head = head162		report.head_files = history.files_of(store, head.id)!.len163	}164	event_list := events.all(store)!165	before := report.problems.len166	doctor_events(event_list, saves, mut report)167	for save in saves {168		doctor_files(save.id, history.files_of(store, save.id)!, event_list, mut report)169		if save.replayed_from != '' && save.replayed_from !in known {170			report.warnings << 'save ${save.id} was replayed from unavailable save ${save.replayed_from}; nothing to do'171		}172	}173	report.check('saved paths and events', report.problems[before..].clone())174	if report.restore_unfinished {175		report.check('restore state', [snapshot.restore_unfinished])176	} else {177		report.check('restore state', []string{})178	}179	for path in report.pending_strips {180		report.warnings << 'remote strip unfinished for ${snapshot.escape_path(path)}; retry koh strip ${shell_quote(path)}'181	}182	if stats.unused > 0 {183		report.warnings << '${fmt.plural(int(stats.unused), 'unreferenced object')} retained; no save uses them and koh doctor can remove them'184	}185}186187fn doctor_working_tree(store &storage.Store, mut report DoctorJson) ! {188	store.drop_cache()!189	report.cache_cleared = true190	if !report.metadata_available || (report.saves > 0 && report.head.id == '') {191		report.checks << DoctorCheck{'working tree', 'skipped', 'head is missing', ''}192		return193	}194	for path in [os.join_path(store.root, snapshot.ignore_file), snapshot.global_ignore_path()] {195		if !os.exists(path) {196			continue197		}198		text := os.read_file(path) or {199			report.check('ignore rules', [err.msg()])200			continue201		}202		report.ignore_files[path] = text.split_into_lines().filter(it.trim_space() != '' && !it.trim_space().starts_with('#')).len203	}204	files := history.files_of(store, report.head_id)!205	ig := snapshot.load(store.root)206	report.ignored_saved_files = files.filter(ig.matches(it.path, false)).map(it.path)207	report.changes = compare_with_head(store, report.head)!208	mut problems := []string{}209	snapshot.check_links(store.root, files) or { problems << err.msg() }210	for path in report.changes.unsafe_paths {211		problems << 'unsafe working name ${path}; this file cannot be saved'212	}213	report.check('working tree safety', problems)214	report.checks << DoctorCheck{'working tree changes', 'pass', '${report.changes.added.len} added, ${report.changes.modified.len} modified, ${report.changes.removed.len} removed', ''}215	if report.ignored_saved_files.len > 0 {216		report.warnings << 'ignore rules now cover ${fmt.plural(report.ignored_saved_files.len, 'saved file')}; older versions remain in history, and koh strip removes them if they must go'217	}218}219220fn doctor_build(root string, mut report DoctorJson) {221	if !os.exists(os.join_path(root, build.config_name)) {222		report.checks << DoctorCheck{'build configuration', 'skipped', 'no koh.build', ''}223		return224	}225	config := build.read_config(root) or {226		report.check('build configuration', [err.msg()])227		return228	}229	report.build_platforms = config.platforms230	report.project_version = build.project_version(root) or {231		report.check('build configuration', [err.msg()])232		return233	}234	mut paths := map[string]bool{}235	mut problems := []string{}236	for platform in config.platforms {237		path := build.output_path(config, platform, report.project_version)238		if path in paths { problems << 'multiple platforms share build output ${path}' }239		paths[path] = true240	}241	report.check('build configuration', problems)242}