codekingpro/portable-devtools
115k
1/*-------------------------------------------------------------------------2 *3 * fd.h4 * Virtual file descriptor definitions.5 *6 *7 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group8 * Portions Copyright (c) 1994, Regents of the University of California9 *10 * src/include/storage/fd.h11 *12 *-------------------------------------------------------------------------13 */14 15/*16 * calls:17 *18 * File {Close, Read, Write, Size, Sync}19 * {Path Name Open, Allocate, Free} File20 *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*); then31 * use FreeFile, not fclose, to close it. AVOID using stdio for files32 * that you intend to hold open for any length of time, since there is33 * no way for them to share kernel file descriptors with other files.34 *35 * Likewise, use AllocateDir/FreeDir, not opendir/closedir, to allocate36 * open directories (DIR*), and OpenTransientFile/CloseTransientFile for an37 * unbuffered file descriptor.38 *39 * If you really can't use any of the above, at least call AcquireExternalFD40 * or ReserveExternalFD to report any file descriptors that are held for any41 * length of time. Failure to do so risks unnecessary EMFILE errors.42 */43#ifndef FD_H44#define FD_H45 46#include <dirent.h>47#include <fcntl.h>48 49typedef enum RecoveryInitSyncMethod50{51 RECOVERY_INIT_SYNC_METHOD_FSYNC,52 RECOVERY_INIT_SYNC_METHOD_SYNCFS53} RecoveryInitSyncMethod;54 55typedef int File;56 57 58#define IO_DIRECT_DATA 0x0159#define IO_DIRECT_WAL 0x0260#define IO_DIRECT_WAL_INIT 0x0461 62 63/* GUC parameter */64extern PGDLLIMPORT int max_files_per_process;65extern PGDLLIMPORT bool data_sync_retry;66extern PGDLLIMPORT int recovery_init_sync_method;67extern PGDLLIMPORT int io_direct_flags;68 69/*70 * This is private to fd.c, but exported for save/restore_backend_variables()71 */72extern PGDLLIMPORT int max_safe_fds;73 74/*75 * On Windows, we have to interpret EACCES as possibly meaning the same as76 * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,77 * that's what you get. Ugh. This code is designed so that we don't78 * actually believe these cases are okay without further evidence (namely,79 * a pending fsync request getting canceled ... see ProcessSyncRequests).80 */81#ifndef WIN3282#define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)83#else84#define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)85#endif86 87/*88 * O_DIRECT is not standard, but almost every Unix has it. We translate it89 * to the appropriate Windows flag in src/port/open.c. We simulate it with90 * fcntl(F_NOCACHE) on macOS inside fd.c's open() wrapper. We use the name91 * PG_O_DIRECT rather than defining O_DIRECT in that case (probably not a good92 * idea on a Unix). We can only use it if the compiler will correctly align93 * PGIOAlignedBlock for us, though.94 */95#if defined(O_DIRECT) && defined(pg_attribute_aligned)96#define PG_O_DIRECT O_DIRECT97#elif defined(F_NOCACHE)98#define PG_O_DIRECT 0x8000000099#define PG_O_DIRECT_USE_F_NOCACHE100#else101#define PG_O_DIRECT 0102#endif103 104/*105 * prototypes for functions in fd.c106 */107 108/* Operations on virtual Files --- equivalent to Unix kernel file ops */109extern File PathNameOpenFile(const char *fileName, int fileFlags);110extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);111extern File OpenTemporaryFile(bool interXact);112extern void FileClose(File file);113extern int FilePrefetch(File file, off_t offset, off_t amount, uint32 wait_event_info);114extern int FileRead(File file, void *buffer, size_t amount, off_t offset, uint32 wait_event_info);115extern int FileWrite(File file, const void *buffer, size_t amount, off_t offset, uint32 wait_event_info);116extern int FileSync(File file, uint32 wait_event_info);117extern int FileZero(File file, off_t offset, off_t amount, uint32 wait_event_info);118extern int FileFallocate(File file, off_t offset, off_t amount, uint32 wait_event_info);119 120extern off_t FileSize(File file);121extern int FileTruncate(File file, off_t offset, uint32 wait_event_info);122extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info);123extern char *FilePathName(File file);124extern int FileGetRawDesc(File file);125extern int FileGetRawFlags(File file);126extern mode_t FileGetRawMode(File file);127 128/* Operations used for sharing named temporary files */129extern File PathNameCreateTemporaryFile(const char *path, bool error_on_failure);130extern File PathNameOpenTemporaryFile(const char *path, int mode);131extern bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure);132extern void PathNameCreateTemporaryDir(const char *basedir, const char *directory);133extern void PathNameDeleteTemporaryDir(const char *dirname);134extern void TempTablespacePath(char *path, Oid tablespace);135 136/* Operations that allow use of regular stdio --- USE WITH CAUTION */137extern FILE *AllocateFile(const char *name, const char *mode);138extern int FreeFile(FILE *file);139 140/* Operations that allow use of pipe streams (popen/pclose) */141extern FILE *OpenPipeStream(const char *command, const char *mode);142extern int ClosePipeStream(FILE *file);143 144/* Operations to allow use of the <dirent.h> library routines */145extern DIR *AllocateDir(const char *dirname);146extern struct dirent *ReadDir(DIR *dir, const char *dirname);147extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,148 int elevel);149extern int FreeDir(DIR *dir);150 151/* Operations to allow use of a plain kernel FD, with automatic cleanup */152extern int OpenTransientFile(const char *fileName, int fileFlags);153extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);154extern int CloseTransientFile(int fd);155 156/* If you've really really gotta have a plain kernel FD, use this */157extern int BasicOpenFile(const char *fileName, int fileFlags);158extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);159 160/* Use these for other cases, and also for long-lived BasicOpenFile FDs */161extern bool AcquireExternalFD(void);162extern void ReserveExternalFD(void);163extern void ReleaseExternalFD(void);164 165/* Make a directory with default permissions */166extern int MakePGDirectory(const char *directoryName);167 168/* Miscellaneous support routines */169extern void InitFileAccess(void);170extern void InitTemporaryFileAccess(void);171extern void set_max_safe_fds(void);172extern void closeAllVfds(void);173extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);174extern bool TempTablespacesAreSet(void);175extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces);176extern Oid GetNextTempTableSpace(void);177extern void AtEOXact_Files(bool isCommit);178extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,179 SubTransactionId parentSubid);180extern void RemovePgTempFiles(void);181extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,182 bool unlink_all);183extern bool looks_like_temp_rel_name(const char *name);184 185extern int pg_fsync(int fd);186extern int pg_fsync_no_writethrough(int fd);187extern int pg_fsync_writethrough(int fd);188extern int pg_fdatasync(int fd);189extern void pg_flush_data(int fd, off_t offset, off_t nbytes);190extern int pg_truncate(const char *path, off_t length);191extern void fsync_fname(const char *fname, bool isdir);192extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);193extern int durable_rename(const char *oldfile, const char *newfile, int elevel);194extern int durable_unlink(const char *fname, int elevel);195extern void SyncDataDirectory(void);196extern int data_sync_elevel(int elevel);197 198/* Filename components */199#define PG_TEMP_FILES_DIR "pgsql_tmp"200#define PG_TEMP_FILE_PREFIX "pgsql_tmp"201 202#endif /* FD_H */203 