<commit_before>package jfdi.storage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.InvalidPathException;

import java.util.ArrayList;
import java.util.Scanner;

import jfdi.storage.exceptions.ExistingFilesFoundException;
import jfdi.storage.exceptions.FilePathPair;

/**
 * This class manages file operations required by Storage
 *
 * @author <NAME>
 */
public class FileManager {

    /**
     * This method creates the necessary directories for files to be stored in
     * storageFolderPath. If there are issues creating the directory or if there
     * is insufficient permission to carry out normal file operations in the
     * directory, then an InvalidPathException is thrown.
     *
     * @param storageFolderPath
     *            the root directory of the data files
     * @throws InvalidPathException
     *             if there are errors creating the directory or if there is
     *             insufficient permission to carry out normal file operations
     *             in the directory
     */
    public static void prepareDirectory(String storageFolderPath) throws InvalidPathException {
        Path directoryPath = Paths.get(storageFolderPath);
        boolean isValidDirectory = true;

        try {
            directoryPath = Files.createDirectories(directoryPath);
            File directory = directoryPath.toFile();
            if (!canUseDirectory(directory)) {
                isValidDirectory = false;
            }
        } catch (IOException e) {
            isValidDirectory = false;
        }

        if (!isValidDirectory) {
            String errorMessage = String.format(Constants.MESSAGE_INVALID_PATH, storageFolderPath);
            throw new InvalidPathException(storageFolderPath, errorMessage);
        }
    }

    /**
     * This method moves all the data files to newStorageFolderPath.
     *
     * @param newStorageFolderPath
     *            the new storage directory
     * @throws ExistingFilesFoundException
     *             if files in the new directory were replaced (with backups
     *             made)
     */
    public static void moveFilesToDirectory(String newStorageFolderPath) throws ExistingFilesFoundException {
        ArrayList<Path> filePaths = RecordManager.getAllFilePaths();
        ArrayList<FilePathPair> replacedFiles = new ArrayList<FilePathPair>();
        FilePathPair filePathPair = null;

        for (Path path : filePaths) {
            String filename = path.getFileName().toString();
            Path destination = Paths.get(newStorageFolderPath, filename);
            filePathPair = moveAndBackup(path, destination);<commit_msg>Hotfix for storage filemanager - only move files if they exist<commit_after>package jfdi.storage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.InvalidPathException;

import java.util.ArrayList;
import java.util.Scanner;

import jfdi.storage.exceptions.ExistingFilesFoundException;
import jfdi.storage.exceptions.FilePathPair;

/**
 * This class manages file operations required by Storage
 *
 * @author <NAME>
 */
public class FileManager {

    /**
     * This method creates the necessary directories for files to be stored in
     * storageFolderPath. If there are issues creating the directory or if there
     * is insufficient permission to carry out normal file operations in the
     * directory, then an InvalidPathException is thrown.
     *
     * @param storageFolderPath
     *            the root directory of the data files
     * @throws InvalidPathException
     *             if there are errors creating the directory or if there is
     *             insufficient permission to carry out normal file operations
     *             in the directory
     */
    public static void prepareDirectory(String storageFolderPath) throws InvalidPathException {
        Path directoryPath = Paths.get(storageFolderPath);
        boolean isValidDirectory = true;

        try {
            directoryPath = Files.createDirectories(directoryPath);
            File directory = directoryPath.toFile();
            if (!canUseDirectory(directory)) {
                isValidDirectory = false;
            }
        } catch (IOException e) {
            isValidDirectory = false;
        }

        if (!isValidDirectory) {
            String errorMessage = String.format(Constants.MESSAGE_INVALID_PATH, storageFolderPath);
            throw new InvalidPathException(storageFolderPath, errorMessage);
        }
    }

    /**
     * This method moves all the data files to newStorageFolderPath.
     *
     * @param newStorageFolderPath
     *            the new storage directory
     * @throws ExistingFilesFoundException
     *             if files in the new directory were replaced (with backups
     *             made)
     */
    public static void moveFilesToDirectory(String newStorageFolderPath) throws ExistingFilesFoundException {
        ArrayList<Path> filePaths = RecordManager.getAllFilePaths();
        ArrayList<FilePathPair> replacedFiles = new ArrayList<FilePathPair>();
        FilePathPair filePathPair = null;

        for (Path path : filePaths) {
            File file = path.toFile();
            if (!file.exists()) {
                continue;
            }

            String filename = path.getFileName().toString();
            Path destination = Paths.get(newStorageFolderPath, filename);
            filePathPair = moveAndBackup(path, destination);
{'fraction_non_alphanumeric': 0.07000173701580685, 'fraction_numerical': 0.0, 'mean_word_length': 2.6489226869455007, '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': '122979', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1368, 'n_words': 432}