<commit_before>#include "FileSystem.hpp"
#include "File.hpp"
#include <iostream>
#include <cmath>

using namespace std;

FileSystem::FileSystem() {
    root = new Directory("/");
	mMemblockDevice = MemBlockDevice(250, 512);
	freeBlockNumbers = vector<bool>(mMemblockDevice.size(), true);
}

FileSystem::~FileSystem() {
    delete root;
}

void FileSystem::format() {
	mMemblockDevice.reset();
	freeBlockNumbers = vector<bool>(mMemblockDevice.size(), true);
    delete root;
    root = new Directory("/");
}

void FileSystem::ls(const std::string &path) const {
    Directory* directory = root->getDirectory(path);
    if (directory == nullptr) {
        cout << "Directory does not exist." << endl;
        return;
    }
    
    directory->ls();
}

void FileSystem::create(const std::string &filePath) {
	if (fileOrDirectoryExists(filePath)) {
		cout << "File or directory already exists.";
		return;
	}
	cout << "Enter file contents: \n";
	string fileContent;
	getline(cin, fileContent);

	int requiredBlocks = ceil(fileContent.length() / (float)mMemblockDevice.getBlockLength());
	vector<int> freeBlock = freeBlocks();
	if (requiredBlocks > freeBlock.size()) {
		cout << "Not enough free blocks to save string." << endl;
		return;
	}

    Directory* directory = root->getDirectory(directoryPart(filePath));
	File* file = directory->createFile(filePart(filePath));

	appendToFile(file, fileContent);
}

void FileSystem::mkdir(const string &path) {
    if (filePart(path).empty()) {
        cout << "Wrong syntax." << endl;
        return;
    }
    
    Directory* directory = root->getDirectory(directoryPart(path));
    if (directory == nullptr) {
        cout << "Directory " << directoryPart(path) << " does not exist." << endl;
        return;
    }
    
    if (fileOrDirectoryExists(path)) {
        cout << "File or directory with that name already exists." << endl;
		return;
    }

    directory->createDirectory(filePart(path));
    
    cout << "Directory created." << endl;
}

void FileSystem::cat(const std::string &fileName) const {
    Directory* directory = root->getDirectory(directoryPart(fileName));
    if (directory == nullptr) {
        cout << "File does not exist." << endl;
        return;
    }
    File* file = directory->getFile(filePart(fileName));
	if (file == nullptr) {
        cout << "File does not exist." << endl;
        return;
	} else if (!file->getReadPermission()) {
		cout << "File is read protected." << endl;
		return;
	}
    
    cout << fileToString(fileName) << endl;
}

void FileSystem::save(const std::string &saveFile) const {
	ofstream file;
	int nrOfBlocks = mMemblockDevice.size();
	int nrOfElements = mMemblockDevice.getBlockLength();
	char* buffer = new char[nrOfElements];
	file.open(saveFile, ios::out|ios::binary);
	
	for (int i = 0; i < nrOfBlocks; i++) {
		for (int j = 0; j < nrOfElements; j++) {
			buffer[j] = mMemblockDevice.readBlock(i)[j];
		}
		file.write(buffer, nrOfElements);
	}
    
    delete[] buffer;
    
    root->save(file);
    
	file.close();
}

void FileSystem::load(const std::string &saveFile) {
    format();
    
	ifstream file;
	int nrOfBlocks = mMemblockDevice.size();
	int nrOfElements = mMemblockDevice.getBlockLength();
	char* buffer = new char[nrOfElements];
	file.open(saveFile, ios::in | ios::binary);
	
	for (int i = 0; i < nrOfBlocks; i++) {
		file.read(buffer, nrOfElements);
		mMemblockDevice.writeBlock(i, buffer);
	}
    
    delete[] buffer;
    
    root->load(file);
    
	file.close();
}

void FileSystem::rm(const std::string &path) {
	if (!fileExists(path))
		return;
	Directory* directory = root->getDirectory(directoryPart(path));
	File* file = directory->getFile(filePart(path));

	if (!file->getWritePermission()) {
		cout << "File is write protected." << endl;
		return;
	}

	for (int i = 0; i < file->getBlockNumbers().size(); i++)
		freeBlockNumbers[file->getBlockNumbers()[i]] = true;
	
	delete file;
	directory->rm(filePart(path));
}

void FileSystem::copy(const std::string &source, const std::string &dest) {
	if (!fileExists(source)) {
		cout << "Can't copy file, file does not exist." << endl;
		return;
	}
	if (fileOrDirectoryExists(dest)) {
		cout << "Can't copy file, destination already exists." << endl;
	}
	Directory* directory = root->getDirectory(directoryPart(source));
	File* sourceFile = directory->getFile(filePart(source));
    if (!sourceFile->getReadPermission()) {
		cout << "File is read protected." << endl;
        return;
    }
    
    Directory* destinationDirectory = root->getDirectory(directoryPart(dest));
    if (destinationDirectory == nullptr) {
        cout << "Destination directory does not exist." << endl;
        return;
    }
    
	File* destinationFile = destinationDirectory->createFile(filePart(dest));
	string contents = fileToString(source);
	appendToFile(destinationFile, contents);
}

void FileSystem::append(const std::string &source, const std::string &destination) {
	if (fileExists(destination) && fileExists(source)) {
		Directory* destinationDirectory = root->getDirectory(directoryPart(destination));
		Directory* sourceDirectory = root->getDirectory(directoryPart(source));
		File* destinationFile = destinationDirectory->getFile(filePart(destination));
		File* sourceFile = sourceDirectory->getFile(filePart(source));

		if (sourceFile->getReadPermission() && destinationFile->getWritePermission()) {
			string appendString = fileToString(source);
			cout << appendString << " will be added." << endl;
			appendToFile(destinationFile, appendString);
		} else {
			cout << "Files did not have proper read/write permission." << endl;
		}
	} else {
		cout << "File(s) were not found." << endl;
	}
}

void FileSystem::rename(const string &source, const string &newName) {
    if (newName.empty()) {
        cout << "New name can't be empty." << endl;
        return;
    }
    
    if (!fileExists(source)) {
        cout << "Can't rename file, source does not exist." << endl;
        return;
    }
    
    if (fileOrDirectoryExists(newName)) {
        cout << "Destination already exists." << endl;<commit_msg>now formats disk when FileSystem is created<commit_after>#include "FileSystem.hpp"
#include "File.hpp"
#include <iostream>
#include <cmath>

using namespace std;

FileSystem::FileSystem() {
    root = new Directory("/");
	mMemblockDevice = MemBlockDevice(250, 512);
	format();
}

FileSystem::~FileSystem() {
    delete root;
}

void FileSystem::format() {
	mMemblockDevice.reset();
	freeBlockNumbers = vector<bool>(mMemblockDevice.size(), true);
    delete root;
    root = new Directory("/");
}

void FileSystem::ls(const std::string &path) const {
    Directory* directory = root->getDirectory(path);
    if (directory == nullptr) {
        cout << "Directory does not exist." << endl;
        return;
    }
    
    directory->ls();
}

void FileSystem::create(const std::string &filePath) {
	if (fileOrDirectoryExists(filePath)) {
		cout << "File or directory already exists.";
		return;
	}
	cout << "Enter file contents: \n";
	string fileContent;
	getline(cin, fileContent);

	int requiredBlocks = ceil(fileContent.length() / (float)mMemblockDevice.getBlockLength());
	vector<int> freeBlock = freeBlocks();
	if (requiredBlocks > freeBlock.size()) {
		cout << "Not enough free blocks to save string." << endl;
		return;
	}

    Directory* directory = root->getDirectory(directoryPart(filePath));
	File* file = directory->createFile(filePart(filePath));

	appendToFile(file, fileContent);
}

void FileSystem::mkdir(const string &path) {
    if (filePart(path).empty()) {
        cout << "Wrong syntax." << endl;
        return;
    }
    
    Directory* directory = root->getDirectory(directoryPart(path));
    if (directory == nullptr) {
        cout << "Directory " << directoryPart(path) << " does not exist." << endl;
        return;
    }
    
    if (fileOrDirectoryExists(path)) {
        cout << "File or directory with that name already exists." << endl;
		return;
    }

    directory->createDirectory(filePart(path));
    
    cout << "Directory created." << endl;
}

void FileSystem::cat(const std::string &fileName) const {
    Directory* directory = root->getDirectory(directoryPart(fileName));
    if (directory == nullptr) {
        cout << "File does not exist." << endl;
        return;
    }
    File* file = directory->getFile(filePart(fileName));
	if (file == nullptr) {
        cout << "File does not exist." << endl;
        return;
	} else if (!file->getReadPermission()) {
		cout << "File is read protected." << endl;
		return;
	}
    
    cout << fileToString(fileName) << endl;
}

void FileSystem::save(const std::string &saveFile) const {
	ofstream file;
	int nrOfBlocks = mMemblockDevice.size();
	int nrOfElements = mMemblockDevice.getBlockLength();
	char* buffer = new char[nrOfElements];
	file.open(saveFile, ios::out|ios::binary);
	
	for (int i = 0; i < nrOfBlocks; i++) {
		for (int j = 0; j < nrOfElements; j++) {
			buffer[j] = mMemblockDevice.readBlock(i)[j];
		}
		file.write(buffer, nrOfElements);
	}
    
    delete[] buffer;
    
    root->save(file);
    
	file.close();
}

void FileSystem::load(const std::string &saveFile) {
    format();
    
	ifstream file;
	int nrOfBlocks = mMemblockDevice.size();
	int nrOfElements = mMemblockDevice.getBlockLength();
	char* buffer = new char[nrOfElements];
	file.open(saveFile, ios::in | ios::binary);
	
	for (int i = 0; i < nrOfBlocks; i++) {
		file.read(buffer, nrOfElements);
		mMemblockDevice.writeBlock(i, buffer);
	}
    
    delete[] buffer;
    
    root->load(file);
    
	file.close();
}

void FileSystem::rm(const std::string &path) {
	if (!fileExists(path))
		return;
	Directory* directory = root->getDirectory(directoryPart(path));
	File* file = directory->getFile(filePart(path));

	if (!file->getWritePermission()) {
		cout << "File is write protected." << endl;
		return;
	}

	for (int i = 0; i < file->getBlockNumbers().size(); i++)
		freeBlockNumbers[file->getBlockNumbers()[i]] = true;
	
	delete file;
	directory->rm(filePart(path));
}

void FileSystem::copy(const std::string &source, const std::string &dest) {
	if (!fileExists(source)) {
		cout << "Can't copy file, file does not exist." << endl;
		return;
	}
	if (fileOrDirectoryExists(dest)) {
		cout << "Can't copy file, destination already exists." << endl;
	}
	Directory* directory = root->getDirectory(directoryPart(source));
	File* sourceFile = directory->getFile(filePart(source));
    if (!sourceFile->getReadPermission()) {
		cout << "File is read protected." << endl;
        return;
    }
    
    Directory* destinationDirectory = root->getDirectory(directoryPart(dest));
    if (destinationDirectory == nullptr) {
        cout << "Destination directory does not exist." << endl;
        return;
    }
    
	File* destinationFile = destinationDirectory->createFile(filePart(dest));
	string contents = fileToString(source);
	appendToFile(destinationFile, contents);
}

void FileSystem::append(const std::string &source, const std::string &destination) {
	if (fileExists(destination) && fileExists(source)) {
		Directory* destinationDirectory = root->getDirectory(directoryPart(destination));
		Directory* sourceDirectory = root->getDirectory(directoryPart(source));
		File* destinationFile = destinationDirectory->getFile(filePart(destination));
		File* sourceFile = sourceDirectory->getFile(filePart(source));

		if (sourceFile->getReadPermission() && destinationFile->getWritePermission()) {
			string appendString = fileToString(source);
			appendToFile(destinationFile, appendString);
		} else {
			cout << "Files did not have proper read/write permission." << endl;
		}
	} else {
		cout << "File(s) were not found." << endl;
	}
}

void FileSystem::rename(const string &source, const string &newName) {
    if (newName.empty()) {
        cout << "New name can't be empty." << endl;
        return;
    }
    
    if (!fileExists(source)) {
        cout << "Can't rename file, source does not exist." << endl;
        return;
    }
    
    if (fileOrDirectoryExists(newName)) {
        cout << "Destination already exists." << endl;
{'fraction_non_alphanumeric': 0.14886222589987588, 'fraction_numerical': 0.0016549441456350847, 'mean_word_length': 3.8911371914204778, 'pattern_counts': {'":': 0, '<': 202, '<?xml version=': 0, '>': 70, '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': '306743', 'n_tokens_mistral': 3799, 'n_tokens_neox': 3658, 'n_words': 909}