summaryrefslogtreecommitdiff
path: root/0005-module-only-deletes-the-temporary-files-it-creates.patch
blob: 50e65156ba1f6e776ca11a1b20ee3f3222565525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
From 69dcd191afbdea5a178fb96a21e28537c2fc6a75 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Sat, 27 Jan 2024 11:16:37 +0800
Subject: [PATCH 05/43] 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 fd0b6e89..7c0c072e 100644
--- a/src/cmd/isulad/main.c
+++ b/src/cmd/isulad/main.c
@@ -1252,7 +1252,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 27d4956e..8a6f86d8 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 9cf2cd4f..ce1c8a6b 100644
--- a/src/daemon/modules/image/oci/oci_image.c
+++ b/src/daemon/modules/image/oci/oci_image.c
@@ -283,10 +283,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) {
@@ -295,10 +327,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 c7304897..482091d6 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 31ae3849..534e2647 100644
--- a/src/daemon/modules/image/oci/oci_load.c
+++ b/src/daemon/modules/image/oci/oci_load.c
@@ -1048,7 +1048,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 751a8727..aed3057a 100644
--- a/src/daemon/modules/image/oci/registry/registry.c
+++ b/src/daemon/modules/image/oci/registry/registry.c
@@ -1908,7 +1908,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.34.1