瀏覽代碼

make tmpfiles actually temporary and random

we can't use ioutil.TempFile because we need to set a prefix, many
language tools expect a certain prefix.

also, this probably means multiuser support!
Lucas Stadler 11 年之前
父節點
當前提交
2535d4dd1d
共有 1 個文件被更改,包括 10 次插入1 次删除
  1. 10 1
      go/linguaevalia/linguaevalia.go

+ 10 - 1
go/linguaevalia/linguaevalia.go

5
// try it with `curl -i localhost:8000/run --data-binary @hello-world.go`
5
// try it with `curl -i localhost:8000/run --data-binary @hello-world.go`
6
6
7
import (
7
import (
8
	"crypto/rand"
8
	"fmt"
9
	"fmt"
9
	"html/template"
10
	"html/template"
10
	"io/ioutil"
11
	"io/ioutil"
11
	"log"
12
	"log"
13
	"math/big"
12
	"net/http"
14
	"net/http"
13
	"os"
15
	"os"
14
	"os/exec"
16
	"os/exec"
17
	"path"
15
)
18
)
16
19
17
type Language interface {
20
type Language interface {
65
68
66
func writeCode(code string, extension string) (*os.File, error) {
69
func writeCode(code string, extension string) (*os.File, error) {
67
	// create tmp file
70
	// create tmp file
68
	f, err := os.Create(fmt.Sprintf("/tmp/linguaevalia-%s.%s", extension, extension)) // FIXME: actually create a tmpfile
71
	f, err := tempFile("/tmp", "linguaevalia", extension)
69
	if err != nil {
72
	if err != nil {
70
		return f, err
73
		return f, err
71
	}
74
	}
77
	return f, nil
80
	return f, nil
78
}
81
}
79
82
83
func tempFile(dir, prefix, suffix string) (*os.File, error) {
84
	rnd, _ := rand.Int(rand.Reader, big.NewInt(999999))
85
	f, err := os.Create(path.Join(dir, fmt.Sprintf("%s%d.%s", prefix, rnd, suffix)))
86
	return f, err
87
}
88
80
func Eval(lang Language, code string) ([]byte, error) {
89
func Eval(lang Language, code string) ([]byte, error) {
81
	// write code to temp file
90
	// write code to temp file
82
	f, err := writeCode(code, lang.Extension())
91
	f, err := writeCode(code, lang.Extension())