diff options
author | Heikki Linnakangas | 2012-11-27 08:25:50 +0000 |
---|---|---|
committer | Heikki Linnakangas | 2012-11-27 08:25:50 +0000 |
commit | 1f67078ea324d492a366a24abb2ac313c629314f (patch) | |
tree | ba8f833b8d1054ec8f687484a744af2f533704a4 /src/include | |
parent | 532994299e2ff208a58376134fab75f5ae471e41 (diff) |
Add OpenTransientFile, with automatic cleanup at end-of-xact.
Files opened with BasicOpenFile or PathNameOpenFile are not automatically
cleaned up on error. That puts unnecessary burden on callers that only want
to keep the file open for a short time. There is AllocateFile, but that
returns a buffered FILE * stream, which in many cases is not the nicest API
to work with. So add function called OpenTransientFile, which returns a
unbuffered fd that's cleaned up like the FILE* returned by AllocateFile().
This plugs a few rare fd leaks in error cases:
1. copy_file() - fixed by by using OpenTransientFile instead of BasicOpenFile
2. XLogFileInit() - fixed by adding close() calls to the error cases. Can't
use OpenTransientFile here because the fd is supposed to persist over
transaction boundaries.
3. lo_import/lo_export - fixed by using OpenTransientFile instead of
PathNameOpenFile.
In addition to plugging those leaks, this replaces many BasicOpenFile() calls
with OpenTransientFile() that were not leaking, because the code meticulously
closed the file on error. That wasn't strictly necessary, but IMHO it's good
for robustness.
The same leaks exist in older versions, but given the rarity of the issues,
I'm not backpatching this. Not yet, anyway - it might be good to backpatch
later, after this mechanism has had some more testing in master branch.
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/storage/fd.h | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h index 849bb1025d9..940d9d47513 100644 --- a/src/include/storage/fd.h +++ b/src/include/storage/fd.h @@ -16,13 +16,13 @@ * calls: * * File {Close, Read, Write, Seek, Tell, Sync} - * {File Name Open, Allocate, Free} File + * {Path Name Open, Allocate, Free} File * * These are NOT JUST RENAMINGS OF THE UNIX ROUTINES. * Use them for all file activity... * * File fd; - * fd = FilePathOpenFile("foo", O_RDONLY, 0600); + * fd = PathNameOpenFile("foo", O_RDONLY, 0600); * * AllocateFile(); * FreeFile(); @@ -33,7 +33,8 @@ * no way for them to share kernel file descriptors with other files. * * Likewise, use AllocateDir/FreeDir, not opendir/closedir, to allocate - * open directories (DIR*). + * open directories (DIR*), and OpenTransientFile/CloseTransient File for an + * unbuffered file descriptor. */ #ifndef FD_H #define FD_H @@ -84,6 +85,10 @@ extern DIR *AllocateDir(const char *dirname); extern struct dirent *ReadDir(DIR *dir, const char *dirname); extern int FreeDir(DIR *dir); +/* Operations to allow use of a plain kernel FD, with automatic cleanup */ +extern int OpenTransientFile(FileName fileName, int fileFlags, int fileMode); +extern int CloseTransientFile(int fd); + /* If you've really really gotta have a plain kernel FD, use this */ extern int BasicOpenFile(FileName fileName, int fileFlags, int fileMode); |