-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk.go
More file actions
75 lines (65 loc) · 1.76 KB
/
disk.go
File metadata and controls
75 lines (65 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package golinker
import (
"bytes"
"compress/gzip"
"io"
"math/rand/v2"
"os"
"path/filepath"
"strconv"
"strings"
)
func getTempDir() string {
tempDir := os.TempDir()
// Create a file to test if permissions allow storage
randN := strconv.Itoa(rand.Int())
randFile := filepath.Join(tempDir, randN)
err := os.WriteFile(randFile, []byte{}, 0644)
if err == nil {
os.Remove(randFile)
return tempDir
}
// os.TempDir failed so try directory of executable
exePath, err1 := os.Executable()
if err1 != nil {
panic(pkgname + ": os.WriteFile(" + tempDir + "): " + err.Error())
}
tempDir = filepath.Dir(exePath)
randFile = filepath.Join(tempDir, randN)
err1 = os.WriteFile(randFile, []byte{}, 0644)
if err1 != nil {
panic(pkgname + ": os.WriteFile(" + tempDir + "): " + err1.Error())
}
os.Remove(randFile)
return tempDir
}
// writeBytesToDisk writes the package's object file to disk and returns the location
func writeBytesToDisk(pkg []byte, fullPackageName string) string {
// Create a temp directory
tempDir := getTempDir()
dst := filepath.Join(tempDir, strings.ReplaceAll(fullPackageName, "/", "_")+"_"+strconv.Itoa(rand.Int())+".golinker")
// Assume gzipped
zr, err := gzip.NewReader(bytes.NewReader(pkg))
if err != nil {
// Not a valid gzip file. Assume it's a raw object file.
err := os.WriteFile(dst, pkg, 0666)
if err != nil {
panic(pkgname + ": os.WriteFile(" + dst + "): " + err.Error())
}
toRemove = append(toRemove, dst)
return dst
}
defer zr.Close()
f, err := os.Create(dst)
if err != nil {
panic(pkgname + ": os.Create(" + dst + "): " + err.Error())
}
toRemove = append(toRemove, dst)
defer f.Close()
// Write file to disk
_, err = io.Copy(f, zr)
if err != nil {
panic(pkgname + ": io.Copy(): " + err.Error())
}
return dst
}