From 0dbf21e22d51721e43fa2c1abecf30da271501c5 Mon Sep 17 00:00:00 2001 From: xuxuepeng Date: Thu, 31 Aug 2023 04:11:22 +0000 Subject: [PATCH 04/33] !2162 Fix rename issue for id manager Merge pull request !2162 from xuxuepeng/master --- src/daemon/common/id_name_manager.c | 21 ++++++++++++++++++- src/daemon/common/id_name_manager.h | 1 + .../container_cb/execution_information.c | 12 +++++++++++ src/daemon/sandbox/sandbox_manager.cc | 6 ++++-- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/daemon/common/id_name_manager.c b/src/daemon/common/id_name_manager.c index e6b24798..3fc1c443 100644 --- a/src/daemon/common/id_name_manager.c +++ b/src/daemon/common/id_name_manager.c @@ -382,4 +382,23 @@ bool id_name_manager_remove_entry(const char *id, const char *name) } return ret; -} \ No newline at end of file +} + +bool id_name_manager_rename(const char *new_name, const char *old_name) +{ + if (old_name == NULL || new_name == NULL) { + ERROR("Failed to rename empty name"); + return false; + } + + if (!try_add_name(new_name)) { + ERROR("Failed to add %s to name map", new_name); + return false; + } + + if (!try_remove_name(old_name)) { + WARN("Failed to remove %s from name map", old_name); + } + + return true; +} diff --git a/src/daemon/common/id_name_manager.h b/src/daemon/common/id_name_manager.h index 3c9f6d45..09f0867e 100644 --- a/src/daemon/common/id_name_manager.h +++ b/src/daemon/common/id_name_manager.h @@ -27,6 +27,7 @@ bool id_name_manager_add_entry_with_existing_id(const char *id, const char *name bool id_name_manager_add_entry_with_new_id(const char *name, char **id); bool id_name_manager_add_entry_with_new_id_and_name(char **id, char **name); bool id_name_manager_remove_entry(const char *id, const char *name); +bool id_name_manager_rename(const char *new_name, const char *old_name); #ifdef __cplusplus } diff --git a/src/daemon/executor/container_cb/execution_information.c b/src/daemon/executor/container_cb/execution_information.c index 28480224..93e5032e 100644 --- a/src/daemon/executor/container_cb/execution_information.c +++ b/src/daemon/executor/container_cb/execution_information.c @@ -60,6 +60,7 @@ #include "utils_convert.h" #include "utils_string.h" #include "utils_verify.h" +#include "id_name_manager.h" static int container_version_cb(const container_version_request *request, container_version_response **response) { @@ -1075,11 +1076,22 @@ static int container_rename(container_t *cont, const char *new_name) goto out; } + if (!id_name_manager_rename(new_name, old_name)) { + ERROR("Failed to rename %s to %s in id-name manager", old_name, new_name); + isulad_set_error_message("Failed to rename %s to %s in id-name manager", old_name, new_name); + ret = -1; + goto out; + } + if (!container_name_index_rename(new_name, old_name, id)) { ERROR("Name %s is in use", new_name); isulad_set_error_message("Conflict. The name \"%s\" is already in use by container %s. " "You have to remove (or rename) that container to be able to reuse that name.", new_name, new_name); + // restore name in id-name manager + if (!id_name_manager_rename(old_name, new_name)) { + ERROR("Failed to restore name from \"%s\" to \"%s\" in id-name manager", new_name, old_name); + } ret = -1; goto out; } diff --git a/src/daemon/sandbox/sandbox_manager.cc b/src/daemon/sandbox/sandbox_manager.cc index 527a9aec..e258320a 100644 --- a/src/daemon/sandbox/sandbox_manager.cc +++ b/src/daemon/sandbox/sandbox_manager.cc @@ -210,11 +210,13 @@ bool SandboxManager::IDNameManagerRemoveEntry(const std::string &id, const std:: // Save the id and name of the sandbox to the map of the id_name_manager module bool SandboxManager::IDNameManagerNewEntry(std::string &id, const std::string &name) { - __isula_auto_free char *tmpId = NULL; bool ret = false; if (id.empty()) { + __isula_auto_free char *tmpId = NULL; ret = id_name_manager_add_entry_with_new_id(name.c_str(), &tmpId); - id = tmpId; + if (tmpId != NULL) { + id = tmpId; + } } else { ret = id_name_manager_add_entry_with_existing_id(id.c_str(), name.c_str()); } -- 2.40.1