<commit_before>package org.callimachusproject.management;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BackupTool implements BackupToolMXBean {
	private static final ThreadFactory THREADFACTORY = new ThreadFactory() {
		public Thread newThread(Runnable r) {
			String name = BackupTool.class.getSimpleName() + "-"
					+ Integer.toHexString(r.hashCode());
			Thread t = new Thread(r, name);
			t.setDaemon(true);
			return t;
		}
	};

	private final Logger logger = LoggerFactory.getLogger(BackupTool.class);
	private final ExecutorService executor = Executors
			.newSingleThreadScheduledExecutor(THREADFACTORY);
	private Exception exception;
	private final File baseDir;
	private final File backupDir;
	private volatile boolean backup;
	private volatile boolean restore;

	public BackupTool(File baseDir, File backupDir) {
		this.baseDir = baseDir;
		this.backupDir = backupDir;
	}

	public String toString() {
		return backupDir.toString();
	}

	public synchronized void checkForErrors() throws IOException {
		try {
			if (exception != null)
				throw exception;
		} catch (RuntimeException e) {
			throw (RuntimeException) e;
		} catch (IOException e) {
			throw (IOException) e;
		} catch (Exception e) {
			throw new UndeclaredThrowableException(e);
		} finally {
			exception = null;
		}
	}

	public boolean isBackupInProgress() {
		return backup;
	}

	public boolean isRestoreInProgress() {
		return restore;
	}

	public void createBackup(final String label) throws Exception {
		submit(new Callable<Void>() {
			public Void call() throws Exception {
				blockCreateBackup(label);
				return null;
			}
		});
	}

	public String getBackupLabels() {
		File[] list = backupDir.listFiles();
		if (list == null)
			return "";
		Arrays.sort(list, new Comparator<File>() {
			public int compare(File o1, File o2) {
				if (o1.lastModified() < o2.lastModified())
					return 1;
				if (o1.lastModified() > o2.lastModified())
					return -1;
				return 0;
			}
		});
		StringBuilder result = new StringBuilder();
		for (File file : list) {
			String name = file.getName();
			if (name.endsWith(".zip") || name.endsWith(".ZIP")) {
				String label = name.substring(0, name.length() - 4);
				result.append(label).append(' ');
			}
		}
		if (result.length() == 0)
			return "";
		return result.substring(0, result.length() - 1);
	}

	public void restoreBackup(final String label) throws IOException {
		submit(new Callable<Void>() {
			public Void call() throws Exception {
				blockRestoreBackup(label);
				return null;
			}
		});
	}

	synchronized void blockCreateBackup(String label) throws IOException {
		backup = true;
		try {<commit_msg>Make back process state more accurate<commit_after>package org.callimachusproject.management;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BackupTool implements BackupToolMXBean {
	private static final ThreadFactory THREADFACTORY = new ThreadFactory() {
		public Thread newThread(Runnable r) {
			String name = BackupTool.class.getSimpleName() + "-"
					+ Integer.toHexString(r.hashCode());
			Thread t = new Thread(r, name);
			t.setDaemon(true);
			return t;
		}
	};

	private final Logger logger = LoggerFactory.getLogger(BackupTool.class);
	private final ExecutorService executor = Executors
			.newSingleThreadScheduledExecutor(THREADFACTORY);
	private Exception exception;
	private final File baseDir;
	private final File backupDir;
	private volatile boolean backup;
	private volatile boolean restore;

	public BackupTool(File baseDir, File backupDir) {
		this.baseDir = baseDir;
		this.backupDir = backupDir;
	}

	public String toString() {
		return backupDir.toString();
	}

	public synchronized void checkForErrors() throws IOException {
		try {
			if (exception != null)
				throw exception;
		} catch (RuntimeException e) {
			throw (RuntimeException) e;
		} catch (IOException e) {
			throw (IOException) e;
		} catch (Exception e) {
			throw new UndeclaredThrowableException(e);
		} finally {
			exception = null;
		}
	}

	public boolean isBackupInProgress() {
		return backup;
	}

	public boolean isRestoreInProgress() {
		return restore;
	}

	public String getBackupLabels() {
		File[] list = backupDir.listFiles();
		if (list == null)
			return "";
		Arrays.sort(list, new Comparator<File>() {
			public int compare(File o1, File o2) {
				if (o1.lastModified() < o2.lastModified())
					return 1;
				if (o1.lastModified() > o2.lastModified())
					return -1;
				return 0;
			}
		});
		StringBuilder result = new StringBuilder();
		for (File file : list) {
			String name = file.getName();
			if (name.endsWith(".zip") || name.endsWith(".ZIP")) {
				String label = name.substring(0, name.length() - 4);
				result.append(label).append(' ');
			}
		}
		if (result.length() == 0)
			return "";
		return result.substring(0, result.length() - 1);
	}

	public synchronized void createBackup(final String label) throws Exception {
		if (isBackupInProgress())
			throw new IllegalStateException("Backup already in progress");
		backup = true;
		submit(new Callable<Void>() {
			public Void call() throws Exception {
				blockCreateBackup(label);
				return null;
			}
		});
	}

	public synchronized void restoreBackup(final String label) throws IOException {
		if (isRestoreInProgress())
			throw new IllegalStateException("Restore already in progress");
		restore = true;
		submit(new Callable<Void>() {
			public Void call() throws Exception {
				blockRestoreBackup(label);
				return null;
			}
		});
	}

	synchronized void blockCreateBackup(String label) throws IOException {
		backup = true;
		try {
{'fraction_non_alphanumeric': 0.10987099579649225, 'fraction_numerical': 0.0046383533845484855, 'mean_word_length': 4.275229357798165, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6609702', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2075, 'n_words': 545}