Refactor rmtree() to use get_dirent_type().
authorThomas Munro <[email protected]>
Tue, 31 Jan 2023 00:07:44 +0000 (13:07 +1300)
committerThomas Munro <[email protected]>
Tue, 31 Jan 2023 00:46:25 +0000 (13:46 +1300)
commit54e72b66ed1a55c2fa558bc60d534bdc61dbb62f
treeb88739207db2516a365c0fa64cd753cec1df8596
parent3bef56e11650a33f70adeb6dd442bc2b48bb9b72
Refactor rmtree() to use get_dirent_type().

Switch to get_dirent_type() instead of lstat() while traversing a
directory tree, to see if that fixes the intermittent ENOTEMPTY failures
seen in recent pg_upgrade tests, on Windows CI.  While refactoring, also
use AllocateDir() instead of opendir() in the backend, which knows how
to handle descriptor pressure.

Our CI system currently uses Windows Server 2019, a version known not to
have POSIX unlink semantics enabled by default yet, unlike typical
Windows 10 and 11 systems.  That might explain why we see this flapping
on CI but (apparently) not in the build farm, though the frequency is
quite low.

The theory is that some directory entry must be in state
STATUS_DELETE_PENDING, which lstat() would report as ENOENT, though
unfortunately we don't know exactly why yet.  With this change, rmtree()
will not skip them, and try to unlink (again).  Our unlink() wrapper
should either wait a short time for them to go away when some other
process closes the handle, or log a message to tell us the path of the
problem file if not, so we can dig further.

Discussion: https://postgr.es/m/20220919213217.ptqfdlcc5idk5xup%40awork3.anarazel.de
src/common/rmtree.c