aboutsummaryrefslogtreecommitdiff
path: root/oop2eindopdr/CacheManager.cpp
blob: 9324b81750b8fe70f0efbf12f2ca4687bd3fb22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "CacheManager.h"

#include <filesystem>
#include <fstream>
#include <iostream>
#include <ctime>

#include <thread>
#include <chrono>

CacheManager::CacheManager(const char* cache_path) : CacheManager(std::string(cache_path)) { }
CacheManager::CacheManager(std::string cache_path) {
	this->cache_path = cache_path;
	this->verify_cache();
}

CacheManager::~CacheManager() {
	for (std::fstream* file : this->files) {
		if (file != nullptr) {
			file->close();
			delete file;
			file = nullptr;
		}
	}
}

void CacheManager::verify_cache() {
	if (!std::filesystem::is_directory(this->cache_path)) this->init_cache();
	this->update_cache();
}

void CacheManager::init_cache() {
	std::filesystem::create_directory(this->cache_path);
	this->update_cache();
}

void CacheManager::update_cache() {
	std::fstream& date = *cache_get("date");
	this->age = std::time(nullptr);
	date << this->age;
	date.close();
}

std::fstream* CacheManager::cache_get(std::string filename) { return cache_get(filename.c_str()); }
std::fstream* CacheManager::cache_get(const char* filename) {
	std::filesystem::path fixed_path = prefix_cache_path(filename); // fix duplicate slashes and add prefix
	unsigned int i = 0;

	i = 0;
	while (!std::filesystem::exists(fixed_path.parent_path())) {
		std::filesystem::create_directories(fixed_path.parent_path()); // make sure folder exists
		std::this_thread::sleep_for(std::chrono::milliseconds(1));
		i++;
		if (i > 100) {
			std::cout << "folder touch doesn't succeed after 100 tries on file " << fixed_path << std::endl;
			exit(1);
		}
	}

	i = 0;
	while (!std::filesystem::exists(fixed_path)) {
		std::fstream(fixed_path, std::ios::trunc | std::ios::out).close(); // touch file
		std::this_thread::sleep_for(std::chrono::milliseconds(1));
		i++;
		if (i > 100) {
			std::cout << "file touch doesn't succeed after 100 tries on file " << fixed_path << std::endl;
			exit(1);
		}
	}

	i = 0;
	std::fstream* file = nullptr;
	do {
		delete file;
		file = new std::fstream(fixed_path.c_str(), std::ios::out | std::ios::in); // open file
		std::this_thread::sleep_for(std::chrono::milliseconds(1));
		i++;
		if (i > 100) {
			std::cout << "file open doesn't succeed after 100 tries on file " << fixed_path << std::endl;
			exit(1);
		}
	} while (!file->is_open());
	this->files.push_back(file); // add file pointer (for closing all files on exit)
	return file;
}

bool CacheManager::cache_exists(std::string filename) { return cache_exists(filename.c_str()); }
bool CacheManager::cache_exists(const char* filename) {
	return std::filesystem::exists(prefix_cache_path(filename));
}


std::string CacheManager::prefix_cache_path(const char* filename) {
	return this->cache_path + "/" + std::string(filename);
}