Add APIs to Wiretap to return the file of the size as supplied by the OS

(so if the file's gzipped, it's *NOT* the size of the file after
uncompressing), and an approximation of the amount of that data read
sequentially so far.

Use those for various progress bars and the like.

Make the fstat() in the Ascend trace reader directly use wth->fd, as
it's inside Wiretap; that gets rid of the last caller of wtap_fd() (as
we're no longer directly using fstat() or lseek() in Ethereal), so get
rid of wtap_fd().

svn path=/trunk/; revision=15437
This commit is contained in:
Guy Harris 2005-08-19 19:40:00 +00:00
parent 06823cdce8
commit 38ec1644e6
13 changed files with 103 additions and 100 deletions

View File

@ -41,10 +41,6 @@
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <glib.h>
#include <epan/packet.h>
@ -70,7 +66,7 @@ static gboolean cap_packet_size = FALSE;
typedef struct _capture_info {
const char *filename;
guint16 file_type;
guint64 filesize;
gint64 filesize;
guint64 packet_bytes;
double start_time;
double stop_time;
@ -106,7 +102,7 @@ print_stats(capture_info *cf_info)
if (cap_file_type) printf("File type: %s\n", file_type_string);
if (cap_packet_count) printf("Number of packets: %u \n", cf_info->packet_count);
if (cap_file_size) printf("File size: %" PRIu64 " bytes\n", cf_info->filesize);
if (cap_file_size) printf("File size: %" PRId64 " bytes\n", cf_info->filesize);
if (cap_data_size) printf("Data size: %" PRIu64 " bytes\n", cf_info->packet_bytes);
if (cap_duration) printf("Capture duration: %f seconds\n", cf_info->duration);
if (cap_start_time) printf("Start time: %s", ctime (&start_time_t));
@ -121,7 +117,7 @@ process_cap_file(wtap *wth, const char *filename)
{
int err;
gchar *err_info;
struct stat cf_stat;
gint64 size;
long data_offset;
guint32 packet = 0;
@ -166,14 +162,15 @@ process_cap_file(wtap *wth, const char *filename)
}
/* File size */
if (fstat(wtap_fd(wth), &cf_stat) < 0) {
size = wtap_file_size(wth, &err);
if (size == -1) {
fprintf(stderr,
"capinfos: Can't fstat \"%s\": %s.\n",
filename, strerror(errno));
"capinfos: Can't get size of \"%s\": %s.\n",
filename, strerror(err));
return 1;
}
cf_info.filesize = cf_stat.st_size;
cf_info.filesize = size;
/* File Type */
cf_info.file_type = wtap_file_type(wth);

View File

@ -43,12 +43,11 @@ typedef enum {
typedef struct _capture_file {
file_state state; /* Current state of capture file */
int filed; /* File descriptor of capture file */
gchar *filename; /* Name of capture file */
gboolean is_tempfile; /* Is capture file a temporary file? */
gboolean user_saved;/* If capture file is temporary, has it been saved by user yet? */
long f_datalen; /* Size of capture file data (uncompressed) */
long f_len; /* Length of capture file (compressed if file is) */
gint64 f_len; /* Length of capture file (compressed if file is) */
guint16 cd_t; /* File type of capture file */
int lnk_t; /* Link-layer type with which to save capture */
guint32 vers; /* Version. For tcpdump minor is appended to major */

71
file.c
View File

@ -43,10 +43,6 @@
#include <errno.h>
#include <signal.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
@ -171,18 +167,15 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
{
wtap *wth;
gchar *err_info;
int fd;
struct stat cf_stat;
gint64 size;
wth = wtap_open_offline(fname, err, &err_info, TRUE);
if (wth == NULL)
goto fail;
/* Find the size of the file. */
fd = wtap_fd(wth);
if (fstat(fd, &cf_stat) < 0) {
*err = errno;
size = wtap_file_size(wth, err);
if (size == -1) {
wtap_close(wth);
goto fail;
}
@ -198,9 +191,8 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
cf->state = FILE_READ_IN_PROGRESS;
cf->wth = wth;
cf->filed = fd;
cf->f_datalen = 0;
cf->f_len = cf_stat.st_size;
cf->f_len = size;
/* Set the file name because we need it to set the follow stream filter.
XXX - is that still true? We need it for other reasons, though,
@ -335,15 +327,8 @@ cf_read(capture_file *cf)
long data_offset;
progdlg_t *progbar = NULL;
gboolean stop_flag;
/*
* XXX - should be "off_t", but Wiretap would need more work to handle
* the full size of "off_t" on platforms where it's more than a "long"
* as well.
*/
long file_pos;
gint64 size, file_pos;
float prog_val;
int fd;
struct stat cf_stat;
GTimeVal start_time;
gchar status_str[100];
int progbar_nextstep;
@ -376,17 +361,17 @@ cf_read(capture_file *cf)
to see if there's any pending input from an X server, and doing
that for every packet can be costly, especially on a big file. */
if (data_offset >= progbar_nextstep) {
file_pos = lseek(cf->filed, 0, SEEK_CUR);
file_pos = wtap_read_so_far(cf->wth, NULL);
prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
if (prog_val > 1.0) {
/* The file probably grew while we were reading it.
Update "cf->f_len", and try again. */
fd = wtap_fd(cf->wth);
if (fstat(fd, &cf_stat) >= 0) {
cf->f_len = cf_stat.st_size;
size = wtap_file_size(cf->wth, NULL);
if (size != -1) {
cf->f_len = size;
prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
}
/* If it's still > 1, either the "fstat()" failed (in which
/* If it's still > 1, either "wtap_file_size()" failed (in which
case there's not much we can do about it), or the file
*shrank* (in which case there's not much we can do about
it); just clip the progress value at 1.0. */
@ -400,7 +385,8 @@ cf_read(capture_file *cf)
}
if (progbar != NULL) {
g_snprintf(status_str, sizeof(status_str),
"%luKB of %luKB", file_pos / 1024, cf->f_len / 1024);
"%" PRId64 "KB of %" PRId64 "KB",
file_pos / 1024, cf->f_len / 1024);
update_progress_dlg(progbar, prog_val, status_str);
}
progbar_nextstep += progbar_quantum;
@ -553,9 +539,7 @@ cf_finish_tail(capture_file *cf, int *err)
{
gchar *err_info;
long data_offset;
int fd;
struct stat cf_stat;
gint64 size;
if(cf->wth == NULL) {
cf_close(cf);
@ -595,10 +579,9 @@ cf_finish_tail(capture_file *cf, int *err)
/* we have to update the f_len field */
/* Find the size of the file. */
fd = wtap_fd(cf->wth);
if (fstat(fd, &cf_stat) >= 0) {
cf->f_len = cf_stat.st_size;
}
size = wtap_file_size(cf->wth, NULL);
if (size != -1)
cf->f_len = size;
/* We're done reading sequentially through the file; close the
sequential I/O side, to free up memory it requires. */
@ -930,17 +913,12 @@ cf_merge_files(char **out_filenamep, int in_file_count,
long data_offset;
progdlg_t *progbar = NULL;
gboolean stop_flag;
/*
* XXX - should be "off_t", but Wiretap would need more work to handle
* the full size of "off_t" on platforms where it's more than a "long"
* as well.
*/
long f_len, file_pos;
float prog_val;
GTimeVal start_time;
gchar status_str[100];
int progbar_nextstep;
int progbar_quantum;
gint64 f_len, file_pos;
float prog_val;
GTimeVal start_time;
gchar status_str[100];
int progbar_nextstep;
int progbar_quantum;
/* open the input files */
if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
@ -1020,7 +998,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
/* Get the sum of the seek positions in all of the files. */
file_pos = 0;
for (i = 0; i < in_file_count; i++)
file_pos += lseek(wtap_fd(in_files[i].wth), 0, SEEK_CUR);
file_pos += wtap_read_so_far(in_files[i].wth, NULL);
prog_val = (gfloat) file_pos / (gfloat) f_len;
if (prog_val > 1.0) {
/* Some file probably grew while we were reading it.
@ -1035,7 +1013,8 @@ cf_merge_files(char **out_filenamep, int in_file_count,
}
if (progbar != NULL) {
g_snprintf(status_str, sizeof(status_str),
"%luKB of %luKB", file_pos / 1024, f_len / 1024);
"%" PRId64 "KB of %" PRId64 "KB",
file_pos / 1024, f_len / 1024);
update_progress_dlg(progbar, prog_val, status_str);
}
progbar_nextstep += progbar_quantum;

View File

@ -116,10 +116,6 @@ static GtkWidget *cfmark_cb;
static GtkWidget *ft_om;
static GtkWidget *range_tb;
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#define PREVIEW_STR_MAX 200
static double
@ -137,9 +133,8 @@ preview_set_filename(GtkWidget *prev, const gchar *cf_name)
wtap *wth;
int err = 0;
gchar *err_info;
struct stat cf_stat;
gchar string_buff[PREVIEW_STR_MAX];
guint64 filesize;
gint64 filesize;
/* init preview labels */
@ -181,14 +176,13 @@ preview_set_filename(GtkWidget *prev, const gchar *cf_name)
}
/* Find the size of the file. */
if (fstat(wtap_fd(wth), &cf_stat) < 0) {
filesize = wtap_file_size(wth, &err);
if (filesize == -1) {
gtk_label_set_text(GTK_LABEL(label), "error getting file size");
wtap_close(wth);
return NULL;
}
/* size */
filesize = cf_stat.st_size;
g_snprintf(string_buff, PREVIEW_STR_MAX, "%" PRIu64 " bytes", filesize);
g_snprintf(string_buff, PREVIEW_STR_MAX, "%" PRId64 " bytes", filesize);
label = OBJECT_GET_DATA(prev, PREVIEW_SIZE_KEY);
gtk_label_set_text(GTK_LABEL(label), string_buff);

View File

@ -36,15 +36,6 @@
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#include <gtk/gtk.h>
#include "globals.h"

12
merge.c
View File

@ -24,10 +24,6 @@
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <string.h>
#include "wtap.h"
#include "merge.h"
@ -43,7 +39,7 @@ merge_open_in_files(int in_file_count, char *const *in_file_names,
int i, j;
int files_size = in_file_count * sizeof(merge_in_file_t);
merge_in_file_t *files;
struct stat statb;
gint64 size;
files = g_malloc(files_size);
*in_files = files;
@ -60,14 +56,14 @@ merge_open_in_files(int in_file_count, char *const *in_file_names,
*err_fileno = i;
return FALSE;
}
if (fstat(wtap_fd(files[i].wth), &statb) < 0) {
*err = errno;
size = wtap_file_size(files[i].wth, err);
if (size == -1) {
for (j = 0; j <= i; j++)
wtap_close(files[j].wth);
*err_fileno = i;
return FALSE;
}
files[i].size = statb.st_size;
files[i].size = size;
}
return TRUE;
}

View File

@ -44,7 +44,7 @@ typedef struct merge_in_file_s {
wtap *wth;
long data_offset;
in_file_state_e state;
long size; /* file size */
gint64 size; /* file size */
} merge_in_file_t;
/** Open a number of input files to merge.

View File

@ -56,10 +56,6 @@
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
@ -69,7 +65,7 @@
#include <time.h>
#include <errno.h>
#include "wiretap/wtap.h"
#include <wiretap/wtap.h>
#include "ringbuffer.h"
/* Win32 needs the O_BINARY flag for open() */

View File

@ -2970,7 +2970,6 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
init_dissection();
cf->wth = wth;
cf->filed = -1; /* not used, but set it anyway */
cf->f_datalen = 0; /* not used, but set it anyway */
cf->f_len = 0; /* not used, but set it anyway */

View File

@ -218,7 +218,7 @@ int ascend_open(wtap *wth, int *err, gchar **err_info _U_)
packet's timestamp from the capture file's ctime, which gives us an
offset that we can apply to each packet.
*/
if (fstat(wtap_fd(wth), &statbuf) == -1) {
if (fstat(wth->fd, &statbuf) == -1) {
*err = errno;
g_free(wth->capture.ascend);
return -1;

View File

@ -27,14 +27,41 @@
#include <string.h>
#include <errno.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
int
wtap_fd(wtap *wth)
/*
* Return the size of the file, as reported by the OS.
* (gint64, in case that's 64 bits.)
*/
gint64
wtap_file_size(wtap *wth, int *err)
{
return wth->fd;
struct stat statb;
if (fstat(wth->fd, &statb) == -1) {
if (err != NULL)
*err = errno;
return -1;
}
return statb.st_size;
}
int
@ -465,6 +492,24 @@ wtap_read(wtap *wth, int *err, gchar **err_info, long *data_offset)
return TRUE; /* success */
}
/*
* Return an approximation of the amount of data we've read sequentially
* from the file so far. (gint64, in case that's 64 bits.)
*/
gint64
wtap_read_so_far(wtap *wth, int *err)
{
off_t file_pos;
file_pos = lseek(wth->fd, 0, SEEK_CUR);
if (file_pos == -1) {
if (err != NULL)
*err = errno;
return -1;
}
return file_pos;
}
struct wtap_pkthdr*
wtap_phdr(wtap *wth)
{

View File

@ -10,8 +10,8 @@ wtap_dump_file
wtap_dump_open
wtap_encap_short_string
wtap_encap_string
wtap_fd
wtap_file_encap
wtap_file_size
wtap_file_type
wtap_file_type_short_string
wtap_file_type_string
@ -23,6 +23,7 @@ wtap_phdr
wtap_process_pcap_packet
wtap_pseudoheader
wtap_read
wtap_read_so_far
wtap_seek_read
wtap_sequential_close
wtap_short_string_to_encap

View File

@ -523,11 +523,17 @@ struct wtap* wtap_open_offline(const char *filename, int *err,
gboolean wtap_read(wtap *wth, int *err, gchar **err_info,
long *data_offset);
/*
* Return an approximation of the amount of data we've read sequentially
* from the file so far. (gint64, in case that's 64 bits.)
*/
gint64 wtap_read_so_far(wtap *wth, int *err);
struct wtap_pkthdr *wtap_phdr(wtap *wth);
union wtap_pseudo_header *wtap_pseudoheader(wtap *wth);
guint8 *wtap_buf_ptr(wtap *wth);
int wtap_fd(wtap *wth);
gint64 wtap_file_size(wtap *wth, int *err);
int wtap_snapshot_length(wtap *wth); /* per file */
int wtap_file_type(wtap *wth);
int wtap_file_encap(wtap *wth);