waveterm/pkg/sstore/fileops.go

65 lines
1.5 KiB
Go
Raw Normal View History

package sstore
import (
"context"
"encoding/base64"
"fmt"
"github.com/scripthaus-dev/mshell/pkg/cirfile"
"github.com/scripthaus-dev/sh2-server/pkg/scbase"
)
func CreateCmdPtyFile(ctx context.Context, sessionId string, cmdId string, maxSize int64) error {
ptyOutFileName, err := scbase.PtyOutFile(sessionId, cmdId)
if err != nil {
return err
}
f, err := cirfile.CreateCirFile(ptyOutFileName, maxSize)
if err != nil {
return err
}
return f.Close()
}
func AppendToCmdPtyBlob(ctx context.Context, sessionId string, cmdId string, data []byte, pos int64) error {
if pos < 0 {
return fmt.Errorf("invalid seek pos '%d' in AppendToCmdPtyBlob", pos)
}
ptyOutFileName, err := scbase.PtyOutFile(sessionId, cmdId)
if err != nil {
return err
}
f, err := cirfile.OpenCirFile(ptyOutFileName)
if err != nil {
return err
}
defer f.Close()
err = f.WriteAt(ctx, data, pos)
if err != nil {
return err
}
data64 := base64.StdEncoding.EncodeToString(data)
update := &PtyDataUpdate{
SessionId: sessionId,
CmdId: cmdId,
PtyPos: pos,
PtyData64: data64,
PtyDataLen: int64(len(data)),
}
MainBus.SendUpdate(sessionId, update)
return nil
}
func ReadFullPtyOutFile(ctx context.Context, sessionId string, cmdId string) (int64, []byte, error) {
ptyOutFileName, err := scbase.PtyOutFile(sessionId, cmdId)
if err != nil {
return 0, nil, err
}
f, err := cirfile.OpenCirFile(ptyOutFileName)
if err != nil {
return 0, nil, err
}
defer f.Close()
return f.ReadAll(ctx)
}