diff --git a/log/log.go b/log/log.go index c56b87a..24533af 100644 --- a/log/log.go +++ b/log/log.go @@ -7,7 +7,8 @@ import ( ) func Init() { - file := "/tmp/thriftls.log" + + file := os.TempDir() + "/thriftls.log" logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766) if err != nil { panic(err) @@ -17,5 +18,4 @@ func Init() { } type Logger struct { - } diff --git a/lsp/cache/file.go b/lsp/cache/file.go index 1987eaf..c7fdeac 100644 --- a/lsp/cache/file.go +++ b/lsp/cache/file.go @@ -5,9 +5,7 @@ import ( "context" "crypto/sha256" "fmt" - "os" "sync" - "syscall" "time" "go.lsp.dev/protocol" @@ -28,18 +26,6 @@ type FileID struct { // Like os.Stat, it reads through symbolic links. func GetFileID(filename string) (FileID, time.Time, error) { return getFileID(filename) } -func getFileID(filename string) (FileID, time.Time, error) { - fi, err := os.Stat(filename) - if err != nil { - return FileID{}, time.Time{}, err - } - stat := fi.Sys().(*syscall.Stat_t) - return FileID{ - device: uint64(stat.Dev), // (int32 on darwin, uint64 on linux) - inode: stat.Ino, - }, fi.ModTime(), nil -} - type Hash [sha256.Size]byte // HashOf returns the hash of some data. diff --git a/lsp/cache/file_posix.go b/lsp/cache/file_posix.go new file mode 100644 index 0000000..a88971c --- /dev/null +++ b/lsp/cache/file_posix.go @@ -0,0 +1,22 @@ +//go:build !windows && !plan9 +// +build !windows,!plan9 + +package cache + +import ( + "os" + "syscall" + "time" +) + +func getFileID(filename string) (FileID, time.Time, error) { + fi, err := os.Stat(filename) + if err != nil { + return FileID{}, time.Time{}, err + } + stat := fi.Sys().(*syscall.Stat_t) + return FileID{ + device: uint64(stat.Dev), // (int32 on darwin, uint64 on linux) + inode: stat.Ino, + }, fi.ModTime(), nil +} diff --git a/lsp/cache/file_windows.go b/lsp/cache/file_windows.go new file mode 100644 index 0000000..5e5b965 --- /dev/null +++ b/lsp/cache/file_windows.go @@ -0,0 +1,33 @@ +//go:build windows +// +build windows + +package cache + +import ( + "syscall" + "time" +) + +// Note: it may be convenient to have this helper return fs.FileInfo, but +// implementing this is actually quite involved on Windows. Since we only +// currently use mtime, keep it simple. +func getFileID(filename string) (FileID, time.Time, error) { + filename16, err := syscall.UTF16PtrFromString(filename) + if err != nil { + return FileID{}, time.Time{}, err + } + h, err := syscall.CreateFile(filename16, 0, 0, nil, syscall.OPEN_EXISTING, uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS), 0) + if err != nil { + return FileID{}, time.Time{}, err + } + defer syscall.CloseHandle(h) + var i syscall.ByHandleFileInformation + if err := syscall.GetFileInformationByHandle(h, &i); err != nil { + return FileID{}, time.Time{}, err + } + mtime := time.Unix(0, i.LastWriteTime.Nanoseconds()) + return FileID{ + device: uint64(i.VolumeSerialNumber), + inode: uint64(i.FileIndexHigh)<<32 | uint64(i.FileIndexLow), + }, mtime, nil +}