mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 08:38:20 +00:00
add streaming reads & automatic re-reading of files to ascii reader.
completely untested, but compiles & old tests still work
This commit is contained in:
parent
91943c2655
commit
d21a450f36
3 changed files with 57 additions and 21 deletions
|
@ -789,13 +789,6 @@ int Manager::SendEntryTable(const ReaderFrontend* reader, const int id, const Va
|
||||||
InputHash *h = filter->lastDict->Lookup(idxhash);
|
InputHash *h = filter->lastDict->Lookup(idxhash);
|
||||||
if ( h != 0 ) {
|
if ( h != 0 ) {
|
||||||
// seen before
|
// seen before
|
||||||
|
|
||||||
valhash->Hash();
|
|
||||||
|
|
||||||
h->valhash->Hash();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ( filter->num_val_fields == 0 || h->valhash->Hash() == valhash->Hash() ) {
|
if ( filter->num_val_fields == 0 || h->valhash->Hash() == valhash->Hash() ) {
|
||||||
// ok, exact duplicate
|
// ok, exact duplicate
|
||||||
filter->lastDict->Remove(idxhash);
|
filter->lastDict->Remove(idxhash);
|
||||||
|
|
|
@ -10,6 +10,11 @@
|
||||||
|
|
||||||
#define MANUAL 0
|
#define MANUAL 0
|
||||||
#define REREAD 1
|
#define REREAD 1
|
||||||
|
#define STREAM 2
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
using namespace input::reader;
|
using namespace input::reader;
|
||||||
using threading::Value;
|
using threading::Value;
|
||||||
|
@ -84,6 +89,7 @@ bool Ascii::DoInit(string path, int arg_mode)
|
||||||
started = false;
|
started = false;
|
||||||
fname = path;
|
fname = path;
|
||||||
mode = arg_mode;
|
mode = arg_mode;
|
||||||
|
mtime = 0;
|
||||||
|
|
||||||
file = new ifstream(path.c_str());
|
file = new ifstream(path.c_str());
|
||||||
if ( !file->is_open() ) {
|
if ( !file->is_open() ) {
|
||||||
|
@ -91,7 +97,7 @@ bool Ascii::DoInit(string path, int arg_mode)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ( mode != MANUAL ) && (mode != REREAD) ) {
|
if ( ( mode != MANUAL ) && (mode != REREAD) && ( mode != STREAM ) ) {
|
||||||
Error(Fmt("Unsupported read mode %d for source %s", mode, path.c_str()));
|
Error(Fmt("Unsupported read mode %d for source %s", mode, path.c_str()));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -402,23 +408,58 @@ Value* Ascii::EntryToVal(string s, FieldMapping field) {
|
||||||
|
|
||||||
// read the entire file and send appropriate thingies back to InputMgr
|
// read the entire file and send appropriate thingies back to InputMgr
|
||||||
bool Ascii::DoUpdate() {
|
bool Ascii::DoUpdate() {
|
||||||
// dirty, fix me. (well, apparently after trying seeking, etc - this is not that bad)
|
switch ( mode ) {
|
||||||
if ( file && file->is_open() ) {
|
case REREAD:
|
||||||
file->close();
|
// check if the file has changed
|
||||||
}
|
struct stat sb;
|
||||||
file = new ifstream(fname.c_str());
|
if ( stat(fname.c_str(), &sb) == -1 ) {
|
||||||
if ( !file->is_open() ) {
|
Error(Fmt("Could not get stat for %s", fname.c_str()));
|
||||||
Error(Fmt("cannot open %s", fname.c_str()));
|
return false;
|
||||||
return false;
|
}
|
||||||
|
|
||||||
|
if ( sb.st_mtime <= mtime ) {
|
||||||
|
// no change
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
mtime = sb.st_mtime;
|
||||||
|
// file changed. reread.
|
||||||
|
|
||||||
|
// fallthrough
|
||||||
|
case MANUAL:
|
||||||
|
case STREAM:
|
||||||
|
|
||||||
|
// dirty, fix me. (well, apparently after trying seeking, etc - this is not that bad)
|
||||||
|
if ( file && file->is_open() ) {
|
||||||
|
if ( mode == STREAM ) {
|
||||||
|
file->clear(); // remove end of file evil bits
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
file->close();
|
||||||
|
}
|
||||||
|
file = new ifstream(fname.c_str());
|
||||||
|
if ( !file->is_open() ) {
|
||||||
|
Error(Fmt("cannot open %s", fname.c_str()));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if ( ReadHeader() == false ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
// file->seekg(0, ios::beg); // do not forget clear.
|
// file->seekg(0, ios::beg); // do not forget clear.
|
||||||
|
|
||||||
|
|
||||||
if ( ReadHeader() == false ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
string line;
|
string line;
|
||||||
while ( GetLine(line ) ) {
|
while ( GetLine(line ) ) {
|
||||||
|
@ -504,8 +545,9 @@ bool Ascii::DoHeartbeat(double network_time, double current_time)
|
||||||
// yay, we do nothing :)
|
// yay, we do nothing :)
|
||||||
break;
|
break;
|
||||||
case REREAD:
|
case REREAD:
|
||||||
|
case STREAM:
|
||||||
|
DoUpdate();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,6 +91,7 @@ private:
|
||||||
int mode;
|
int mode;
|
||||||
|
|
||||||
bool started;
|
bool started;
|
||||||
|
time_t mtime;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue