PostgreSQL Source Code  git master
file_utils.h File Reference
#include <dirent.h>
Include dependency graph for file_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PG_TEMP_FILES_DIR   "pgsql_tmp"
 
#define PG_TEMP_FILE_PREFIX   "pgsql_tmp"
 

Typedefs

typedef enum PGFileType PGFileType
 
typedef enum DataDirSyncMethod DataDirSyncMethod
 

Enumerations

enum  PGFileType {
  PGFILETYPE_ERROR , PGFILETYPE_UNKNOWN , PGFILETYPE_REG , PGFILETYPE_DIR ,
  PGFILETYPE_LNK
}
 
enum  DataDirSyncMethod { DATA_DIR_SYNC_METHOD_FSYNC , DATA_DIR_SYNC_METHOD_SYNCFS }
 

Functions

PGFileType get_dirent_type (const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
 
int compute_remaining_iovec (struct iovec *destination, const struct iovec *source, int iovcnt, size_t transferred)
 
ssize_t pg_pwritev_with_retry (int fd, const struct iovec *iov, int iovcnt, off_t offset)
 
ssize_t pg_pwrite_zeros (int fd, size_t size, off_t offset)
 

Macro Definition Documentation

◆ PG_TEMP_FILE_PREFIX

#define PG_TEMP_FILE_PREFIX   "pgsql_tmp"

Definition at line 63 of file file_utils.h.

◆ PG_TEMP_FILES_DIR

#define PG_TEMP_FILES_DIR   "pgsql_tmp"

Definition at line 62 of file file_utils.h.

Typedef Documentation

◆ DataDirSyncMethod

◆ PGFileType

typedef enum PGFileType PGFileType

Enumeration Type Documentation

◆ DataDirSyncMethod

Enumerator
DATA_DIR_SYNC_METHOD_FSYNC 
DATA_DIR_SYNC_METHOD_SYNCFS 

Definition at line 27 of file file_utils.h.

28 {
DataDirSyncMethod
Definition: file_utils.h:28
@ DATA_DIR_SYNC_METHOD_SYNCFS
Definition: file_utils.h:30
@ DATA_DIR_SYNC_METHOD_FSYNC
Definition: file_utils.h:29

◆ PGFileType

enum PGFileType
Enumerator
PGFILETYPE_ERROR 
PGFILETYPE_UNKNOWN 
PGFILETYPE_REG 
PGFILETYPE_DIR 
PGFILETYPE_LNK 

Definition at line 18 of file file_utils.h.

19 {
25 } PGFileType;
PGFileType
Definition: file_utils.h:19
@ PGFILETYPE_LNK
Definition: file_utils.h:24
@ PGFILETYPE_UNKNOWN
Definition: file_utils.h:21
@ PGFILETYPE_DIR
Definition: file_utils.h:23
@ PGFILETYPE_REG
Definition: file_utils.h:22
@ PGFILETYPE_ERROR
Definition: file_utils.h:20

Function Documentation

◆ compute_remaining_iovec()

int compute_remaining_iovec ( struct iovec *  destination,
const struct iovec *  source,
int  iovcnt,
size_t  transferred 
)

Definition at line 592 of file file_utils.c.

596 {
597  Assert(iovcnt > 0);
598 
599  /* Skip wholly transferred iovecs. */
600  while (source->iov_len <= transferred)
601  {
602  transferred -= source->iov_len;
603  source++;
604  iovcnt--;
605 
606  /* All iovecs transferred? */
607  if (iovcnt == 0)
608  {
609  /*
610  * We don't expect the kernel to transfer more than we asked it
611  * to, or something is out of sync.
612  */
613  Assert(transferred == 0);
614  return 0;
615  }
616  }
617 
618  /* Copy the remaining iovecs to the front of the array. */
619  if (source != destination)
620  memmove(destination, source, sizeof(*source) * iovcnt);
621 
622  /* Adjust leading iovec, which may have been partially transferred. */
623  Assert(destination->iov_len > transferred);
624  destination->iov_base = (char *) destination->iov_base + transferred;
625  destination->iov_len -= transferred;
626 
627  return iovcnt;
628 }
#define Assert(condition)
Definition: c.h:858
static rewind_source * source
Definition: pg_rewind.c:89

References Assert, and source.

Referenced by mdreadv(), mdwritev(), and pg_pwritev_with_retry().

◆ get_dirent_type()

PGFileType get_dirent_type ( const char *  path,
const struct dirent de,
bool  look_through_symlinks,
int  elevel 
)

Definition at line 525 of file file_utils.c.

529 {
530  PGFileType result;
531 
532  /*
533  * Some systems tell us the type directly in the dirent struct, but that's
534  * a BSD and Linux extension not required by POSIX. Even when the
535  * interface is present, sometimes the type is unknown, depending on the
536  * filesystem.
537  */
538 #if defined(DT_REG) && defined(DT_DIR) && defined(DT_LNK)
539  if (de->d_type == DT_REG)
540  result = PGFILETYPE_REG;
541  else if (de->d_type == DT_DIR)
542  result = PGFILETYPE_DIR;
543  else if (de->d_type == DT_LNK && !look_through_symlinks)
544  result = PGFILETYPE_LNK;
545  else
546  result = PGFILETYPE_UNKNOWN;
547 #else
548  result = PGFILETYPE_UNKNOWN;
549 #endif
550 
551  if (result == PGFILETYPE_UNKNOWN)
552  {
553  struct stat fst;
554  int sret;
555 
556 
557  if (look_through_symlinks)
558  sret = stat(path, &fst);
559  else
560  sret = lstat(path, &fst);
561 
562  if (sret < 0)
563  {
564  result = PGFILETYPE_ERROR;
565 #ifdef FRONTEND
566  pg_log_generic(elevel, PG_LOG_PRIMARY, "could not stat file \"%s\": %m", path);
567 #else
568  ereport(elevel,
570  errmsg("could not stat file \"%s\": %m", path)));
571 #endif
572  }
573  else if (S_ISREG(fst.st_mode))
574  result = PGFILETYPE_REG;
575  else if (S_ISDIR(fst.st_mode))
576  result = PGFILETYPE_DIR;
577  else if (S_ISLNK(fst.st_mode))
578  result = PGFILETYPE_LNK;
579  }
580 
581  return result;
582 }
#define DT_DIR
Definition: dirent.h:28
#define DT_REG
Definition: dirent.h:30
#define DT_LNK
Definition: dirent.h:31
int errcode_for_file_access(void)
Definition: elog.c:880
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
void pg_log_generic(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt,...)
Definition: logging.c:205
@ PG_LOG_PRIMARY
Definition: logging.h:67
unsigned char d_type
Definition: dirent.h:13
#define stat
Definition: win32_port.h:284
#define lstat(path, sb)
Definition: win32_port.h:285
#define S_ISDIR(m)
Definition: win32_port.h:325
#define S_ISLNK(m)
Definition: win32_port.h:344
#define S_ISREG(m)
Definition: win32_port.h:328

References dirent::d_type, DT_DIR, DT_LNK, DT_REG, ereport, errcode_for_file_access(), errmsg(), lstat, pg_log_generic(), PG_LOG_PRIMARY, PGFILETYPE_DIR, PGFILETYPE_ERROR, PGFILETYPE_LNK, PGFILETYPE_REG, PGFILETYPE_UNKNOWN, S_ISDIR, S_ISLNK, S_ISREG, stat::st_mode, and stat.

Referenced by CheckPointLogicalRewriteHeap(), CheckPointSnapBuild(), CheckTablespaceDirectory(), copydir(), do_pg_backup_start(), GetConfFilesInDir(), pg_tzenumerate_next(), process_directory_recursively(), RemovePgTempFilesInDir(), RemoveXlogFile(), rmtree(), scan_for_existing_tablespaces(), StartupReplicationSlots(), and walkdir().

◆ pg_pwrite_zeros()

ssize_t pg_pwrite_zeros ( int  fd,
size_t  size,
off_t  offset 
)

Definition at line 687 of file file_utils.c.

688 {
689  static const PGIOAlignedBlock zbuffer = {{0}}; /* worth BLCKSZ */
690  void *zerobuf_addr = unconstify(PGIOAlignedBlock *, &zbuffer)->data;
691  struct iovec iov[PG_IOV_MAX];
692  size_t remaining_size = size;
693  ssize_t total_written = 0;
694 
695  /* Loop, writing as many blocks as we can for each system call. */
696  while (remaining_size > 0)
697  {
698  int iovcnt = 0;
699  ssize_t written;
700 
701  for (; iovcnt < PG_IOV_MAX && remaining_size > 0; iovcnt++)
702  {
703  size_t this_iov_size;
704 
705  iov[iovcnt].iov_base = zerobuf_addr;
706 
707  if (remaining_size < BLCKSZ)
708  this_iov_size = remaining_size;
709  else
710  this_iov_size = BLCKSZ;
711 
712  iov[iovcnt].iov_len = this_iov_size;
713  remaining_size -= this_iov_size;
714  }
715 
716  written = pg_pwritev_with_retry(fd, iov, iovcnt, offset);
717 
718  if (written < 0)
719  return written;
720 
721  offset += written;
722  total_written += written;
723  }
724 
725  Assert(total_written == size);
726 
727  return total_written;
728 }
#define unconstify(underlying_type, expr)
Definition: c.h:1245
ssize_t pg_pwritev_with_retry(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: file_utils.c:637
#define PG_IOV_MAX
Definition: pg_iovec.h:37
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static pg_noinline void Size size
Definition: slab.c:607

References Assert, fd(), PG_IOV_MAX, pg_pwritev_with_retry(), size, and unconstify.

Referenced by dir_open_for_write(), FileZero(), and XLogFileInitInternal().

◆ pg_pwritev_with_retry()

ssize_t pg_pwritev_with_retry ( int  fd,
const struct iovec *  iov,
int  iovcnt,
off_t  offset 
)

Definition at line 637 of file file_utils.c.

638 {
639  struct iovec iov_copy[PG_IOV_MAX];
640  ssize_t sum = 0;
641  ssize_t part;
642 
643  /* We'd better have space to make a copy, in case we need to retry. */
644  if (iovcnt > PG_IOV_MAX)
645  {
646  errno = EINVAL;
647  return -1;
648  }
649 
650  do
651  {
652  /* Write as much as we can. */
653  part = pg_pwritev(fd, iov, iovcnt, offset);
654  if (part < 0)
655  return -1;
656 
657 #ifdef SIMULATE_SHORT_WRITE
658  part = Min(part, 4096);
659 #endif
660 
661  /* Count our progress. */
662  sum += part;
663  offset += part;
664 
665  /*
666  * See what is left. On the first loop we used the caller's array,
667  * but in later loops we'll use our local copy that we are allowed to
668  * mutate.
669  */
670  iovcnt = compute_remaining_iovec(iov_copy, iov, iovcnt, part);
671  iov = iov_copy;
672  } while (iovcnt > 0);
673 
674  return sum;
675 }
#define Min(x, y)
Definition: c.h:1004
int compute_remaining_iovec(struct iovec *destination, const struct iovec *source, int iovcnt, size_t transferred)
Definition: file_utils.c:592
static ssize_t pg_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: pg_iovec.h:83

References compute_remaining_iovec(), fd(), Min, PG_IOV_MAX, and pg_pwritev().

Referenced by pg_pwrite_zeros().