mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-27 19:52:02 +02:00
2c8118ee5d
Most callers of strerror() are removed from the backend code. The
remaining callers require special handling with a saved errno from a
previous system call. The frontend code still needs strerror() where
error states need to be handled outside of fprintf.
Note that pg_regress is not changed to use %m as the TAP output may
clobber errno, since those functions call fprintf() and friends before
evaluating the format string.
Support for %m in src/port/snprintf.c has been added in d6c55de1f9
,
hence all the stable branches currently supported include it.
Author: Dagfinn Ilmari Mannsåker
Discussion: https://postgr.es/m/87sf13jhuw.fsf@wibble.ilmari.org
215 lines
3.8 KiB
C
215 lines
3.8 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* compress_none.c
|
|
* Routines for archivers to read or write an uncompressed stream.
|
|
*
|
|
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* IDENTIFICATION
|
|
* src/bin/pg_dump/compress_none.c
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#include "postgres_fe.h"
|
|
#include <unistd.h>
|
|
|
|
#include "compress_none.h"
|
|
#include "pg_backup_utils.h"
|
|
|
|
/*----------------------
|
|
* Compressor API
|
|
*----------------------
|
|
*/
|
|
|
|
/*
|
|
* Private routines
|
|
*/
|
|
|
|
static void
|
|
ReadDataFromArchiveNone(ArchiveHandle *AH, CompressorState *cs)
|
|
{
|
|
size_t cnt;
|
|
char *buf;
|
|
size_t buflen;
|
|
|
|
buflen = DEFAULT_IO_BUFFER_SIZE;
|
|
buf = pg_malloc(buflen);
|
|
|
|
while ((cnt = cs->readF(AH, &buf, &buflen)))
|
|
{
|
|
ahwrite(buf, 1, cnt, AH);
|
|
}
|
|
|
|
free(buf);
|
|
}
|
|
|
|
|
|
static void
|
|
WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
|
|
const void *data, size_t dLen)
|
|
{
|
|
cs->writeF(AH, data, dLen);
|
|
}
|
|
|
|
static void
|
|
EndCompressorNone(ArchiveHandle *AH, CompressorState *cs)
|
|
{
|
|
/* no op */
|
|
}
|
|
|
|
/*
|
|
* Public interface
|
|
*/
|
|
|
|
void
|
|
InitCompressorNone(CompressorState *cs,
|
|
const pg_compress_specification compression_spec)
|
|
{
|
|
cs->readData = ReadDataFromArchiveNone;
|
|
cs->writeData = WriteDataToArchiveNone;
|
|
cs->end = EndCompressorNone;
|
|
|
|
cs->compression_spec = compression_spec;
|
|
}
|
|
|
|
|
|
/*----------------------
|
|
* Compress File API
|
|
*----------------------
|
|
*/
|
|
|
|
/*
|
|
* Private routines
|
|
*/
|
|
|
|
static bool
|
|
read_none(void *ptr, size_t size, size_t *rsize, CompressFileHandle *CFH)
|
|
{
|
|
FILE *fp = (FILE *) CFH->private_data;
|
|
size_t ret;
|
|
|
|
if (size == 0)
|
|
return true;
|
|
|
|
ret = fread(ptr, 1, size, fp);
|
|
if (ret != size && !feof(fp))
|
|
pg_fatal("could not read from input file: %m");
|
|
|
|
if (rsize)
|
|
*rsize = ret;
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
write_none(const void *ptr, size_t size, CompressFileHandle *CFH)
|
|
{
|
|
size_t ret;
|
|
|
|
ret = fwrite(ptr, 1, size, (FILE *) CFH->private_data);
|
|
if (ret != size)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
static const char *
|
|
get_error_none(CompressFileHandle *CFH)
|
|
{
|
|
return strerror(errno);
|
|
}
|
|
|
|
static char *
|
|
gets_none(char *ptr, int size, CompressFileHandle *CFH)
|
|
{
|
|
return fgets(ptr, size, (FILE *) CFH->private_data);
|
|
}
|
|
|
|
static int
|
|
getc_none(CompressFileHandle *CFH)
|
|
{
|
|
FILE *fp = (FILE *) CFH->private_data;
|
|
int ret;
|
|
|
|
ret = fgetc(fp);
|
|
if (ret == EOF)
|
|
{
|
|
if (!feof(fp))
|
|
pg_fatal("could not read from input file: %m");
|
|
else
|
|
pg_fatal("could not read from input file: end of file");
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static bool
|
|
close_none(CompressFileHandle *CFH)
|
|
{
|
|
FILE *fp = (FILE *) CFH->private_data;
|
|
int ret = 0;
|
|
|
|
CFH->private_data = NULL;
|
|
|
|
if (fp)
|
|
ret = fclose(fp);
|
|
|
|
return ret == 0;
|
|
}
|
|
|
|
static bool
|
|
eof_none(CompressFileHandle *CFH)
|
|
{
|
|
return feof((FILE *) CFH->private_data) != 0;
|
|
}
|
|
|
|
static bool
|
|
open_none(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
|
|
{
|
|
Assert(CFH->private_data == NULL);
|
|
|
|
if (fd >= 0)
|
|
CFH->private_data = fdopen(dup(fd), mode);
|
|
else
|
|
CFH->private_data = fopen(path, mode);
|
|
|
|
if (CFH->private_data == NULL)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
open_write_none(const char *path, const char *mode, CompressFileHandle *CFH)
|
|
{
|
|
Assert(CFH->private_data == NULL);
|
|
|
|
CFH->private_data = fopen(path, mode);
|
|
if (CFH->private_data == NULL)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* Public interface
|
|
*/
|
|
|
|
void
|
|
InitCompressFileHandleNone(CompressFileHandle *CFH,
|
|
const pg_compress_specification compression_spec)
|
|
{
|
|
CFH->open_func = open_none;
|
|
CFH->open_write_func = open_write_none;
|
|
CFH->read_func = read_none;
|
|
CFH->write_func = write_none;
|
|
CFH->gets_func = gets_none;
|
|
CFH->getc_func = getc_none;
|
|
CFH->close_func = close_none;
|
|
CFH->eof_func = eof_none;
|
|
CFH->get_error_func = get_error_none;
|
|
|
|
CFH->private_data = NULL;
|
|
}
|