From 775898893bf54700118db301a915a600a3b81c43 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 11 Apr 2016 08:57:07 -0700 Subject: [PATCH] add deletion of unmapped files during delete op --- assignment-client/src/assets/AssetServer.cpp | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 0542da45d7..00fbdf7ab7 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -544,6 +544,8 @@ bool AssetServer::deleteMappings(AssetPathList& paths) { // take a copy of the current mappings in case persistence of these deletes fails auto oldMappings = _fileMappings; + QSet hashesToCheck; + // enumerate the paths to delete and remove them all for (auto& path : paths) { @@ -557,6 +559,9 @@ bool AssetServer::deleteMappings(AssetPathList& paths) { while (it != _fileMappings.end()) { if (it.key().startsWith(path)) { + // add this hash to the list we need to check for asset removal from the server + hashesToCheck << it.value().toString(); + it = _fileMappings.erase(it); } else { ++it; @@ -583,6 +588,30 @@ bool AssetServer::deleteMappings(AssetPathList& paths) { // deleted the old mappings, attempt to persist to file if (writeMappingsToFile()) { // persistence succeeded we are good to go + + // grab the current mapped hashes + auto mappedHashes = _fileMappings.values(); + + // enumerate the mapped hashes and clear the list of hashes to check for anything that's present + for (auto& hashVariant : mappedHashes) { + auto it = hashesToCheck.find(hashVariant.toString()); + if (it != hashesToCheck.end()) { + hashesToCheck.erase(it); + } + } + + // we now have a set of hashes that are unmapped - we will delete those asset files + for (auto& hash : hashesToCheck) { + // remove the unmapped file + QFile removeableFile { _filesDirectory.absoluteFilePath(hash) }; + + if (removeableFile.remove()) { + qDebug() << "\tDeleted" << hash << "from asset files directory since it is now unmapped."; + } else { + qDebug() << "\tAttempt to delete unmapped file" << hash << "failed"; + } + } + return true; } else { qWarning() << "Failed to persist deleted mappings, rolling back";