Skip to content

Commit 1b43463

Browse files
committed
codereview by PostgreSQL team member (Alvaro Herrera): final state of the patch which was commited to git.posgresql.org (https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=4d57e8381677)
1 parent 1578f50 commit 1b43463

File tree

4 files changed

+68
-49
lines changed

4 files changed

+68
-49
lines changed

src/bin/pg_dump/compress_io.c

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF)
388388
free(out);
389389
free(zp);
390390
}
391-
#endif /* HAVE_LIBZ */
391+
#endif /* HAVE_LIBZ */
392392

393393

394394
/*
@@ -422,6 +422,23 @@ WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
422422
}
423423

424424

425+
/*----------------------
426+
* Compressed stream API
427+
*----------------------
428+
*/
429+
430+
/*
431+
* cfp represents an open stream, wrapping the underlying FILE or gzFile
432+
* pointer. This is opaque to the callers.
433+
*/
434+
struct cfp
435+
{
436+
FILE *uncompressedfp;
437+
#ifdef HAVE_LIBZ
438+
gzFile compressedfp;
439+
#endif
440+
};
441+
425442
#ifdef HAVE_LIBZ
426443
static int hasSuffix(const char *filename, const char *suffix);
427444
#endif
@@ -575,8 +592,14 @@ cfread(void *ptr, int size, cfp *fp)
575592
{
576593
ret = gzread(fp->compressedfp, ptr, size);
577594
if (ret != size && !gzeof(fp->compressedfp))
595+
{
596+
int errnum;
597+
const char *errmsg = gzerror(fp->compressedfp, &errnum);
598+
578599
exit_horribly(modulename,
579-
"could not read from input file: %s\n", get_gz_error(fp->compressedfp));
600+
"could not read from input file: %s\n",
601+
errnum == Z_ERRNO ? strerror(errno) : errmsg);
602+
}
580603
}
581604
else
582605
#endif
@@ -612,10 +635,10 @@ cfgetc(cfp *fp)
612635
{
613636
if (!gzeof(fp->compressedfp))
614637
exit_horribly(modulename,
615-
"could not read from input file: %s\n", get_gz_error(fp->compressedfp));
638+
"could not read from input file: %s\n", strerror(errno));
616639
else
617640
exit_horribly(modulename,
618-
"could not read from input file: end of file\n");
641+
"could not read from input file: end of file\n");
619642
}
620643
}
621644
else
@@ -678,6 +701,22 @@ cfeof(cfp *fp)
678701
return feof(fp->uncompressedfp);
679702
}
680703

704+
const char *
705+
get_cfp_error(cfp *fp)
706+
{
707+
#ifdef HAVE_LIBZ
708+
if (fp->compressedfp)
709+
{
710+
int errnum;
711+
const char *errmsg = gzerror(fp->compressedfp, &errnum);
712+
713+
if (errnum != Z_ERRNO)
714+
return errmsg;
715+
}
716+
#endif
717+
return strerror(errno);
718+
}
719+
681720
#ifdef HAVE_LIBZ
682721
static int
683722
hasSuffix(const char *filename, const char *suffix)
@@ -693,16 +732,4 @@ hasSuffix(const char *filename, const char *suffix)
693732
suffixlen) == 0;
694733
}
695734

696-
const char *
697-
get_gz_error(gzFile gzf)
698-
{
699-
int errnum;
700-
static const char fallback[] = "Zlib error";
701-
const int maxlen = 255;
702-
const char *errmsg = gzerror(gzf, &errnum);
703-
if(!errmsg || !memchr(errmsg, 0, maxlen))
704-
errmsg = fallback;
705-
706-
return errnum == Z_ERRNO ? strerror(errno) : errmsg;
707-
}
708735
#endif

src/bin/pg_dump/compress_io.h

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,6 @@ typedef enum
2727
COMPR_ALG_LIBZ
2828
} CompressionAlgorithm;
2929

30-
/*----------------------
31-
* Compressed stream API
32-
*----------------------
33-
*/
34-
35-
/*
36-
* cfp represents an open stream, wrapping the underlying FILE or gzFile
37-
* pointer. This is opaque to the callers.
38-
*/
39-
typedef struct
40-
{
41-
FILE *uncompressedfp;
42-
#ifdef HAVE_LIBZ
43-
gzFile compressedfp;
44-
#endif
45-
} cfp;
46-
47-
4830
/* Prototype for callback function to WriteDataToArchive() */
4931
typedef void (*WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len);
5032

@@ -70,6 +52,10 @@ extern void ReadDataFromArchive(ArchiveHandle *AH, int compression,
7052
extern void WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
7153
const void *data, size_t dLen);
7254
extern void EndCompressor(ArchiveHandle *AH, CompressorState *cs);
55+
56+
57+
typedef struct cfp cfp;
58+
7359
extern cfp *cfopen(const char *path, const char *mode, int compression);
7460
extern cfp *cfopen_read(const char *path, const char *mode);
7561
extern cfp *cfopen_write(const char *path, const char *mode, int compression);
@@ -79,6 +65,6 @@ extern int cfgetc(cfp *fp);
7965
extern char *cfgets(cfp *fp, char *buf, int len);
8066
extern int cfclose(cfp *fp);
8167
extern int cfeof(cfp *fp);
82-
extern const char * get_gz_error(gzFile gzf);
68+
extern const char *get_cfp_error(cfp *fp);
8369

8470
#endif

src/bin/pg_dump/pg_backup_directory.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,6 @@
4242
#include <dirent.h>
4343
#include <sys/stat.h>
4444

45-
#ifdef WRITE_ERROR_EXIT
46-
#undef WRITE_ERROR_EXIT
47-
#endif
48-
#define WRITE_ERROR_EXIT \
49-
do { \
50-
exit_horribly(modulename, "could not write to output file: %s\n", \
51-
ctx && ctx->dataFH && ctx->dataFH->compressedfp? get_gz_error(ctx->dataFH->compressedfp) : strerror(errno)); \
52-
} while (0)
53-
5445
typedef struct
5546
{
5647
/*
@@ -361,7 +352,9 @@ _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
361352
lclContext *ctx = (lclContext *) AH->formatData;
362353

363354
if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
364-
WRITE_ERROR_EXIT;
355+
exit_horribly(modulename, "could not write to output file: %s\n",
356+
get_cfp_error(ctx->dataFH));
357+
365358

366359
return;
367360
}
@@ -499,7 +492,8 @@ _WriteByte(ArchiveHandle *AH, const int i)
499492
lclContext *ctx = (lclContext *) AH->formatData;
500493

501494
if (cfwrite(&c, 1, ctx->dataFH) != 1)
502-
WRITE_ERROR_EXIT;
495+
exit_horribly(modulename, "could not write to output file: %s\n",
496+
get_cfp_error(ctx->dataFH));
503497

504498
return 1;
505499
}
@@ -528,7 +522,8 @@ _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
528522
lclContext *ctx = (lclContext *) AH->formatData;
529523

530524
if (cfwrite(buf, len, ctx->dataFH) != len)
531-
WRITE_ERROR_EXIT;
525+
exit_horribly(modulename, "could not write to output file: %s\n",
526+
get_cfp_error(ctx->dataFH));
532527

533528
return;
534529
}

src/bin/pg_dump/pg_backup_tar.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "pgtar.h"
3636
#include "common/file_utils.h"
3737
#include "fe_utils/string_utils.h"
38-
#include "compress_io.h"
3938

4039
#include <sys/stat.h>
4140
#include <ctype.h>
@@ -556,8 +555,20 @@ _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
556555
{
557556
res = GZREAD(&((char *) buf)[used], 1, len, th->zFH);
558557
if (res != len && !GZEOF(th->zFH))
558+
{
559+
#ifdef HAVE_LIBZ
560+
int errnum;
561+
const char *errmsg = gzerror(th->zFH, &errnum);
562+
559563
exit_horribly(modulename,
560-
"could not read from input file: %s\n", get_gz_error(th->zFH));
564+
"could not read from input file: %s\n",
565+
errnum == Z_ERRNO ? strerror(errno) : errmsg);
566+
#else
567+
exit_horribly(modulename,
568+
"could not read from input file: %s\n",
569+
strerror(errno));
570+
#endif
571+
}
561572
}
562573
else
563574
{

0 commit comments

Comments
 (0)