go-judge/file/memfile/memfile.go
2019-12-30 19:32:17 +08:00

45 lines
858 B
Go

package memfile
import (
"bytes"
"fmt"
"os"
"github.com/criyle/go-judge/file"
"github.com/criyle/go-sandbox/pkg/memfd"
)
// File represent a file like byte array
type File struct {
name string
content []byte
}
// New create a file interface from content and content should not be modified afterwards
func New(name string, content []byte) file.File {
return &File{
name: name,
content: content,
}
}
// Name returns the file name
func (m *File) Name() string {
return m.name
}
// Content returns the file content
func (m *File) Content() ([]byte, error) {
return m.content, nil
}
// Open creates a memfd file
func (m *File) Open() (*os.File, error) {
return memfd.DupToMemfd(m.name, bytes.NewReader(m.content))
}
// String stringer
func (m *File) String() string {
return fmt.Sprintf("[memfile:%v,%d]", m.name, len(m.content))
}