From d059f53cad4f3063df4f7f93107ad2fbffdb301c Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Fri, 26 Apr 2024 03:46:43 +0000
Subject: [PATCH 11/14] remove lcr-created spec only if create failed

Signed-off-by: jikai <jikai11@huawei.com>
---
 src/runtime/lcrcontainer.c        |  8 +++---
 src/runtime/lcrcontainer_extend.c | 44 +++++++++++++++++++++++++++++++
 src/runtime/lcrcontainer_extend.h |  2 ++
 3 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/src/runtime/lcrcontainer.c b/src/runtime/lcrcontainer.c
index 2f0c9dd..c6959aa 100644
--- a/src/runtime/lcrcontainer.c
+++ b/src/runtime/lcrcontainer.c
@@ -186,15 +186,13 @@ bool lcr_create(const char *name, const char *lcrpath, void *oci_config)
 
     bret = true;
 out_unlock:
+    if (!bret) {
+        lcr_delete_spec(c, oci_spec);
+    }
     if (partial_fd >= 0) {
         close(partial_fd);
         remove_partial(c);
     }
-    if (!bret) {
-        if (!c->destroy(c)) {
-            WARN("Unable to clean lxc resources");
-        }
-    }
     lxc_container_put(c);
     isula_libutils_free_log_prefix();
     return bret;
diff --git a/src/runtime/lcrcontainer_extend.c b/src/runtime/lcrcontainer_extend.c
index 1409ea4..0b420d2 100644
--- a/src/runtime/lcrcontainer_extend.c
+++ b/src/runtime/lcrcontainer_extend.c
@@ -999,3 +999,47 @@ out_free_conf:
     return ret;
 }
 
+static void delete_specific_spec(const char *bundle, const char *name)
+{
+    char filepath[PATH_MAX] = { 0 };
+    int nret = snprintf(filepath, sizeof(filepath), "%s/%s", bundle, name);
+    if (nret < 0 || (size_t)nret >= sizeof(filepath)) {
+        ERROR("Failed to print string");
+        return;
+    }
+
+    if (unlink(filepath) != 0) {
+        SYSERROR("Failed to delete %s", filepath);
+        return;
+    }
+}
+
+void lcr_delete_spec(const struct lxc_container *c, oci_runtime_spec *container)
+{
+    const char *path = NULL;
+    const char *name = NULL;
+    char *bundle = NULL;
+
+    if (c == NULL || c->name == NULL || container == NULL) {
+        ERROR("Invalid arguments");
+        return;
+    }
+
+    path = c->config_path ? c->config_path : LCRPATH;
+    name = c->name;
+    bundle = lcr_get_bundle(path, name);
+    if (bundle == NULL) {
+        return;
+    }
+
+    if (container->hooks != NULL) {
+        delete_specific_spec(bundle, OCIHOOKSFILE);
+    }
+
+    delete_specific_spec(bundle, "config");
+
+    // There might not exist seccomp file, try to delete anyway
+    delete_specific_spec(bundle, "seccomp");
+
+    free(bundle);
+}
diff --git a/src/runtime/lcrcontainer_extend.h b/src/runtime/lcrcontainer_extend.h
index 539747c..c286450 100644
--- a/src/runtime/lcrcontainer_extend.h
+++ b/src/runtime/lcrcontainer_extend.h
@@ -76,6 +76,8 @@ bool lcr_save_spec(const char *name, const char *lcrpath, const struct isula_lin
 
 bool translate_spec(const struct lxc_container *c, oci_runtime_spec *container);
 
+void lcr_delete_spec(const struct lxc_container *c, oci_runtime_spec *container);
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.34.1