From 382b1d9556cf926134401eaafe2d470782277354 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Sun, 12 Dec 2021 13:25:40 -0800 Subject: [PATCH] Add file transfer progress --- blefs/file.go | 21 ++++++++++++++++++++- blefs/iofs.go | 15 +++++++++++++++ go.mod | 1 + go.sum | 5 ++--- 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 blefs/iofs.go diff --git a/blefs/file.go b/blefs/file.go index 6fe9c5d..b1938d0 100644 --- a/blefs/file.go +++ b/blefs/file.go @@ -12,6 +12,7 @@ type File struct { fs *FS path string offset uint32 + offsetCh chan uint32 length uint32 amtLeft uint32 amtTferd uint32 @@ -22,7 +23,7 @@ type File struct { // Open opens a file and returns it as an fs.File to // satisfy the fs.FS interface -func (blefs *FS) Open(path string) (fs.File, error) { +func (blefs *FS) Open(path string) (*File, error) { // Make a read file request. This opens the file for reading. err := blefs.request( FSCmdReadFile, @@ -40,6 +41,7 @@ func (blefs *FS) Open(path string) (fs.File, error) { path: path, length: 0, offset: 0, + offsetCh: make(chan uint32, 5), isReadOnly: true, isWriteOnly: false, }, nil @@ -66,11 +68,23 @@ func (blefs *FS) Create(path string, size uint32) (*File, error) { length: size, amtLeft: size, offset: 0, + offsetCh: make(chan uint32, 5), isReadOnly: false, isWriteOnly: true, }, nil } +// Size returns the total size of the opened file +func (file *File) Size() uint32 { + return file.length +} + +// Progress returns a channel that receives the amount +// of bytes sent as they are sent +func (file *File) Progress() <-chan uint32 { + return file.offsetCh +} + // Read reads data from a file into b func (fl *File) Read(b []byte) (n int, err error) { // If file is write only (opened by FS.Create()) @@ -149,6 +163,7 @@ func (fl *File) Read(b []byte) (n int, err error) { fl.length = resp.length // Add returned chunk length to offset and amount transferred fl.offset += resp.chunkLen + fl.offsetCh <- fl.offset fl.amtTferd += resp.chunkLen // Calculate amount of bytes to be sent in next request @@ -168,6 +183,7 @@ func (fl *File) Read(b []byte) (n int, err error) { chunkLen, ) } + close(fl.offsetCh) // Copy buffer contents to b copied := copy(b, buf) // Return amount of bytes copied with EOF error @@ -262,8 +278,11 @@ func (fl *File) Write(b []byte) (n int, err error) { ) // Add chunk length to offset and amount transferred fl.offset += chunkLen + fl.offsetCh <- fl.offset fl.amtTferd += chunkLen } + + close(fl.offsetCh) return int(fl.offset), nil } diff --git a/blefs/iofs.go b/blefs/iofs.go new file mode 100644 index 0000000..05d1c1c --- /dev/null +++ b/blefs/iofs.go @@ -0,0 +1,15 @@ +package blefs + +import "io/fs" + +type goFS struct { + *FS +} + +func (iofs goFS) Open(path string) (fs.File, error) { + return iofs.FS.Open(path) +} + +func (blefs *FS) GoFS() fs.FS { + return goFS{blefs} +} \ No newline at end of file diff --git a/go.mod b/go.mod index c591e2f..7448154 100644 --- a/go.mod +++ b/go.mod @@ -6,4 +6,5 @@ require ( github.com/fxamacker/cbor/v2 v2.3.0 github.com/godbus/dbus/v5 v5.0.3 github.com/muka/go-bluetooth v0.0.0-20211122080231-b99792bbe62a + golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect ) diff --git a/go.sum b/go.sum index 216009e..5b1b7bc 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= -github.com/fxamacker/cbor v1.5.1 h1:XjQWBgdmQyqimslUh5r4tUGmoqzHmBFQOImkWGi2awg= -github.com/fxamacker/cbor v1.5.1/go.mod h1:3aPGItF174ni7dDzd6JZ206H8cmr4GDNBGpPa971zsU= github.com/fxamacker/cbor/v2 v2.3.0 h1:aM45YGMctNakddNNAezPxDUpv38j44Abh+hifNuqXik= github.com/fxamacker/cbor/v2 v2.3.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME= @@ -44,8 +42,9 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1 h1:sIky/MyNRSHTrdxfsiUSS4WIAMvInbeXljJz+jDjeYE= golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A= +golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200925191224-5d1fdd8fa346/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=