Avoid losing errno if readdir() fails and closedir() works. This also
avoids leaking the directory handle when readdir() fails. Commit
6f03927fce introduced logic to better
handle readdir() and closedir() failures, bu it missed these cases.
Extracted from a larger patch by Marco Nenciarini.
Clear errno before calling readdir() and handle old MinGW errno bug
while adding full test coverage for readdir/closedir failures.
Backpatch through 8.4.
mkdir_p and check_data_dir will be useful in CREATE TABLESPACE, since we
have agreed that that command should handle subdirectory creation just like
initdb creates the PGDATA directory. Push them into src/port/ so that they
are available to both initdb and the backend. Rename to pg_mkdir_p and
pg_check_dir, just to be on the safe side. Add FreeBSD's copyright notice
to pgmkdirp.c, since that's where the code came from originally (this
really should have been in initdb.c). Very marginal code/comment cleanup.