zeek/src/ScannedFile.cc
Robin Sommer 34eaf42b92 Add new hook HookLoadFileExtended that allows plugins to supply Zeek script code to parse.
The new hooks works similar to the existing `HookLoadFile` but,
additionally, allows the plugin to return a string that contains the
code to be used for the file being loaded. If the plugin does so, the
content of any actual file on disk will be ignored (in fact, there
doesn't even need to be a file on disk in that case). This works for
both Zeek scripts and signatures.

There's a new test that covers the new functionality, testing loading
both scripts and signatures from memory. I also manually tested that the
debugger integration works, but I don't see much of a way to add a
regression test for that part.

We keep the existing hook as well for backwards compatibility. We could
decide to deprecate it, but not sure that buys us much, so left that
out.

Closes #1757.
2021-11-05 13:01:19 +01:00

57 lines
1.4 KiB
C++

#include "zeek/ScannedFile.h"
#include <errno.h>
#include <limits.h> // for PATH_MAX
#include "zeek/DebugLogger.h"
#include "zeek/Reporter.h"
namespace zeek::detail
{
std::list<ScannedFile> files_scanned;
std::vector<SignatureFile> sig_files;
ScannedFile::ScannedFile(int arg_include_level, std::string arg_name, bool arg_skipped,
bool arg_prefixes_checked)
: include_level(arg_include_level), skipped(arg_skipped),
prefixes_checked(arg_prefixes_checked), name(std::move(arg_name))
{
if ( name == canonical_stdin_path )
canonical_path = canonical_stdin_path;
else
{
char buf[PATH_MAX];
auto res = realpath(name.data(), buf);
if ( ! res )
zeek::reporter->FatalError("failed to get realpath() of %s: %s", name.data(),
strerror(errno));
canonical_path = res;
}
}
bool ScannedFile::AlreadyScanned() const
{
auto rval = false;
for ( const auto& it : files_scanned )
if ( it.canonical_path == canonical_path )
{
rval = true;
break;
}
DBG_LOG(zeek::DBG_SCRIPTS, "AlreadyScanned result (%d) %s", rval, canonical_path.data());
return rval;
}
SignatureFile::SignatureFile(std::string file) : file(std::move(file)) { }
SignatureFile::SignatureFile(std::string file, std::string full_path)
: file(std::move(file)), full_path(std::move(full_path))
{
}
} // namespace zeek::detail