From 7f13d95572040d30b70edbfac3c4b7350ee8855c Mon Sep 17 00:00:00 2001 From: zhongtao Date: Fri, 26 Jan 2024 12:59:45 +0800 Subject: [PATCH 04/43] do not cleanup if the directory does not exist Signed-off-by: zhongtao --- src/cmd/isulad/main.c | 20 ++++++++++++++++++- .../container/leftover_cleanup/cleanup.c | 13 +++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c index deca72be..fd0b6e89 100644 --- a/src/cmd/isulad/main.c +++ b/src/cmd/isulad/main.c @@ -1270,8 +1270,26 @@ static int do_ensure_isulad_tmpdir_security(const char *isulad_tmp_dir) int nret; char tmp_dir[PATH_MAX] = { 0 }; char cleanpath[PATH_MAX] = { 0 }; + char isulad_tmp_cleanpath[PATH_MAX] = { 0 }; - if (realpath(isulad_tmp_dir, cleanpath) == NULL) { + if (util_clean_path(isulad_tmp_dir, isulad_tmp_cleanpath, sizeof(isulad_tmp_cleanpath)) == NULL) { + ERROR("Failed to clean path for %s", isulad_tmp_dir); + return -1; + } + + // Determine whether isulad_tmp_dir exists. If it does not exist, create it + // to prevent realpath from reporting errors because the folder does not exist. + if (!util_dir_exists(isulad_tmp_cleanpath)) { + nret = snprintf(tmp_dir, PATH_MAX, "%s/isulad_tmpdir", isulad_tmp_cleanpath); + if (nret < 0 || (size_t)nret >= PATH_MAX) { + ERROR("Failed to snprintf"); + return -1; + } + INFO("iSulad tmpdir: %s does not exist, create it", isulad_tmp_dir); + return recreate_tmpdir(tmp_dir); + } + + if (realpath(isulad_tmp_cleanpath, cleanpath) == NULL) { ERROR("Failed to get real path for %s", tmp_dir); return -1; } diff --git a/src/daemon/modules/container/leftover_cleanup/cleanup.c b/src/daemon/modules/container/leftover_cleanup/cleanup.c index 08151f42..16dba630 100644 --- a/src/daemon/modules/container/leftover_cleanup/cleanup.c +++ b/src/daemon/modules/container/leftover_cleanup/cleanup.c @@ -174,8 +174,19 @@ static void cleanup_path(char *dir) int nret; char tmp_dir[PATH_MAX] = { 0 }; char cleanpath[PATH_MAX] = { 0 }; + char dir_cleanpath[PATH_MAX] = { 0 }; - if (realpath(dir, cleanpath) == NULL) { + if (util_clean_path(dir, dir_cleanpath, sizeof(dir_cleanpath)) == NULL) { + ERROR("clean path for %s failed", dir); + return; + } + + // If dir does not exist, skip cleanup + if (!util_dir_exists(dir_cleanpath)) { + return; + } + + if (realpath(dir_cleanpath, cleanpath) == NULL) { ERROR("get real path for %s failed", tmp_dir); return; } -- 2.34.1