mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 03:58:07 +02:00
wip
This commit is contained in:
parent
b03f51dd26
commit
de23a11dee
1 changed files with 43 additions and 26 deletions
|
@ -54,13 +54,14 @@ FilePointer FileCache::writeFile(const Key& key, const char* data, size_t length
|
||||||
// if file already exists, return it
|
// if file already exists, return it
|
||||||
FilePointer file = getFile(key);
|
FilePointer file = getFile(key);
|
||||||
if (file) {
|
if (file) {
|
||||||
qCWarning(file_cache) << "Attempted to overwrite" << filepath.c_str();
|
qCWarning(file_cache, "Attempted to overwrite %", key.c_str());
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the new file
|
// write the new file
|
||||||
FILE* saveFile = fopen(filepath.c_str(), "wb");
|
FILE* saveFile = fopen(filepath.c_str(), "wb");
|
||||||
if (saveFile != nullptr && fwrite(data, length, 1, saveFile) && fclose(saveFile) == 0) {
|
if (saveFile != nullptr && fwrite(data, length, 1, saveFile) && fclose(saveFile) == 0) {
|
||||||
|
qCInfo(file_cache, "Wrote %s", key.c_str());
|
||||||
file.reset(createFile(key, filepath, length, extra), &fileDeleter);
|
file.reset(createFile(key, filepath, length, extra), &fileDeleter);
|
||||||
file->_cache = this;
|
file->_cache = this;
|
||||||
_files[key] = file;
|
_files[key] = file;
|
||||||
|
@ -69,7 +70,7 @@ FilePointer FileCache::writeFile(const Key& key, const char* data, size_t length
|
||||||
|
|
||||||
emit dirty();
|
emit dirty();
|
||||||
} else {
|
} else {
|
||||||
qCWarning(file_cache, "Failed to write %s (%s)", filepath.c_str(), strerror(errno));
|
qCWarning(file_cache, "Failed to write %s (%s)", key.c_str(), strerror(errno));
|
||||||
errno = 0;
|
errno = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,6 +89,7 @@ FilePointer FileCache::getFile(const Key& key) {
|
||||||
if (file) {
|
if (file) {
|
||||||
// if it exists, it is active - remove it from the cache
|
// if it exists, it is active - remove it from the cache
|
||||||
removeUnusedFile(file);
|
removeUnusedFile(file);
|
||||||
|
qCInfo(file_cache, "Found %s", key.c_str());
|
||||||
emit dirty();
|
emit dirty();
|
||||||
} else {
|
} else {
|
||||||
// if not, remove the weak_ptr
|
// if not, remove the weak_ptr
|
||||||
|
@ -115,28 +117,30 @@ std::string FileCache::createDir(const std::string& dirname) {
|
||||||
std::string entry;
|
std::string entry;
|
||||||
manifest >> entry;
|
manifest >> entry;
|
||||||
persistedEntries.insert(entry);
|
persistedEntries.insert(entry);
|
||||||
|
qCInfo(file_cache, "Manifest contents: %s", entry.c_str());
|
||||||
// ZZMP: rm
|
|
||||||
for (const auto& entry : persistedEntries)
|
|
||||||
qDebug() << "ZZMP" << entry.c_str();
|
|
||||||
qDebug() << "ZZMP" << "---";
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
qCWarning(file_cache, "Missing manifest");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
foreach(QString filename, dir.entryList()) {
|
foreach(QString filename, dir.entryList()) {
|
||||||
if (persistedEntries.find(filename.toStdString()) == persistedEntries.cend()) {
|
if (persistedEntries.find(filename.toStdString()) == persistedEntries.cend()) {
|
||||||
dir.remove(filename);
|
dir.remove(filename);
|
||||||
|
qCInfo(file_cache) << "Cleaned" << filename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
qCDebug(file_cache, "Initiated %s", dirpath.data());
|
||||||
} else {
|
} else {
|
||||||
dir.mkpath(dirpath);
|
dir.mkpath(dirpath);
|
||||||
|
qCDebug(file_cache, "Created %s", dirpath.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
return dirpath.toStdString();
|
return dirpath.toStdString();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string FileCache::getFilepath(const Key& key) {
|
std::string FileCache::getFilepath(const Key& key) {
|
||||||
return _dir + key + '.' + _ext;
|
return _dir + '/' + key + '.' + _ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileCache::addUnusedFile(const FilePointer file) {
|
void FileCache::addUnusedFile(const FilePointer file) {
|
||||||
|
@ -197,34 +201,46 @@ void FileCache::reserve(size_t length) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileCache::clear() {
|
void FileCache::clear() {
|
||||||
std::string manifestPath= _dir + MANIFEST_NAME;
|
try {
|
||||||
FILE* manifest = fopen(manifestPath.c_str(), "wb");
|
std::string manifestPath= _dir + '/' + MANIFEST_NAME;
|
||||||
|
std::ofstream manifest(manifestPath);
|
||||||
|
|
||||||
Lock lock(_unusedFilesMutex);
|
bool firstEntry = true;
|
||||||
for (const auto& val : _unusedFiles) {
|
|
||||||
const FilePointer& file = val.second;
|
|
||||||
file->_cache = nullptr;
|
|
||||||
|
|
||||||
if (_unusedFilesSize > _offlineFilesMaxSize) {
|
{
|
||||||
_unusedFilesSize -= file->getLength();
|
Lock lock(_unusedFilesMutex);
|
||||||
} else {
|
for (const auto& val : _unusedFiles) {
|
||||||
std::string key = file->getKey() + '.' + _ext + '\n';
|
const FilePointer& file = val.second;
|
||||||
if (manifest != nullptr && !fwrite(key.c_str(), key.length(), 1, manifest)) {
|
file->_cache = nullptr;
|
||||||
manifest = nullptr; // to prevent future writes
|
|
||||||
|
if (_unusedFilesSize > _offlineFilesMaxSize) {
|
||||||
|
_unusedFilesSize -= file->getLength();
|
||||||
|
} else {
|
||||||
|
if (!firstEntry) {
|
||||||
|
manifest << '\n';
|
||||||
|
}
|
||||||
|
firstEntry = false;
|
||||||
|
manifest << file->getKey();
|
||||||
|
|
||||||
|
file->_shouldPersist = true;
|
||||||
|
qCInfo(file_cache, "Persisting %s", file->getKey().c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
file->_shouldPersist = true;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (manifest == nullptr || fclose(manifest) != 0) {
|
{
|
||||||
|
Lock lock(_filesMutex);
|
||||||
|
for (const auto& val : _files) {
|
||||||
|
const FilePointer& file = val.second
|
||||||
|
}
|
||||||
|
} catch (std::exception& e) {
|
||||||
|
qCWarning(file_cache, "Failed to write manifest (%s)", e.what());
|
||||||
for (const auto& val : _unusedFiles) {
|
for (const auto& val : _unusedFiles) {
|
||||||
val.second->_shouldPersist = false;
|
val.second->_shouldPersist = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
qCWarning(file_cache, "Failed to write %s (%s)", manifestPath.c_str(), strerror(errno));
|
|
||||||
errno = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Lock lock(_unusedFilesMutex);
|
||||||
_unusedFiles.clear();
|
_unusedFiles.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,6 +256,7 @@ void File::deleter() {
|
||||||
File::~File() {
|
File::~File() {
|
||||||
QFile file(getFilepath().c_str());
|
QFile file(getFilepath().c_str());
|
||||||
if (file.exists() && !_shouldPersist) {
|
if (file.exists() && !_shouldPersist) {
|
||||||
|
qCInfo(file_cache, "Unlinked %s", getFilepath().c_str());
|
||||||
file.remove();
|
file.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue