From 03c38eada3952833bd3381fb91c8689f6de27411 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 9 Mar 2016 12:06:33 -0800 Subject: [PATCH] add folder rename handling --- assignment-client/src/assets/AssetServer.cpp | 58 +++++++++++++++++--- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 13257967ac..403941221d 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -511,6 +511,10 @@ bool AssetServer::setMapping(const AssetPath& path, const AssetHash& hash) { } } +bool pathIsFolder(const AssetPath& path) { + return path.endsWith('/'); +} + bool AssetServer::deleteMappings(const AssetPathList& paths) { // take a copy of the current mappings in case persistence of these deletes fails auto oldMappings = _fileMappings; @@ -538,23 +542,59 @@ bool AssetServer::deleteMappings(const AssetPathList& paths) { } bool AssetServer::renameMapping(const AssetPath& oldPath, const AssetPath& newPath) { - // take the old hash to remove the old mapping - auto oldMapping = _fileMappings[oldPath].toString(); + if (pathIsFolder(oldPath)) { + if (!pathIsFolder(newPath)) { + // we were asked to rename a path to a folder to a path that isn't a folder, this is a fail + return false; + } - if (!oldMapping.isEmpty()) { - _fileMappings[newPath] = oldMapping; + // take a copy of the old mappings + auto oldMappings = _fileMappings; + + // iterate the current mappings and adjust any that matches the renamed folder + auto it = oldMappings.begin(); + while (it != oldMappings.end()) { + + if (it->toString().startsWith(oldPath)) { + auto oldKey = it.key(); + auto newKey = oldKey.replace(0, oldPath.size(), newPath); + + // remove the old version from the in memory file mappings + _fileMappings.remove(oldKey); + _fileMappings.insert(newKey, it.value()); + } + + ++it; + } if (writeMappingsToFile()) { - // persisted the renamed mapping, return success + // persisted the changed mappings return success return true; } else { - // we couldn't persist the renamed mapping, rollback and return failure - _fileMappings[oldPath] = oldMapping; + // couldn't persist the renamed paths, rollback and return failure + _fileMappings = oldMappings; return false; } } else { - // failed to find a mapping that was to be renamed, return failure - return false; + // take the old hash to remove the old mapping + auto oldMapping = _fileMappings[oldPath].toString(); + + if (!oldMapping.isEmpty()) { + _fileMappings[newPath] = oldMapping; + + if (writeMappingsToFile()) { + // persisted the renamed mapping, return success + return true; + } else { + // we couldn't persist the renamed mapping, rollback and return failure + _fileMappings[oldPath] = oldMapping; + + return false; + } + } else { + // failed to find a mapping that was to be renamed, return failure + return false; + } } }