#ifdef HAVE_LIBZ
    if (state->ztarfile != NULL)
    {
+       errno = 0;
        if (gzwrite(state->ztarfile, buf, r) != r)
        {
+           /* if write didn't set errno, assume problem is no disk space */
+           if (errno == 0)
+               errno = ENOSPC;
            pg_log_error("could not write to compressed file \"%s\": %s",
                         state->filename, get_gz_error(state->ztarfile));
            exit(1);
    else
 #endif
    {
+       errno = 0;
        if (fwrite(buf, r, 1, state->tarfile) != 1)
        {
+           /* if write didn't set errno, assume problem is no disk space */
+           if (errno == 0)
+               errno = ENOSPC;
            pg_log_error("could not write to file \"%s\": %m",
                         state->filename);
            exit(1);
            return;
        }
 
+       errno = 0;
        if (fwrite(copybuf, r, 1, state->file) != 1)
        {
+           /* if write didn't set errno, assume problem is no disk space */
+           if (errno == 0)
+               errno = ENOSPC;
            pg_log_error("could not write to file \"%s\": %m", state->filename);
            exit(1);
        }
 {
    WriteManifestState *state = callback_data;
 
+   errno = 0;
    if (fwrite(copybuf, r, 1, state->file) != 1)
    {
+       /* if write didn't set errno, assume problem is no disk space */
+       if (errno == 0)
+           errno = ENOSPC;
        pg_log_error("could not write to file \"%s\": %m", state->filename);
        exit(1);
    }
 
 {
    lclContext *ctx = (lclContext *) AH->formatData;
 
+   errno = 0;
    if (dLen > 0 && cfwrite(data, dLen, ctx->dataFH) != dLen)
+   {
+       /* if write didn't set errno, assume problem is no disk space */
+       if (errno == 0)
+           errno = ENOSPC;
        fatal("could not write to output file: %s",
              get_cfp_error(ctx->dataFH));
+   }
 }
 
 /*
    unsigned char c = (unsigned char) i;
    lclContext *ctx = (lclContext *) AH->formatData;
 
+   errno = 0;
    if (cfwrite(&c, 1, ctx->dataFH) != 1)
+   {
+       /* if write didn't set errno, assume problem is no disk space */
+       if (errno == 0)
+           errno = ENOSPC;
        fatal("could not write to output file: %s",
              get_cfp_error(ctx->dataFH));
+   }
 
    return 1;
 }
 {
    lclContext *ctx = (lclContext *) AH->formatData;
 
+   errno = 0;
    if (cfwrite(buf, len, ctx->dataFH) != len)
+   {
+       /* if write didn't set errno, assume problem is no disk space */
+       if (errno == 0)
+           errno = ENOSPC;
        fatal("could not write to output file: %s",
              get_cfp_error(ctx->dataFH));
+   }
 }
 
 /*