add server side handling for mapping rename

This commit is contained in:
Stephen Birarda 2016-03-08 14:57:22 -08:00
parent 181b20f673
commit 33cadb36da
2 changed files with 47 additions and 2 deletions

View file

@ -203,6 +203,10 @@ void AssetServer::handleAssetMappingOperation(QSharedPointer<ReceivedMessage> me
handleDeleteMappingsOperation(*message, senderNode, *replyPacket);
break;
}
case AssetMappingOperationType::Rename: {
handleRenameMappingOperation(*message, senderNode, *replyPacket);
break;
}
}
auto nodeList = DependencyManager::get<NodeList>();
@ -274,6 +278,21 @@ void AssetServer::handleDeleteMappingsOperation(ReceivedMessage& message, Shared
}
}
void AssetServer::handleRenameMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) {
if (senderNode->getCanRez()) {
QString oldPath = message.readString();
QString newPath = message.readString();
if (renameMapping(oldPath, newPath)) {
replyPacket.writePrimitive(AssetServerError::NoError);
} else {
replyPacket.writePrimitive(AssetServerError::MappingOperationFailed);
}
} else {
replyPacket.writePrimitive(AssetServerError::PermissionDenied);
}
}
void AssetServer::handleAssetGetInfo(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
QByteArray assetHash;
MessageID messageID;
@ -468,7 +487,7 @@ bool AssetServer::writeMappingsToFile() {
return false;
}
bool AssetServer::setMapping(AssetPath path, AssetHash hash) {
bool AssetServer::setMapping(const AssetPath& path, const AssetHash& hash) {
// remember what the old mapping was in case persistence fails
auto oldMapping = _fileMappings.value(path).toString();
@ -516,3 +535,25 @@ bool AssetServer::deleteMappings(const AssetPathList& paths) {
return false;
}
}
bool AssetServer::renameMapping(const AssetPath& oldPath, const AssetPath& newPath) {
// 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;
}
}

View file

@ -45,17 +45,21 @@ private:
void handleGetAllMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket);
void handleSetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket);
void handleDeleteMappingsOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket);
void handleRenameMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket);
// Mapping file operations must be called from main assignment thread only
void loadMappingsFromFile();
bool writeMappingsToFile();
/// Set the mapping for path to hash
bool setMapping(AssetPath path, AssetHash hash);
bool setMapping(const AssetPath& path, const AssetHash& hash);
/// Delete mapping `path`. Returns `true` if deletion of mappings succeeds, else `false`.
bool deleteMappings(const AssetPathList& paths);
/// Rename mapping from `oldPath` to `newPath`. Returns true if successful
bool renameMapping(const AssetPath& oldPath, const AssetPath& newPath);
static void writeError(NLPacketList* packetList, AssetServerError error);
void performMappingMigration();