github.com/vmware/govmomi@v0.51.0/vim25/debug/file.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package debug 6 7 import ( 8 "io" 9 "os" 10 "path" 11 "sync" 12 ) 13 14 // FileProvider implements a debugging provider that creates a real file for 15 // every call to NewFile. It maintains a list of all files that it creates, 16 // such that it can close them when its Flush function is called. 17 type FileProvider struct { 18 Path string 19 20 mu sync.Mutex 21 files []*os.File 22 } 23 24 func (fp *FileProvider) NewFile(p string) io.WriteCloser { 25 f, err := os.Create(path.Join(fp.Path, p)) 26 if err != nil { 27 panic(err) 28 } 29 30 fp.mu.Lock() 31 defer fp.mu.Unlock() 32 fp.files = append(fp.files, f) 33 34 return NewFileWriterCloser(f, p) 35 } 36 37 func (fp *FileProvider) Flush() { 38 fp.mu.Lock() 39 defer fp.mu.Unlock() 40 for _, f := range fp.files { 41 f.Close() 42 } 43 } 44 45 type FileWriterCloser struct { 46 f *os.File 47 p string 48 } 49 50 func NewFileWriterCloser(f *os.File, p string) *FileWriterCloser { 51 return &FileWriterCloser{ 52 f, 53 p, 54 } 55 } 56 57 func (fwc *FileWriterCloser) Write(p []byte) (n int, err error) { 58 return fwc.f.Write(Scrub(p)) 59 } 60 61 func (fwc *FileWriterCloser) Close() error { 62 return fwc.f.Close() 63 }