From b2e274466fdc3b8cffeba952844bcd2d62bece31 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Sat, 27 Jan 2024 11:16:37 +0800
Subject: [PATCH 193/198] module only deletes the temporary files it creates

Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
 src/cmd/isulad/main.c                         |  2 +-
 src/common/constants.h                        |  2 +-
 src/daemon/modules/image/oci/oci_image.c      | 44 +++++++++++++++++--
 src/daemon/modules/image/oci/oci_image.h      |  4 ++
 src/daemon/modules/image/oci/oci_load.c       |  2 +-
 .../modules/image/oci/registry/registry.c     |  2 +-
 6 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c
index 5fb702b6..58319ad0 100644
--- a/src/cmd/isulad/main.c
+++ b/src/cmd/isulad/main.c
@@ -1258,7 +1258,7 @@ static int isulad_tmpdir_security_check(const char *tmp_dir)
 
 static int recreate_tmpdir(const char *tmp_dir)
 {
-    if (util_recursive_rmdir(tmp_dir, 0) != 0) {
+    if (util_path_remove(tmp_dir) != 0) {
         ERROR("Failed to remove directory %s", tmp_dir);
         return -1;
     }
diff --git a/src/common/constants.h b/src/common/constants.h
index 9ea3def7..d6fe6e50 100644
--- a/src/common/constants.h
+++ b/src/common/constants.h
@@ -50,7 +50,7 @@ extern "C" {
 
 #define TEMP_DIRECTORY_MODE 0700
 
-#define ISULAD_TEMP_DIRECTORY_MODE 0600
+#define ISULAD_TEMP_DIRECTORY_MODE 0700
 
 #define CONSOLE_FIFO_DIRECTORY_MODE 0770
 
diff --git a/src/daemon/modules/image/oci/oci_image.c b/src/daemon/modules/image/oci/oci_image.c
index f525f621..e3f28051 100644
--- a/src/daemon/modules/image/oci/oci_image.c
+++ b/src/daemon/modules/image/oci/oci_image.c
@@ -279,10 +279,42 @@ out:
     return ret;
 }
 
+// remove dir that image module created
+// return false when failed to rmdir
+// eg: oci-image-load-XXXXXX && registry-XXXXXX
+static bool remove_image_tmpdir_cb(const char *path_name, const struct dirent *sub_dir, void *context)
+{
+    int nret = 0;
+    char tmpdir[PATH_MAX] = { 0 };
+
+    if (sub_dir == NULL) {
+        return true;
+    }
+
+    if (!util_has_prefix(sub_dir->d_name, LOAD_TMPDIR_PREFIX) && !util_has_prefix(sub_dir->d_name, REGISTRY_TMPDIR_PREFIX)) {
+        // only remove directory that image module created
+        return true;
+    }
+
+    nret = snprintf(tmpdir, PATH_MAX, "%s/%s", path_name, sub_dir->d_name);
+    if (nret < 0 || (size_t)nret >= PATH_MAX) {
+        ERROR("Failed to snprintf for %s", sub_dir->d_name);
+        return false;
+    }
+
+    if (util_recursive_rmdir(tmpdir, 0) != 0) {
+        ERROR("Failed to remove path %s", tmpdir);
+        return false;
+    }
+
+    return true;
+}
+
 static int recreate_image_tmpdir()
 {
     char *image_tmp_path = NULL;
     int ret = 0;
+    int nret = 0;
 
     image_tmp_path = oci_get_isulad_tmpdir(g_oci_image_module_data.root_dir);
     if (image_tmp_path == NULL) {
@@ -291,10 +323,14 @@ static int recreate_image_tmpdir()
         goto out;
     }
 
-    if (util_recursive_rmdir(image_tmp_path, 0)) {
-        ERROR("failed to remove directory %s", image_tmp_path);
-        ret = -1;
-        goto out;
+    // If image_tmp_path exist, cleanup it
+    if (util_dir_exists(image_tmp_path)) {
+        nret = util_scan_subdirs(image_tmp_path, remove_image_tmpdir_cb, NULL);
+        if (nret != 0) {
+            ERROR("Failed to scan isulad tmp subdirs");
+            ret = -1;
+            goto out;
+        }
     }
 
     if (util_mkdir_p(image_tmp_path, TEMP_DIRECTORY_MODE)) {
diff --git a/src/daemon/modules/image/oci/oci_image.h b/src/daemon/modules/image/oci/oci_image.h
index 3f0a87b7..cb180e1c 100644
--- a/src/daemon/modules/image/oci/oci_image.h
+++ b/src/daemon/modules/image/oci/oci_image.h
@@ -38,6 +38,10 @@ struct oci_image_module_data {
     char **insecure_registries;
     size_t insecure_registries_len;
 };
+
+#define LOAD_TMPDIR_PREFIX "oci-image-load-"
+#define REGISTRY_TMPDIR_PREFIX "registry-"
+
 struct oci_image_module_data *get_oci_image_data(void);
 
 int oci_init(const isulad_daemon_configs *args);
diff --git a/src/daemon/modules/image/oci/oci_load.c b/src/daemon/modules/image/oci/oci_load.c
index 3fc8cfb8..efffa6f5 100644
--- a/src/daemon/modules/image/oci/oci_load.c
+++ b/src/daemon/modules/image/oci/oci_load.c
@@ -1044,7 +1044,7 @@ static char *oci_load_path_create()
         goto out;
     }
 
-    nret = snprintf(tmp_dir, PATH_MAX, "%s/oci-image-load-XXXXXX", image_tmp_path);
+    nret = snprintf(tmp_dir, PATH_MAX, "%s/%sXXXXXX", image_tmp_path, LOAD_TMPDIR_PREFIX);
     if (nret < 0 || (size_t)nret >= sizeof(tmp_dir)) {
         ERROR("Path is too long");
         ret = -1;
diff --git a/src/daemon/modules/image/oci/registry/registry.c b/src/daemon/modules/image/oci/registry/registry.c
index d586d1c7..5265b5ab 100644
--- a/src/daemon/modules/image/oci/registry/registry.c
+++ b/src/daemon/modules/image/oci/registry/registry.c
@@ -1902,7 +1902,7 @@ static int prepare_pull_desc(pull_descriptor *desc, registry_pull_options *optio
         goto out;
     }
 
-    sret = snprintf(blobpath, PATH_MAX, "%s/registry-XXXXXX", image_tmp_path);
+    sret = snprintf(blobpath, PATH_MAX, "%s/%sXXXXXX", image_tmp_path, REGISTRY_TMPDIR_PREFIX);
     if (sret < 0 || (size_t)sret >= PATH_MAX) {
         ERROR("image tmp work path too long");
         ret = -1;
-- 
2.25.1

