Commit e9e1d7df authored by Jordan Sissel's avatar Jordan Sissel

- support stdin again

- restart any known files at their previous positions as recorded by the
registrar.
parent 8d9fb2db
......@@ -3,6 +3,7 @@ package main
import (
"time"
"path/filepath"
"encoding/json"
"syscall"
"os"
"log"
......@@ -10,6 +11,21 @@ import (
func Prospect(fileconfig FileConfig, output chan *FileEvent) {
fileinfo := make(map[string]os.FileInfo)
// Handle any "-" (stdin) paths
for i, path := range fileconfig.Paths {
if path == "-" {
harvester := Harvester{Path: path, Fields: fileconfig.Fields}
go harvester.Harvest(output)
// Remove it from the file list
fileconfig.Paths = append(fileconfig.Paths[:i], fileconfig.Paths[i+1:]...)
}
}
// Use the registrar db to reopen any files at their last positions
resume_tracking(fileconfig, fileinfo, output)
for {
for _, path := range fileconfig.Paths {
prospector_scan(path, fileconfig.Fields, fileinfo, output)
......@@ -20,6 +36,40 @@ func Prospect(fileconfig FileConfig, output chan *FileEvent) {
}
} /* Prospect */
func resume_tracking(fileconfig FileConfig, fileinfo map[string]os.FileInfo, output chan *FileEvent) {
// Start up with any registrar data.
history, err := os.Open(".lumberjack")
if err == nil {
historical_state := make(map[string]*FileState)
log.Printf("Loading registrar data\n")
decoder := json.NewDecoder(history)
decoder.Decode(&historical_state)
history.Close()
for path, state := range historical_state {
// if the file is the same inode/device as we last saw,
// start a harvester on it at the last known position
info, err := os.Stat(path)
if err != nil { continue }
fstat := info.Sys().(*syscall.Stat_t)
if fstat.Ino != state.Inode && fstat.Dev == state.Device {
// same file, seek to last known position
fileinfo[path] = info
for _, pathglob := range fileconfig.Paths {
match, _ := filepath.Match(pathglob, path)
if match {
harvester := Harvester{Path: path, Fields: fileconfig.Fields, Offset: state.Offset }
go harvester.Harvest(output)
break
}
}
}
}
}
}
func prospector_scan(path string, fields map[string]string,
fileinfo map[string]os.FileInfo,
output chan *FileEvent) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment