refactor: move sandbox convert functions out of Run
This commit is contained in:
		
							parent
							
								
									ad7e74c0f5
								
							
						
					
					
						commit
						916511d91a
					
				|  | @ -1,6 +1,7 @@ | ||||||
| package sandbox | package sandbox | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
|  | @ -26,7 +27,7 @@ func convertPBCmd(cmd []stage.Cmd) []*pb.Request_CmdType { | ||||||
| 			CpuSetLimit:       c.CPUSetLimit, | 			CpuSetLimit:       c.CPUSetLimit, | ||||||
| 			DataSegmentLimit:  c.DataSegmentLimit, | 			DataSegmentLimit:  c.DataSegmentLimit, | ||||||
| 			AddressSpaceLimit: c.AddressSpaceLimit, | 			AddressSpaceLimit: c.AddressSpaceLimit, | ||||||
| 			CopyIn:            convertPBCopyIn(c.CopyIn), | 			CopyIn:            convertPBCopyIn(c.CopyIn, c.CopyInCwd), | ||||||
| 			CopyOut:           convertPBCopyOut(c.CopyOut), | 			CopyOut:           convertPBCopyOut(c.CopyOut), | ||||||
| 			CopyOutCached:     convertPBCopyOut(c.CopyOutCached), | 			CopyOutCached:     convertPBCopyOut(c.CopyOutCached), | ||||||
| 			CopyOutMax:        c.CopyOutMax, | 			CopyOutMax:        c.CopyOutMax, | ||||||
|  | @ -37,7 +38,23 @@ func convertPBCmd(cmd []stage.Cmd) []*pb.Request_CmdType { | ||||||
| 	return ret | 	return ret | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func convertPBCopyIn(copyIn map[string]stage.CmdFile) map[string]*pb.Request_File { | func convertPBCopyIn(copyIn map[string]stage.CmdFile, copyInCwd bool) map[string]*pb.Request_File { | ||||||
|  | 	if copyInCwd { | ||||||
|  | 		_ = filepath.Walk(".", | ||||||
|  | 			func(path string, info os.FileInfo, err error) error { | ||||||
|  | 				if err != nil { | ||||||
|  | 					return nil | ||||||
|  | 				} | ||||||
|  | 				absPath, err := filepath.Abs(path) | ||||||
|  | 				if err != nil { | ||||||
|  | 					return nil | ||||||
|  | 				} | ||||||
|  | 				if !info.IsDir() { | ||||||
|  | 					copyIn[path] = stage.CmdFile{Src: &absPath} | ||||||
|  | 				} | ||||||
|  | 				return nil | ||||||
|  | 			}) | ||||||
|  | 	} | ||||||
| 	rt := make(map[string]*pb.Request_File, len(copyIn)) | 	rt := make(map[string]*pb.Request_File, len(copyIn)) | ||||||
| 	for k, i := range copyIn { | 	for k, i := range copyIn { | ||||||
| 		if i.Symlink != nil { | 		if i.Symlink != nil { | ||||||
|  | @ -90,6 +107,12 @@ func convertPBFiles(files []*stage.CmdFile) []*pb.Request_File { | ||||||
| func convertPBFile(i stage.CmdFile) *pb.Request_File { | func convertPBFile(i stage.CmdFile) *pb.Request_File { | ||||||
| 	switch { | 	switch { | ||||||
| 	case i.Src != nil: | 	case i.Src != nil: | ||||||
|  | 		if !filepath.IsAbs(*i.Src) { | ||||||
|  | 			absPath, err := filepath.Abs(*i.Src) | ||||||
|  | 			if err == nil { | ||||||
|  | 				i.Src = &absPath | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
| 		return &pb.Request_File{File: &pb.Request_File_Local{Local: &pb.Request_LocalFile{Src: *i.Src}}} | 		return &pb.Request_File{File: &pb.Request_File_Local{Local: &pb.Request_LocalFile{Src: *i.Src}}} | ||||||
| 	case i.Content != nil: | 	case i.Content != nil: | ||||||
| 		s := strToBytes(*i.Content) | 		s := strToBytes(*i.Content) | ||||||
|  | @ -106,6 +129,17 @@ func convertPBFile(i stage.CmdFile) *pb.Request_File { | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func convertAbsPath(cmdFile *stage.CmdFile) error { | ||||||
|  | 	if cmdFile.Src != nil && !filepath.IsAbs(*cmdFile.Src) { | ||||||
|  | 		absPath, err := filepath.Abs(*cmdFile.Src) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 		cmdFile.Src = &absPath | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func convertPBResult(res []*pb.Response_Result) []stage.ExecutorResult { | func convertPBResult(res []*pb.Response_Result) []stage.ExecutorResult { | ||||||
| 	var ret []stage.ExecutorResult | 	var ret []stage.ExecutorResult | ||||||
| 	for _, r := range res { | 	for _, r := range res { | ||||||
|  | @ -144,14 +178,3 @@ func convertPBFileError(fe []*pb.Response_FileError) []stage.FileError { | ||||||
| 	} | 	} | ||||||
| 	return ret | 	return ret | ||||||
| } | } | ||||||
| 
 |  | ||||||
| func convertAbsPath(cmdFile *stage.CmdFile) error { |  | ||||||
| 	if cmdFile.Src != nil && !filepath.IsAbs(*cmdFile.Src) { |  | ||||||
| 		absPath, err := filepath.Abs(*cmdFile.Src) |  | ||||||
| 		if err != nil { |  | ||||||
| 			return err |  | ||||||
| 		} |  | ||||||
| 		cmdFile.Src = &absPath |  | ||||||
| 	} |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -4,8 +4,6 @@ import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"log/slog" | 	"log/slog" | ||||||
| 	"os" |  | ||||||
| 	"path/filepath" |  | ||||||
| 
 | 
 | ||||||
| 	"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" | 	"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" | ||||||
| 	"github.com/criyle/go-judge/pb" | 	"github.com/criyle/go-judge/pb" | ||||||
|  | @ -26,35 +24,6 @@ func (e *Sandbox) Run(cmd stage.Cmd) (*stage.ExecutorResult, error) { | ||||||
| 			cmd.CopyIn[k] = stage.CmdFile{FileID: &fileID} | 			cmd.CopyIn[k] = stage.CmdFile{FileID: &fileID} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	if cmd.CopyInCwd { |  | ||||||
| 		err := filepath.Walk(".", |  | ||||||
| 			func(path string, info os.FileInfo, err error) error { |  | ||||||
| 				if err != nil { |  | ||||||
| 					return err |  | ||||||
| 				} |  | ||||||
| 				absPath, err := filepath.Abs(path) |  | ||||||
| 				if err != nil { |  | ||||||
| 					return err |  | ||||||
| 				} |  | ||||||
| 				if !info.IsDir() { |  | ||||||
| 					cmd.CopyIn[path] = stage.CmdFile{Src: &absPath} |  | ||||||
| 				} |  | ||||||
| 				return nil |  | ||||||
| 			}) |  | ||||||
| 		if err != nil { |  | ||||||
| 			return nil, err |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	for _, file := range cmd.Files { |  | ||||||
| 		if err := convertAbsPath(file); err != nil { |  | ||||||
| 			return nil, err |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	for _, file := range cmd.CopyIn { |  | ||||||
| 		if err := convertAbsPath(&file); err != nil { |  | ||||||
| 			return nil, err |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	req := &pb.Request{Cmd: convertPBCmd([]stage.Cmd{cmd})} | 	req := &pb.Request{Cmd: convertPBCmd([]stage.Cmd{cmd})} | ||||||
| 	ret, err := e.execClient.Exec(context.TODO(), req) | 	ret, err := e.execClient.Exec(context.TODO(), req) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user