Logging changes. Use --debug-file for a replaced log. Use --append to append to the log file if it exists. Also fixed a bug where cthulhu could try to restart without replacing the old running version.
This commit is contained in:
1 parent
4643f8e47f
commit
50e32f24df
9 files changed
+125
-13
No files matched your search
@@ -359,11 +359,17 @@ type fileWriter struct {
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
func OpenFile(path string, fallback io.Writer) (DiagnosticsWriter, error) {
|
||||
func OpenFile(path string, fallback io.Writer, appendLog bool) (DiagnosticsWriter, error) {
|
||||
if path == "" || path == "." || path == ".." {
|
||||
return nil, errors.New("invalid diagnostics file path")
|
||||
}
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600)
|
||||
// Keep writes at the end even after truncation: an outgoing reader may
|
||||
// still append its shutdown records while --replace is in progress.
|
||||
flags := os.O_WRONLY | os.O_CREATE | os.O_APPEND
|
||||
if !appendLog {
|
||||
flags |= os.O_TRUNC
|
||||
}
|
||||
file, err := os.OpenFile(path, flags, 0o600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open diagnostics file: %w", err)
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ func TestAsyncRepeatedStopAfterSuccess(t *testing.T) {
|
||||
|
||||
func TestOpenFileIsAppendOnlyOwnerRestrictedAndCancellable(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "diagnostics.log")
|
||||
writer, err := OpenFile(path, nil)
|
||||
writer, err := OpenFile(path, nil, true)
|
||||
if err != nil {
|
||||
t.Fatalf("OpenFile: %v", err)
|
||||
}
|
||||
@@ -312,7 +312,7 @@ func TestOpenFileIsAppendOnlyOwnerRestrictedAndCancellable(t *testing.T) {
|
||||
if info.Mode().Perm() != 0o600 {
|
||||
t.Fatalf("file mode = %o, want 0600", info.Mode().Perm())
|
||||
}
|
||||
writer, err = OpenFile(path, nil)
|
||||
writer, err = OpenFile(path, nil, true)
|
||||
if err != nil {
|
||||
t.Fatalf("reopen: %v", err)
|
||||
}
|
||||
@@ -332,3 +332,57 @@ func TestOpenFileIsAppendOnlyOwnerRestrictedAndCancellable(t *testing.T) {
|
||||
}
|
||||
|
||||
var _ io.Writer = (*controlledWriter)(nil)
|
||||
|
||||
func TestOpenFileReplacesPreviousContents(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "diagnostics.log")
|
||||
if err := os.WriteFile(path, []byte("previous session with longer contents\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writer, err := OpenFile(path, nil, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := writer.Write([]byte("new session\n")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != "new session\n" {
|
||||
t.Fatalf("file contents = %q", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreshLogPreservesWritesFromOutgoingReader(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "diagnostics.log")
|
||||
old, err := OpenFile(path, nil, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer old.Close()
|
||||
if _, err := old.Write([]byte("old session\n")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
next, err := OpenFile(path, nil, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer next.Close()
|
||||
if _, err := old.Write([]byte("outgoing shutdown\n")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := next.Write([]byte("new session\n")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != "outgoing shutdown\nnew session\n" {
|
||||
t.Fatalf("overlapping reader writes = %q", data)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user