2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-24 10:08:04 -05:00
|
|
|
// Package streamer implements streaming of files over gRPC.
|
2023-01-20 04:11:41 -05:00
|
|
|
package streamer
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
pb "github.com/edgelesssys/constellation/v2/debugd/service"
|
2022-03-22 11:03:15 -04:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
// New creates a new FileStreamer.
|
|
|
|
func New(fs afero.Fs) *FileStreamer {
|
2022-03-22 11:03:15 -04:00
|
|
|
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 {
|
|
|
|
f.mux.Lock()
|
|
|
|
defer f.mux.Unlock()
|
2023-01-20 04:11:41 -05:00
|
|
|
file, err := f.fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE, os.ModePerm)
|
2022-03-22 11:03:15 -04:00
|
|
|
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()
|
2023-01-20 04:11:41 -05:00
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("performing stat on %v to get the file size: %w", filename, err)
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
var bar *progressbar.ProgressBar
|
|
|
|
if showProgress {
|
2023-01-20 04:11:41 -05:00
|
|
|
bar = newProgressBar(stat.Size())
|
2022-03-22 11:03:15 -04:00
|
|
|
defer bar.Close()
|
|
|
|
}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
return writeInner(file, stream, bar)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-01-20 04:11:41 -05:00
|
|
|
return errors.New("a file is opened for writing so cannot read at this time")
|
2022-03-23 05:30:49 -04:00
|
|
|
}
|
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()
|
2023-01-20 04:11:41 -05:00
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("performing stat on %v to get the file size: %w", filename, err)
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
var bar *progressbar.ProgressBar
|
|
|
|
if showProgress {
|
2023-01-20 04:11:41 -05:00
|
|
|
bar = newProgressBar(stat.Size())
|
2022-03-22 11:03:15 -04:00
|
|
|
defer bar.Close()
|
|
|
|
}
|
|
|
|
|
2023-01-20 04:11:41 -05:00
|
|
|
return readInner(file, stream, chunksize, bar)
|
|
|
|
}
|
|
|
|
|
|
|
|
// readInner reads from a an io.Reader and sends chunks over a gRPC stream.
|
|
|
|
func readInner(fp io.Reader, stream WriteChunkStream, chunksize uint, bar *progressbar.ProgressBar) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
buf := make([]byte, chunksize)
|
|
|
|
for {
|
2023-01-20 04:11:41 -05:00
|
|
|
n, readErr := fp.Read(buf)
|
|
|
|
isLast := errors.Is(readErr, io.EOF)
|
|
|
|
if readErr != nil && !isLast {
|
|
|
|
return fmt.Errorf("reading file chunk: %w", readErr)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-01-20 04:11:41 -05:00
|
|
|
if err := stream.Send(&pb.Chunk{Content: buf[:n], Last: isLast}); 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
|
|
|
}
|
2023-01-20 04:11:41 -05:00
|
|
|
if bar != nil {
|
2022-03-25 07:50:07 -04:00
|
|
|
_ = bar.Add(n)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-01-20 04:11:41 -05:00
|
|
|
if isLast {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeInner writes chunks from a gRPC stream to an io.Writer.
|
|
|
|
func writeInner(fp io.Writer, stream ReadChunkStream, bar *progressbar.ProgressBar) error {
|
|
|
|
for {
|
|
|
|
chunk, recvErr := stream.Recv()
|
|
|
|
if recvErr != nil {
|
|
|
|
return fmt.Errorf("reading stream: %w", recvErr)
|
|
|
|
}
|
|
|
|
if _, err := fp.Write(chunk.Content); err != nil {
|
|
|
|
return fmt.Errorf("writing chunk to disk: %w", err)
|
|
|
|
}
|
|
|
|
if bar != nil {
|
|
|
|
_ = bar.Add(len(chunk.Content))
|
|
|
|
}
|
|
|
|
if chunk.Last {
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
2023-01-20 04:11:41 -05:00
|
|
|
|
|
|
|
// newProgressBar creates a new progress bar.
|
|
|
|
func newProgressBar(size int64) *progressbar.ProgressBar {
|
|
|
|
return progressbar.NewOptions64(
|
|
|
|
size,
|
|
|
|
progressbar.OptionSetDescription("transferring file"),
|
|
|
|
progressbar.OptionShowBytes(true),
|
|
|
|
progressbar.OptionClearOnFinish(),
|
|
|
|
)
|
|
|
|
}
|