PostgreSQL Source Code  git master
fd.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * fd.h
4  * Virtual file descriptor definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/fd.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 /*
16  * calls:
17  *
18  * File {Close, Read, Write, Size, Sync}
19  * {Path Name Open, Allocate, Free} File
20  *
21  * These are NOT JUST RENAMINGS OF THE UNIX ROUTINES.
22  * Use them for all file activity...
23  *
24  * File fd;
25  * fd = PathNameOpenFile("foo", O_RDONLY);
26  *
27  * AllocateFile();
28  * FreeFile();
29  *
30  * Use AllocateFile, not fopen, if you need a stdio file (FILE*); then
31  * use FreeFile, not fclose, to close it. AVOID using stdio for files
32  * that you intend to hold open for any length of time, since there is
33  * no way for them to share kernel file descriptors with other files.
34  *
35  * Likewise, use AllocateDir/FreeDir, not opendir/closedir, to allocate
36  * open directories (DIR*), and OpenTransientFile/CloseTransientFile for an
37  * unbuffered file descriptor.
38  *
39  * If you really can't use any of the above, at least call AcquireExternalFD
40  * or ReserveExternalFD to report any file descriptors that are held for any
41  * length of time. Failure to do so risks unnecessary EMFILE errors.
42  */
43 #ifndef FD_H
44 #define FD_H
45 
46 #include <dirent.h>
47 
48 
49 typedef int File;
50 
51 
52 /* GUC parameter */
54 extern PGDLLIMPORT bool data_sync_retry;
55 
56 /*
57  * This is private to fd.c, but exported for save/restore_backend_variables()
58  */
59 extern int max_safe_fds;
60 
61 /*
62  * On Windows, we have to interpret EACCES as possibly meaning the same as
63  * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
64  * that's what you get. Ugh. This code is designed so that we don't
65  * actually believe these cases are okay without further evidence (namely,
66  * a pending fsync request getting canceled ... see ProcessSyncRequests).
67  */
68 #ifndef WIN32
69 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)
70 #else
71 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)
72 #endif
73 
74 /*
75  * prototypes for functions in fd.c
76  */
77 
78 /* Operations on virtual Files --- equivalent to Unix kernel file ops */
79 extern File PathNameOpenFile(const char *fileName, int fileFlags);
80 extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
81 extern File OpenTemporaryFile(bool interXact);
82 extern void FileClose(File file);
83 extern int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info);
84 extern int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
85 extern int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
86 extern int FileSync(File file, uint32 wait_event_info);
87 extern off_t FileSize(File file);
88 extern int FileTruncate(File file, off_t offset, uint32 wait_event_info);
89 extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info);
90 extern char *FilePathName(File file);
91 extern int FileGetRawDesc(File file);
92 extern int FileGetRawFlags(File file);
93 extern mode_t FileGetRawMode(File file);
94 
95 /* Operations used for sharing named temporary files */
96 extern File PathNameCreateTemporaryFile(const char *name, bool error_on_failure);
97 extern File PathNameOpenTemporaryFile(const char *path, int mode);
98 extern bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure);
99 extern void PathNameCreateTemporaryDir(const char *base, const char *name);
100 extern void PathNameDeleteTemporaryDir(const char *name);
101 extern void TempTablespacePath(char *path, Oid tablespace);
102 
103 /* Operations that allow use of regular stdio --- USE WITH CAUTION */
104 extern FILE *AllocateFile(const char *name, const char *mode);
105 extern int FreeFile(FILE *file);
106 
107 /* Operations that allow use of pipe streams (popen/pclose) */
108 extern FILE *OpenPipeStream(const char *command, const char *mode);
109 extern int ClosePipeStream(FILE *file);
110 
111 /* Operations to allow use of the <dirent.h> library routines */
112 extern DIR *AllocateDir(const char *dirname);
113 extern struct dirent *ReadDir(DIR *dir, const char *dirname);
114 extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
115  int elevel);
116 extern int FreeDir(DIR *dir);
117 
118 /* Operations to allow use of a plain kernel FD, with automatic cleanup */
119 extern int OpenTransientFile(const char *fileName, int fileFlags);
120 extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
121 extern int CloseTransientFile(int fd);
122 
123 /* If you've really really gotta have a plain kernel FD, use this */
124 extern int BasicOpenFile(const char *fileName, int fileFlags);
125 extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
126 
127 /* Use these for other cases, and also for long-lived BasicOpenFile FDs */
128 extern bool AcquireExternalFD(void);
129 extern void ReserveExternalFD(void);
130 extern void ReleaseExternalFD(void);
131 
132 /* Make a directory with default permissions */
133 extern int MakePGDirectory(const char *directoryName);
134 
135 /* Miscellaneous support routines */
136 extern void InitFileAccess(void);
137 extern void set_max_safe_fds(void);
138 extern void closeAllVfds(void);
139 extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
140 extern bool TempTablespacesAreSet(void);
141 extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces);
142 extern Oid GetNextTempTableSpace(void);
143 extern void AtEOXact_Files(bool isCommit);
144 extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
145  SubTransactionId parentSubid);
146 extern void RemovePgTempFiles(void);
147 extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
148  bool unlink_all);
149 extern bool looks_like_temp_rel_name(const char *name);
150 
151 extern int pg_fsync(int fd);
152 extern int pg_fsync_no_writethrough(int fd);
153 extern int pg_fsync_writethrough(int fd);
154 extern int pg_fdatasync(int fd);
155 extern void pg_flush_data(int fd, off_t offset, off_t amount);
156 extern int pg_truncate(const char *path, off_t length);
157 extern void fsync_fname(const char *fname, bool isdir);
158 extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
159 extern int durable_rename(const char *oldfile, const char *newfile, int loglevel);
160 extern int durable_unlink(const char *fname, int loglevel);
161 extern int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel);
162 extern void SyncDataDirectory(void);
163 extern int data_sync_elevel(int elevel);
164 
165 /* Filename components */
166 #define PG_TEMP_FILES_DIR "pgsql_tmp"
167 #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
168 
169 #endif /* FD_H */
void PathNameCreateTemporaryDir(const char *base, const char *name)
Definition: fd.c:1541
int FreeDir(DIR *dir)
Definition: fd.c:2728
int durable_unlink(const char *fname, int loglevel)
Definition: fd.c:776
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2859
static PgChecksumMode mode
Definition: pg_checksums.c:61
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1464
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2898
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3491
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition: fd.c:3092
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3607
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2610
void FileClose(File file)
Definition: fd.c:1853
int max_safe_fds
Definition: fd.c:155
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
Definition: fd.c:1977
File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1477
int CloseTransientFile(int fd)
Definition: fd.c:2576
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2931
bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1807
int pg_fsync_writethrough(int fd)
Definition: fd.c:413
int FreeFile(FILE *file)
Definition: fd.c:2548
uint32 SubTransactionId
Definition: c.h:579
void PathNameDeleteTemporaryDir(const char *name)
Definition: fd.c:1572
unsigned int Oid
Definition: postgres_ext.h:31
Definition: dirent.h:9
static int fd(const char *x, int i)
Definition: preproc-init.c:105
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
Definition: fd.c:2691
#define PGDLLIMPORT
Definition: c.h:1313
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1605
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2399
Definition: dirent.c:25
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2452
bool AcquireExternalFD(void)
Definition: fd.c:1075
int durable_rename(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:686
int ClosePipeStream(FILE *file)
Definition: fd.c:2758
void ReserveExternalFD(void)
Definition: fd.c:1110
char * tablespace
Definition: pgbench.c:189
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2676
bool TempTablespacesAreSet(void)
Definition: fd.c:2844
char * FilePathName(File file)
Definition: fd.c:2227
unsigned int uint32
Definition: c.h:429
Oid GetNextTempTableSpace(void)
Definition: fd.c:2877
void pg_flush_data(int fd, off_t offset, off_t amount)
Definition: fd.c:456
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2349
void SyncDataDirectory(void)
Definition: fd.c:3284
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2154
off_t FileSize(File file)
Definition: fd.c:2175
static int elevel
Definition: vacuumlazy.c:333
void ReleaseExternalFD(void)
Definition: fd.c:1128
void closeAllVfds(void)
Definition: fd.c:2787
int FileGetRawFlags(File file)
Definition: fd.c:2253
File PathNameOpenTemporaryFile(const char *path, int mode)
Definition: fd.c:1778
File PathNameCreateTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1740
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:401
int FileGetRawDesc(File file)
Definition: fd.c:2243
bool looks_like_temp_rel_name(const char *name)
Definition: fd.c:3214
int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:813
int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2056
void set_max_safe_fds(void)
Definition: fd.c:970
int pg_fdatasync(int fd)
Definition: fd.c:436
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2192
void InitFileAccess(void)
Definition: fd.c:853
int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info)
Definition: fd.c:1949
int pg_fsync(int fd)
Definition: fd.c:346
int data_sync_elevel(int elevel)
Definition: fd.c:3630
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:660
void RemovePgTempFiles(void)
Definition: fd.c:3033
void SetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2816
const char * name
Definition: encode.c:561
mode_t FileGetRawMode(File file)
Definition: fd.c:2263
int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2000
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1035
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1658
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1013
int pg_truncate(const char *path, off_t length)
Definition: fd.c:629
PGDLLIMPORT bool data_sync_retry
Definition: fd.c:158
PGDLLIMPORT int max_files_per_process
Definition: fd.c:142
int File
Definition: fd.h:49
int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:2408