2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-09-23 11:37:33 +00:00
|
|
|
package uiserver
|
|
|
|
|
|
|
|
import (
|
2022-06-01 15:09:51 +00:00
|
|
|
"bytes"
|
2022-05-31 19:33:56 +00:00
|
|
|
"io/fs"
|
2022-06-01 15:09:51 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
2020-09-23 11:37:33 +00:00
|
|
|
)
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
// bufferedFile implements fs.File and allows us to modify a file from disk by
|
2020-09-23 11:37:33 +00:00
|
|
|
// writing out the new version into a buffer and then serving file reads from
|
|
|
|
// that.
|
|
|
|
type bufferedFile struct {
|
2022-06-01 15:09:51 +00:00
|
|
|
buf *bytes.Buffer
|
2022-05-31 19:33:56 +00:00
|
|
|
info fs.FileInfo
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 15:09:51 +00:00
|
|
|
var _ fs.FileInfo = (*bufferedFile)(nil)
|
|
|
|
|
|
|
|
func newBufferedFile(buf *bytes.Buffer, info fs.FileInfo) *bufferedFile {
|
|
|
|
return &bufferedFile{
|
|
|
|
buf: buf,
|
|
|
|
info: info,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
func (b *bufferedFile) Stat() (fs.FileInfo, error) {
|
2022-06-01 15:09:51 +00:00
|
|
|
return b, nil
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
func (b *bufferedFile) Read(bytes []byte) (int, error) {
|
|
|
|
return b.buf.Read(bytes)
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 19:33:56 +00:00
|
|
|
func (b *bufferedFile) Close() error {
|
2020-09-23 11:37:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-01 15:09:51 +00:00
|
|
|
func (b *bufferedFile) Name() string {
|
|
|
|
return b.info.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-09-23 11:37:33 +00:00
|
|
|
}
|