package ops2 import ( "fmt" clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types" "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/dag" "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/exec" "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/pool" ) func init() { exec.UseOp[*S2STransfer]() exec.UseOp[*S2STransferDyn]() } type S2STransfer struct { SrcSpace clitypes.UserSpaceDetail SrcPath clitypes.JPath DstSpace clitypes.UserSpaceDetail DstPath clitypes.JPath Output exec.VarID } func (o *S2STransfer) Execute(ctx *exec.ExecContext, e *exec.Executor) error { stgPool, err := exec.GetValueByType[*pool.Pool](ctx) if err != nil { return fmt.Errorf("getting storage pool: %w", err) } s2s, err := stgPool.GetS2STransfer(&o.DstSpace) if err != nil { return err } // 传输文件 fileInfo, err := s2s.Transfer(ctx.Context, &o.SrcSpace, o.SrcPath, o.DstPath) if err != nil { return err } defer s2s.Close() // 告知后续Op处理临时文件 e.PutVar(o.Output, &FileInfoValue{FileInfo: fileInfo}) return nil } func (o *S2STransfer) String() string { return fmt.Sprintf("S2STransfer %v:%v -> %v:%v, %v", o.SrcSpace.UserSpace.Storage.String(), o.SrcPath, o.DstSpace.UserSpace.Storage.String(), o.DstPath, o.Output) } type S2STransferDyn struct { SrcSpace clitypes.UserSpaceDetail SrcFileInfo exec.VarID DstSpace clitypes.UserSpaceDetail DstPath clitypes.JPath Output exec.VarID } func (o *S2STransferDyn) Execute(ctx *exec.ExecContext, e *exec.Executor) error { stgPool, err := exec.GetValueByType[*pool.Pool](ctx) if err != nil { return fmt.Errorf("getting storage pool: %w", err) } srcInfo, err := exec.BindVar[*FileInfoValue](e, ctx.Context, o.SrcFileInfo) if err != nil { return err } s2s, err := stgPool.GetS2STransfer(&o.DstSpace) if err != nil { return err } // 传输文件 fileInfo, err := s2s.Transfer(ctx.Context, &o.SrcSpace, srcInfo.Path, o.DstPath) if err != nil { return err } defer s2s.Close() // 告知后续Op处理临时文件 e.PutVar(o.Output, &FileInfoValue{FileInfo: fileInfo}) return nil } func (o *S2STransferDyn) String() string { return fmt.Sprintf("S2STransferDyn %v:%v -> %v:%v, %v", o.SrcSpace.UserSpace.Storage.String(), o.SrcFileInfo, o.DstSpace.UserSpace.Storage.String(), o.DstPath, o.Output) } type S2STransferNode struct { dag.NodeBase SrcSpace clitypes.UserSpaceDetail SrcPath clitypes.JPath DstSpace clitypes.UserSpaceDetail DstPath clitypes.JPath } func (b *GraphNodeBuilder) NewS2STransfer(srcSpace clitypes.UserSpaceDetail, srcPath clitypes.JPath, dstSpace clitypes.UserSpaceDetail, dstPath clitypes.JPath) *S2STransferNode { n := &S2STransferNode{ SrcSpace: srcSpace, SrcPath: srcPath, DstSpace: dstSpace, DstPath: dstPath, } b.AddNode(n) n.OutputValues().Init(n, 1) return n } func (n *S2STransferNode) FileInfoVar() dag.ValueOutputSlot { return dag.ValueOutputSlot{ Node: n, Index: 0, } } func (n *S2STransferNode) GenerateOp() (exec.Op, error) { return &S2STransfer{ SrcSpace: n.SrcSpace, SrcPath: n.SrcPath, DstSpace: n.DstSpace, DstPath: n.DstPath, Output: n.FileInfoVar().Var().VarID, }, nil } type S2STransferDynNode struct { dag.NodeBase SrcSpace clitypes.UserSpaceDetail DstSpace clitypes.UserSpaceDetail DstPath clitypes.JPath } func (b *GraphNodeBuilder) NewS2STransferDyn(srcSpace clitypes.UserSpaceDetail, dstSpace clitypes.UserSpaceDetail, dstPath clitypes.JPath) *S2STransferDynNode { n := &S2STransferDynNode{ SrcSpace: srcSpace, DstSpace: dstSpace, DstPath: dstPath, } b.AddNode(n) n.InputValues().Init(1) n.OutputValues().Init(n, 1) return n } func (n *S2STransferDynNode) SrcFileInfoSlot() dag.ValueInputSlot { return dag.ValueInputSlot{ Node: n, Index: 0, } } func (n *S2STransferDynNode) FileInfoVar() dag.ValueOutputSlot { return dag.ValueOutputSlot{ Node: n, Index: 0, } } func (n *S2STransferDynNode) GenerateOp() (exec.Op, error) { return &S2STransferDyn{ SrcSpace: n.SrcSpace, SrcFileInfo: n.SrcFileInfoSlot().Var().VarID, DstSpace: n.DstSpace, DstPath: n.DstPath, Output: n.FileInfoVar().Var().VarID, }, nil }