From fe43a608d00e2ada79e3b423e18cda33275a2c4e Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Sun, 26 Feb 2023 13:09:41 +0000 Subject: [PATCH] 6. Used io.Copy to move files --- fuse.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/fuse.go b/fuse.go index 80d867f..7bb7535 100644 --- a/fuse.go +++ b/fuse.go @@ -10,6 +10,8 @@ import ( "github.com/hanwen/go-fuse/v2/fs" "github.com/hanwen/go-fuse/v2/fuse" "strconv" + "io" + "bytes" ) type Device struct { @@ -266,8 +268,17 @@ func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno) log.Error("Flush failed: create").Str("path", fh.path).Err(err).Send() return syscall.EROFS } - nread, err := fp.Write(fh.content) - if err != nil || nread != len(fh.content) { + + go func() { + // For every progress event + for sent := range fp.Progress() { + log.Info("Progress").Int("bytes", int(sent)).Int("of", len(fh.content)).Send(); + } + }() + + r := bytes.NewReader(fh.content) + nread, err := io.Copy(fp, r) + if err != nil { log.Error("Flush failed: write").Str("path", fh.path).Err(err).Send() fp.Close() return syscall.EROFS @@ -337,16 +348,24 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, defer fp.Close() - buf := make([]byte, f.self.size); - nread, err := fp.Read(buf) - if err != nil || nread != int(f.self.size) { - log.Error("Reading file failed").Str("path", f.path).Err(err).Send(); + b := &bytes.Buffer{} + + go func() { + // For every progress event + for sent := range fp.Progress() { + log.Info("Progress").Int("bytes", int(sent)).Int("of", int(f.self.size)).Send(); + } + }() + + _, err = io.Copy(b, fp) + if err != nil { + log.Error("Read failed").Str("path", f.path).Err(err).Send() fp.Close() return nil, 0, syscall.EROFS } fh = &bytesFileReadHandle{ - content: buf, + content: b.Bytes(), } return fh, fuse.FOPEN_DIRECT_IO, 0 }