package env
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
func (s *Sandbox) CompileCgiBin(sourceRelativePath string) (string, error) {
cwd, _ := os.Getwd()
sourcePath := filepath.Join(cwd, sourceRelativePath)
if _, err := os.Stat(sourcePath); os.IsNotExist(err) {
return "", fmt.Errorf("C source not found at %s", sourcePath)
}
outPath := filepath.Join(s.RootDir, "cgi_test_bin")
cmd := exec.Command("cc", "-o", outPath, sourcePath)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("compilation failed: %v\n%s", err, string(output))
}
return outPath, nil
}
func (s *Sandbox) CompileGoBin(sourceContent string) (string, error) {
srcPath := filepath.Join(s.RootDir, "plugin.go")
if err := os.WriteFile(srcPath, []byte(sourceContent), 0644); err != nil {
return "", fmt.Errorf("failed to write go source: %w", err)
}
binDir := filepath.Join(s.ConfigDir, "bin")
if err := os.MkdirAll(binDir, 0755); err != nil {
return "", fmt.Errorf("failed to create trusted bin dir: %w", err)
}
outPath := filepath.Join(binDir, "plugin_bin")
cmd := exec.Command("go", "build", "-o", outPath, srcPath)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("go compilation failed: %v\n%s", err, string(output))
}
return outPath, nil
}