LCOV - code coverage report
Current view: top level - src/include/storage - fd.h (source / functions) Coverage Total Hit
Test: Code coverage Lines: 100.0 % 12 12
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 2 2
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * fd.h
       4              :  *        Virtual file descriptor definitions.
       5              :  *
       6              :  *
       7              :  * Portions Copyright (c) 1996-2026, 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).
      89              :  */
      90              : #if defined(O_DIRECT)
      91              : #define         PG_O_DIRECT O_DIRECT
      92              : #elif defined(F_NOCACHE)
      93              : #define         PG_O_DIRECT 0x80000000
      94              : #define         PG_O_DIRECT_USE_F_NOCACHE
      95              : /*
      96              :  * The value we defined to stand in for O_DIRECT when simulating it with
      97              :  * F_NOCACHE had better not collide with any of the standard flags.
      98              :  */
      99              : StaticAssertDecl((PG_O_DIRECT &
     100              :                                   (O_APPEND |
     101              :                                    O_CLOEXEC |
     102              :                                    O_CREAT |
     103              :                                    O_DSYNC |
     104              :                                    O_EXCL |
     105              :                                    O_RDWR |
     106              :                                    O_RDONLY |
     107              :                                    O_SYNC |
     108              :                                    O_TRUNC |
     109              :                                    O_WRONLY)) == 0,
     110              :                                  "PG_O_DIRECT value collides with standard flag");
     111              : #else
     112              : #define         PG_O_DIRECT 0
     113              : #endif
     114              : 
     115              : /*
     116              :  * prototypes for functions in fd.c
     117              :  */
     118              : 
     119              : struct PgAioHandle;
     120              : 
     121              : /* Operations on virtual Files --- equivalent to Unix kernel file ops */
     122              : extern File PathNameOpenFile(const char *fileName, int fileFlags);
     123              : extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
     124              : extern File OpenTemporaryFile(bool interXact);
     125              : extern void FileClose(File file);
     126              : extern int      FilePrefetch(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info);
     127              : extern ssize_t FileReadV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info);
     128              : extern ssize_t FileWriteV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info);
     129              : extern int      FileStartReadV(struct PgAioHandle *ioh, File file, int iovcnt, pgoff_t offset, uint32 wait_event_info);
     130              : extern int      FileSync(File file, uint32 wait_event_info);
     131              : extern int      FileZero(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info);
     132              : extern int      FileFallocate(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info);
     133              : 
     134              : extern pgoff_t FileSize(File file);
     135              : extern int      FileTruncate(File file, pgoff_t offset, uint32 wait_event_info);
     136              : extern void FileWriteback(File file, pgoff_t offset, pgoff_t nbytes, uint32 wait_event_info);
     137              : extern char *FilePathName(File file);
     138              : extern int      FileGetRawDesc(File file);
     139              : extern int      FileGetRawFlags(File file);
     140              : extern mode_t FileGetRawMode(File file);
     141              : 
     142              : /* Operations used for sharing named temporary files */
     143              : extern File PathNameCreateTemporaryFile(const char *path, bool error_on_failure);
     144              : extern File PathNameOpenTemporaryFile(const char *path, int mode);
     145              : extern bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure);
     146              : extern void PathNameCreateTemporaryDir(const char *basedir, const char *directory);
     147              : extern void PathNameDeleteTemporaryDir(const char *dirname);
     148              : extern void TempTablespacePath(char *path, Oid tablespace);
     149              : 
     150              : /* Operations that allow use of regular stdio --- USE WITH CAUTION */
     151              : extern FILE *AllocateFile(const char *name, const char *mode);
     152              : extern int      FreeFile(FILE *file);
     153              : 
     154              : /* Operations that allow use of pipe streams (popen/pclose) */
     155              : extern FILE *OpenPipeStream(const char *command, const char *mode);
     156              : extern int      ClosePipeStream(FILE *file);
     157              : 
     158              : /* Operations to allow use of the <dirent.h> library routines */
     159              : extern DIR *AllocateDir(const char *dirname);
     160              : extern struct dirent *ReadDir(DIR *dir, const char *dirname);
     161              : extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
     162              :                                                                           int elevel);
     163              : extern int      FreeDir(DIR *dir);
     164              : 
     165              : /* Operations to allow use of a plain kernel FD, with automatic cleanup */
     166              : extern int      OpenTransientFile(const char *fileName, int fileFlags);
     167              : extern int      OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
     168              : extern int      CloseTransientFile(int fd);
     169              : 
     170              : /* If you've really really gotta have a plain kernel FD, use this */
     171              : extern int      BasicOpenFile(const char *fileName, int fileFlags);
     172              : extern int      BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
     173              : 
     174              : /* Use these for other cases, and also for long-lived BasicOpenFile FDs */
     175              : extern bool AcquireExternalFD(void);
     176              : extern void ReserveExternalFD(void);
     177              : extern void ReleaseExternalFD(void);
     178              : 
     179              : /* Make a directory with default permissions */
     180              : extern int      MakePGDirectory(const char *directoryName);
     181              : 
     182              : /* Miscellaneous support routines */
     183              : extern void InitFileAccess(void);
     184              : extern void InitTemporaryFileAccess(void);
     185              : extern void set_max_safe_fds(void);
     186              : extern void closeAllVfds(void);
     187              : extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
     188              : extern bool TempTablespacesAreSet(void);
     189              : extern int      GetTempTablespaces(Oid *tableSpaces, int numSpaces);
     190              : extern Oid      GetNextTempTableSpace(void);
     191              : extern void AtEOXact_Files(bool isCommit);
     192              : extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
     193              :                                                           SubTransactionId parentSubid);
     194              : extern void RemovePgTempFiles(void);
     195              : extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
     196              :                                                                    bool unlink_all);
     197              : extern bool looks_like_temp_rel_name(const char *name);
     198              : 
     199              : extern int      pg_fsync(int fd);
     200              : extern int      pg_fsync_no_writethrough(int fd);
     201              : extern int      pg_fsync_writethrough(int fd);
     202              : extern int      pg_fdatasync(int fd);
     203              : extern bool pg_file_exists(const char *name);
     204              : extern void pg_flush_data(int fd, pgoff_t offset, pgoff_t nbytes);
     205              : extern int      pg_truncate(const char *path, pgoff_t length);
     206              : extern void fsync_fname(const char *fname, bool isdir);
     207              : extern int      fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
     208              : extern int      durable_rename(const char *oldfile, const char *newfile, int elevel);
     209              : extern int      durable_unlink(const char *fname, int elevel);
     210              : extern void SyncDataDirectory(void);
     211              : extern int      data_sync_elevel(int elevel);
     212              : 
     213              : static inline ssize_t
     214        34558 : FileRead(File file, void *buffer, size_t amount, pgoff_t offset,
     215              :                  uint32 wait_event_info)
     216              : {
     217       103674 :         struct iovec iov = {
     218        34558 :                 .iov_base = buffer,
     219        34558 :                 .iov_len = amount
     220              :         };
     221              : 
     222        69116 :         return FileReadV(file, &iov, 1, offset, wait_event_info);
     223        34558 : }
     224              : 
     225              : static inline ssize_t
     226        45923 : FileWrite(File file, const void *buffer, size_t amount, pgoff_t offset,
     227              :                   uint32 wait_event_info)
     228              : {
     229       137769 :         struct iovec iov = {
     230        45923 :                 .iov_base = unconstify(void *, buffer),
     231        45923 :                 .iov_len = amount
     232              :         };
     233              : 
     234        91846 :         return FileWriteV(file, &iov, 1, offset, wait_event_info);
     235        45923 : }
     236              : 
     237              : #endif                                                  /* FD_H */
        

Generated by: LCOV version 2.3.2-1