-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
54 lines (45 loc) · 1017 Bytes
/
utils.go
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
package lsp_server
import (
"fmt"
"hash/fnv"
"os"
"path/filepath"
)
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
var isTempDirInited = false
var tempDirPath = ""
func getTempDir() string {
if isTempDirInited {
return tempDirPath
}
path := filepath.Join(os.TempDir(), "bugbuddy")
if !isTempDirInited {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.Mkdir(path, 0700); err == nil {
isTempDirInited = true
}
} else {
isTempDirInited = true
}
}
tempDirPath = path
return path
}
func getTempFilePath(name string) string {
return filepath.Join(getTempDir(), fmt.Sprintf("bugbuddy-%d.md", hash(name)))
}
func getTempFileForFile(name string) (*os.File, error) {
fullPath := getTempFilePath(name)
flags := os.O_RDWR | os.O_EXCL
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
flags |= os.O_CREATE
}
return os.OpenFile(fullPath, flags, 0600)
}
func removeAllTempFiles() error {
return os.RemoveAll(getTempDir())
}