From dcec9f593b20f245f72fb5850be005de2a54bff7 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Mon, 13 Dec 2021 09:58:34 -0800 Subject: [PATCH] Propagate FS errors on read/write and close files when finished writing --- api/fs.go | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- socket.go | 7 ++++++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/api/fs.go b/api/fs.go index 1f4a071..1102e11 100644 --- a/api/fs.go +++ b/api/fs.go @@ -69,7 +69,7 @@ func (c *Client) ReadDir(path string) ([]types.FileInfo, error) { func (c *Client) ReadFile(localPath, remotePath string) (<-chan types.FSTransferProgress, error) { c.readProgressCh = make(chan types.FSTransferProgress, 5) - err := c.requestNoRes(types.Request{ + _, err := c.request(types.Request{ Type: types.ReqTypeFS, Data: types.ReqDataFS{ Type: types.FSTypeRead, @@ -87,7 +87,7 @@ func (c *Client) ReadFile(localPath, remotePath string) (<-chan types.FSTransfer func (c *Client) WriteFile(localPath, remotePath string) (<-chan types.FSTransferProgress, error) { c.writeProgressCh = make(chan types.FSTransferProgress, 5) - err := c.requestNoRes(types.Request{ + _, err := c.request(types.Request{ Type: types.ReqTypeFS, Data: types.ReqDataFS{ Type: types.FSTypeWrite, diff --git a/go.mod b/go.mod index 8ef646a..e40971e 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/srwiley/oksvg v0.0.0-20211120171407-1837d6608d8c // indirect github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect github.com/yuin/goldmark v1.4.4 // indirect - go.arsenm.dev/infinitime v0.0.0-20211212212540-382b1d9556cf + go.arsenm.dev/infinitime v0.0.0-20211213175634-9250d26fdc8c golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect diff --git a/go.sum b/go.sum index 1f88044..ddb1855 100644 --- a/go.sum +++ b/go.sum @@ -447,8 +447,8 @@ github.com/yuin/goldmark v1.3.8/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.4 h1:zNWRjYUW32G9KirMXYHQHVNFkXvMI7LpgNW2AgYAoIs= github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg= -go.arsenm.dev/infinitime v0.0.0-20211212212540-382b1d9556cf h1:TJhKfJLW9rMOCZnn7j8PJp8Ye4qxRX4HG1oaK0M7gQY= -go.arsenm.dev/infinitime v0.0.0-20211212212540-382b1d9556cf/go.mod h1:xioFSrICUc4ghNJPOMSZjF7nj7QHAFLUJ2qkrSsgFfQ= +go.arsenm.dev/infinitime v0.0.0-20211213175634-9250d26fdc8c h1:cnvVLBNWyBHRhNUI01xKL+gE/2UQ49fFDnvQ5YdMwvQ= +go.arsenm.dev/infinitime v0.0.0-20211213175634-9250d26fdc8c/go.mod h1:xioFSrICUc4ghNJPOMSZjF7nj7QHAFLUJ2qkrSsgFfQ= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/api/v3 v3.5.1/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= diff --git a/socket.go b/socket.go index 72487b6..6b9b883 100644 --- a/socket.go +++ b/socket.go @@ -582,6 +582,8 @@ func handleConnection(conn net.Conn, dev *infinitime.Device, fs *blefs.FS) { } }() + json.NewEncoder(conn).Encode(types.Response{Type: req.Type}) + io.Copy(remoteFile, localFile) json.NewEncoder(conn).Encode(types.Response{ @@ -603,12 +605,14 @@ func handleConnection(conn net.Conn, dev *infinitime.Device, fs *blefs.FS) { connErr(conn, req.Type, err, "Error creating local file") break } + defer localFile.Close() remoteFile, err := fs.Open(reqData.Files[1]) if err != nil { connErr(conn, req.Type, err, "Error opening remote file") break } + defer remoteFile.Close() go func() { // For every progress event @@ -626,8 +630,9 @@ func handleConnection(conn net.Conn, dev *infinitime.Device, fs *blefs.FS) { } }() + json.NewEncoder(conn).Encode(types.Response{Type: req.Type}) + io.Copy(localFile, remoteFile) - localFile.Sync() json.NewEncoder(conn).Encode(types.Response{ Type: req.Type,