Skip to content

Commit

Permalink
Have a common "capture file close alert box" routine.
Browse files Browse the repository at this point in the history
Take cf_close_failure_alert_box() and put it into libui, with the name
cfile_close_failure_alert_box().  Use it not only in file.c but also
in ui/export_pdu_ui_utils.c, ui/gtk/file_import_dlg.c, and
ui/qt/import_text_dialog.cpp where the error we get back isn't
necessarily an errno.

Have ui/gtk/file_import_dlg.c and ui/qt/import_text_dialog.cpp also use
cfile_open_failure_alert_box() on open errors.

Change-Id: I987f339a23ea58609390306a319923e7f92d5c07
Reviewed-on: https://code.wireshark.org/review/21203
Reviewed-by: Guy Harris <guy@alum.mit.edu>
  • Loading branch information
guyharris committed Apr 18, 2017
1 parent 870b3d2 commit 1015fa0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 48 deletions.
45 changes: 2 additions & 43 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ static gboolean find_packet(capture_file *cf,
static const char *cf_get_user_packet_comment(capture_file *cf, const frame_data *fd);

static void cf_rename_failure_alert_box(const char *filename, int err);
static void cf_close_failure_alert_box(const char *filename, int err);
static void ref_time_packets(capture_file *cf);

/* Seconds spent processing packets between pushing UI updates. */
Expand Down Expand Up @@ -4658,7 +4657,7 @@ cf_save_records(capture_file *cf, const char *fname, guint save_format,
}

if (!wtap_dump_close(pdh, &err)) {
cf_close_failure_alert_box(fname, err);
cfile_close_failure_alert_box(fname, err);
goto fail;
}

Expand Down Expand Up @@ -4897,7 +4896,7 @@ cf_export_specified_packets(capture_file *cf, const char *fname,
}

if (!wtap_dump_close(pdh, &err)) {
cf_close_failure_alert_box(fname, err);
cfile_close_failure_alert_box(fname, err);
goto fail;
}

Expand Down Expand Up @@ -4968,46 +4967,6 @@ cf_rename_failure_alert_box(const char *filename, int err)
g_free(display_basename);
}

/* Check for write errors - if the file is being written to an NFS server,
a write error may not show up until the file is closed, as NFS clients
might not send writes to the server until the "write()" call finishes,
so that the write may fail on the server but the "write()" may succeed. */
static void
cf_close_failure_alert_box(const char *filename, int err)
{
gchar *display_basename;

if (err < 0) {
/* Wiretap error. */
display_basename = g_filename_display_basename(filename);
switch (err) {

case WTAP_ERR_CANT_CLOSE:
simple_error_message_box(
"The file \"%s\" couldn't be closed for some unknown reason.",
display_basename);
break;

case WTAP_ERR_SHORT_WRITE:
simple_error_message_box(
"Not all the packets could be written to the file \"%s\".",
display_basename);
break;

default:
simple_error_message_box(
"An error occurred while closing the file \"%s\": %s.",
display_basename, wtap_strerror(err));
break;
}
g_free(display_basename);
} else {
/* OS error.
We assume that a close error from the OS is really a write error. */
write_failure_alert_box(filename, err);
}
}

/* Reload the current capture file. */
void
cf_reload(capture_file *cf) {
Expand Down
69 changes: 69 additions & 0 deletions ui/alert_box.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,75 @@ cfile_open_failure_alert_box(const char *filename, int err, gchar *err_info,
}
}

/*
* Alert box for a failed attempt to close a capture file.
* "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value;
* "err_info" is assumed to be a string giving further information for
* some WTAP_ERR_ values; "for_writing" is TRUE if the file is being
* opened for writing and FALSE if it's being opened for reading;
* "file_type" is a WTAP_FILE_TYPE_SUBTYPE_ value for the type of
* file being written (it's ignored for opening-for-reading errors).
*
* When closing a capture file:
*
* some information in the file that can't be determined until
* all packets have been written might be written to the file
* (such as a table of the file offsets of all packets);
*
* data buffered in the low-level file writing code might be
* flushed to the file;
*
* for remote file systems, data written to the file but not
* yet sent to the server might be sent to the server or, if
* that data was sent asynchronously, "out of space", "disk
* quota exceeded", or "I/O error" indications might have
* been received but not yet delivered, and the close operation
* could deliver them;
*
* so we have to check for write errors here.
*
* XXX - add explanatory secondary text for at least some of the errors;
* various HIGs suggest that you should, for example, suggest that the
* user remove files if the file system is full. Perhaps that's because
* they're providing guidelines for people less sophisticated than the
* typical Wireshark user is, but....
*/
void
cfile_close_failure_alert_box(const char *filename, int err)
{
gchar *display_basename;

if (err < 0) {
/* Wiretap error. */
display_basename = g_filename_display_basename(filename);
switch (err) {

case WTAP_ERR_CANT_CLOSE:
simple_error_message_box(
"The file \"%s\" couldn't be closed for some unknown reason.",
display_basename);
break;

case WTAP_ERR_SHORT_WRITE:
simple_error_message_box(
"Not all the packets could be written to the file \"%s\".",
display_basename);
break;

default:
simple_error_message_box(
"An error occurred while closing the file \"%s\": %s.",
display_basename, wtap_strerror(err));
break;
}
g_free(display_basename);
} else {
/* OS error.
We assume that a close error from the OS is really a write error. */
write_failure_alert_box(filename, err);
}
}

/*
* Alert box for a failed attempt to open or create a file.
* "err" is assumed to be a UNIX-style errno; "for_writing" is TRUE if
Expand Down
29 changes: 29 additions & 0 deletions ui/alert_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ extern void cfile_open_failure_alert_box(const char *filename, int err,
gchar *err_info, gboolean for_writing,
int file_type);

/*
* Alert box for a failed attempt to close a capture file.
* "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value;
* "err_info" is assumed to be a string giving further information for
* some WTAP_ERR_ values; "for_writing" is TRUE if the file is being
* opened for writing and FALSE if it's being opened for reading;
* "file_type" is a WTAP_FILE_TYPE_SUBTYPE_ value for the type of
* file being written (it's ignored for opening-for-reading errors).
*
* When closing a capture file:
*
* some information in the file that can't be determined until
* all packets have been written might be written to the file
* (such as a table of the file offsets of all packets);
*
* data buffered in the low-level file writing code might be
* flushed to the file;
*
* for remote file systems, data written to the file but not
* yet sent to the server might be sent to the server or, if
* that data was sent asynchronously, "out of space", "disk
* quota exceeded", or "I/O error" indications might have
* been received but not yet delivered, and the close operation
* could deliver them;
*
* so we have to check for write errors here.
*/
extern void cfile_close_failure_alert_box(const char *filename, int err);

/*
* Alert box for a failed attempt to open or create a file.
* "err" is assumed to be a UNIX-style errno; "for_writing" is TRUE if
Expand Down
2 changes: 1 addition & 1 deletion ui/export_pdu_ui_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)

err = exp_pdu_close(exp_pdu_tap_data);
if (err!= 0) {
write_failure_alert_box(capfile_name, err);
cfile_close_failure_alert_box(capfile_name, err);
}

/* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
Expand Down
6 changes: 4 additions & 2 deletions ui/gtk/file_import_dlg.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,9 @@ file_import_open(text_import_info_t *info)
shb_hdrs, idb_inf, NULL, &err);
capfile_name = g_strdup(tmpname);
if (info->wdh == NULL) {
open_failure_alert_box(tmpname ? tmpname : "temporary file", err, TRUE);
cfile_open_failure_alert_box(tmpname ? tmpname : "temporary file",
err, NULL, TRUE,
WTAP_FILE_TYPE_SUBTYPE_PCAPNG);
fclose(info->import_text_file);
goto end;
}
Expand All @@ -533,7 +535,7 @@ file_import_open(text_import_info_t *info)
}

if (!wtap_dump_close(info->wdh, &err)) {
write_failure_alert_box(capfile_name, err);
cfile_close_failure_alert_box(capfile_name, err);
}

if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
Expand Down
4 changes: 2 additions & 2 deletions ui/qt/import_text_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void ImportTextDialog::convertTextFile() {
capfile_name_.append(tmpname ? tmpname : "temporary file");
qDebug() << capfile_name_ << ":" << import_info_.wdh << import_info_.encapsulation << import_info_.max_frame_length;
if (import_info_.wdh == NULL) {
open_failure_alert_box(capfile_name_.toUtf8().constData(), err, TRUE);
cfile_open_failure_alert_box(capfile_name_.toUtf8().constData(), err, NULL, TRUE, WTAP_FILE_TYPE_SUBTYPE_PCAP);
fclose(import_info_.import_text_file);
setResult(QDialog::Rejected);
return;
Expand All @@ -155,7 +155,7 @@ void ImportTextDialog::convertTextFile() {

if (!wtap_dump_close(import_info_.wdh, &err))
{
write_failure_alert_box(capfile_name_.toUtf8().constData(), err);
cfile_close_failure_alert_box(capfile_name_.toUtf8().constData(), err);
}
}

Expand Down

0 comments on commit 1015fa0

Please sign in to comment.