PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
file_utils.c File Reference
#include "postgres.h"
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "common/file_utils.h"
#include "common/relpath.h"
#include "port/pg_iovec.h"
Include dependency graph for file_utils.c:

Go to the source code of this file.

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)
 

Function Documentation

◆ compute_remaining_iovec()

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

Definition at line 614 of file file_utils.c.

618{
619 Assert(iovcnt > 0);
620
621 /* Skip wholly transferred iovecs. */
622 while (source->iov_len <= transferred)
623 {
624 transferred -= source->iov_len;
625 source++;
626 iovcnt--;
627
628 /* All iovecs transferred? */
629 if (iovcnt == 0)
630 {
631 /*
632 * We don't expect the kernel to transfer more than we asked it
633 * to, or something is out of sync.
634 */
635 Assert(transferred == 0);
636 return 0;
637 }
638 }
639
640 /* Copy the remaining iovecs to the front of the array. */
641 if (source != destination)
642 memmove(destination, source, sizeof(*source) * iovcnt);
643
644 /* Adjust leading iovec, which may have been partially transferred. */
645 Assert(destination->iov_len > transferred);
646 destination->iov_base = (char *) destination->iov_base + transferred;
647 destination->iov_len -= transferred;
648
649 return iovcnt;
650}
Assert(PointerIsAligned(start, uint64))
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 547 of file file_utils.c.

551{
552 PGFileType result;
553
554 /*
555 * Some systems tell us the type directly in the dirent struct, but that's
556 * a BSD and Linux extension not required by POSIX. Even when the
557 * interface is present, sometimes the type is unknown, depending on the
558 * filesystem.
559 */
560#if defined(DT_REG) && defined(DT_DIR) && defined(DT_LNK)
561 if (de->d_type == DT_REG)
562 result = PGFILETYPE_REG;
563 else if (de->d_type == DT_DIR)
564 result = PGFILETYPE_DIR;
565 else if (de->d_type == DT_LNK && !look_through_symlinks)
566 result = PGFILETYPE_LNK;
567 else
568 result = PGFILETYPE_UNKNOWN;
569#else
570 result = PGFILETYPE_UNKNOWN;
571#endif
572
573 if (result == PGFILETYPE_UNKNOWN)
574 {
575 struct stat fst;
576 int sret;
577
578
579 if (look_through_symlinks)
580 sret = stat(path, &fst);
581 else
582 sret = lstat(path, &fst);
583
584 if (sret < 0)
585 {
586 result = PGFILETYPE_ERROR;
587#ifdef FRONTEND
588 pg_log_generic(elevel, PG_LOG_PRIMARY, "could not stat file \"%s\": %m", path);
589#else
590 ereport(elevel,
592 errmsg("could not stat file \"%s\": %m", path)));
593#endif
594 }
595 else if (S_ISREG(fst.st_mode))
596 result = PGFILETYPE_REG;
597 else if (S_ISDIR(fst.st_mode))
598 result = PGFILETYPE_DIR;
599 else if (S_ISLNK(fst.st_mode))
600 result = PGFILETYPE_LNK;
601 }
602
603 return result;
604}
#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:877
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ereport(elevel,...)
Definition: elog.h:149
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
void pg_log_generic(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt,...)
Definition: logging.c:208
@ PG_LOG_PRIMARY
Definition: logging.h:67
unsigned char d_type
Definition: dirent.h:13
#define stat
Definition: win32_port.h:274
#define lstat(path, sb)
Definition: win32_port.h:275
#define S_ISDIR(m)
Definition: win32_port.h:315
#define S_ISLNK(m)
Definition: win32_port.h:334
#define S_ISREG(m)
Definition: win32_port.h:318

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(), swap_catalog_files(), and walkdir().

◆ pg_pwrite_zeros()

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

Definition at line 709 of file file_utils.c.

710{
711 static const PGIOAlignedBlock zbuffer = {0}; /* worth BLCKSZ */
712 void *zerobuf_addr = unconstify(PGIOAlignedBlock *, &zbuffer)->data;
713 struct iovec iov[PG_IOV_MAX];
714 size_t remaining_size = size;
715 ssize_t total_written = 0;
716
717 /* Loop, writing as many blocks as we can for each system call. */
718 while (remaining_size > 0)
719 {
720 int iovcnt = 0;
721 ssize_t written;
722
723 for (; iovcnt < PG_IOV_MAX && remaining_size > 0; iovcnt++)
724 {
725 size_t this_iov_size;
726
727 iov[iovcnt].iov_base = zerobuf_addr;
728
729 if (remaining_size < BLCKSZ)
730 this_iov_size = remaining_size;
731 else
732 this_iov_size = BLCKSZ;
733
734 iov[iovcnt].iov_len = this_iov_size;
735 remaining_size -= this_iov_size;
736 }
737
738 written = pg_pwritev_with_retry(fd, iov, iovcnt, offset);
739
740 if (written < 0)
741 return written;
742
743 offset += written;
744 total_written += written;
745 }
746
747 Assert(total_written == size);
748
749 return total_written;
750}
#define unconstify(underlying_type, expr)
Definition: c.h:1216
ssize_t pg_pwritev_with_retry(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: file_utils.c:659
#define PG_IOV_MAX
Definition: pg_iovec.h:41
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 659 of file file_utils.c.

660{
661 struct iovec iov_copy[PG_IOV_MAX];
662 ssize_t sum = 0;
663 ssize_t part;
664
665 /* We'd better have space to make a copy, in case we need to retry. */
666 if (iovcnt > PG_IOV_MAX)
667 {
668 errno = EINVAL;
669 return -1;
670 }
671
672 do
673 {
674 /* Write as much as we can. */
675 part = pg_pwritev(fd, iov, iovcnt, offset);
676 if (part < 0)
677 return -1;
678
679#ifdef SIMULATE_SHORT_WRITE
680 part = Min(part, 4096);
681#endif
682
683 /* Count our progress. */
684 sum += part;
685 offset += part;
686
687 /*
688 * See what is left. On the first loop we used the caller's array,
689 * but in later loops we'll use our local copy that we are allowed to
690 * mutate.
691 */
692 iovcnt = compute_remaining_iovec(iov_copy, iov, iovcnt, part);
693 iov = iov_copy;
694 } while (iovcnt > 0);
695
696 return sum;
697}
#define Min(x, y)
Definition: c.h:975
int compute_remaining_iovec(struct iovec *destination, const struct iovec *source, int iovcnt, size_t transferred)
Definition: file_utils.c:614
static ssize_t pg_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: pg_iovec.h:87

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

Referenced by pg_pwrite_zeros().