Update script search logic for new file extension

When searching for script files, look for both the new and old file
extensions.  If a file with ".zeek" can't be found, then search for
a file with ".bro" as a fallback.
This commit is contained in:
Daniel Thayer 2019-04-09 01:26:16 -05:00
parent bff8392ad4
commit 7366155bad
9 changed files with 129 additions and 43 deletions

View file

@ -20,6 +20,7 @@
#endif
#include <string>
#include <array>
#include <vector>
#include <algorithm>
#include <ctype.h>
@ -1007,7 +1008,18 @@ string bro_prefixes()
return rval;
}
const char* PACKAGE_LOADER = "__load__.bro";
const array<string, 2> script_extensions = {".zeek", ".bro"};
bool is_package_loader(const string& path)
{
for (const string& ext : script_extensions)
{
if (path == "__load__" + ext)
return true;
}
return false;
}
FILE* open_file(const string& path, const string& mode)
{
@ -1034,13 +1046,22 @@ static bool can_read(const string& path)
FILE* open_package(string& path, const string& mode)
{
string arg_path = path;
path.append("/").append(PACKAGE_LOADER);
path.append("/__load__");
if ( can_read(path) )
return open_file(path, mode);
for (const string& ext : script_extensions)
{
string p = path + ext;
if ( can_read(p) )
{
path.append(ext);
return open_file(path, mode);
}
}
path.append(script_extensions[0]);
string package_loader = "__load__" + script_extensions[0];
reporter->Error("Failed to open package '%s': missing '%s' file",
arg_path.c_str(), PACKAGE_LOADER);
arg_path.c_str(), package_loader.c_str());
return 0;
}
@ -1123,7 +1144,7 @@ string flatten_script_name(const string& name, const string& prefix)
if ( ! rval.empty() )
rval.append(".");
if ( SafeBasename(name).result == PACKAGE_LOADER )
if ( is_package_loader(SafeBasename(name).result) )
rval.append(SafeDirname(name).result);
else
rval.append(name);
@ -1221,7 +1242,7 @@ string without_bropath_component(const string& path)
}
static string find_file_in_path(const string& filename, const string& path,
const string& opt_ext = "")
const vector<string>& opt_ext)
{
if ( filename.empty() )
return string();
@ -1239,10 +1260,13 @@ static string find_file_in_path(const string& filename, const string& path,
if ( ! opt_ext.empty() )
{
string with_ext = abs_path + '.' + opt_ext;
for (const string& ext : opt_ext)
{
string with_ext = abs_path + ext;
if ( can_read(with_ext) )
return with_ext;
if ( can_read(with_ext) )
return with_ext;
}
}
if ( can_read(abs_path) )
@ -1257,9 +1281,31 @@ string find_file(const string& filename, const string& path_set,
vector<string> paths;
tokenize_string(path_set, ":", &paths);
vector<string> ext;
if ( ! opt_ext.empty() )
ext.push_back(opt_ext);
for ( size_t n = 0; n < paths.size(); ++n )
{
string f = find_file_in_path(filename, paths[n], opt_ext);
string f = find_file_in_path(filename, paths[n], ext);
if ( ! f.empty() )
return f;
}
return string();
}
string find_script_file(const string& filename, const string& path_set)
{
vector<string> paths;
tokenize_string(path_set, ":", &paths);
vector<string> ext(script_extensions.begin(), script_extensions.end());
for ( size_t n = 0; n < paths.size(); ++n )
{
string f = find_file_in_path(filename, paths[n], ext);
if ( ! f.empty() )
return f;