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.

Typedefs

typedef enum PGFileType PGFileType
 

Enumerations

enum  PGFileType {
  PGFILETYPE_ERROR , PGFILETYPE_UNKNOWN , PGFILETYPE_REG , PGFILETYPE_DIR ,
  PGFILETYPE_LNK
}
 

Functions

PGFileType get_dirent_type (const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
 
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)
 

Typedef Documentation

◆ PGFileType

typedef enum PGFileType PGFileType

Enumeration Type Documentation

◆ 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

◆ get_dirent_type()

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

Definition at line 406 of file file_utils.c.

410 {
411  PGFileType result;
412 
413  /*
414  * Some systems tell us the type directly in the dirent struct, but that's
415  * a BSD and Linux extension not required by POSIX. Even when the
416  * interface is present, sometimes the type is unknown, depending on the
417  * filesystem.
418  */
419 #if defined(DT_REG) && defined(DT_DIR) && defined(DT_LNK)
420  if (de->d_type == DT_REG)
421  result = PGFILETYPE_REG;
422  else if (de->d_type == DT_DIR)
423  result = PGFILETYPE_DIR;
424  else if (de->d_type == DT_LNK && !look_through_symlinks)
425  result = PGFILETYPE_LNK;
426  else
427  result = PGFILETYPE_UNKNOWN;
428 #else
429  result = PGFILETYPE_UNKNOWN;
430 #endif
431 
432  if (result == PGFILETYPE_UNKNOWN)
433  {
434  struct stat fst;
435  int sret;
436 
437 
438  if (look_through_symlinks)
439  sret = stat(path, &fst);
440  else
441  sret = lstat(path, &fst);
442 
443  if (sret < 0)
444  {
445  result = PGFILETYPE_ERROR;
446 #ifdef FRONTEND
447  pg_log_generic(elevel, PG_LOG_PRIMARY, "could not stat file \"%s\": %m", path);
448 #else
449  ereport(elevel,
451  errmsg("could not stat file \"%s\": %m", path)));
452 #endif
453  }
454  else if (S_ISREG(fst.st_mode))
455  result = PGFILETYPE_REG;
456  else if (S_ISDIR(fst.st_mode))
457  result = PGFILETYPE_DIR;
458  else if (S_ISLNK(fst.st_mode))
459  result = PGFILETYPE_LNK;
460  }
461 
462  return result;
463 }
#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:881
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#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:292
#define lstat(path, sb)
Definition: win32_port.h:293
#define S_ISDIR(m)
Definition: win32_port.h:333
#define S_ISLNK(m)
Definition: win32_port.h:352
#define S_ISREG(m)
Definition: win32_port.h:336

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(), RemovePgTempFilesInDir(), RemoveXlogFile(), rmtree(), StartupReplicationSlots(), and walkdir().

◆ pg_pwrite_zeros()

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

Definition at line 541 of file file_utils.c.

542 {
543  static const PGIOAlignedBlock zbuffer = {{0}}; /* worth BLCKSZ */
544  void *zerobuf_addr = unconstify(PGIOAlignedBlock *, &zbuffer)->data;
545  struct iovec iov[PG_IOV_MAX];
546  size_t remaining_size = size;
547  ssize_t total_written = 0;
548 
549  /* Loop, writing as many blocks as we can for each system call. */
550  while (remaining_size > 0)
551  {
552  int iovcnt = 0;
553  ssize_t written;
554 
555  for (; iovcnt < PG_IOV_MAX && remaining_size > 0; iovcnt++)
556  {
557  size_t this_iov_size;
558 
559  iov[iovcnt].iov_base = zerobuf_addr;
560 
561  if (remaining_size < BLCKSZ)
562  this_iov_size = remaining_size;
563  else
564  this_iov_size = BLCKSZ;
565 
566  iov[iovcnt].iov_len = this_iov_size;
567  remaining_size -= this_iov_size;
568  }
569 
570  written = pg_pwritev_with_retry(fd, iov, iovcnt, offset);
571 
572  if (written < 0)
573  return written;
574 
575  offset += written;
576  total_written += written;
577  }
578 
579  Assert(total_written == size);
580 
581  return total_written;
582 }
#define unconstify(underlying_type, expr)
Definition: c.h:1250
ssize_t pg_pwritev_with_retry(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: file_utils.c:472
Assert(fmt[strlen(fmt) - 1] !='\n')
#define PG_IOV_MAX
Definition: pg_iovec.h:36
static int fd(const char *x, int i)
Definition: preproc-init.c:105

References Assert(), fd(), PG_IOV_MAX, pg_pwritev_with_retry(), 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 472 of file file_utils.c.

473 {
474  struct iovec iov_copy[PG_IOV_MAX];
475  ssize_t sum = 0;
476  ssize_t part;
477 
478  /* We'd better have space to make a copy, in case we need to retry. */
479  if (iovcnt > PG_IOV_MAX)
480  {
481  errno = EINVAL;
482  return -1;
483  }
484 
485  for (;;)
486  {
487  /* Write as much as we can. */
488  part = pg_pwritev(fd, iov, iovcnt, offset);
489  if (part < 0)
490  return -1;
491 
492 #ifdef SIMULATE_SHORT_WRITE
493  part = Min(part, 4096);
494 #endif
495 
496  /* Count our progress. */
497  sum += part;
498  offset += part;
499 
500  /* Step over iovecs that are done. */
501  while (iovcnt > 0 && iov->iov_len <= part)
502  {
503  part -= iov->iov_len;
504  ++iov;
505  --iovcnt;
506  }
507 
508  /* Are they all done? */
509  if (iovcnt == 0)
510  {
511  /* We don't expect the kernel to write more than requested. */
512  Assert(part == 0);
513  break;
514  }
515 
516  /*
517  * Move whatever's left to the front of our mutable copy and adjust
518  * the leading iovec.
519  */
520  Assert(iovcnt > 0);
521  memmove(iov_copy, iov, sizeof(*iov) * iovcnt);
522  Assert(iov->iov_len > part);
523  iov_copy[0].iov_base = (char *) iov_copy[0].iov_base + part;
524  iov_copy[0].iov_len -= part;
525  iov = iov_copy;
526  }
527 
528  return sum;
529 }
#define Min(x, y)
Definition: c.h:988
ssize_t pg_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: pwritev.c:22

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

Referenced by pg_pwrite_zeros().