diff --git a/src/bin/pg_basebackup/walmethods.c b/src/bin/pg_basebackup/walmethods.c
index f74bd13..35cf5a8 100644
--- a/src/bin/pg_basebackup/walmethods.c
+++ b/src/bin/pg_basebackup/walmethods.c
@@ -580,7 +580,10 @@ dir_existsfile(const char *pathname)
 
 	fd = open(tmppath, O_RDONLY | PG_BINARY, 0);
 	if (fd < 0)
+	{
+		dir_data->lasterrno = errno;
 		return false;
+	}
 	close(fd);
 	return true;
 }
