1 /*------------------------------------------------------------------------- 2 * 3 * fd.h 4 * Virtual file descriptor definitions. 5 * 6 * 7 * Portions Copyright (c) 1996-2021, 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 typedef enum RecoveryInitSyncMethod 49 { 50 RECOVERY_INIT_SYNC_METHOD_FSYNC, 51 RECOVERY_INIT_SYNC_METHOD_SYNCFS 52 } RecoveryInitSyncMethod; 53 54 struct iovec; /* avoid including port/pg_iovec.h here */ 55 56 typedef int File; 57 58 59 /* GUC parameter */ 60 extern PGDLLIMPORT int max_files_per_process; 61 extern PGDLLIMPORT bool data_sync_retry; 62 extern int recovery_init_sync_method; 63 64 /* 65 * This is private to fd.c, but exported for save/restore_backend_variables() 66 */ 67 extern int max_safe_fds; 68 69 /* 70 * On Windows, we have to interpret EACCES as possibly meaning the same as 71 * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform, 72 * that's what you get. Ugh. This code is designed so that we don't 73 * actually believe these cases are okay without further evidence (namely, 74 * a pending fsync request getting canceled ... see ProcessSyncRequests). 75 */ 76 #ifndef WIN32 77 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT) 78 #else 79 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES) 80 #endif 81 82 /* 83 * prototypes for functions in fd.c 84 */ 85 86 /* Operations on virtual Files --- equivalent to Unix kernel file ops */ 87 extern File PathNameOpenFile(const char *fileName, int fileFlags); 88 extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode); 89 extern File OpenTemporaryFile(bool interXact); 90 extern void FileClose(File file); 91 extern int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info); 92 extern int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info); 93 extern int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info); 94 extern int FileSync(File file, uint32 wait_event_info); 95 extern off_t FileSize(File file); 96 extern int FileTruncate(File file, off_t offset, uint32 wait_event_info); 97 extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info); 98 extern char *FilePathName(File file); 99 extern int FileGetRawDesc(File file); 100 extern int FileGetRawFlags(File file); 101 extern mode_t FileGetRawMode(File file); 102 103 /* Operations used for sharing named temporary files */ 104 extern File PathNameCreateTemporaryFile(const char *name, bool error_on_failure); 105 extern File PathNameOpenTemporaryFile(const char *path, int mode); 106 extern bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure); 107 extern void PathNameCreateTemporaryDir(const char *base, const char *name); 108 extern void PathNameDeleteTemporaryDir(const char *name); 109 extern void TempTablespacePath(char *path, Oid tablespace); 110 111 /* Operations that allow use of regular stdio --- USE WITH CAUTION */ 112 extern FILE *AllocateFile(const char *name, const char *mode); 113 extern int FreeFile(FILE *file); 114 115 /* Operations that allow use of pipe streams (popen/pclose) */ 116 extern FILE *OpenPipeStream(const char *command, const char *mode); 117 extern int ClosePipeStream(FILE *file); 118 119 /* Operations to allow use of the <dirent.h> library routines */ 120 extern DIR *AllocateDir(const char *dirname); 121 extern struct dirent *ReadDir(DIR *dir, const char *dirname); 122 extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname, 123 int elevel); 124 extern int FreeDir(DIR *dir); 125 126 /* Operations to allow use of a plain kernel FD, with automatic cleanup */ 127 extern int OpenTransientFile(const char *fileName, int fileFlags); 128 extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode); 129 extern int CloseTransientFile(int fd); 130 131 /* If you've really really gotta have a plain kernel FD, use this */ 132 extern int BasicOpenFile(const char *fileName, int fileFlags); 133 extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode); 134 135 /* Use these for other cases, and also for long-lived BasicOpenFile FDs */ 136 extern bool AcquireExternalFD(void); 137 extern void ReserveExternalFD(void); 138 extern void ReleaseExternalFD(void); 139 140 /* Make a directory with default permissions */ 141 extern int MakePGDirectory(const char *directoryName); 142 143 /* Miscellaneous support routines */ 144 extern void InitFileAccess(void); 145 extern void set_max_safe_fds(void); 146 extern void closeAllVfds(void); 147 extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces); 148 extern bool TempTablespacesAreSet(void); 149 extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces); 150 extern Oid GetNextTempTableSpace(void); 151 extern void AtEOXact_Files(bool isCommit); 152 extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, 153 SubTransactionId parentSubid); 154 extern void RemovePgTempFiles(void); 155 extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, 156 bool unlink_all); 157 extern bool looks_like_temp_rel_name(const char *name); 158 159 extern int pg_fsync(int fd); 160 extern int pg_fsync_no_writethrough(int fd); 161 extern int pg_fsync_writethrough(int fd); 162 extern int pg_fdatasync(int fd); 163 extern void pg_flush_data(int fd, off_t offset, off_t amount); 164 extern ssize_t pg_pwritev_with_retry(int fd, 165 const struct iovec *iov, 166 int iovcnt, 167 off_t offset); 168 extern int pg_truncate(const char *path, off_t length); 169 extern void fsync_fname(const char *fname, bool isdir); 170 extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel); 171 extern int durable_rename(const char *oldfile, const char *newfile, int loglevel); 172 extern int durable_unlink(const char *fname, int loglevel); 173 extern int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel); 174 extern void SyncDataDirectory(void); 175 extern int data_sync_elevel(int elevel); 176 177 /* Filename components */ 178 #define PG_TEMP_FILES_DIR "pgsql_tmp" 179 #define PG_TEMP_FILE_PREFIX "pgsql_tmp" 180 181 #endif /* FD_H */ 182