提交 6fc0f024 编写于 作者: L lifeng68

image: fix lock error when list all images

Signed-off-by: Nlifeng68 <lifeng68@huawei.com>
上级 512b8468
......@@ -1674,35 +1674,15 @@ static int remove_image_dir(const char *id)
return 0;
}
int image_store_delete(const char *id)
static int do_delete_image_info(const char *id)
{
int ret = 0;
image_t *img = NULL;
if (id == NULL) {
ERROR("Invalid input parameter: empty id");
return -1;
}
if (g_image_store == NULL) {
ERROR("Image store is not ready");
return -1;
}
if (!image_store_exists(id)) {
WARN("image %s not exists already, return success", id);
return 0;
}
if (!image_store_lock(EXCLUSIVE)) {
ERROR("Failed to lock image store with exclusive lock, not allowed to delete image from store");
return -1;
}
img = lookup(id);
if (img == NULL) {
ERROR("Image not known");
ret = -1;
WARN("image %s not exists already, return success", id);
ret = 0;
goto out;
}
......@@ -1720,14 +1700,12 @@ int image_store_delete(const char *id)
out:
image_ref_dec(img);
image_store_unlock();
return ret;
}
static int delete_image_from_store_without_lock(const char *id)
int image_store_delete(const char *id)
{
int ret = 0;
image_t *img = NULL;
if (id == NULL) {
ERROR("Invalid input parameter: empty id");
......@@ -1739,59 +1717,18 @@ static int delete_image_from_store_without_lock(const char *id)
return -1;
}
img = lookup(id);
if (img == NULL) {
ERROR("Image not known");
if (!image_store_lock(EXCLUSIVE)) {
ERROR("Failed to lock image store with exclusive lock, not allowed to delete image from store");
return -1;
}
if (remove_image_from_memory(img->simage->id) != 0) {
ERROR("Failed to remove image from memory");
ret = -1;
goto out;
}
if (remove_image_dir(img->simage->id) != 0) {
ERROR("Failed to delete image directory");
if (do_delete_image_info(id) != 0) {
ERROR("Failed to delete image info %s", id);
ret = -1;
goto out;
}
out:
image_ref_dec(img);
return ret;
}
int image_store_wipe()
{
int ret = 0;
char *id = NULL;
struct linked_list *item = NULL;
struct linked_list *next = NULL;
if (g_image_store == NULL) {
ERROR("Image store is not ready");
return -1;
}
if (!image_store_lock(EXCLUSIVE)) {
ERROR("Failed to lock image store with exclusive lock, not allowed to delete images");
return -1;
}
linked_list_for_each_safe(item, &(g_image_store->images_list), next) {
id = util_strdup_s(((image_t *)item->elem)->simage->id);
if (delete_image_from_store_without_lock(id) != 0) {
ERROR("Failed to delete image: %s", id);
ret = -1;
goto out;
}
free(id);
id = NULL;
}
out:
free(id);
image_store_unlock();
return ret;
}
......@@ -3335,35 +3272,6 @@ out:
return imginfo;
}
static int image_list_shrink_to_fit(imagetool_images_list *images_list)
{
int ret = 0;
imagetool_image **tmp = NULL;
// all images loaded
if (images_list->images_len == g_image_store->images_list_len) {
return 0;
}
// all images damaged
if (images_list->images_len == 0) {
free(images_list->images);
images_list->images = NULL;
return 0;
}
// memory shrink to fit
ret = mem_realloc((void **)(&tmp), images_list->images_len * sizeof(imagetool_image *),
images_list->images, g_image_store->images_list_len * sizeof(imagetool_image *));
if (ret != 0) {
ERROR("Failed to realloc memory for memory shrink to fit");
return -1;
}
images_list->images = tmp;
return 0;
}
int image_store_get_all_images(imagetool_images_list *images_list)
{
int ret = 0;
......@@ -3380,8 +3288,8 @@ int image_store_get_all_images(imagetool_images_list *images_list)
return -1;
}
if (!image_store_lock(SHARED)) {
ERROR("Failed to lock image store with shared lock, not allowed to get all the known images");
if (!image_store_lock(EXCLUSIVE)) {
ERROR("Failed to lock image store with exclusive lock, not allowed to get all the known images");
return -1;
}
......@@ -3399,33 +3307,20 @@ int image_store_get_all_images(imagetool_images_list *images_list)
linked_list_for_each_safe(item, &(g_image_store->images_list), next) {
imagetool_image *imginfo = NULL;
image_t *img = (image_t *)item->elem;
imginfo = get_image_info(img);
if (imginfo == NULL) {
ERROR("Delete image %s due to: Get image information failed, image may be damaged", img->simage->id);
image_store_unlock();
if (image_store_delete(img->simage->id) != 0) {
if (do_delete_image_info(img->simage->id) != 0) {
ERROR("Failed to delete image, please delete residual file manually");
}
if (!image_store_lock(SHARED)) {
ERROR("Failed to relock image store with shared lock, not allowed to get the known images");
ret = -1;
goto out;
}
continue;
}
images_list->images[images_list->images_len++] = imginfo;
imginfo = NULL;
}
if (image_list_shrink_to_fit(images_list) != 0) {
WARN("Failed to shrink to fit memory");
goto unlock;
}
unlock:
image_store_unlock();
out:
return ret;
}
......
......@@ -49,9 +49,6 @@ char *image_store_lookup(const char *id);
// Remove the record of the image.
int image_store_delete(const char *id);
// Remove records of all images
int image_store_wipe();
// Stores a (potentially large) piece of data associated with this ID.
int image_store_set_big_data(const char *id, const char *key, const char *data);
......
......@@ -999,11 +999,6 @@ int rootfs_store_delete(const char *id)
return -1;
}
if (!rootfs_store_exists(id)) {
WARN("rootfs %s not exists already, return success", id);
return 0;
}
if (!rootfs_store_lock(EXCLUSIVE)) {
ERROR("Failed to lock rootfs store");
return -1;
......@@ -1011,8 +1006,8 @@ int rootfs_store_delete(const char *id)
cntr = lookup(id);
if (cntr == NULL) {
ERROR("Rootfs %s not known", id);
ret = -1;
WARN("rootfs %s not exists already, return success", id);
ret = 0;
goto out;
}
......
......@@ -641,24 +641,6 @@ TEST_F(StorageImagesUnitTest, test_image_store_delete)
Restore();
}
TEST_F(StorageImagesUnitTest, test_image_store_wipe)
{
BackUp();
for (auto elem : ids) {
ASSERT_TRUE(image_store_exists(elem.c_str()));
ASSERT_TRUE(dirExists((std::string(store_real_path) + "/overlay-images/" + elem).c_str()));
}
ASSERT_EQ(image_store_wipe(), 0);
for (auto elem : ids) {
ASSERT_FALSE(image_store_exists(elem.c_str()));
ASSERT_FALSE(dirExists((std::string(store_real_path) + "/overlay-images/" + elem).c_str()));
}
Restore();
}
TEST_F(StorageImagesUnitTest, test_image_store_remove_single_name)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册