2022-06-29 09:26:29 -04:00
|
|
|
package bootstrapper
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
pb "github.com/edgelesssys/constellation/debugd/service"
|
|
|
|
"github.com/schollz/progressbar/v3"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FileStreamer handles reading and writing of a file using a stream of chunks.
|
|
|
|
type FileStreamer struct {
|
|
|
|
fs afero.Fs
|
|
|
|
mux sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadChunkStream is abstraction over a gRPC stream that allows us to receive chunks via gRPC.
|
|
|
|
type ReadChunkStream interface {
|
|
|
|
Recv() (*pb.Chunk, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteChunkStream is abstraction over a gRPC stream that allows us to send chunks via gRPC.
|
|
|
|
type WriteChunkStream interface {
|
|
|
|
Send(chunk *pb.Chunk) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileStreamer creates a new FileStreamer.
|
|
|
|
func NewFileStreamer(fs afero.Fs) *FileStreamer {
|
|
|
|
return &FileStreamer{
|
|
|
|
fs: fs,
|
|
|
|
mux: sync.RWMutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteStream opens a file to write to and streams chunks from a gRPC stream into the file.
|
|
|
|
func (f *FileStreamer) WriteStream(filename string, stream ReadChunkStream, showProgress bool) error {
|
2022-03-23 05:30:49 -04:00
|
|
|
// try to read from stream once before acquiring write lock
|
|
|
|
chunk, err := stream.Recv()
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("reading stream: %w", err)
|
2022-03-23 05:30:49 -04:00
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
f.mux.Lock()
|
|
|
|
defer f.mux.Unlock()
|
|
|
|
file, err := f.fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o755)
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("open %v for writing: %w", filename, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var bar *progressbar.ProgressBar
|
|
|
|
if showProgress {
|
|
|
|
bar = progressbar.NewOptions64(
|
|
|
|
-1,
|
2022-06-29 09:26:29 -04:00
|
|
|
progressbar.OptionSetDescription("receiving bootstrapper"),
|
2022-03-22 11:03:15 -04:00
|
|
|
progressbar.OptionShowBytes(true),
|
|
|
|
progressbar.OptionClearOnFinish(),
|
|
|
|
)
|
|
|
|
defer bar.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
2022-03-23 05:30:49 -04:00
|
|
|
_ = file.Close()
|
|
|
|
_ = f.fs.Remove(filename)
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("reading stream: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
if _, err := file.Write(chunk.Content); err != nil {
|
2022-03-23 05:30:49 -04:00
|
|
|
_ = file.Close()
|
|
|
|
_ = f.fs.Remove(filename)
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("writing chunk to disk: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
if showProgress {
|
2022-03-25 07:50:07 -04:00
|
|
|
_ = bar.Add(len(chunk.Content))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-03-23 05:30:49 -04:00
|
|
|
chunk, err = stream.Recv()
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadStream opens a file to read from and streams its contents chunkwise over gRPC.
|
|
|
|
func (f *FileStreamer) ReadStream(filename string, stream WriteChunkStream, chunksize uint, showProgress bool) error {
|
|
|
|
if chunksize == 0 {
|
|
|
|
return errors.New("invalid chunksize")
|
|
|
|
}
|
2022-03-23 05:30:49 -04:00
|
|
|
// fail if file is currently RW locked
|
|
|
|
if f.mux.TryRLock() {
|
|
|
|
defer f.mux.RUnlock()
|
|
|
|
} else {
|
|
|
|
return errors.New("file is opened for writing cannot be read at this time")
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
file, err := f.fs.OpenFile(filename, os.O_RDONLY, 0o755)
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("open %v for reading: %w", filename, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var bar *progressbar.ProgressBar
|
|
|
|
if showProgress {
|
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("performing stat on %v to get the file size: %w", filename, err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
bar = progressbar.NewOptions64(
|
|
|
|
stat.Size(),
|
2022-06-29 09:26:29 -04:00
|
|
|
progressbar.OptionSetDescription("uploading bootstrapper"),
|
2022-03-22 11:03:15 -04:00
|
|
|
progressbar.OptionShowBytes(true),
|
|
|
|
progressbar.OptionClearOnFinish(),
|
|
|
|
)
|
|
|
|
defer bar.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, chunksize)
|
|
|
|
for {
|
|
|
|
n, err := file.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("reading file chunk: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = stream.Send(&pb.Chunk{Content: buf[:n]}); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("sending chunk: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
if showProgress {
|
2022-03-25 07:50:07 -04:00
|
|
|
_ = bar.Add(n)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|