<commit_before>#include "Storage.h"
#include <fstream>
#include <iostream>
#include "utils/ServiceThreadpool.h"
#include "utils/SHA.h"

mtt::Storage::Storage(TorrentInfo& info)
{
	init(info, ".//");
}

mtt::Storage::~Storage()
{
	flush();
}

void mtt::Storage::init(TorrentInfo& info, const std::string& locationPath)
{
	pieceSize = info.pieceSize;
	files = info.files;
	path = locationPath;

	if (!path.empty() && path.back() != '\\')
		path += '\\';
}

mtt::Status mtt::Storage::setPath(std::string p, bool moveFiles)
{
	if (!p.empty() && p.back() != '\\')
		p += '\\';

	if (path != p)
	{
		std::lock_guard<std::mutex> guard(storageMutex);

		if (files.size() >= 1)
		{
			std::error_code ec;
			auto originalPath = path + files.back().path.front();
			if (std::filesystem::exists(originalPath, ec) && moveFiles)
			{
				if (!std::filesystem::exists(p, ec))
					return mtt::Status::E_InvalidPath;

				auto newPath = p + files.back().path.front();

				for (auto& f : files)
				{
					auto originalPathF = originalPath;
					auto newPathF = newPath;

					for (size_t i = 1; i < f.path.size(); i++)
					{
						if (i + 1 == f.path.size())
						{
							if (!std::filesystem::create_directories(newPathF, ec) && ec.value() != 0)
								return mtt::Status::E_AllocationProblem;
						}

						originalPathF += '\\' + f.path[i];
						newPathF += '\\' + f.path[i];
					}

					if (!std::filesystem::exists(originalPathF, ec))
						continue;

					std::filesystem::rename(originalPathF, newPathF, ec);

					if (ec)
						return mtt::Status::E_AllocationProblem;
				}

				if (files.size() > 1)
					std::filesystem::remove(originalPath, ec);
			}

			path = p;
		}
	}

	return Status::Success;
}

std::string mtt::Storage::getPath()
{
	return path;
}

mtt::Status mtt::Storage::storePiece(DownloadedPiece& piece)<commit_msg>Storage: dont check for file existing if not neessary<commit_after>#include "Storage.h"
#include <fstream>
#include <iostream>
#include "utils/ServiceThreadpool.h"
#include "utils/SHA.h"

mtt::Storage::Storage(TorrentInfo& info)
{
	init(info, ".//");
}

mtt::Storage::~Storage()
{
	flush();
}

void mtt::Storage::init(TorrentInfo& info, const std::string& locationPath)
{
	pieceSize = info.pieceSize;
	files = info.files;
	path = locationPath;

	if (!path.empty() && path.back() != '\\')
		path += '\\';
}

mtt::Status mtt::Storage::setPath(std::string p, bool moveFiles)
{
	if (!p.empty() && p.back() != '\\')
		p += '\\';

	if (path != p)
	{
		std::lock_guard<std::mutex> guard(storageMutex);

		if (moveFiles && !files.empty())
		{
			auto originalPath = path + files.back().path.front();
			std::error_code ec;
			if (std::filesystem::exists(originalPath, ec))
			{
				if (!std::filesystem::exists(p, ec))
					return mtt::Status::E_InvalidPath;

				auto newPath = p + files.back().path.front();

				for (auto& f : files)
				{
					auto originalPathF = originalPath;
					auto newPathF = newPath;

					for (size_t i = 1; i < f.path.size(); i++)
					{
						if (i + 1 == f.path.size())
						{
							if (!std::filesystem::create_directories(newPathF, ec) && ec.value() != 0)
								return mtt::Status::E_AllocationProblem;
						}

						originalPathF += '\\' + f.path[i];
						newPathF += '\\' + f.path[i];
					}

					if (!std::filesystem::exists(originalPathF, ec))
						continue;

					std::filesystem::rename(originalPathF, newPathF, ec);

					if (ec)
						return mtt::Status::E_AllocationProblem;
				}

				if (files.size() > 1)
					std::filesystem::remove(originalPath, ec);
			}
		}

		path = p;
	}

	return Status::Success;
}

std::string mtt::Storage::getPath()
{
	return path;
}

mtt::Status mtt::Storage::storePiece(DownloadedPiece& piece)
{'fraction_non_alphanumeric': 0.1917882225823879, 'fraction_numerical': 0.0024311183144246355, 'mean_word_length': 3.3978622327790973, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6481097', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1347, 'n_words': 258}