protected void scanFiles(boolean repair) throws FileNotFoundException {
		synchronized (syncObject) {

// TODO: May need to make repair an option (may not have write privilege)

			ArrayList<String> bufFiles = new ArrayList<>();
			ArrayList<String> verFiles = new ArrayList<>();
			ArrayList<String> changeFiles = new ArrayList<>();
			//		ArrayList delFiles = new ArrayList();

			String[] fileList = dbDir.list();
			if (fileList == null) {
				throw new FileNotFoundException(dbDir + " not found");
			}

			for (int i = 0; i < fileList.length; i++) {
				// Identify files of interest
				String fname = fileList[i];
				if (fname.endsWith(LocalBufferFile.BUFFER_FILE_EXTENSION)) {
					if (fname.startsWith(DATABASE_FILE_PREFIX)) {
						bufFiles.add(fname);
					}
					else if (fname.startsWith(VERSION_FILE_PREFIX)) {
						verFiles.add(fname);
					}
					else if (fname.startsWith(CHANGE_FILE_PREFIX)) {
						changeFiles.add(fname);
					}
					//				else {
					//					// unknown buffer file
					//					delFiles.add(fname);
					//				}
				}
				//			else if (fname.endsWith(LocalBufferFile.PRESAVE_FILE_EXT) ||
				//					fname.endsWith(LocalBufferFile.TEMP_FILE_EXT)) {
				//				// Attempt to remove all presave and temp files 
				//				// Open files on Windows will not be deleted, however they will under Unix
				// TODO This can cause problems under UNIX since it can be deleted while open
				//				delFiles.add(fname);	
				//			}
			}

			// Identify buffer files and current version - keep current version only
			int[] bufVersions = getFileVersions(bufFiles);
			currentVersion = bufVersions.length == 0 ? 0 : bufVersions[bufVersions.length - 1];
			minVersion = currentVersion;
			lastModified = bfMgr.getBufferFile(currentVersion).lastModified();

			// Remove old buffer files
			if (repair) {
				for (int i = 0; i < (bufVersions.length - 1); i++) {
					bfMgr.getBufferFile(bufVersions[i]).delete();
				}
			}
			if (isVersioned) {
				// Check version files
				int[] versions = getFileVersions(verFiles);
				boolean filesOrphaned = false;
				for (int i = versions.length - 1; i >= 0; i--) {
					if (versions[i] >= minVersion) {
						if (repair) {
							File f = bfMgr.getVersionFile(versions[i]);
							log.warn(dbDir + ": removing unexpected version file: " + f);
							f.delete();
						}
					}
					else if (versions[i] == (minVersion - 1)) {
						--minVersion;
					}
					else {
						log.warn(dbDir + ": missing version file " + (minVersion - 1));
						filesOrphaned = true;
						break;
					}
				}

				// Check change files		
				int[] changes = getFileVersions(changeFiles);
				int minChangeVer = currentVersion;
				for (int i = changes.length - 1; i >= 0; i--) {
					if (changes[i] >= minChangeVer) {
						if (repair) {
							File f = bfMgr.getChangeDataFile(changes[i]);
							log.warn(dbDir + ": removing unexpected change file: " + f);
							f.delete();
						}
					}
					else if (changes[i] == (minChangeVer - 1)) {
						--minChangeVer;
					}
					else {
						log.warn(dbDir + ": missing change file " + (minVersion - 1));
						filesOrphaned = true;
						break;
					}
				}

				if (minChangeVer > minVersion) {
					log.warn(dbDir + ": missing change files prior to " + minChangeVer);
					minVersion = minChangeVer;
					filesOrphaned = true;
				}
				if (repair && filesOrphaned) {
					log.warn(dbDir + ": versions prior to " + minVersion +
						" have been orphaned and will be removed");
					for (int i = 0; i < versions.length && versions[i] < minVersion; ++i) {
						bfMgr.getVersionFile(versions[i]).delete();
					}
					for (int i = 0; i < changes.length && changes[i] < minVersion; ++i) {
						bfMgr.getChangeDataFile(changes[i]).delete();
					}
				}
			}

			// Attempt to remove unwanted files
			//		if (repair) {
			//			int cnt = delFiles.size();
			//			for (int i = 0; i < cnt; i++) {
			//				File f = new File(dbDir, (String) delFiles.get(i));
			//				f.delete();
			//			}
			//		}
		}
	}
Scan files and update state.
@param repair if true files are repaired if needed.
Ghidra/Framework/DB/src/main/java/db/Database.java