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-2018, 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, Seek, Tell, 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/CloseTransient File for an
37  * unbuffered file descriptor.
38  */
39 #ifndef FD_H
40 #define FD_H
41 
42 #include <dirent.h>
43 
44 
45 /*
46  * FileSeek uses the standard UNIX lseek(2) flags.
47  */
48 
49 typedef int File;
50 
51 
52 /* GUC parameter */
54 
55 /*
56  * This is private to fd.c, but exported for save/restore_backend_variables()
57  */
58 extern int max_safe_fds;
59 
60 
61 /*
62  * prototypes for functions in fd.c
63  */
64 
65 /* Operations on virtual Files --- equivalent to Unix kernel file ops */
66 extern File PathNameOpenFile(const char *fileName, int fileFlags);
67 extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
68 extern File OpenTemporaryFile(bool interXact);
69 extern void FileClose(File file);
70 extern int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info);
71 extern int FileRead(File file, char *buffer, int amount, uint32 wait_event_info);
72 extern int FileWrite(File file, char *buffer, int amount, uint32 wait_event_info);
73 extern int FileSync(File file, uint32 wait_event_info);
74 extern off_t FileSeek(File file, off_t offset, int whence);
75 extern int FileTruncate(File file, off_t offset, uint32 wait_event_info);
76 extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info);
77 extern char *FilePathName(File file);
78 extern int FileGetRawDesc(File file);
79 extern int FileGetRawFlags(File file);
80 extern mode_t FileGetRawMode(File file);
81 extern off_t FileGetSize(File file);
82 
83 /* Operations used for sharing named temporary files */
84 extern File PathNameCreateTemporaryFile(const char *name, bool error_on_failure);
85 extern File PathNameOpenTemporaryFile(const char *name);
86 extern bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure);
87 extern void PathNameCreateTemporaryDir(const char *base, const char *name);
88 extern void PathNameDeleteTemporaryDir(const char *name);
89 extern void TempTablespacePath(char *path, Oid tablespace);
90 
91 /* Operations that allow use of regular stdio --- USE WITH CAUTION */
92 extern FILE *AllocateFile(const char *name, const char *mode);
93 extern int FreeFile(FILE *file);
94 
95 /* Operations that allow use of pipe streams (popen/pclose) */
96 extern FILE *OpenPipeStream(const char *command, const char *mode);
97 extern int ClosePipeStream(FILE *file);
98 
99 /* Operations to allow use of the <dirent.h> library routines */
100 extern DIR *AllocateDir(const char *dirname);
101 extern struct dirent *ReadDir(DIR *dir, const char *dirname);
102 extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
103  int elevel);
104 extern int FreeDir(DIR *dir);
105 
106 /* Operations to allow use of a plain kernel FD, with automatic cleanup */
107 extern int OpenTransientFile(const char *fileName, int fileFlags);
108 extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
109 extern int CloseTransientFile(int fd);
110 
111 /* If you've really really gotta have a plain kernel FD, use this */
112 extern int BasicOpenFile(const char *fileName, int fileFlags);
113 extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
114 
115  /* Make a directory with default permissions */
116 extern int MakePGDirectory(const char *directoryName);
117 
118 /* Miscellaneous support routines */
119 extern void InitFileAccess(void);
120 extern void set_max_safe_fds(void);
121 extern void closeAllVfds(void);
122 extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
123 extern bool TempTablespacesAreSet(void);
124 extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces);
125 extern Oid GetNextTempTableSpace(void);
126 extern void AtEOXact_Files(void);
127 extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
128  SubTransactionId parentSubid);
129 extern void RemovePgTempFiles(void);
130 extern bool looks_like_temp_rel_name(const char *name);
131 
132 extern int pg_fsync(int fd);
133 extern int pg_fsync_no_writethrough(int fd);
134 extern int pg_fsync_writethrough(int fd);
135 extern int pg_fdatasync(int fd);
136 extern void pg_flush_data(int fd, off_t offset, off_t amount);
137 extern void fsync_fname(const char *fname, bool isdir);
138 extern int durable_rename(const char *oldfile, const char *newfile, int loglevel);
139 extern int durable_unlink(const char *fname, int loglevel);
140 extern int durable_link_or_rename(const char *oldfile, const char *newfile, int loglevel);
141 extern void SyncDataDirectory(void);
142 
143 /* Filename components */
144 #define PG_TEMP_FILES_DIR "pgsql_tmp"
145 #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
146 
147 #endif /* FD_H */
void PathNameCreateTemporaryDir(const char *base, const char *name)
Definition: fd.c:1429
int FreeDir(DIR *dir)
Definition: fd.c:2718
int durable_unlink(const char *fname, int loglevel)
Definition: fd.c:684
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2843
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1351
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2882
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3571
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2600
void FileClose(File file)
Definition: fd.c:1742
int max_safe_fds
Definition: fd.c:146
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
Definition: fd.c:1860
File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1364
int FileRead(File file, char *buffer, int amount, uint32 wait_event_info)
Definition: fd.c:1887
off_t FileGetSize(File file)
Definition: fd.c:2262
int CloseTransientFile(int fd)
Definition: fd.c:2566
bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1696
int pg_fsync_writethrough(int fd)
Definition: fd.c:370
int FreeFile(FILE *file)
Definition: fd.c:2538
uint32 SubTransactionId
Definition: c.h:478
void PathNameDeleteTemporaryDir(const char *name)
Definition: fd.c:1460
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:2681
File PathNameOpenTemporaryFile(const char *name)
Definition: fd.c:1666
#define PGDLLIMPORT
Definition: c.h:1105
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1493
off_t FileSeek(File file, off_t offset, int whence)
Definition: fd.c:2093
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2396
Definition: dirent.c:25
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2445
int durable_rename(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:601
int ClosePipeStream(FILE *file)
Definition: fd.c:2748
char * tablespace
Definition: pgbench.c:155
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2666
bool TempTablespacesAreSet(void)
Definition: fd.c:2831
char * FilePathName(File file)
Definition: fd.c:2216
unsigned int uint32
Definition: c.h:325
Oid GetNextTempTableSpace(void)
Definition: fd.c:2861
void pg_flush_data(int fd, off_t offset, off_t amount)
Definition: fd.c:415
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2346
void SyncDataDirectory(void)
Definition: fd.c:3256
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2072
static int elevel
Definition: vacuumlazy.c:144
void closeAllVfds(void)
Definition: fd.c:2777
int FileGetRawFlags(File file)
Definition: fd.c:2242
File PathNameCreateTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1628
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:358
void AtEOXact_Files(void)
Definition: fd.c:2913
int FileGetRawDesc(File file)
Definition: fd.c:2232
bool looks_like_temp_rel_name(const char *name)
Definition: fd.c:3189
int durable_link_or_rename(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:720
void set_max_safe_fds(void)
Definition: fd.c:889
int pg_fdatasync(int fd)
Definition: fd.c:393
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2181
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
void InitFileAccess(void)
Definition: fd.c:772
int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info)
Definition: fd.c:1832
int pg_fsync(int fd)
Definition: fd.c:341
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:575
void RemovePgTempFiles(void)
Definition: fd.c:3007
void SetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2803
const char * name
Definition: encode.c:521
mode_t FileGetRawMode(File file)
Definition: fd.c:2252
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:954
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1546
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:932
int FileWrite(File file, char *buffer, int amount, uint32 wait_event_info)
Definition: fd.c:1951
PGDLLIMPORT int max_files_per_process
Definition: fd.c:133
int File
Definition: fd.h:49
int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:2405