Browse Source

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 years ago
parent
commit
2535d4dd1d
1 changed files with 10 additions and 1 deletions
  1. 10 1
      go/linguaevalia/linguaevalia.go

+ 10 - 1
go/linguaevalia/linguaevalia.go

@ -5,13 +5,16 @@ package main
5 5
// try it with `curl -i localhost:8000/run --data-binary @hello-world.go`
6 6
7 7
import (
8
	"crypto/rand"
8 9
	"fmt"
9 10
	"html/template"
10 11
	"io/ioutil"
11 12
	"log"
13
	"math/big"
12 14
	"net/http"
13 15
	"os"
14 16
	"os/exec"
17
	"path"
15 18
)
16 19
17 20
type Language interface {
@ -65,7 +68,7 @@ var languageMappings = map[string]Language{
65 68
66 69
func writeCode(code string, extension string) (*os.File, error) {
67 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 72
	if err != nil {
70 73
		return f, err
71 74
	}
@ -77,6 +80,12 @@ func writeCode(code string, extension string) (*os.File, error) {
77 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 89
func Eval(lang Language, code string) ([]byte, error) {
81 90
	// write code to temp file
82 91
	f, err := writeCode(code, lang.Extension())