File U_10-Use-rmdir_recursive.patch of Package slurm.31080

From: Tim Wickberg <tim@schedmd.com>
Date: Wed Oct 11 12:45:25 2023 -0600
Subject: [PATCH 10/19]Use rmdir_recursive().
Patch-mainline: Upstream
Git-repo: https://github.com/SchedMD/slurm
Git-commit: 44a5547a705ba1c21e93729a3f002cb006dc43c7
References: bsc#1216207
Signed-off-by: Egbert Eich <eich@suse.de>

force_rm behaves counter-intuitively. When true, it'll lead to nftw()
returning an error and bailing out immediately - potentially leaving
more files behind than when not set.

Emulate the prior behavior by only using the rmdir_recursive() to change
control flow in _create_ns(). Only print an error in _delete_ns().
---
 .../job_container/tmpfs/job_container_tmpfs.c      | 62 ++++------------------
 1 file changed, 9 insertions(+), 53 deletions(-)

diff --git a/src/plugins/job_container/tmpfs/job_container_tmpfs.c b/src/plugins/job_container/tmpfs/job_container_tmpfs.c
index 88658aabaa..1e6d8c2366 100644
--- a/src/plugins/job_container/tmpfs/job_container_tmpfs.c
+++ b/src/plugins/job_container/tmpfs/job_container_tmpfs.c
@@ -39,7 +39,6 @@
 \*****************************************************************************/
 
 #define _GNU_SOURCE
-#define _XOPEN_SOURCE 500 /* For ftw.h */
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -47,12 +46,12 @@
 #include <sys/mman.h>
 #include <sched.h>
 #include <fcntl.h>
-#include <ftw.h>
 #include <sys/mount.h>
 #include <linux/limits.h>
 #include <semaphore.h>
 
 #include "src/common/slurm_xlator.h"
+#include "src/common/fd.h"
 #include "src/common/log.h"
 #include "src/common/run_command.h"
 
@@ -73,7 +72,6 @@ const uint32_t plugin_version   = SLURM_VERSION_NUMBER;
 
 static slurm_jc_conf_t *jc_conf = NULL;
 static int step_ns_fd = -1;
-static bool force_rm = true;
 static List running_job_ids = NULL;
 
 static int _create_paths(uint32_t job_id,
@@ -395,35 +393,6 @@ static int _mount_private_shm(void)
 	return rc;
 }
 
-static int _rm_data(const char *path, const struct stat *st_buf,
-		    int type, struct FTW *ftwbuf)
-{
-	int rc = SLURM_SUCCESS;
-
-	if (remove(path) < 0) {
-		log_level_t log_lvl;
-		if (force_rm) {
-			rc = SLURM_ERROR;
-			log_lvl = LOG_LEVEL_ERROR;
-		} else
-			log_lvl = LOG_LEVEL_DEBUG2;
-
-		if (type == FTW_NS)
-			log_var(log_lvl,
-					"%s: Unreachable file of FTW_NS type: %s",
-					__func__, path);
-		else if (type == FTW_DNR)
-			log_var(log_lvl,
-					"%s: Unreadable directory: %s",
-					__func__, path);
-
-		log_var(log_lvl,
-				"%s: could not remove path: %s: %s",
-				__func__, path, strerror(errno));
-	}
-
-	return rc;
-}
 
 static int _create_ns(uint32_t job_id, bool remount)
 {
@@ -653,11 +622,11 @@ exit1:
 
 exit2:
 	if (rc) {
+		int failures;
 		/* cleanup the job mount */
-		force_rm = true;
-		if (nftw(job_mount, _rm_data, 64, FTW_DEPTH|FTW_PHYS) < 0) {
-			error("%s: Directory traversal failed: %s: %s",
-			      __func__, job_mount, strerror(errno));
+		if ((failures = rmdir_recursive(job_mount, false))) {
+			error("%s: failed to remove %d files from %s",
+			      __func__, failures, job_mount);
 			return SLURM_ERROR;
 		}
 
@@ -782,7 +751,7 @@ static int _delete_ns(uint32_t job_id)
 {
 	char job_mount[PATH_MAX];
 	char ns_holder[PATH_MAX];
-	int rc = 0;
+	int rc = 0, failures = 0;
 
 #ifdef HAVE_NATIVE_CRAY
 	return SLURM_SUCCESS;
@@ -801,22 +770,9 @@ static int _delete_ns(uint32_t job_id)
 		return SLURM_ERROR;
 	}
 
-	/*
-	 * Traverses the job directory, and delete all files.
-	 * Doesn't -
-	 *	traverse filesystem boundaries,
-	 *	follow symbolic links
-	 * Does -
-	 *	a post order traversal and delete directory after processing
-	 *      contents
-	 * NOTE: Can happen EBUSY here so we need to ignore this.
-	 */
-	force_rm = false;
-	if (nftw(job_mount, _rm_data, 64, FTW_DEPTH|FTW_PHYS) < 0) {
-		error("%s: Directory traversal failed: %s: %s",
-		      __func__, job_mount, strerror(errno));
-		return SLURM_ERROR;
-	}
+	if ((failures = rmdir_recursive(job_mount, false)))
+		error("%s: failed to remove %d files from %s",
+		      __func__, failures, job_mount);
 
 	return SLURM_SUCCESS;
 }
openSUSE Build Service is sponsored by