Move ScannedFile class and associated globals into ScannedFile.h and out of Net.h and scan.l

This commit is contained in:
Tim Wojtulewicz 2020-08-19 16:45:44 -07:00
parent 01ce18894b
commit d608e7c9a5
7 changed files with 120 additions and 91 deletions

View file

@ -255,6 +255,7 @@ set(MAIN_SRCS
NFA.cc
Net.cc
NetVar.cc
Notifier.cc
Obj.cc
OpaqueVal.cc
Options.cc
@ -270,12 +271,12 @@ set(MAIN_SRCS
RuleAction.cc
RuleCondition.cc
RuleMatcher.cc
SmithWaterman.cc
ScannedFile.cc
Scope.cc
ScriptCoverageManager.cc
SerializationFormat.cc
Sessions.cc
Notifier.cc
SmithWaterman.cc
Stats.cc
Stmt.cc
Tag.cc

View file

@ -397,9 +397,6 @@ void net_delete()
delete zeek::detail::ip_anonymizer[i];
}
std::list<ScannedFile> files_scanned;
std::vector<std::string> sig_files;
} // namespace detail
bool reading_live = false;
@ -461,6 +458,3 @@ bool& is_parsing = zeek::net::is_parsing;
const zeek::Packet*& current_pkt = zeek::net::current_pkt;
int& current_dispatched = zeek::net::current_dispatched;
double& current_timestamp = zeek::net::current_timestamp;
std::list<zeek::net::detail::ScannedFile>& files_scanned = zeek::net::detail::files_scanned;
std::vector<std::string>& sig_files = zeek::net::detail::sig_files;

View file

@ -11,6 +11,8 @@
#include <string>
#include <optional>
#include "ScannedFile.h"
ZEEK_FORWARD_DECLARE_NAMESPACED(IOSource, zeek, iosource);
ZEEK_FORWARD_DECLARE_NAMESPACED(PktSrc, zeek, iosource);
ZEEK_FORWARD_DECLARE_NAMESPACED(PktDumper, zeek, iosource);
@ -44,30 +46,6 @@ extern zeek::iosource::PktDumper* pkt_dumper; // where to save packets
// on future timers).
extern bool have_pending_timers;
// Script file we have already scanned (or are in the process of scanning).
// They are identified by normalized realpath.
struct ScannedFile {
int include_level;
bool skipped; // This ScannedFile was @unload'd.
bool prefixes_checked; // If loading prefixes for this file has been tried.
std::string name;
std::string canonical_path; // normalized, absolute path via realpath()
ScannedFile(int arg_include_level,
std::string arg_name, bool arg_skipped = false,
bool arg_prefixes_checked = false);
/**
* Compares the canonical path of this file against every canonical path
* in files_scanned and returns whether there's any match.
*/
bool AlreadyScanned() const;
};
extern std::list<ScannedFile> files_scanned;
extern std::vector<std::string> sig_files;
} // namespace detail
// Functions to temporarily suspend processing of live input (network packets
@ -144,7 +122,3 @@ extern bool& is_parsing [[deprecated("Remove in v4.1. Use zeek::net::is_parsing.
extern const zeek::Packet*& current_pkt [[deprecated("Remove in v4.1. Use zeek::net::current_pkt.")]];
extern int& current_dispatched [[deprecated("Remove in v4.1. Use zeek::net::current_dispatched.")]];
extern double& current_timestamp [[deprecated("Remove in v4.1. Use zeek::net::current_timestamp.")]];
using ScannedFile [[deprecated("Remove in v4.1. Use zeek::net::detail::ScannedFile.")]] = zeek::net::detail::ScannedFile;
extern std::list<zeek::net::detail::ScannedFile>& files_scanned [[deprecated("Remove in v4.1. Use zeek::net::detail::files_scanned.")]];
extern std::vector<std::string>& sig_files [[deprecated("Remove in v4.1. Use zeek::net::detail::sig_files.")]];

56
src/ScannedFile.cc Normal file
View file

@ -0,0 +1,56 @@
#include "ScannedFile.h"
#include <sys/errno.h>
#include <limits.h> // for PATH_MAX
#include "DebugLogger.h"
#include "Reporter.h"
namespace zeek::detail {
std::list<ScannedFile> files_scanned;
std::vector<std::string> 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;
}
} // namespace zeek::detail
std::list<zeek::detail::ScannedFile>& files_scanned = zeek::detail::files_scanned;
std::vector<std::string>& sig_files = zeek::detail::sig_files;

44
src/ScannedFile.h Normal file
View file

@ -0,0 +1,44 @@
// See the file "COPYING" in the main distribution directory for copyright.
#pragma once
#include <string>
#include <list>
#include <vector>
namespace zeek::detail {
// Script file we have already scanned (or are in the process of scanning).
// They are identified by normalized realpath.
class ScannedFile {
public:
ScannedFile(int arg_include_level,
std::string arg_name, bool arg_skipped = false,
bool arg_prefixes_checked = false);
/**
* Compares the canonical path of this file against every canonical path
* in files_scanned and returns whether there's any match.
*/
bool AlreadyScanned() const;
int include_level;
bool skipped; // This ScannedFile was @unload'd.
bool prefixes_checked; // If loading prefixes for this file has been tried.
std::string name;
std::string canonical_path; // normalized, absolute path via realpath()
static auto constexpr canonical_stdin_path = "<stdin>";
};
extern std::list<ScannedFile> files_scanned;
extern std::vector<std::string> sig_files;
} // namespace zeek::detail
using ScannedFile [[deprecated("Remove in v4.1. Use zeek::detail::ScannedFile.")]] = zeek::detail::ScannedFile;
extern std::list<zeek::detail::ScannedFile>& files_scanned [[deprecated("Remove in v4.1. Use zeek::detail::files_scanned.")]];
extern std::vector<std::string>& sig_files [[deprecated("Remove in v4.1. Use zeek::detail::sig_files.")]];

View file

@ -34,6 +34,7 @@
#include "Net.h"
#include "Traverse.h"
#include "module_util.h"
#include "ScannedFile.h"
#include "analyzer/Analyzer.h"
#include "zeekygen/Manager.h"
@ -347,7 +348,7 @@ when return TOK_WHEN;
zeek::reporter->Error("failed to find file associated with @load-sigs %s",
file);
else
zeek::net::detail::sig_files.push_back(zeek::util::copy_string(path.c_str()));
zeek::detail::sig_files.push_back(zeek::util::copy_string(path.c_str()));
break;
case 0:
@ -404,8 +405,8 @@ when return TOK_WHEN;
else
{
// All we have to do is pretend we've already scanned it.
zeek::net::detail::ScannedFile sf(file_stack.length(), std::move(path), true);
zeek::net::detail::files_scanned.push_back(std::move(sf));
zeek::detail::ScannedFile sf(file_stack.length(), std::move(path), true);
zeek::detail::files_scanned.push_back(std::move(sf));
}
}
@ -571,8 +572,6 @@ YYLTYPE zeek::detail::GetCurrentLocation()
return currloc;
}
static auto constexpr canonical_stdin_path = "<stdin>";
static int load_files(const char* orig_file)
{
std::string file_path = find_relative_script_file(orig_file);
@ -599,7 +598,7 @@ static int load_files(const char* orig_file)
if ( zeek::util::streq(orig_file, "-") )
{
f = stdin;
file_path = canonical_stdin_path;
file_path = zeek::detail::ScannedFile::canonical_stdin_path;
if ( zeek::detail::g_policy_debug )
{
@ -622,7 +621,7 @@ static int load_files(const char* orig_file)
zeek::reporter->FatalError("can't open %s", file_path.c_str());
}
zeek::net::detail::ScannedFile sf(file_stack.length(), file_path);
zeek::detail::ScannedFile sf(file_stack.length(), file_path);
if ( sf.AlreadyScanned() )
{
@ -632,7 +631,7 @@ static int load_files(const char* orig_file)
return 0;
}
zeek::net::detail::files_scanned.push_back(std::move(sf));
zeek::detail::files_scanned.push_back(std::move(sf));
if ( zeek::detail::g_policy_debug && ! file_path.empty() )
{
@ -889,13 +888,12 @@ int yywrap()
// file name is discarded. If the prefix is non-empty, it gets placed
// in front of the flattened path, separated with another '.'
bool found_prefixed_files = false;
for ( auto it = zeek::net::detail::files_scanned.begin();
it != zeek::net::detail::files_scanned.end(); ++it )
for ( auto& scanned_file : zeek::detail::files_scanned )
{
if ( it->skipped || it->prefixes_checked )
if ( scanned_file.skipped || scanned_file.prefixes_checked )
continue;
it->prefixes_checked = true;
scanned_file.prefixes_checked = true;
// Prefixes are pushed onto a stack, so iterate backwards.
for ( int i = zeek::detail::zeek_script_prefixes.size() - 1; i >= 0; --i )
{
@ -903,7 +901,7 @@ int yywrap()
if ( ! zeek::detail::zeek_script_prefixes[i][0] )
continue;
std::string canon = zeek::util::detail::without_zeekpath_component(it->name);
std::string canon = zeek::util::detail::without_zeekpath_component(scanned_file.name);
std::string flat = zeek::util::detail::flatten_script_name(canon, zeek::detail::zeek_script_prefixes[i]);
std::string path = find_relative_script_file(flat);
@ -914,7 +912,7 @@ int yywrap()
}
//printf("====== prefix search ======\n");
//printf("File : %s\n", it->name.c_str());
//printf("File : %s\n", scanned_file.name.c_str());
//printf("Canon : %s\n", canon.c_str());
//printf("Flat : %s\n", flat.c_str());
//printf("Found : %s\n", path.empty() ? "F" : "T");
@ -1027,42 +1025,3 @@ FileInfo::~FileInfo()
if ( restore_module != "" )
zeek::detail::current_module = restore_module;
}
zeek::net::detail::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 zeek::net::detail::ScannedFile::AlreadyScanned() const
{
auto rval = false;
for ( const auto& it : zeek::net::detail::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;
}

View file

@ -46,6 +46,7 @@ extern "C" {
#include "Trigger.h"
#include "Hash.h"
#include "Func.h"
#include "ScannedFile.h"
#include "supervisor/Supervisor.h"
#include "threading/Manager.h"
@ -712,7 +713,7 @@ zeek::detail::SetupResult setup(int argc, char** argv,
all_signature_files.emplace_back(std::move(sf));
// Append signature files defined in @load-sigs
for ( const auto& sf : zeek::net::detail::sig_files )
for ( const auto& sf : zeek::detail::sig_files )
all_signature_files.emplace_back(sf);
if ( ! all_signature_files.empty() )
@ -850,7 +851,7 @@ zeek::detail::SetupResult setup(int argc, char** argv,
if ( zeek_script_loaded )
{
// Queue events reporting loaded scripts.
for ( const auto& file : zeek::net::detail::files_scanned )
for ( const auto& file : zeek::detail::files_scanned )
{
if ( file.skipped )
continue;