Skip to content

Handle file creation and locking for Windows #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions logstash-forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ func main() {

// Load the previous log file locations now, for use in prospector
restart.files = make(map[string]*FileState)
if existing, e := os.Open(".logstash-forwarder"); e == nil {
defer existing.Close()

if existing, e := os.OpenFile(".logstash-forwarder", os.O_RDONLY | os.O_CREATE, 0666); e == nil {
wd := ""
if wd, e = os.Getwd(); e != nil {
emit("WARNING: os.Getwd retuned unexpected error %s -- ignoring\n", e.Error())
Expand All @@ -159,6 +159,7 @@ func main() {

decoder := json.NewDecoder(existing)
decoder.Decode(&restart.files)
existing.Close()
}

pendingProspectorCnt := 0
Expand Down
3 changes: 2 additions & 1 deletion registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ func writeRegistry(state map[string]*FileState, path string) error {
emit("Failed to create tempfile (%s) for writing: %s\n", tempfile, e)
return e
}
defer file.Close()

encoder := json.NewEncoder(file)
encoder.Encode(state)

file.Close()

return onRegistryWrite(path, tempfile)
}
6 changes: 4 additions & 2 deletions registrar_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
func onRegistryWrite(path, tempfile string) error {
old := path + ".old"
var e error


os.Remove(old);
if e = os.Rename(path, old); e != nil {
emit("registry rotate: rename of %s to %s - %s\n", path, old, e)
return e
}


os.Remove(path);
if e = os.Rename(tempfile, path); e != nil {
emit("registry rotate: rename of %s to %s - %s\n", tempfile, path, e)
return e
Expand Down