Skip to content

Commit

Permalink
Use g_file_open_tmp within create_tempfile
Browse files Browse the repository at this point in the history
Much better to use a known library than create it ourselves.

Also remove get_tempfile_path as it's not used.

Bug: 15992
Change-Id: I17b9bd879e8bdb540f79db83c6c138f8ee724764
Reviewed-on: https://code.wireshark.org/review/34420
Reviewed-by: Tomasz Moń <desowin@gmail.com>
Petri-Dish: Tomasz Moń <desowin@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Roland Knall <rknall@gmail.com>
  • Loading branch information
mmann78 authored and rknall committed Dec 20, 2019
1 parent c247a83 commit 2925fb0
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 199 deletions.
1 change: 0 additions & 1 deletion debian/libwsutil0.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ libwsutil.so.0 libwsutil0 #MINVER#
get_progfile_dir@Base 1.12.0~rc1
get_resource_usage@Base 2.3.0
get_systemfile_dir@Base 1.12.0~rc1
get_tempfile_path@Base 1.12.0~rc1
has_global_profiles@Base 1.12.0~rc1
hkdf_expand@Base 2.5.1
ieee80211_chan_to_mhz@Base 1.99.7
Expand Down
8 changes: 4 additions & 4 deletions dumpcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3428,10 +3428,10 @@ static gboolean
capture_loop_open_output(capture_options *capture_opts, int *save_file_fd,
char *errmsg, int errmsg_len)
{
char *tmpname;
gchar *capfile_name;
gchar *prefix, *suffix;
gboolean is_tempfile;
GError *err_tempfile = NULL;

g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_open_output: %s",
(capture_opts->save_file) ? capture_opts->save_file : "(not specified)");
Expand Down Expand Up @@ -3556,9 +3556,8 @@ capture_loop_open_output(capture_options *capture_opts, int *save_file_fd,
} else {
suffix = ".pcap";
}
*save_file_fd = create_tempfile(&tmpname, prefix, suffix);
*save_file_fd = create_tempfile(&capfile_name, prefix, suffix, &err_tempfile);
g_free(prefix);
capfile_name = g_strdup(tmpname);
is_tempfile = TRUE;
}

Expand All @@ -3567,7 +3566,8 @@ capture_loop_open_output(capture_options *capture_opts, int *save_file_fd,
if (is_tempfile) {
g_snprintf(errmsg, errmsg_len,
"The temporary file to which the capture would be saved (\"%s\") "
"could not be opened: %s.", capfile_name, g_strerror(errno));
"could not be opened: %s.", capfile_name, err_tempfile->message);
g_error_free(err_tempfile);
} else {
if (capture_opts->multi_files_on) {
/* Ensures that the ringbuffer is not used. This ensures that
Expand Down
9 changes: 6 additions & 3 deletions extcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,7 @@ static gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gcha
int fd = 0;

gchar *pfx = g_strconcat(pipe_prefix, "_", ifname, NULL);
if ((fd = create_tempfile(&temp_name, pfx, NULL)) < 0)
if ((fd = create_tempfile(&temp_name, pfx, NULL, NULL)) < 0)
{
g_free(pfx);
return FALSE;
Expand All @@ -1560,9 +1560,12 @@ static gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gcha

if (mkfifo(temp_name, 0600) == 0)
{
*fifo = g_strdup(temp_name);
*fifo = temp_name;
}
else
{
g_free(temp_name);
}

return TRUE;
}
#endif
Expand Down
1 change: 0 additions & 1 deletion file.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <ctype.h>
#include <errno.h>

#include <wsutil/tempfile.h>
#include <wsutil/file_util.h>
#include <wsutil/filesystem.h>
#include <wsutil/json_dumper.h>
Expand Down
11 changes: 8 additions & 3 deletions ui/export_pdu_ui_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ static void
exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
{
int import_file_fd;
char *tmpname, *capfile_name, *comment;
char *capfile_name, *comment;
int err;

/* Choose a random name for the temporary import buffer */
import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_", NULL);
capfile_name = g_strdup(tmpname);
GError *err_tempfile = NULL;
import_file_fd = create_tempfile(&capfile_name, "Wireshark_PDU_", NULL, &err_tempfile);
if (import_file_fd < 0) {
failure_alert_box("Temporary file could not be created: %s", err_tempfile->message);
g_error_free(err_tempfile);
goto end;
}

comment = g_strdup_printf("Dump of PDUs from %s", cfile.filename);
err = exp_pdu_open(exp_pdu_tap_data, import_file_fd, comment);
Expand Down
30 changes: 16 additions & 14 deletions ui/qt/endpoint_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "wsutil/file_util.h"
#include "wsutil/pint.h"
#include "wsutil/str_util.h"
#include "wsutil/tempfile.h"
#include <wsutil/utf8_entities.h>

#include <ui/qt/utils/qt_ui_utils.h>
Expand All @@ -33,6 +32,7 @@
#include <QMessageBox>
#include <QPushButton>
#include <QUrl>
#include <QTemporaryFile>

static const QString table_name_ = QObject::tr("Endpoint");
EndpointDialog::EndpointDialog(QWidget &parent, CaptureFile &cf, int cli_proto_id, const char *filter) :
Expand Down Expand Up @@ -200,47 +200,49 @@ QUrl EndpointDialog::createMap(bool json_only)
g_ptr_array_add(hosts_arr, NULL);
hostlist_talker_t **hosts = (hostlist_talker_t **)g_ptr_array_free(hosts_arr, FALSE);

char *map_path = NULL;
int fd = create_tempfile(&map_path, "ipmap", ".html");
FILE *fp = NULL;
if (fd != -1) {
fp = ws_fdopen(fd, "wb");
if (!fp) {
ws_close(fd);
fd = -1;
}
QTemporaryFile tf("ipmapXXXXXX.html");
tf.setAutoRemove(false);
if (!tf.open()) {
QMessageBox::warning(this, tr("Map file error"), tr("Unable to create temporary file"));
g_free(hosts);
return QUrl();
}
int fd = tf.handle();
FILE* fp = ws_fdopen(fd, "wb");
if (fd == -1) {
QMessageBox::warning(this, tr("Map file error"), tr("Unable to create temporary file"));
g_free(hosts);
tf.remove();
return QUrl();
}
QString map_path_str(map_path);

gchar *err_str;
if (!write_endpoint_geoip_map(fp, json_only, hosts, &err_str)) {
QMessageBox::warning(this, tr("Map file error"), err_str);
g_free(err_str);
g_free(hosts);
fclose(fp);
ws_unlink(qPrintable(map_path_str));
tf.remove();
return QUrl();
}
g_free(hosts);
if (fclose(fp) == EOF) {
QMessageBox::warning(this, tr("Map file error"), g_strerror(errno));
ws_unlink(qPrintable(map_path_str));
tf.remove();
return QUrl();
}

return QUrl::fromLocalFile(map_path_str);
return QUrl::fromLocalFile(tf.fileName());
}

void EndpointDialog::openMap()
{
QUrl map_file = createMap(false);
if (!map_file.isEmpty()) {
QDesktopServices::openUrl(map_file);
QString source_file = map_file.toLocalFile();
if (!source_file.isEmpty())
QFile::remove(source_file);
}
}

Expand Down
8 changes: 3 additions & 5 deletions wiretap/file_access.c
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,6 @@ wtap_dump_open_tempfile(char **filenamep, const char *pfx,
int fd;
const char *ext;
char sfx[16];
char *tmpname;
wtap_dumper *wdh;
WFILE_T fh;

Expand All @@ -2444,13 +2443,12 @@ wtap_dump_open_tempfile(char **filenamep, const char *pfx,
g_strlcat(sfx, ext, 16);

/* Choose a random name for the file */
fd = create_tempfile(&tmpname, pfx, sfx);
fd = create_tempfile(filenamep, pfx, sfx, NULL);
if (fd == -1) {
*err = errno;
*err = WTAP_ERR_CANT_OPEN;
g_free(wdh);
return NULL; /* can't create file */
}
*filenamep = g_strdup(tmpname);

/* In case "fopen()" fails but doesn't set "errno", set "errno"
to a generic "the open failed" error. */
Expand All @@ -2468,7 +2466,7 @@ wtap_dump_open_tempfile(char **filenamep, const char *pfx,
/* Get rid of the file we created; we couldn't finish
opening it. */
wtap_dump_file_close(wdh);
ws_unlink(tmpname);
ws_unlink(*filenamep);
g_free(wdh);
return NULL;
}
Expand Down
9 changes: 6 additions & 3 deletions wiretap/nettrace_3gpp_32_423.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ nettrace_close(wtap *wth)

/* delete the temp file */
ws_unlink(file_info->tmpname);

g_free(file_info->tmpname);
}

/* This attribute specification contains a timestamp that refers to the start of the
Expand Down Expand Up @@ -813,7 +813,7 @@ create_temp_pcapng_file(wtap *wth, int *err, gchar **err_info, nettrace_3gpp_32_
wtap_open_return_val result = WTAP_OPEN_MINE;

/* pcapng defs */
GArray *shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
GArray *shb_hdrs;
wtap_block_t shb_hdr;
wtapng_iface_descriptions_t *idb_inf = NULL;
wtap_block_t int_data;
Expand All @@ -839,10 +839,13 @@ create_temp_pcapng_file(wtap *wth, int *err, gchar **err_info, nettrace_3gpp_32_

memset(&exported_pdu_info, 0x0, sizeof(exported_pdu_info_t));

import_file_fd = create_tempfile(&(file_info->tmpname), "Wireshark_PDU_", NULL);
import_file_fd = create_tempfile(&(file_info->tmpname), "Wireshark_PDU_", NULL, NULL);
if (import_file_fd < 0)
return WTAP_OPEN_ERROR;

/* Now open a file and dump to it */
/* Create data for SHB */
shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
os_info_str = g_string_new("");
get_os_version_info(os_info_str);

Expand Down
Loading

0 comments on commit 2925fb0

Please sign in to comment.