doctor_checks.v

9.2 KB · raw

1//! Doctor's local history, event and working-tree checks.2module cli34import os5import src.build6import src.exchange7import src.fmt8import src.events9import src.history10import src.snapshot11import src.storage1213fn doctor_history(saves []history.Save, head string) []string {14	mut problems := []string{}15	mut known := map[string]history.Save{}16	for save in saves {17		known[save.id] = save18	}19	if head == '' && saves.len > 0 { problems << 'history has saves but no head' }20	if head != '' && head !in known { problems << 'head ${head} names a missing save' }21	mut done := map[string]bool{}22	for save in saves {23		if save.parent != '' && save.parent !in known {24			problems << 'save ${save.id} has missing parent ${save.parent}'25		}26		if save.restores != '' && save.restores !in known {27			problems << 'save ${save.id} restores missing save ${save.restores}'28		}29		mut cursor := save.id30		mut seen := map[string]bool{}31		for cursor != '' && cursor in known && cursor !in done {32			if cursor in seen {33				problems << 'history has a parent cycle at ${cursor}'34				break35			}36			seen[cursor] = true37			cursor = known[cursor].parent38		}39		for id in seen.keys() {40			done[id] = true41		}42	}43	return problems44}4546// Saves that history names (as parent, restored save or head) but does not hold.47fn missing_saves(saves []history.Save, head string) []string {48	mut known := map[string]bool{}49	for save in saves {50		known[save.id] = true51	}52	mut missing := []string{}53	for save in saves {54		for id in [save.parent, save.restores] {55			if id != '' && id !in known && id !in missing {56				missing << id57			}58		}59	}60	if head != '' && head !in known && head !in missing {61		missing << head62	}63	return missing64}6566fn doctor_files(id string, files []snapshot.FileEntry, event_list []events.Event,67	mut report DoctorJson) {68	mut paths := map[string]bool{}69	for file in files {70		paths[file.path] = true71	}72	for file in files {73		if !snapshot.legal_path(file.path) {74			report.problems << 'save [${id}] has unsafe path ${snapshot.escape_path(file.path)} — koh strip ${shell_quote(file.path)}'75		}76		mut parent := os.dir(file.path)77		for parent != '.' && parent != '' && parent != '/' {78			if parent in paths {79				report.problems << 'save ${id}: ${snapshot.escape_path(parent)} is both a file and a directory'80				break81			}82			parent = os.dir(parent)83		}84		for event in event_list {85			if events.valid_strip(event) && (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				if event.path !in report.strip_residue_paths {90					report.strip_residue_paths << event.path91				}92			}93		}94	}95}9697fn doctor_events(event_list []events.Event, saves []history.Save, mut report DoctorJson) {98	ids := saves.map(it.id)99	mut flags := map[string]events.Event{}100	for event in event_list {101		if event.created_at < 0 { report.problems << 'event ${event.id} has a negative timestamp' }102		if event.kind !in [events.kind_flag, events.kind_clear, events.kind_strip] {103			report.problems << 'event ${event.id} has unknown kind ${event.kind}'104		} else if event.kind != events.kind_strip && event.save_id == '' {105			report.problems << 'event ${event.id} has no save target'106		} else if event.kind == events.kind_strip && event.path == '' {107			report.problems << 'strip event ${event.id} has no path'108		}109		// Sync may retain an event for an off-line save this receiver never fetched.110		if event.save_id != '' && event.save_id !in ids {111			report.warnings << 'event ${event.id} targets unavailable save ${event.save_id}; nothing to do'112		}113		if event.kind in [events.kind_flag, events.kind_clear] {114			old := flags[event.save_id] or { events.Event{} }115			if event.created_at > old.created_at || (event.created_at == old.created_at && event.id > old.id) {116				flags[event.save_id] = event117			}118		}119	}120	report.events = event_list.len121	report.flagged_saves = flags.values().filter(it.kind == events.kind_flag).len122	if head_flag := flags[report.head_id] {123		report.head_flagged = head_flag.kind == events.kind_flag124		if report.head_flagged {125			report.warnings << 'head is flagged: ${head_flag.reason}; koh flag --clear removes the flag'126		}127	}128}129130fn doctor_local(store &storage.Store, mut report DoctorJson) ! {131	stats := store.doctor_stats()!132	report.metadata_available = true133	report.objects = int(stats.objects)134	report.saves = int(stats.saves)135	report.checkpoints = int(stats.checkpoints)136	report.cached_files = int(stats.cached_files)137	report.file_entries = stats.file_entries138	report.object_bytes = stats.object_bytes139	report.packed_bytes = stats.packed_bytes140	report.unreferenced_objects = stats.unused141	report.source = stats.state['source']142	report.remote_repo = stats.state['remote_repo']143	report.last_offered_head = stats.state['remote_head']144	report.head_id = stats.state[storage.head_key]145	report.restore_unfinished = 'restoring' in stats.state146	report.pending_strips = stats.pending147	report.upload_unsettled = exchange.unsettled_key in stats.state148	report.check('references and metadata', stats.problems)149	saves := history.all_saves(store)!150	report.check('history links and cycles', doctor_history(saves, report.head_id))151	report.missing_saves = missing_saves(saves, report.head_id)152	mut known := map[string]history.Save{}153	for save in saves {154		known[save.id] = save155	}156	mut seen := map[string]bool{}157	mut cursor := report.head_id158	for cursor != '' && cursor in known && cursor !in seen {159		seen[cursor] = true160		cursor = known[cursor].parent161	}162	report.line_saves = seen.len163	report.off_line_saves = saves.len - seen.len164	if head := known[report.head_id] {165		report.head = head166		report.head_files = history.files_of(store, head.id)!.len167	}168	event_list := events.all(store)!169	before := report.problems.len170	doctor_events(event_list, saves, mut report)171	for save in saves {172		doctor_files(save.id, history.files_of(store, save.id)!, event_list, mut report)173		if save.replayed_from != '' && save.replayed_from !in known {174			report.warnings << 'save ${save.id} was replayed from unavailable save ${save.replayed_from}; nothing to do'175		}176	}177	for path in stats.pending {178		exchange.read_pending(store, path) or { report.problems << err.msg() }179	}180	exchange.unsettled(store) or { report.problems << err.msg() }181	report.check('saved paths and events', report.problems[before..].clone())182	if report.restore_unfinished {183		report.check('restore state', [snapshot.restore_unfinished])184	} else {185		report.check('restore state', []string{})186	}187	for path in report.pending_strips {188		report.warnings << 'remote strip unfinished for ${snapshot.escape_path(path)}; retry koh strip ${shell_quote(path)}'189	}190	if report.upload_unsettled {191		report.warnings << 'an upload stopped before it could remove what a remote strip covers; after koh steal, koh doctor can finish it'192	}193	if stats.unused > 0 {194		report.warnings << '${fmt.plural(int(stats.unused), 'unreferenced object')} retained; no save uses them and koh doctor can remove them'195	}196}197198fn doctor_working_tree(store &storage.Store, mut report DoctorJson) ! {199	store.drop_cache()!200	report.cache_cleared = true201	if !report.metadata_available || (report.saves > 0 && report.head.id == '') {202		report.checks << DoctorCheck{'working tree', 'skipped', 'head is missing', ''}203		return204	}205	for path in [os.join_path(store.root, snapshot.ignore_file), snapshot.global_ignore_path()] {206		if !os.exists(path) {207			continue208		}209		text := os.read_file(path) or {210			report.check('ignore rules', [err.msg()])211			continue212		}213		report.ignore_files[path] = text.split_into_lines().filter(it.trim_space() != '' && !it.trim_space().starts_with('#')).len214	}215	files := history.files_of(store, report.head_id)!216	ig := snapshot.load(store.root)217	report.ignored_saved_files = files.filter(ig.matches(it.path, false)).map(it.path)218	report.changes = compare_with_head(store, report.head)!219	mut problems := []string{}220	snapshot.check_links(store.root, files) or { problems << err.msg() }221	for path in report.changes.unsafe_paths {222		problems << 'unsafe working name ${path}; this file cannot be saved'223	}224	report.check('working tree safety', problems)225	report.checks << DoctorCheck{'working tree changes', 'pass', '${report.changes.added.len} added, ${report.changes.modified.len} modified, ${report.changes.removed.len} removed', ''}226	if report.ignored_saved_files.len > 0 {227		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'228	}229}230231fn doctor_build(root string, mut report DoctorJson) {232	if !os.exists(os.join_path(root, build.config_name)) {233		report.checks << DoctorCheck{'build configuration', 'skipped', 'no koh.build', ''}234		return235	}236	config := build.read_config(root) or {237		report.check('build configuration', [err.msg()])238		return239	}240	report.build_platforms = config.platforms241	report.project_version = build.project_version(root) or {242		report.check('build configuration', [err.msg()])243		return244	}245	mut paths := map[string]bool{}246	mut problems := []string{}247	for platform in config.platforms {248		path := build.output_path(config, platform, report.project_version)249		if path in paths { problems << 'multiple platforms share build output ${path}' }250		paths[path] = true251	}252	report.check('build configuration', problems)253}