In the Lua dumper code, don't bother checking whether the encapsulation

is supported before trying to open for writing - the attempt to open for
writing will do the check for you.  Instead, check for specific errors
if the attempt to open for writing fails, and use somewhat more specific
error messages for certain error codes.  (We should perhaps check for
even more error codes in those cases.)

That gets rid of all external calls to wtap_dump_can_write_encap(), so
remove it from wtap.h and make it static.

svn path=/trunk/; revision=48691
This commit is contained in:
Guy Harris 2013-04-01 21:39:28 +00:00
parent b1ecd8d217
commit d517ab3698
3 changed files with 51 additions and 32 deletions

View File

@ -214,16 +214,28 @@ WSLUA_CONSTRUCTOR Dumper_new(lua_State* L) {
filename = cross_plat_fname(fname);
if (!wtap_dump_can_write_encap(filetype, encap))
WSLUA_ERROR(Dumper_new,"Not every filetype handles every encap");
d = wtap_dump_open(filename, filetype, encap,0 , FALSE, &err);
d = wtap_dump_open(filename, filetype, encap, 0, FALSE, &err);
if (! d ) {
/* WSLUA_ERROR("Error while opening file for writing"); */
luaL_error(L,"error while opening `%s': %s",
filename,
wtap_strerror(err));
switch (err) {
case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
luaL_error(L,"Files of file type %s cannot be written",
wtap_file_type_string(filetype));
break;
case WTAP_ERR_UNSUPPORTED_ENCAP:
luaL_error(L,"Files of file type %s don't support encapsulation %s",
wtap_file_type_string(filetype),
wtap_encap_short_string(encap));
break;
default:
luaL_error(L,"error while opening `%s': %s",
filename,
wtap_strerror(err));
break;
}
return 0;
}
@ -338,19 +350,27 @@ WSLUA_METHOD Dumper_new_for_current(lua_State* L) {
encap = lua_pinfo->fd->lnk_t;
if (!wtap_dump_can_write_encap(filetype, encap)) {
luaL_error(L,"Cannot write encap %s in filetype %s",
wtap_encap_short_string(encap),
wtap_file_type_string(filetype));
return 0;
}
d = wtap_dump_open(filename, filetype, encap, 0 , FALSE, &err);
d = wtap_dump_open(filename, filetype, encap, 0, FALSE, &err);
if (! d ) {
luaL_error(L,"error while opening `%s': %s",
filename,
wtap_strerror(err));
switch (err) {
case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
luaL_error(L,"Files of file type %s cannot be written",
wtap_file_type_string(filetype));
break;
case WTAP_ERR_UNSUPPORTED_ENCAP:
luaL_error(L,"Files of file type %s don't support encapsulation %s",
wtap_file_type_string(filetype),
wtap_encap_short_string(encap));
break;
default:
luaL_error(L,"error while opening `%s': %s",
filename,
wtap_strerror(err));
break;
}
return 0;
}

View File

@ -876,6 +876,19 @@ wtap_dump_file_encap_type(const GArray *file_encaps)
return encap;
}
static gboolean
wtap_dump_can_write_encap(int filetype, int encap)
{
if (filetype < 0 || filetype >= wtap_num_file_types
|| dump_open_table[filetype].can_write_encap == NULL)
return FALSE;
if ((*dump_open_table[filetype].can_write_encap)(encap) != 0)
return FALSE;
return TRUE;
}
/*
* Return TRUE if a capture with a given GArray of encapsulation types
* and a given bitset of comment types can be written in a specified
@ -1187,18 +1200,6 @@ gboolean wtap_dump_can_open(int filetype)
return TRUE;
}
gboolean wtap_dump_can_write_encap(int filetype, int encap)
{
if (filetype < 0 || filetype >= wtap_num_file_types
|| dump_open_table[filetype].can_write_encap == NULL)
return FALSE;
if ((*dump_open_table[filetype].can_write_encap)(encap) != 0)
return FALSE;
return TRUE;
}
#ifdef HAVE_LIBZ
gboolean wtap_dump_can_compress(int filetype)
{

View File

@ -1238,8 +1238,6 @@ void wtap_close(wtap *wth);
/*** dump packets into a capture file ***/
WS_DLL_PUBLIC
gboolean wtap_dump_can_open(int filetype);
WS_DLL_PUBLIC
gboolean wtap_dump_can_write_encap(int filetype, int encap);
/**
* Given a GArray of WTAP_ENCAP_ types, return the per-file encapsulation