Reformat the world

This commit is contained in:
Tim Wojtulewicz 2021-09-16 15:35:39 -07:00
parent 194cb24547
commit b2f171ec69
714 changed files with 35149 additions and 35203 deletions

View file

@ -2,21 +2,21 @@
#include "zeek/input/readers/ascii/Ascii.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sstream>
#include "zeek/input/readers/ascii/ascii.bif.h"
#include "zeek/threading/SerialTypes.h"
#include "zeek/input/readers/ascii/ascii.bif.h"
using namespace std;
using zeek::threading::Value;
using zeek::threading::Field;
using zeek::threading::Value;
namespace zeek::input::reader::detail {
namespace zeek::input::reader::detail
{
FieldMapping::FieldMapping(const string& arg_name, const TypeTag& arg_type, int arg_position)
: name(arg_name), type(arg_type), subtype(TYPE_ERROR)
@ -27,7 +27,7 @@ FieldMapping::FieldMapping(const string& arg_name, const TypeTag& arg_type, int
}
FieldMapping::FieldMapping(const string& arg_name, const TypeTag& arg_type,
const TypeTag& arg_subtype, int arg_position)
const TypeTag& arg_subtype, int arg_position)
: name(arg_name), type(arg_type), subtype(arg_subtype)
{
position = arg_position;
@ -47,7 +47,7 @@ FieldMapping FieldMapping::subType()
return FieldMapping(name, subtype, position);
}
Ascii::Ascii(ReaderFrontend *frontend) : ReaderBackend(frontend)
Ascii::Ascii(ReaderFrontend* frontend) : ReaderBackend(frontend)
{
mtime = 0;
ino = 0;
@ -55,38 +55,35 @@ Ascii::Ascii(ReaderFrontend *frontend) : ReaderBackend(frontend)
fail_on_invalid_lines = false;
}
Ascii::~Ascii()
{
}
Ascii::~Ascii() { }
void Ascii::DoClose()
{
}
void Ascii::DoClose() { }
bool Ascii::DoInit(const ReaderInfo& info, int num_fields, const Field* const* fields)
{
StopWarningSuppression();
separator.assign( (const char*) BifConst::InputAscii::separator->Bytes(),
separator.assign((const char*)BifConst::InputAscii::separator->Bytes(),
BifConst::InputAscii::separator->Len());
set_separator.assign( (const char*) BifConst::InputAscii::set_separator->Bytes(),
set_separator.assign((const char*)BifConst::InputAscii::set_separator->Bytes(),
BifConst::InputAscii::set_separator->Len());
empty_field.assign( (const char*) BifConst::InputAscii::empty_field->Bytes(),
empty_field.assign((const char*)BifConst::InputAscii::empty_field->Bytes(),
BifConst::InputAscii::empty_field->Len());
unset_field.assign( (const char*) BifConst::InputAscii::unset_field->Bytes(),
unset_field.assign((const char*)BifConst::InputAscii::unset_field->Bytes(),
BifConst::InputAscii::unset_field->Len());
fail_on_invalid_lines = BifConst::InputAscii::fail_on_invalid_lines;
fail_on_file_problem = BifConst::InputAscii::fail_on_file_problem;
path_prefix.assign((const char*) BifConst::InputAscii::path_prefix->Bytes(),
path_prefix.assign((const char*)BifConst::InputAscii::path_prefix->Bytes(),
BifConst::InputAscii::path_prefix->Len());
// Set per-filter configuration options.
for ( ReaderInfo::config_map::const_iterator i = info.config.begin(); i != info.config.end(); i++ )
for ( ReaderInfo::config_map::const_iterator i = info.config.begin(); i != info.config.end();
i++ )
{
if ( strcmp(i->first, "separator") == 0 )
separator.assign(i->second);
@ -113,13 +110,13 @@ bool Ascii::DoInit(const ReaderInfo& info, int num_fields, const Field* const* f
if ( set_separator.size() != 1 )
Error("set_separator length has to be 1. Separator will be truncated.");
threading::formatter::Ascii::SeparatorInfo sep_info(separator, set_separator, unset_field, empty_field);
threading::formatter::Ascii::SeparatorInfo sep_info(separator, set_separator, unset_field,
empty_field);
formatter = unique_ptr<threading::Formatter>(new threading::formatter::Ascii(this, sep_info));
return DoUpdate();
}
bool Ascii::OpenFile()
{
if ( file.is_open() )
@ -152,7 +149,8 @@ bool Ascii::OpenFile()
if ( ReadHeader(false) == false )
{
FailWarn(fail_on_file_problem, Fmt("Init: cannot open %s; problem reading file header", fname.c_str()), true);
FailWarn(fail_on_file_problem,
Fmt("Init: cannot open %s; problem reading file header", fname.c_str()), true);
file.close();
return ! fail_on_file_problem;
@ -172,8 +170,10 @@ bool Ascii::ReadHeader(bool useCached)
{
if ( ! GetLine(line) )
{
FailWarn(fail_on_file_problem, Fmt("Could not read input data file %s; first line could not be read",
fname.c_str()), true);
FailWarn(fail_on_file_problem,
Fmt("Could not read input data file %s; first line could not be read",
fname.c_str()),
true);
return false;
}
@ -185,11 +185,11 @@ bool Ascii::ReadHeader(bool useCached)
// construct list of field names.
istringstream splitstream(line);
int pos=0;
int pos = 0;
while ( splitstream )
{
string s;
if ( ! getline(splitstream, s, separator[0]))
if ( ! getline(splitstream, s, separator[0]) )
break;
ifields[s] = pos;
@ -208,15 +208,18 @@ bool Ascii::ReadHeader(bool useCached)
{
if ( field->optional )
{
// we do not really need this field. mark it as not present and always send an undef back.
// we do not really need this field. mark it as not present and always send an undef
// back.
FieldMapping f(field->name, field->type, field->subtype, -1);
f.present = false;
columnMap.push_back(f);
continue;
}
FailWarn(fail_on_file_problem, Fmt("Did not find requested field %s in input data file %s.",
field->name, fname.c_str()), true);
FailWarn(fail_on_file_problem,
Fmt("Did not find requested field %s in input data file %s.", field->name,
fname.c_str()),
true);
return false;
}
@ -228,8 +231,10 @@ bool Ascii::ReadHeader(bool useCached)
map<string, uint32_t>::iterator fit2 = ifields.find(field->secondary_name);
if ( fit2 == ifields.end() )
{
FailWarn(fail_on_file_problem, Fmt("Could not find requested port type field %s in input data file %s.",
field->secondary_name, fname.c_str()), true);
FailWarn(fail_on_file_problem,
Fmt("Could not find requested port type field %s in input data file %s.",
field->secondary_name, fname.c_str()),
true);
return false;
}
@ -257,7 +262,7 @@ bool Ascii::GetLine(string& str)
if ( str[0] != '#' )
return true;
if ( ( str.length() > 8 ) && ( str.compare(0,7, "#fields") == 0 ) && ( str[7] == separator[0] ) )
if ( (str.length() > 8) && (str.compare(0, 7, "#fields") == 0) && (str[7] == separator[0]) )
{
str = str.substr(8);
return true;
@ -273,63 +278,64 @@ bool Ascii::DoUpdate()
if ( ! OpenFile() )
return ! fail_on_file_problem;
switch ( Info().mode ) {
switch ( Info().mode )
{
case MODE_REREAD:
{
// check if the file has changed
struct stat sb;
if ( stat(fname.c_str(), &sb) == -1 )
{
FailWarn(fail_on_file_problem, Fmt("Could not get stat for %s", fname.c_str()), true);
// check if the file has changed
struct stat sb;
if ( stat(fname.c_str(), &sb) == -1 )
{
FailWarn(fail_on_file_problem, Fmt("Could not get stat for %s", fname.c_str()),
true);
file.close();
return ! fail_on_file_problem;
file.close();
return ! fail_on_file_problem;
}
if ( sb.st_ino == ino && sb.st_mtime == mtime )
// no change
return true;
// Warn again in case of trouble if the file changes. The comparison to 0
// is to suppress an extra warning that we'd otherwise get on the initial
// inode assignment.
if ( ino != 0 )
StopWarningSuppression();
mtime = sb.st_mtime;
ino = sb.st_ino;
// File changed. Fall through to re-read.
}
if ( sb.st_ino == ino && sb.st_mtime == mtime )
// no change
return true;
// Warn again in case of trouble if the file changes. The comparison to 0
// is to suppress an extra warning that we'd otherwise get on the initial
// inode assignment.
if ( ino != 0 )
StopWarningSuppression();
mtime = sb.st_mtime;
ino = sb.st_ino;
// File changed. Fall through to re-read.
}
case MODE_MANUAL:
case MODE_STREAM:
{
// dirty, fix me. (well, apparently after trying seeking, etc
// - this is not that bad)
if ( file.is_open() )
{
if ( Info().mode == MODE_STREAM )
// dirty, fix me. (well, apparently after trying seeking, etc
// - this is not that bad)
if ( file.is_open() )
{
file.clear(); // remove end of file evil bits
if ( ! ReadHeader(true) )
if ( Info().mode == MODE_STREAM )
{
return ! fail_on_file_problem; // header reading failed
file.clear(); // remove end of file evil bits
if ( ! ReadHeader(true) )
{
return ! fail_on_file_problem; // header reading failed
}
break;
}
break;
file.close();
}
file.close();
OpenFile();
break;
}
OpenFile();
break;
}
default:
assert(false);
}
string line;
@ -359,9 +365,8 @@ bool Ascii::DoUpdate()
Value** fields = new Value*[NumFields()];
int fpos = 0;
for ( vector<FieldMapping>::iterator fit = columnMap.begin();
fit != columnMap.end();
fit++ )
for ( vector<FieldMapping>::iterator fit = columnMap.begin(); fit != columnMap.end();
fit++ )
{
if ( ! fit->present )
@ -372,19 +377,21 @@ bool Ascii::DoUpdate()
continue;
}
assert(fit->position >= 0 );
assert(fit->position >= 0);
if ( (*fit).position > pos || (*fit).secondary_position > pos )
{
FailWarn(fail_on_invalid_lines, Fmt("Not enough fields in line '%s' of %s. Found %d fields, want positions %d and %d",
line.c_str(), fname.c_str(), pos, (*fit).position, (*fit).secondary_position));
FailWarn(fail_on_invalid_lines, Fmt("Not enough fields in line '%s' of %s. Found "
"%d fields, want positions %d and %d",
line.c_str(), fname.c_str(), pos,
(*fit).position, (*fit).secondary_position));
if ( fail_on_invalid_lines )
{
for ( int i = 0; i < fpos; i++ )
delete fields[i];
delete [] fields;
delete[] fields;
return false;
}
@ -395,11 +402,13 @@ bool Ascii::DoUpdate()
}
}
Value* val = formatter->ParseValue(stringfields[(*fit).position], (*fit).name, (*fit).type, (*fit).subtype);
Value* val = formatter->ParseValue(stringfields[(*fit).position], (*fit).name,
(*fit).type, (*fit).subtype);
if ( ! val )
{
Warning(Fmt("Could not convert line '%s' of %s to Val. Ignoring line.", line.c_str(), fname.c_str()));
Warning(Fmt("Could not convert line '%s' of %s to Val. Ignoring line.",
line.c_str(), fname.c_str()));
error = true;
break;
}
@ -407,10 +416,11 @@ bool Ascii::DoUpdate()
if ( (*fit).secondary_position != -1 )
{
// we have a port definition :)
assert(val->type == TYPE_PORT );
assert(val->type == TYPE_PORT);
// Error(Fmt("Got type %d != PORT with secondary position!", val->type));
val->val.port_val.proto = formatter->ParseProto(stringfields[(*fit).secondary_position]);
val->val.port_val.proto =
formatter->ParseProto(stringfields[(*fit).secondary_position]);
}
fields[fpos] = val;
@ -427,12 +437,12 @@ bool Ascii::DoUpdate()
for ( int i = 0; i < fpos; i++ )
delete fields[i];
delete [] fields;
delete[] fields;
continue;
}
//printf("fpos: %d, second.num_fields: %d\n", fpos, (*it).second.num_fields);
assert ( fpos == NumFields() );
// printf("fpos: %d, second.num_fields: %d\n", fpos, (*it).second.num_fields);
assert(fpos == NumFields());
if ( Info().mode == MODE_STREAM )
Put(fields);
@ -460,7 +470,7 @@ bool Ascii::DoHeartbeat(double network_time, double current_time)
case MODE_REREAD:
case MODE_STREAM:
Update(); // Call Update, not DoUpdate, because Update
// checks the "disabled" flag.
// checks the "disabled" flag.
break;
default:
@ -470,4 +480,4 @@ bool Ascii::DoHeartbeat(double network_time, double current_time)
return true;
}
} // namespace zeek::input::reader::detail
} // namespace zeek::input::reader::detail