diff --git a/ui/logger/logger.go b/ui/logger/logger.go index 7d9687bec..15c413dd5 100644 --- a/ui/logger/logger.go +++ b/ui/logger/logger.go @@ -38,6 +38,7 @@ import ( "path/filepath" "strconv" "sync" + "syscall" ) type Logger interface { @@ -93,10 +94,19 @@ func fileRotation(from, baseName, ext string, cur, max int) error { // existing files to .#., keeping up to maxCount files. // .1. is the most recent backup, .2. is the // second most recent backup, etc. -// -// TODO: This function is not guaranteed to be atomic, if there are multiple -// users attempting to do the same operation, the result is undefined. func CreateFileWithRotation(filename string, maxCount int) (*os.File, error) { + lockFileName := filepath.Join(filepath.Dir(filename), ".lock_"+filepath.Base(filename)) + lockFile, err := os.OpenFile(lockFileName, os.O_RDWR|os.O_CREATE, 0666) + if err != nil { + return nil, err + } + defer lockFile.Close() + + err = syscall.Flock(int(lockFile.Fd()), syscall.LOCK_EX) + if err != nil { + return nil, err + } + if _, err := os.Lstat(filename); err == nil { ext := filepath.Ext(filename) basename := filename[:len(filename)-len(ext)] diff --git a/ui/logger/logger_test.go b/ui/logger/logger_test.go index 0f88ab374..dc6f2e918 100644 --- a/ui/logger/logger_test.go +++ b/ui/logger/logger_test.go @@ -67,7 +67,7 @@ func TestCreateFileWithRotation(t *testing.T) { t.Fatalf("Failed to read dir: %v", err) } sort.Strings(names) - expected := []string{"build.1.log", "build.2.log", "build.3.log", "build.log"} + expected := []string{".lock_build.log", "build.1.log", "build.2.log", "build.3.log", "build.log"} if !reflect.DeepEqual(names, expected) { t.Errorf("File list does not match.") t.Errorf(" got: %v", names)