Directly read/write files from ITD

This commit is contained in:
Elara 2021-12-11 22:11:01 -08:00
parent a9ef386883
commit 873df67d1f
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
}
func (c *Client) ReadFile(path string) (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 {
func (c *Client) ReadFile(localPath, remotePath string) error {
_, err := c.request(types.Request{
Type: types.ReqTypeFS,
Data: types.ReqDataFS{
Type: types.FSTypeWrite,
Files: []string{path},
Data: data,
Type: types.FSTypeRead,
Files: []string{localPath, remotePath},
},
})
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 {

View File

@ -19,8 +19,9 @@
package filesystem
import (
"io"
"io/ioutil"
"os"
"time"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
@ -38,33 +39,32 @@ var readCmd = &cobra.Command{
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)
data, err := client.ReadFile(args[0])
err = client.ReadFile(path, args[0])
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] == "-" {
out = os.Stdout
suffix = "\n"
} else {
out, err = os.Create(args[1])
if err != nil {
log.Fatal().Err(err).Msg("Error opening local file")
}
io.Copy(os.Stdout, tmpFile)
os.Stdout.WriteString("\n")
tmpFile.Close()
os.Remove(path)
}
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 (
"io"
"io/ioutil"
"os"
"time"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
@ -39,31 +39,31 @@ var writeCmd = &cobra.Command{
log.Fatal().Msg("Command write requires two arguments")
}
start := time.Now()
client := viper.Get("client").(*api.Client)
var in *os.File
var tmpFile *os.File
var path string
var err error
if args[0] == "-" {
in = os.Stdin
} else {
fl, err := os.Open(args[0])
tmpFile, err = ioutil.TempFile("/tmp", "itctl.*")
if err != nil {
log.Fatal().Err(err).Msg("Error opening local file")
log.Fatal().Err(err).Msg("Error creating temporary file")
}
in = fl
path = tmpFile.Name()
} else {
path = args[0]
}
data, err := io.ReadAll(in)
client := viper.Get("client").(*api.Client)
if args[0] == "-" {
io.Copy(tmpFile, os.Stdin)
defer tmpFile.Close()
defer os.Remove(path)
}
err = client.WriteFile(path, args[1])
if err != nil {
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,
})
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")
break
}
file, err := fs.Create(reqData.Files[0], uint32(len(reqData.Data)))
localFile, err := os.Open(reqData.Files[1])
if err != nil {
connErr(conn, req.Type, err, "Error creating file")
connErr(conn, req.Type, err, "Error opening local file")
break
}
_, err = file.WriteString(reqData.Data)
defer localFile.Close()
localInfo, err := localFile.Stat()
if err != nil {
connErr(conn, req.Type, err, "Error writing to file")
connErr(conn, req.Type, err, "Error getting local file information")
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})
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")
break
}
file, err := fs.Open(reqData.Files[0])
localFile, err := os.Create(reqData.Files[0])
if err != nil {
connErr(conn, req.Type, err, "Error opening file")
connErr(conn, req.Type, err, "Error creating local file")
break
}
data, err := io.ReadAll(file)
remoteFile, err := fs.Open(reqData.Files[1])
if err != nil {
connErr(conn, req.Type, err, "Error reading from file")
connErr(conn, req.Type, err, "Error opening remote file")
break
}
io.Copy(localFile, remoteFile)
json.NewEncoder(conn).Encode(types.Response{
Type: req.Type,
Value: string(data),
Type: req.Type,
})
}
case types.ReqTypeCancel: