Rename wtap_dump_file_write_all() to wtap_dump_file_write(), and have

everybody use it; the places using the old wtap_dump_file_write() were
using it in the same way the old wtap_dump_file_write_all() did.

That also lets us get rid of wtap_dump_file_ferror().

Also, have the new wtap_dump_file_write() check for errors from
gzwrite() and fwrite() differently - the former returns 0 on error, the
latter can return a short write on error.

svn path=/trunk/; revision=33113
This commit is contained in:
Guy Harris 2010-06-06 19:14:32 +00:00
parent 1b3be7a754
commit 1bf478fdef
6 changed files with 76 additions and 204 deletions

View File

@ -388,7 +388,7 @@ static gboolean btsnoop_dump_h1(wtap_dumper *wdh,
rec_hdr.incl_len = GUINT32_TO_BE(phdr->caplen-1);
rec_hdr.orig_len = GUINT32_TO_BE(phdr->len-1);
if (!wtap_dump_file_write_all(wdh, &rec_hdr, sizeof rec_hdr, err))
if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
return FALSE;
wdh->bytes_dumped += sizeof rec_hdr;
@ -396,7 +396,7 @@ static gboolean btsnoop_dump_h1(wtap_dumper *wdh,
/* Skip HCI packet type */
++pd;
if (!wtap_dump_file_write_all(wdh, pd, phdr->caplen-1, err))
if (!wtap_dump_file_write(wdh, pd, phdr->caplen-1, err))
return FALSE;
wdh->bytes_dumped += phdr->caplen-1;
@ -417,12 +417,12 @@ static gboolean btsnoop_dump_h4(wtap_dumper *wdh,
rec_hdr.incl_len = GUINT32_TO_BE(phdr->caplen);
rec_hdr.orig_len = GUINT32_TO_BE(phdr->len);
if (!wtap_dump_file_write_all(wdh, &rec_hdr, sizeof rec_hdr, err))
if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
return FALSE;
wdh->bytes_dumped += sizeof rec_hdr;
if (!wtap_dump_file_write_all(wdh, pd, phdr->caplen, err))
if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
return FALSE;
wdh->bytes_dumped += phdr->caplen;
@ -453,7 +453,7 @@ gboolean btsnoop_dump_open_h1(wtap_dumper *wdh, gboolean cant_seek _U_, int *err
return FALSE;
}
if (!wtap_dump_file_write_all(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
if (!wtap_dump_file_write(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
return FALSE;
wdh->bytes_dumped += sizeof btsnoop_magic;
@ -463,7 +463,7 @@ gboolean btsnoop_dump_open_h1(wtap_dumper *wdh, gboolean cant_seek _U_, int *err
/* HCI type encoded in first byte */
file_hdr.datalink = GUINT32_TO_BE(KHciLoggerDatalinkTypeH1);
if (!wtap_dump_file_write_all(wdh, &file_hdr, sizeof file_hdr, err))
if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
return FALSE;
wdh->bytes_dumped += sizeof file_hdr;
@ -495,7 +495,7 @@ gboolean btsnoop_dump_open_h4(wtap_dumper *wdh, gboolean cant_seek _U_, int *err
return FALSE;
}
if (!wtap_dump_file_write_all(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
if (!wtap_dump_file_write(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
return FALSE;
wdh->bytes_dumped += sizeof btsnoop_magic;
@ -505,7 +505,7 @@ gboolean btsnoop_dump_open_h4(wtap_dumper *wdh, gboolean cant_seek _U_, int *err
/* HCI type encoded in first byte */
file_hdr.datalink = GUINT32_TO_BE(KHciLoggerDatalinkTypeH4);
if (!wtap_dump_file_write_all(wdh, &file_hdr, sizeof file_hdr, err))
if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
return FALSE;
wdh->bytes_dumped += sizeof file_hdr;

View File

@ -1024,31 +1024,53 @@ static FILE_T wtap_dump_file_fdopen(wtap_dumper *wdh _U_, int fd)
#endif
/* internally writing raw bytes (compressed or not) */
size_t wtap_dump_file_write(wtap_dumper *wdh, const void *buf, size_t bufsize)
gboolean
wtap_dump_file_write(wtap_dumper *wdh, const void *buf, size_t bufsize,
int *err)
{
size_t nwritten;
#ifdef HAVE_LIBZ
if(wdh->compressed) {
return gzwrite(wdh->fh, buf, (unsigned) bufsize);
int errnum;
#endif
#ifdef HAVE_LIBZ
if (wdh->compressed) {
nwritten = gzwrite(wdh->fh, buf, (unsigned) bufsize);
/*
* At least according to zlib.h, gzwrite returns 0
* on error; that appears to be the case in libz
* 1.2.5.
*/
if (nwritten == 0) {
gzerror(wdh->fh, &errnum);
if (errnum == Z_ERRNO)
*err = errno;
else {
/*
* XXX - what to do with this zlib-specific
* number?
*/
*err = errnum;
}
return FALSE;
}
} else
#endif
{
return fwrite(buf, 1, bufsize, wdh->fh);
nwritten = fwrite(buf, 1, bufsize, wdh->fh);
/*
* At least according to the Mac OS X man page,
* this can return a short count on an error.
*/
if (nwritten != bufsize) {
if (ferror(wdh->fh))
*err = errno;
else
*err = WTAP_ERR_SHORT_WRITE;
return FALSE;
}
}
}
gboolean wtap_dump_file_write_all(wtap_dumper *wdh, const void *buf, unsigned bufsize, int *err)
{
size_t nwritten;
nwritten = wtap_dump_file_write(wdh, buf, bufsize);
if (nwritten != bufsize) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
return FALSE;
}
return TRUE;
return TRUE;
}
/* internally close a file for writing (compressed or not) */
@ -1063,25 +1085,3 @@ static int wtap_dump_file_close(wtap_dumper *wdh)
return fclose(wdh->fh);
}
}
int wtap_dump_file_ferror(wtap_dumper *wdh)
{
#ifdef HAVE_LIBZ
int errnum;
if(wdh->compressed) {
gzerror(wdh->fh, &errnum);
if(errnum == Z_ERRNO) {
return errno;
} else {
/* XXX - what to do with this zlib specific number? */
return errnum;
}
} else
#endif
{
return ferror(wdh->fh);
}
}

View File

@ -911,7 +911,6 @@ gboolean libpcap_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
{
guint32 magic;
struct pcap_hdr file_hdr;
size_t nwritten;
/* This is a libpcap file */
wdh->subtype_write = libpcap_dump;
@ -945,14 +944,8 @@ gboolean libpcap_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
return FALSE;
}
nwritten = wtap_dump_file_write(wdh, &magic, sizeof magic);
if (nwritten != sizeof magic) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &magic, sizeof magic, err))
return FALSE;
}
wdh->bytes_dumped += sizeof magic;
/* current "libpcap" format is 2.4 */
@ -974,14 +967,8 @@ gboolean libpcap_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
file_hdr.snaplen = (wdh->snaplen != 0) ? wdh->snaplen :
WTAP_MAX_PACKET_SIZE;
file_hdr.network = wtap_wtap_encap_to_pcap_encap(wdh->encap);
nwritten = wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr);
if (nwritten != sizeof file_hdr) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
return FALSE;
}
wdh->bytes_dumped += sizeof file_hdr;
return TRUE;
@ -996,7 +983,6 @@ static gboolean libpcap_dump(wtap_dumper *wdh,
{
struct pcaprec_ss990915_hdr rec_hdr;
size_t hdr_size;
size_t nwritten;
int phdrsize;
phdrsize = pcap_get_phdr_size(wdh->encap, pseudo_header);
@ -1069,27 +1055,15 @@ static gboolean libpcap_dump(wtap_dumper *wdh,
return FALSE;
}
nwritten = wtap_dump_file_write(wdh, &rec_hdr, hdr_size);
if (nwritten != hdr_size) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &rec_hdr, hdr_size, err))
return FALSE;
}
wdh->bytes_dumped += hdr_size;
if (!pcap_write_phdr(wdh, wdh->encap, pseudo_header, err))
return FALSE;
nwritten = wtap_dump_file_write(wdh, pd, phdr->caplen);
if (nwritten != phdr->caplen) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
return FALSE;
}
wdh->bytes_dumped += phdr->caplen;
return TRUE;
}

View File

@ -1687,14 +1687,8 @@ pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pse
}
atm_hdr[SUNATM_VPI] = (guint8)pseudo_header->atm.vpi;
phtons(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
nwritten = wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr));
if (nwritten != sizeof(atm_hdr)) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr), err))
return FALSE;
}
wdh->bytes_dumped += sizeof(atm_hdr);
break;
@ -1706,14 +1700,8 @@ pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pse
phtons(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET],
pseudo_header->irda.pkttype);
phtons(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
nwritten = wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr));
if (nwritten != sizeof(irda_hdr)) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr), err))
return FALSE;
}
wdh->bytes_dumped += sizeof(irda_hdr);
break;
@ -1726,14 +1714,8 @@ pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pse
mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
phtons(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
pseudo_header->mtp2.link_number);
nwritten = wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr));
if (nwritten != sizeof(mtp2_hdr)) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr), err))
return FALSE;
}
wdh->bytes_dumped += sizeof(mtp2_hdr);
break;
@ -1812,14 +1794,8 @@ pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pse
default:
break;
}
nwritten = wtap_dump_file_write(wdh, erf_hdr, size);
if (nwritten != (guint) size) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, erf_hdr, size, err))
return FALSE;
}
wdh->bytes_dumped += size;
break;
@ -1844,27 +1820,15 @@ pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pse
case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
bt_hdr.direction = GUINT32_TO_BE(pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV);
nwritten = wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr);
if (nwritten != sizeof bt_hdr) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr, err))
return FALSE;
}
wdh->bytes_dumped += sizeof bt_hdr;
break;
case WTAP_ENCAP_PPP_WITH_PHDR:
ppp_hdr.direction = (pseudo_header->p2p.sent ? LIBPCAP_PPP_PHDR_SENT : LIBPCAP_PPP_PHDR_RECV);
nwritten = wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr);
if (nwritten != sizeof ppp_hdr) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr, err))
return FALSE;
}
wdh->bytes_dumped += sizeof ppp_hdr;
break;
}

View File

@ -1482,21 +1482,14 @@ pcapng_write_section_header_block(wtap_dumper *wdh, wtapng_block_t *wblock, int
{
pcapng_block_header_t bh;
pcapng_section_header_block_t shb;
size_t nwritten;
/* write block header */
bh.block_type = wblock->type;
bh.block_total_length = sizeof(bh) + sizeof(shb) /* + options */ + 4;
nwritten = wtap_dump_file_write(wdh, &bh, sizeof bh);
if (nwritten != sizeof bh) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
}
wdh->bytes_dumped += sizeof bh;
/* write block fixed content */
@ -1506,27 +1499,16 @@ pcapng_write_section_header_block(wtap_dumper *wdh, wtapng_block_t *wblock, int
shb.version_minor = 0;
shb.section_length = -1;
nwritten = wtap_dump_file_write(wdh, &shb, sizeof shb);
if (nwritten != sizeof shb) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &shb, sizeof shb, err))
return FALSE;
}
wdh->bytes_dumped += sizeof shb;
/* XXX - write (optional) block options */
/* write block footer */
nwritten = wtap_dump_file_write(wdh, &bh.block_total_length, sizeof bh.block_total_length);
if (nwritten != sizeof bh.block_total_length) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
}
wdh->bytes_dumped += sizeof bh.block_total_length;
return TRUE;
@ -1539,7 +1521,6 @@ pcapng_write_if_descr_block(wtap_dumper *wdh, wtapng_block_t *wblock, int *err)
{
pcapng_block_header_t bh;
pcapng_interface_description_block_t idb;
size_t nwritten;
pcapng_debug3("pcapng_write_if_descr_block: encap = %d (%s), snaplen = %d",
@ -1556,14 +1537,8 @@ pcapng_write_if_descr_block(wtap_dumper *wdh, wtapng_block_t *wblock, int *err)
bh.block_type = wblock->type;
bh.block_total_length = sizeof(bh) + sizeof(idb) /* + options */ + 4;
nwritten = wtap_dump_file_write(wdh, &bh, sizeof bh);
if (nwritten != sizeof bh) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
}
wdh->bytes_dumped += sizeof bh;
/* write block fixed content */
@ -1571,27 +1546,16 @@ pcapng_write_if_descr_block(wtap_dumper *wdh, wtapng_block_t *wblock, int *err)
idb.reserved = 0;
idb.snaplen = wblock->data.if_descr.snap_len;
nwritten = wtap_dump_file_write(wdh, &idb, sizeof idb);
if (nwritten != sizeof idb) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &idb, sizeof idb, err))
return FALSE;
}
wdh->bytes_dumped += sizeof idb;
/* XXX - write (optional) block options */
/* write block footer */
nwritten = wtap_dump_file_write(wdh, &bh.block_total_length, sizeof bh.block_total_length);
if (nwritten != sizeof bh.block_total_length) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
}
wdh->bytes_dumped += sizeof bh.block_total_length;
return TRUE;
@ -1603,7 +1567,6 @@ pcapng_write_packet_block(wtap_dumper *wdh, wtapng_block_t *wblock, int *err)
{
pcapng_block_header_t bh;
pcapng_enhanced_packet_block_t epb;
size_t nwritten;
const guint32 zero_pad = 0;
guint32 pad_len;
guint32 phdr_len;
@ -1619,14 +1582,8 @@ pcapng_write_packet_block(wtap_dumper *wdh, wtapng_block_t *wblock, int *err)
bh.block_type = wblock->type;
bh.block_total_length = (guint32)sizeof(bh) + (guint32)sizeof(epb) + phdr_len + wblock->data.packet.cap_len + pad_len /* + options */ + 4;
nwritten = wtap_dump_file_write(wdh, &bh, sizeof bh);
if (nwritten != sizeof bh) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
}
wdh->bytes_dumped += sizeof bh;
/* write block fixed content */
@ -1636,14 +1593,8 @@ pcapng_write_packet_block(wtap_dumper *wdh, wtapng_block_t *wblock, int *err)
epb.captured_len = wblock->data.packet.cap_len + phdr_len;
epb.packet_len = wblock->data.packet.packet_len + phdr_len;
nwritten = wtap_dump_file_write(wdh, &epb, sizeof epb);
if (nwritten != sizeof epb) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &epb, sizeof epb, err))
return FALSE;
}
wdh->bytes_dumped += sizeof epb;
/* write pseudo header */
@ -1653,40 +1604,24 @@ pcapng_write_packet_block(wtap_dumper *wdh, wtapng_block_t *wblock, int *err)
wdh->bytes_dumped += phdr_len;
/* write packet data */
nwritten = wtap_dump_file_write(wdh, wblock->frame_buffer, wblock->data.packet.cap_len);
if (nwritten != wblock->data.packet.cap_len) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, wblock->frame_buffer,
wblock->data.packet.cap_len, err))
return FALSE;
}
wdh->bytes_dumped += wblock->data.packet.cap_len;
/* write padding (if any) */
if (pad_len != 0) {
nwritten = wtap_dump_file_write(wdh, &zero_pad, pad_len);
if (nwritten != pad_len) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &zero_pad, pad_len, err))
return FALSE;
}
wdh->bytes_dumped += pad_len;
}
/* XXX - write (optional) block options */
/* write block footer */
nwritten = wtap_dump_file_write(wdh, &bh.block_total_length, sizeof bh.block_total_length);
if (nwritten != sizeof bh.block_total_length) {
if (nwritten == 0 && wtap_dump_file_ferror(wdh))
*err = wtap_dump_file_ferror(wdh);
else
*err = WTAP_ERR_SHORT_WRITE;
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
}
wdh->bytes_dumped += sizeof bh.block_total_length;
return TRUE;

View File

@ -97,9 +97,8 @@ struct wtap_dumper {
* e.g. WTAP_FILE_TSPREC_USEC */
};
extern gboolean wtap_dump_file_write_all(wtap_dumper *wdh, const void *buf, unsigned bufsize, int *err);
extern size_t wtap_dump_file_write(wtap_dumper *wdh, const void *buf, size_t bufsize);
extern int wtap_dump_file_ferror(wtap_dumper *wdh);
extern gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
size_t bufsize, int *err);
extern gint wtap_num_file_types;