Browse Source

Reimplement fs.FileInfo interface (#13315)

Co-authored-by: R.B. Boyer <4903+rboyer@users.noreply.github.com>
pull/13322/head
Chris S. Kim 3 years ago committed by GitHub
parent
commit
67860bd248
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      agent/uiserver/buffered_file.go

44
agent/uiserver/buffered_file.go

@ -1,20 +1,31 @@
package uiserver package uiserver
import ( import (
"io" "bytes"
"io/fs" "io/fs"
"os"
"time"
) )
// bufferedFile implements fs.File and allows us to modify a file from disk by // bufferedFile implements fs.File and allows us to modify a file from disk by
// writing out the new version into a buffer and then serving file reads from // writing out the new version into a buffer and then serving file reads from
// that. // that.
type bufferedFile struct { type bufferedFile struct {
buf io.Reader buf *bytes.Buffer
info fs.FileInfo info fs.FileInfo
} }
var _ fs.FileInfo = (*bufferedFile)(nil)
func newBufferedFile(buf *bytes.Buffer, info fs.FileInfo) *bufferedFile {
return &bufferedFile{
buf: buf,
info: info,
}
}
func (b *bufferedFile) Stat() (fs.FileInfo, error) { func (b *bufferedFile) Stat() (fs.FileInfo, error) {
return b.info, nil return b, nil
} }
func (b *bufferedFile) Read(bytes []byte) (int, error) { func (b *bufferedFile) Read(bytes []byte) (int, error) {
@ -25,9 +36,26 @@ func (b *bufferedFile) Close() error {
return nil return nil
} }
func newBufferedFile(buf io.Reader, info fs.FileInfo) *bufferedFile { func (b *bufferedFile) Name() string {
return &bufferedFile{ return b.info.Name()
buf: buf, }
info: info,
} func (b *bufferedFile) Size() int64 {
return int64(b.buf.Len())
}
func (b *bufferedFile) Mode() os.FileMode {
return b.info.Mode()
}
func (b *bufferedFile) ModTime() time.Time {
return b.info.ModTime()
}
func (b *bufferedFile) IsDir() bool {
return false
}
func (b *bufferedFile) Sys() any {
return nil
} }

Loading…
Cancel
Save