From 0d2e248a25f5e2bd4af5859521d4fc6b7be743c1 Mon Sep 17 00:00:00 2001 From: John Thacker Date: Mon, 30 May 2022 15:18:50 -0400 Subject: [PATCH] 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 --- file.c | 14 ++++++-------- wiretap/file_access.c | 4 ++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/file.c b/file.c index 0d62d51cda..52e069e8c2 100644 --- a/file.c +++ b/file.c @@ -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 { diff --git a/wiretap/file_access.c b/wiretap/file_access.c index 9724c8ef9e..7b7e3f8b66 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -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; }