Directly read/write files from ITD

This commit is contained in:
Elara 2021-12-11 22:11:01 -08:00
parent 488b5a7d89
commit 9bfdaef70c
4 changed files with 85 additions and 69 deletions

View File

@ -66,27 +66,26 @@ func (c *Client) ReadDir(path string) ([]types.FileInfo, error) {
return out, nil return out, nil
} }
func (c *Client) ReadFile(path string) (string, error) { func (c *Client) ReadFile(localPath, remotePath string) error {
res, err := c.request(types.Request{
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeRead,
Files: []string{path},
},
})
if err != nil {
return "", err
}
return res.Value.(string), nil
}
func (c *Client) WriteFile(path, data string) error {
_, err := c.request(types.Request{ _, err := c.request(types.Request{
Type: types.ReqTypeFS, Type: types.ReqTypeFS,
Data: types.ReqDataFS{ Data: types.ReqDataFS{
Type: types.FSTypeWrite, Type: types.FSTypeRead,
Files: []string{path}, Files: []string{localPath, remotePath},
Data: data, },
})
if err != nil {
return err
}
return nil
}
func (c *Client) WriteFile(localPath, remotePath string) error {
_, err := c.request(types.Request{
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeWrite,
Files: []string{remotePath, localPath},
}, },
}) })
if err != nil { if err != nil {

View File

@ -19,8 +19,9 @@
package filesystem package filesystem
import ( import (
"io"
"io/ioutil"
"os" "os"
"time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -38,33 +39,32 @@ var readCmd = &cobra.Command{
log.Fatal().Msg("Command read requires two arguments") log.Fatal().Msg("Command read requires two arguments")
} }
start := time.Now() var tmpFile *os.File
var path string
var err error
if args[1] == "-" {
tmpFile, err = ioutil.TempFile("/tmp", "itctl.*")
if err != nil {
log.Fatal().Err(err).Msg("Error creating temporary file")
}
path = tmpFile.Name()
} else {
path = args[1]
}
client := viper.Get("client").(*api.Client) client := viper.Get("client").(*api.Client)
data, err := client.ReadFile(args[0]) err = client.ReadFile(path, args[0])
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error moving file or directory") log.Fatal().Err(err).Msg("Error reading remote file")
} }
var suffix string
var out *os.File
if args[1] == "-" { if args[1] == "-" {
out = os.Stdout io.Copy(os.Stdout, tmpFile)
suffix = "\n" os.Stdout.WriteString("\n")
} else { tmpFile.Close()
out, err = os.Create(args[1]) os.Remove(path)
if err != nil {
log.Fatal().Err(err).Msg("Error opening local file")
} }
}
n, err := out.WriteString(data)
if err != nil {
log.Fatal().Err(err).Msg("Error writing to local file")
}
out.WriteString(suffix)
log.Info().Msgf("Read %d bytes in %s", n, time.Since(start))
}, },
} }

View File

@ -20,8 +20,8 @@ package filesystem
import ( import (
"io" "io"
"io/ioutil"
"os" "os"
"time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -39,31 +39,31 @@ var writeCmd = &cobra.Command{
log.Fatal().Msg("Command write requires two arguments") log.Fatal().Msg("Command write requires two arguments")
} }
start := time.Now() var tmpFile *os.File
var path string
var err error
if args[0] == "-" {
tmpFile, err = ioutil.TempFile("/tmp", "itctl.*")
if err != nil {
log.Fatal().Err(err).Msg("Error creating temporary file")
}
path = tmpFile.Name()
} else {
path = args[0]
}
client := viper.Get("client").(*api.Client) client := viper.Get("client").(*api.Client)
var in *os.File
if args[0] == "-" { if args[0] == "-" {
in = os.Stdin io.Copy(tmpFile, os.Stdin)
} else { defer tmpFile.Close()
fl, err := os.Open(args[0]) defer os.Remove(path)
if err != nil {
log.Fatal().Err(err).Msg("Error opening local file")
}
in = fl
} }
data, err := io.ReadAll(in) err = client.WriteFile(path, args[1])
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error moving file or directory") log.Fatal().Err(err).Msg("Error moving file or directory")
} }
err = client.WriteFile(args[1], string(data))
if err != nil {
log.Fatal().Err(err).Msg("Error writing to remote file")
}
log.Info().Msgf("Wrote %d bytes in %s", len(data), time.Since(start))
}, },
} }

View File

@ -535,39 +535,56 @@ func handleConnection(conn net.Conn, dev *infinitime.Device, fs *blefs.FS) {
Value: out, Value: out,
}) })
case types.FSTypeWrite: case types.FSTypeWrite:
if len(reqData.Files) != 1 { if len(reqData.Files) != 2 {
connErr(conn, req.Type, nil, "Write FS command requires a path to the file to write") connErr(conn, req.Type, nil, "Write FS command requires a path to the file to write")
break break
} }
file, err := fs.Create(reqData.Files[0], uint32(len(reqData.Data)))
localFile, err := os.Open(reqData.Files[1])
if err != nil { if err != nil {
connErr(conn, req.Type, err, "Error creating file") connErr(conn, req.Type, err, "Error opening local file")
break break
} }
_, err = file.WriteString(reqData.Data) defer localFile.Close()
localInfo, err := localFile.Stat()
if err != nil { if err != nil {
connErr(conn, req.Type, err, "Error writing to file") connErr(conn, req.Type, err, "Error getting local file information")
break break
} }
remoteFile, err := fs.Create(reqData.Files[0], uint32(localInfo.Size()))
if err != nil {
connErr(conn, req.Type, err, "Error creating remote file")
break
}
defer remoteFile.Close()
io.Copy(remoteFile, localFile)
json.NewEncoder(conn).Encode(types.Response{Type: req.Type}) json.NewEncoder(conn).Encode(types.Response{Type: req.Type})
case types.FSTypeRead: case types.FSTypeRead:
if len(reqData.Files) != 1 { fmt.Println(scanner.Text(), reqData)
if len(reqData.Files) != 2 {
connErr(conn, req.Type, nil, "Read FS command requires a path to the file to read") connErr(conn, req.Type, nil, "Read FS command requires a path to the file to read")
break break
} }
file, err := fs.Open(reqData.Files[0]) localFile, err := os.Create(reqData.Files[0])
if err != nil { if err != nil {
connErr(conn, req.Type, err, "Error opening file") connErr(conn, req.Type, err, "Error creating local file")
break break
} }
data, err := io.ReadAll(file)
remoteFile, err := fs.Open(reqData.Files[1])
if err != nil { if err != nil {
connErr(conn, req.Type, err, "Error reading from file") connErr(conn, req.Type, err, "Error opening remote file")
break break
} }
io.Copy(localFile, remoteFile)
json.NewEncoder(conn).Encode(types.Response{ json.NewEncoder(conn).Encode(types.Response{
Type: req.Type, Type: req.Type,
Value: string(data),
}) })
} }
case types.ReqTypeCancel: case types.ReqTypeCancel: