| 1 | package diagnostics |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | ) |
| 8 | |
| 9 | |
| 10 | |
| 11 | func (s *Store) ImportLegacy(profile string) error { |
| 12 | paths := []string{"control.log", filepath.Join("proxy", "runtime.log"), filepath.Join("proxy", "validation.log"), filepath.Join("state", "setup.log")} |
| 13 | for _, relative := range paths { |
| 14 | path := filepath.Join(profile, relative) |
| 15 | st, err := os.Lstat(path) |
| 16 | if os.IsNotExist(err) { |
| 17 | continue |
| 18 | } |
| 19 | if err != nil { |
| 20 | return err |
| 21 | } |
| 22 | if !st.Mode().IsRegular() { |
| 23 | continue |
| 24 | } |
| 25 | if s.now().Sub(st.ModTime()) > s.retention { |
| 26 | if err := os.Remove(path); err != nil { |
| 27 | return err |
| 28 | } |
| 29 | continue |
| 30 | } |
| 31 | f, err := os.Open(path) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | const limit = 256 << 10 |
| 36 | if st.Size() > limit { |
| 37 | _, _ = f.Seek(st.Size()-limit, io.SeekStart) |
| 38 | } |
| 39 | r := io.LimitReader(f, limit) |
| 40 | if st.Size() > limit { |
| 41 | var one [1]byte |
| 42 | for { |
| 43 | n, e := r.Read(one[:]) |
| 44 | if n == 0 || e != nil || one[0] == '\n' { |
| 45 | break |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | stream := s.Stream("legacy", "warn") |
| 50 | _, err = io.Copy(stream, r) |
| 51 | _ = stream.Close() |
| 52 | f.Close() |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | if err = os.Remove(path); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | } |
| 60 | return nil |
| 61 | } |