From dd4e538d75bc68c573485cab48247c2c9a36a35e Mon Sep 17 00:00:00 2001
From: Daria Lepikhova <daria.n.lepikhova@gmail.com>
Date: Mon, 21 Sep 2026 16:43:07 +0200
Subject: [PATCH v1] Check for readdir() failures in pg_verifybackup and
 pg_combinebackup

pg_verifybackup and pg_combinebackup cleared errno before each readdir()
call but never checked it after the loop, so a failure partway through a
scan was indistinguishable from the end of the directory.  pg_verifybackup
reported every entry the scan never reached as missing from the backup;
pg_combinebackup wrote out the truncated directory and exited normally.

In pg_verifybackup, also add the directory to the ignore list, as is
already done when opendir() fails, so the unread entries aren't reported
missing.
---
 src/bin/pg_combinebackup/pg_combinebackup.c |  6 ++++++
 src/bin/pg_verifybackup/pg_verifybackup.c   | 13 +++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/src/bin/pg_combinebackup/pg_combinebackup.c b/src/bin/pg_combinebackup/pg_combinebackup.c
index 254a27b125b..41d6c28be07 100644
--- a/src/bin/pg_combinebackup/pg_combinebackup.c
+++ b/src/bin/pg_combinebackup/pg_combinebackup.c
@@ -1170,6 +1170,9 @@ process_directory_recursively(Oid tsoid,
 			pg_free(checksum_payload);
 	}
 
+	if (errno)
+		pg_fatal("could not read directory \"%s\": %m", ifulldir);
+
 	closedir(dir);
 }
 
@@ -1335,6 +1338,9 @@ scan_for_existing_tablespaces(char *pathname, cb_options *opt)
 		tslist = ts;
 	}
 
+	if (errno)
+		pg_fatal("could not read directory \"%s\": %m", pg_tblspc);
+
 	if (closedir(dir) != 0)
 		pg_fatal("could not close directory \"%s\": %m", pg_tblspc);
 
diff --git a/src/bin/pg_verifybackup/pg_verifybackup.c b/src/bin/pg_verifybackup/pg_verifybackup.c
index 81694144b46..9a4544ac8b4 100644
--- a/src/bin/pg_verifybackup/pg_verifybackup.c
+++ b/src/bin/pg_verifybackup/pg_verifybackup.c
@@ -670,6 +670,14 @@ verify_plain_backup_directory(verifier_context *context, char *relpath,
 		pfree(newrelpath);
 	}
 
+	if (errno)
+	{
+		report_backup_error(context,
+							"could not read directory \"%s\": %m", fullpath);
+		if (relpath != NULL)
+			simple_string_list_append(&context->ignore_list, relpath);
+	}
+
 	if (closedir(dir))
 	{
 		report_backup_error(context,
@@ -842,6 +850,11 @@ verify_tar_backup(verifier_context *context, DIR *dir, char **base_archive_path,
 		}
 	}
 
+	if (errno)
+		report_backup_error(context,
+							"could not read directory \"%s\": %m",
+							context->backup_directory);
+
 	if (closedir(dir))
 	{
 		report_backup_error(context,
-- 
2.50.1 (Apple Git-155)

