file: Only change the file descriptors on a Save with Copy

If we do a save with copy, so that we just copied the binary
file, everything in the wtap structure should be the same except
for the filename and the file descriptors, so just change that
instead of closing wtap and reopening it.

The current behavior of calling wtap_open_offline does not work
for files that have blocks (SHBs, IDBs, NRBs, DSBs, ISBs, etc.) in
the middle of the file instead of at the beginning, but we shouldn't
have to waste time rescanning the entire file either.

In the case where a specific file format reader was manually selected,
this will keep the same file format as selected instead of switching to
the auto-detection when opening the copy, just as SAVE_WITH_MOVE already
does and presumably what the user wants.

Update wtap_fdreopen to change the wtap struct's pathname if
wtap_fdreopen is called with a different filename than currently.

Fix #17472
This commit is contained in:
John Thacker 2022-05-30 15:18:50 -04:00 committed by A Wireshark GitLab Utility
parent 429c7a6ce3
commit 0d2e248a25
2 changed files with 10 additions and 8 deletions

14
file.c
View File

@ -4961,15 +4961,13 @@ cf_save_records(capture_file *cf, const char *fname, guint save_format,
case SAVE_WITH_COPY:
/* We just copied the file, so all the information other than
the wtap structure, the filename, and the "is temporary"
the file descriptors, the filename, and the "is temporary"
status applies to the new file; just update that. */
wtap_close(cf->provider.wth);
/* Although we're just "copying" and then opening the copy, it will
try all open_routine readers to open the copy, so we need to
reset the cfile's open_type. */
cf->open_type = WTAP_TYPE_AUTO;
cf->provider.wth = wtap_open_offline(fname, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
if (cf->provider.wth == NULL) {
wtap_fdclose(cf->provider.wth);
/* Attempt to reopen the random file descriptor using the
new file's filename. (At this point, the sequential
file descriptor is closed.) */
if (!wtap_fdreopen(cf->provider.wth, fname, &err)) {
cfile_open_failure_alert_box(fname, err, err_info);
cf_close(cf);
} else {

View File

@ -1237,6 +1237,10 @@ wtap_fdreopen(wtap *wth, const char *filename, int *err)
*err = errno;
return FALSE;
}
if (strcmp(filename, wth->pathname) != 0) {
g_free(wth->pathname);
wth->pathname = g_strdup(filename);
}
return TRUE;
}