LCOV - differential code coverage report
Current view: top level - src/include/storage - fd.h (source / functions) Coverage Total Hit CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 100.0 % 7 7 7
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 2 2 2
Baseline: lcov-20250906-005545-baseline Line coverage date bins:
Baseline Date: 2025-09-05 08:21:35 +0100 (360..) days: 100.0 % 7 7 7
Legend: Lines:     hit not hit Function coverage date bins:
(360..) days: 100.0 % 2 2 2

 Age         Owner                  TLA  Line data    Source code
                                  1                 : /*-------------------------------------------------------------------------
                                  2                 :  *
                                  3                 :  * fd.h
                                  4                 :  *    Virtual file descriptor definitions.
                                  5                 :  *
                                  6                 :  *
                                  7                 :  * Portions Copyright (c) 1996-2025, 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, ReadV, Write, WriteV, 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 "port/pg_iovec.h"
                                 47                 : 
                                 48                 : #include <dirent.h>
                                 49                 : #include <fcntl.h>
                                 50                 : 
                                 51                 : typedef int File;
                                 52                 : 
                                 53                 : 
                                 54                 : #define IO_DIRECT_DATA          0x01
                                 55                 : #define IO_DIRECT_WAL           0x02
                                 56                 : #define IO_DIRECT_WAL_INIT      0x04
                                 57                 : 
                                 58                 : 
                                 59                 : /* GUC parameter */
                                 60                 : extern PGDLLIMPORT int max_files_per_process;
                                 61                 : extern PGDLLIMPORT bool data_sync_retry;
                                 62                 : extern PGDLLIMPORT int recovery_init_sync_method;
                                 63                 : extern PGDLLIMPORT int io_direct_flags;
                                 64                 : 
                                 65                 : /*
                                 66                 :  * This is private to fd.c, but exported for save/restore_backend_variables()
                                 67                 :  */
                                 68                 : extern PGDLLIMPORT int max_safe_fds;
                                 69                 : 
                                 70                 : /*
                                 71                 :  * On Windows, we have to interpret EACCES as possibly meaning the same as
                                 72                 :  * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
                                 73                 :  * that's what you get.  Ugh.  This code is designed so that we don't
                                 74                 :  * actually believe these cases are okay without further evidence (namely,
                                 75                 :  * a pending fsync request getting canceled ... see ProcessSyncRequests).
                                 76                 :  */
                                 77                 : #ifndef WIN32
                                 78                 : #define FILE_POSSIBLY_DELETED(err)  ((err) == ENOENT)
                                 79                 : #else
                                 80                 : #define FILE_POSSIBLY_DELETED(err)  ((err) == ENOENT || (err) == EACCES)
                                 81                 : #endif
                                 82                 : 
                                 83                 : /*
                                 84                 :  * O_DIRECT is not standard, but almost every Unix has it.  We translate it
                                 85                 :  * to the appropriate Windows flag in src/port/open.c.  We simulate it with
                                 86                 :  * fcntl(F_NOCACHE) on macOS inside fd.c's open() wrapper.  We use the name
                                 87                 :  * PG_O_DIRECT rather than defining O_DIRECT in that case (probably not a good
                                 88                 :  * idea on a Unix).  We can only use it if the compiler will correctly align
                                 89                 :  * PGIOAlignedBlock for us, though.
                                 90                 :  */
                                 91                 : #if defined(O_DIRECT) && defined(pg_attribute_aligned)
                                 92                 : #define     PG_O_DIRECT O_DIRECT
                                 93                 : #elif defined(F_NOCACHE)
                                 94                 : #define     PG_O_DIRECT 0x80000000
                                 95                 : #define     PG_O_DIRECT_USE_F_NOCACHE
                                 96                 : #else
                                 97                 : #define     PG_O_DIRECT 0
                                 98                 : #endif
                                 99                 : 
                                100                 : /*
                                101                 :  * prototypes for functions in fd.c
                                102                 :  */
                                103                 : 
                                104                 : struct PgAioHandle;
                                105                 : 
                                106                 : /* Operations on virtual Files --- equivalent to Unix kernel file ops */
                                107                 : extern File PathNameOpenFile(const char *fileName, int fileFlags);
                                108                 : extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
                                109                 : extern File OpenTemporaryFile(bool interXact);
                                110                 : extern void FileClose(File file);
                                111                 : extern int  FilePrefetch(File file, off_t offset, off_t amount, uint32 wait_event_info);
                                112                 : extern ssize_t FileReadV(File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info);
                                113                 : extern ssize_t FileWriteV(File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info);
                                114                 : extern int  FileStartReadV(struct PgAioHandle *ioh, File file, int iovcnt, off_t offset, uint32 wait_event_info);
                                115                 : extern int  FileSync(File file, uint32 wait_event_info);
                                116                 : extern int  FileZero(File file, off_t offset, off_t amount, uint32 wait_event_info);
                                117                 : extern int  FileFallocate(File file, off_t offset, off_t amount, uint32 wait_event_info);
                                118                 : 
                                119                 : extern off_t FileSize(File file);
                                120                 : extern int  FileTruncate(File file, off_t offset, uint32 wait_event_info);
                                121                 : extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info);
                                122                 : extern char *FilePathName(File file);
                                123                 : extern int  FileGetRawDesc(File file);
                                124                 : extern int  FileGetRawFlags(File file);
                                125                 : extern mode_t FileGetRawMode(File file);
                                126                 : 
                                127                 : /* Operations used for sharing named temporary files */
                                128                 : extern File PathNameCreateTemporaryFile(const char *path, bool error_on_failure);
                                129                 : extern File PathNameOpenTemporaryFile(const char *path, int mode);
                                130                 : extern bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure);
                                131                 : extern void PathNameCreateTemporaryDir(const char *basedir, const char *directory);
                                132                 : extern void PathNameDeleteTemporaryDir(const char *dirname);
                                133                 : extern void TempTablespacePath(char *path, Oid tablespace);
                                134                 : 
                                135                 : /* Operations that allow use of regular stdio --- USE WITH CAUTION */
                                136                 : extern FILE *AllocateFile(const char *name, const char *mode);
                                137                 : extern int  FreeFile(FILE *file);
                                138                 : 
                                139                 : /* Operations that allow use of pipe streams (popen/pclose) */
                                140                 : extern FILE *OpenPipeStream(const char *command, const char *mode);
                                141                 : extern int  ClosePipeStream(FILE *file);
                                142                 : 
                                143                 : /* Operations to allow use of the <dirent.h> library routines */
                                144                 : extern DIR *AllocateDir(const char *dirname);
                                145                 : extern struct dirent *ReadDir(DIR *dir, const char *dirname);
                                146                 : extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
                                147                 :                                       int elevel);
                                148                 : extern int  FreeDir(DIR *dir);
                                149                 : 
                                150                 : /* Operations to allow use of a plain kernel FD, with automatic cleanup */
                                151                 : extern int  OpenTransientFile(const char *fileName, int fileFlags);
                                152                 : extern int  OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
                                153                 : extern int  CloseTransientFile(int fd);
                                154                 : 
                                155                 : /* If you've really really gotta have a plain kernel FD, use this */
                                156                 : extern int  BasicOpenFile(const char *fileName, int fileFlags);
                                157                 : extern int  BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
                                158                 : 
                                159                 : /* Use these for other cases, and also for long-lived BasicOpenFile FDs */
                                160                 : extern bool AcquireExternalFD(void);
                                161                 : extern void ReserveExternalFD(void);
                                162                 : extern void ReleaseExternalFD(void);
                                163                 : 
                                164                 : /* Make a directory with default permissions */
                                165                 : extern int  MakePGDirectory(const char *directoryName);
                                166                 : 
                                167                 : /* Miscellaneous support routines */
                                168                 : extern void InitFileAccess(void);
                                169                 : extern void InitTemporaryFileAccess(void);
                                170                 : extern void set_max_safe_fds(void);
                                171                 : extern void closeAllVfds(void);
                                172                 : extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
                                173                 : extern bool TempTablespacesAreSet(void);
                                174                 : extern int  GetTempTablespaces(Oid *tableSpaces, int numSpaces);
                                175                 : extern Oid  GetNextTempTableSpace(void);
                                176                 : extern void AtEOXact_Files(bool isCommit);
                                177                 : extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
                                178                 :                               SubTransactionId parentSubid);
                                179                 : extern void RemovePgTempFiles(void);
                                180                 : extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
                                181                 :                                    bool unlink_all);
                                182                 : extern bool looks_like_temp_rel_name(const char *name);
                                183                 : 
                                184                 : extern int  pg_fsync(int fd);
                                185                 : extern int  pg_fsync_no_writethrough(int fd);
                                186                 : extern int  pg_fsync_writethrough(int fd);
                                187                 : extern int  pg_fdatasync(int fd);
                                188                 : extern bool pg_file_exists(const char *name);
                                189                 : extern void pg_flush_data(int fd, off_t offset, off_t nbytes);
                                190                 : extern int  pg_truncate(const char *path, off_t length);
                                191                 : extern void fsync_fname(const char *fname, bool isdir);
                                192                 : extern int  fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
                                193                 : extern int  durable_rename(const char *oldfile, const char *newfile, int elevel);
                                194                 : extern int  durable_unlink(const char *fname, int elevel);
                                195                 : extern void SyncDataDirectory(void);
                                196                 : extern int  data_sync_elevel(int elevel);
                                197                 : 
                                198                 : static inline ssize_t
  634 tmunro@postgresql.or      199 CBC      398234 : FileRead(File file, void *buffer, size_t amount, off_t offset,
                                200                 :          uint32 wait_event_info)
                                201                 : {
                                202          398234 :     struct iovec iov = {
                                203                 :         .iov_base = buffer,
                                204                 :         .iov_len = amount
                                205                 :     };
                                206                 : 
                                207          398234 :     return FileReadV(file, &iov, 1, offset, wait_event_info);
                                208                 : }
                                209                 : 
                                210                 : static inline ssize_t
                                211          185079 : FileWrite(File file, const void *buffer, size_t amount, off_t offset,
                                212                 :           uint32 wait_event_info)
                                213                 : {
                                214          185079 :     struct iovec iov = {
                                215          185079 :         .iov_base = unconstify(void *, buffer),
                                216                 :         .iov_len = amount
                                217                 :     };
                                218                 : 
                                219          185079 :     return FileWriteV(file, &iov, 1, offset, wait_event_info);
                                220                 : }
                                221                 : 
                                222                 : #endif                          /* FD_H */
        

Generated by: LCOV version 2.4-beta