Commit 1f66755f authored by Jordan Sissel's avatar Jordan Sissel

os.Rename fails on windows if the file exists. I can find no way to

invoke ReplaceFile via Go, so lumberjack's registrar updates will not
be atomic on Windows platform. On windows, the "old" registrar db is
saved as ".lumberjack.old"
parent 2b2c6e38
...@@ -34,6 +34,8 @@ func (h *Harvester) Harvest(output chan *FileEvent) { ...@@ -34,6 +34,8 @@ func (h *Harvester) Harvest(output chan *FileEvent) {
// get current offset in file // get current offset in file
offset, _ := h.file.Seek(0, os.SEEK_CUR) offset, _ := h.file.Seek(0, os.SEEK_CUR)
log.Printf("Current file offset: %d\n", offset)
// TODO(sissel): Make the buffer size tunable at start-time // TODO(sissel): Make the buffer size tunable at start-time
reader := bufio.NewReaderSize(h.file, 16<<10) // 16kb buffer by default reader := bufio.NewReaderSize(h.file, 16<<10) // 16kb buffer by default
......
...@@ -2,8 +2,6 @@ package main ...@@ -2,8 +2,6 @@ package main
import ( import (
"log" "log"
"os"
"encoding/json"
) )
func Registrar(input chan []*FileEvent) { func Registrar(input chan []*FileEvent) {
...@@ -28,25 +26,12 @@ func Registrar(input chan []*FileEvent) { ...@@ -28,25 +26,12 @@ func Registrar(input chan []*FileEvent) {
Inode: ino, Inode: ino,
Device: dev, Device: dev,
} }
log.Printf("State %s: %d\n", *event.Source, event.Offset)
} }
if len(state) > 0 { if len(state) > 0 {
write(state) WriteRegistry(state, ".lumberjack")
os.Rename(".lumberjack.new", ".lumberjack")
} }
} }
} }
func write(state map[string]*FileState) {
log.Printf("Saving registrar state.\n")
// Open tmp file, write, flush, rename
file, err := os.Create(".lumberjack.new")
if err != nil {
log.Printf("Failed to open .lumberjack.new for writing: %s\n", err)
return
}
encoder := json.NewEncoder(file)
encoder.Encode(state)
file.Close()
}
// +build !windows
package main
import (
"encoding/json"
"os"
"log"
)
func WriteRegistry(state map[string]*FileState, path string) {
// Open tmp file, write, flush, rename
file, err := os.Create(".lumberjack.new")
if err != nil {
log.Printf("Failed to open .lumberjack.new for writing: %s\n", err)
return
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.Encode(state)
os.Rename(".lumberjack.new", path)
}
package main
import (
"encoding/json"
"os"
"log"
)
func WriteRegistry(state map[string]*FileState, path string) {
tmp := path + ".new"
file, err := os.Create(tmp)
if err != nil {
log.Printf("Failed to open .lumberjack.new for writing: %s\n", err)
return
}
encoder := json.NewEncoder(file)
encoder.Encode(state)
file.Close()
old := path + ".old"
os.Rename(path, old)
os.Rename(tmp, path)
}
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