diff options
author | Guy Harris <guy@alum.mit.edu> | 2015-11-11 16:59:10 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2015-11-12 00:59:40 +0000 |
commit | 75dc2051e861294a0e75955e896de7494f086b42 (patch) | |
tree | 5e4738506009583066d083b648958bef11115590 /wiretap | |
parent | af8c70cb7d7b81492df7dc11690195598086990c (diff) |
Add wtap_dump_open_tempfile routines, to write to a temporary file.
It includes the temporary-file generation, so you don't have to do it
yourself.
Change-Id: I0798df95a5c5646224ec49612f50b423ed78547a
Reviewed-on: https://code.wireshark.org/review/11751
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'wiretap')
-rw-r--r-- | wiretap/file_access.c | 57 | ||||
-rw-r--r-- | wiretap/wtap.h | 31 |
2 files changed, 88 insertions, 0 deletions
diff --git a/wiretap/file_access.c b/wiretap/file_access.c index 14c9b81b96..3ef75283bc 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -26,6 +26,7 @@ #include <errno.h> #include <wsutil/file_util.h> +#include <wsutil/tempfile.h> #include "wtap-int.h" #include "file_wrappers.h" @@ -2240,6 +2241,62 @@ wtap_dump_open_ng(const char *filename, int file_type_subtype, int encap, } wtap_dumper * +wtap_dump_open_tempfile(char **filenamep, const char *pfx, + int file_type_subtype, int encap, + int snaplen, gboolean compressed, int *err) +{ + return wtap_dump_open_tempfile_ng(filenamep, pfx, file_type_subtype, encap,snaplen, compressed, NULL, NULL, NULL, err); +} + +wtap_dumper * +wtap_dump_open_tempfile_ng(char **filenamep, const char *pfx, + int file_type_subtype, int encap, + int snaplen, gboolean compressed, + wtapng_section_t *shb_hdr, + wtapng_iface_descriptions_t *idb_inf, + wtapng_name_res_t *nrb_hdr, int *err) +{ + int fd; + char *tmpname; + wtap_dumper *wdh; + WFILE_T fh; + + /* No path name for the temporary file yet. */ + *filenamep = NULL; + + /* Allocate and initialize a data structure for the output stream. */ + wdh = wtap_dump_init_dumper(file_type_subtype, encap, snaplen, compressed, + shb_hdr, idb_inf, nrb_hdr, err); + if (wdh == NULL) + return NULL; + + /* Choose a random name for the file */ + fd = create_tempfile(&tmpname, pfx); + *filenamep = tmpname; + + /* In case "fopen()" fails but doesn't set "errno", set "errno" + to a generic "the open failed" error. */ + errno = WTAP_ERR_CANT_OPEN; + fh = wtap_dump_file_fdopen(wdh, fd); + if (fh == NULL) { + *err = errno; + g_free(wdh); + return NULL; /* can't create file */ + } + wdh->fh = fh; + + if (!wtap_dump_open_finish(wdh, file_type_subtype, compressed, err)) { + /* Get rid of the file we created; we couldn't finish + opening it. */ + wtap_dump_file_close(wdh); + ws_unlink(tmpname); + g_free(wdh); + return NULL; + } + return wdh; +} + +wtap_dumper * wtap_dump_fdopen(int fd, int file_type_subtype, int encap, int snaplen, gboolean compressed, int *err) { diff --git a/wiretap/wtap.h b/wiretap/wtap.h index ccbc598c5e..e11c61adce 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -1941,6 +1941,37 @@ wtap_dumper* wtap_dump_open_ng(const char *filename, int file_type_subtype, int wtapng_name_res_t *nrb_hdr, int *err); WS_DLL_PUBLIC +wtap_dumper* wtap_dump_open_tempfile(char **filenamep, const char *pfx, + int file_type_subtype, int encap, int snaplen, gboolean compressed, + int *err); + +/** + * @brief Creates a dumper for a temporary file. + * + * @note The shb_hdr, idb_inf, and nrb_hdr arguments will be used until + * wtap_dump_close() is called, but will not be free'd by the dumper. If + * you created them, you must free them yourself after wtap_dump_close(). + * + * @param filenamep Points to a pointer that's set to point to the + * pathname of the temporary file; it's allocated with g_malloc() + * @param pfx A string to be used as the prefix for the temporary file name + * @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type. + * @param encap The WTAP_ENCAP_XXX encapsulation type (WTAP_ENCAP_PER_PACKET for multi) + * @param snaplen The maximum packet capture length. + * @param compressed True if file should be compressed. + * @param shb_hdr The section header block information, or NULL. + * @param idb_inf The interface description information, or NULL. + * @param nrb_hdr The name resolution comment/custom_opts information, or NULL. + * @param[out] err Will be set to an error code on failure. + * @return The newly created dumper object, or NULL on failure. + */ +WS_DLL_PUBLIC +wtap_dumper* wtap_dump_open_tempfile_ng(char **filenamep, const char *pfx, + int file_type_subtype, int encap, int snaplen, gboolean compressed, + wtapng_section_t *shb_hdr, wtapng_iface_descriptions_t *idb_inf, + wtapng_name_res_t *nrb_hdr, int *err); + +WS_DLL_PUBLIC wtap_dumper* wtap_dump_fdopen(int fd, int file_type_subtype, int encap, int snaplen, gboolean compressed, int *err); |