1996-08-28 03:59:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* fd.h
|
1997-09-07 07:04:48 +02:00
|
|
|
* Virtual file descriptor definitions.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*
|
2013-01-01 23:15:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/include/storage/fd.h
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-10-13 17:02:32 +02:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
|
|
|
* calls:
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2004-05-31 05:48:10 +02:00
|
|
|
* File {Close, Read, Write, Seek, Tell, Sync}
|
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.
2012-11-27 09:25:50 +01:00
|
|
|
* {Path Name Open, Allocate, Free} File
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* These are NOT JUST RENAMINGS OF THE UNIX ROUTINES.
|
1999-05-09 02:52:08 +02:00
|
|
|
* Use them for all file activity...
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* File fd;
|
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.
2012-11-27 09:25:50 +01:00
|
|
|
* fd = PathNameOpenFile("foo", O_RDONLY, 0600);
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* AllocateFile();
|
|
|
|
* FreeFile();
|
1999-05-09 02:52:08 +02:00
|
|
|
*
|
|
|
|
* Use AllocateFile, not fopen, if you need a stdio file (FILE*); then
|
|
|
|
* use FreeFile, not fclose, to close it. AVOID using stdio for files
|
|
|
|
* that you intend to hold open for any length of time, since there is
|
|
|
|
* no way for them to share kernel file descriptors with other files.
|
2004-02-24 00:03:10 +01:00
|
|
|
*
|
|
|
|
* Likewise, use AllocateDir/FreeDir, not opendir/closedir, to allocate
|
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.
2012-11-27 09:25:50 +01:00
|
|
|
* open directories (DIR*), and OpenTransientFile/CloseTransient File for an
|
|
|
|
* unbuffered file descriptor.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifndef FD_H
|
1996-08-28 03:59:28 +02:00
|
|
|
#define FD_H
|
|
|
|
|
2004-02-24 00:03:10 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
|
|
|
* FileSeek uses the standard UNIX lseek(2) flags.
|
|
|
|
*/
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
typedef char *FileName;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
typedef int File;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2001-09-30 20:57:45 +02:00
|
|
|
|
|
|
|
/* GUC parameter */
|
2001-10-25 07:50:21 +02:00
|
|
|
extern int max_files_per_process;
|
2001-09-30 20:57:45 +02:00
|
|
|
|
2012-03-29 07:19:11 +02:00
|
|
|
/*
|
|
|
|
* This is private to fd.c, but exported for save/restore_backend_variables()
|
|
|
|
*/
|
|
|
|
extern int max_safe_fds;
|
|
|
|
|
2001-09-30 20:57:45 +02:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
|
|
|
* prototypes for functions in fd.c
|
|
|
|
*/
|
1999-05-09 02:52:08 +02:00
|
|
|
|
|
|
|
/* Operations on virtual Files --- equivalent to Unix kernel file ops */
|
1997-09-08 04:41:22 +02:00
|
|
|
extern File PathNameOpenFile(FileName fileName, int fileFlags, int fileMode);
|
2007-06-07 21:19:57 +02:00
|
|
|
extern File OpenTemporaryFile(bool interXact);
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void FileClose(File file);
|
2009-01-12 06:10:45 +01:00
|
|
|
extern int FilePrefetch(File file, off_t offset, int amount);
|
1997-09-08 04:41:22 +02:00
|
|
|
extern int FileRead(File file, char *buffer, int amount);
|
|
|
|
extern int FileWrite(File file, char *buffer, int amount);
|
2004-05-31 05:48:10 +02:00
|
|
|
extern int FileSync(File file);
|
2008-03-10 21:06:27 +01:00
|
|
|
extern off_t FileSeek(File file, off_t offset, int whence);
|
|
|
|
extern int FileTruncate(File file, off_t offset);
|
2009-08-05 20:01:54 +02:00
|
|
|
extern char *FilePathName(File file);
|
1999-05-09 02:52:08 +02:00
|
|
|
|
|
|
|
/* Operations that allow use of regular stdio --- USE WITH CAUTION */
|
2006-03-04 22:32:47 +01:00
|
|
|
extern FILE *AllocateFile(const char *name, const char *mode);
|
2004-02-24 00:03:10 +01:00
|
|
|
extern int FreeFile(FILE *file);
|
|
|
|
|
|
|
|
/* Operations to allow use of the <dirent.h> library routines */
|
|
|
|
extern DIR *AllocateDir(const char *dirname);
|
2005-06-19 23:34:03 +02:00
|
|
|
extern struct dirent *ReadDir(DIR *dir, const char *dirname);
|
2004-02-24 00:03:10 +01:00
|
|
|
extern int FreeDir(DIR *dir);
|
1999-05-09 02:52:08 +02:00
|
|
|
|
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.
2012-11-27 09:25:50 +01:00
|
|
|
/* 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);
|
|
|
|
|
2000-06-02 05:58:34 +02:00
|
|
|
/* If you've really really gotta have a plain kernel FD, use this */
|
|
|
|
extern int BasicOpenFile(FileName fileName, int fileFlags, int fileMode);
|
|
|
|
|
1999-05-09 02:52:08 +02:00
|
|
|
/* Miscellaneous support routines */
|
2005-08-08 05:12:16 +02:00
|
|
|
extern void InitFileAccess(void);
|
2004-02-23 21:45:59 +01:00
|
|
|
extern void set_max_safe_fds(void);
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void closeAllVfds(void);
|
2007-06-07 21:19:57 +02:00
|
|
|
extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
|
|
|
|
extern bool TempTablespacesAreSet(void);
|
|
|
|
extern Oid GetNextTempTableSpace(void);
|
1999-05-09 02:52:08 +02:00
|
|
|
extern void AtEOXact_Files(void);
|
2004-09-16 18:58:44 +02:00
|
|
|
extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
|
2005-10-15 04:49:52 +02:00
|
|
|
SubTransactionId parentSubid);
|
2001-06-11 06:12:29 +02:00
|
|
|
extern void RemovePgTempFiles(void);
|
2007-06-07 21:19:57 +02:00
|
|
|
|
2000-12-08 23:21:33 +01:00
|
|
|
extern int pg_fsync(int fd);
|
2005-05-20 16:53:26 +02:00
|
|
|
extern int pg_fsync_no_writethrough(int fd);
|
|
|
|
extern int pg_fsync_writethrough(int fd);
|
2001-02-18 05:39:42 +01:00
|
|
|
extern int pg_fdatasync(int fd);
|
2010-02-26 03:01:40 +01:00
|
|
|
extern int pg_flush_data(int fd, off_t offset, off_t amount);
|
2001-10-28 07:26:15 +01:00
|
|
|
|
2003-12-20 18:31:21 +01:00
|
|
|
/* Filename components for OpenTemporaryFile */
|
|
|
|
#define PG_TEMP_FILES_DIR "pgsql_tmp"
|
|
|
|
#define PG_TEMP_FILE_PREFIX "pgsql_tmp"
|
|
|
|
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* FD_H */
|