2019-05-17 10:15:22 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2019-05-31 17:41:39 -06:00
|
|
|
"context"
|
2019-05-17 10:15:22 -06:00
|
|
|
"io/ioutil"
|
2019-06-21 15:00:02 -06:00
|
|
|
"os"
|
2019-05-17 10:15:22 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
// nativeFileSystem implements FileSystem reading from the normal os file system.
|
|
|
|
type nativeFileSystem struct{}
|
|
|
|
|
2019-05-31 17:41:39 -06:00
|
|
|
// nativeFileHandle implements FileHandle for nativeFileSystem
|
|
|
|
type nativeFileHandle struct {
|
|
|
|
fs *nativeFileSystem
|
|
|
|
identity source.FileIdentity
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *nativeFileSystem) GetFile(uri span.URI) source.FileHandle {
|
2019-06-26 13:24:04 -06:00
|
|
|
version := "DOES NOT EXIST"
|
|
|
|
if fi, err := os.Stat(uri.Filename()); err == nil {
|
|
|
|
version = fi.ModTime().String()
|
2019-06-21 15:00:02 -06:00
|
|
|
}
|
2019-05-31 17:41:39 -06:00
|
|
|
return &nativeFileHandle{
|
|
|
|
fs: fs,
|
|
|
|
identity: source.FileIdentity{
|
2019-06-21 15:00:02 -06:00
|
|
|
URI: uri,
|
|
|
|
Version: version,
|
2019-05-31 17:41:39 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *nativeFileHandle) FileSystem() source.FileSystem {
|
|
|
|
return h.fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *nativeFileHandle) Identity() source.FileIdentity {
|
|
|
|
return h.identity
|
|
|
|
}
|
|
|
|
|
2019-06-21 15:00:02 -06:00
|
|
|
func (h *nativeFileHandle) Kind() source.FileKind {
|
|
|
|
// TODO: How should we determine the file kind?
|
|
|
|
return source.Go
|
|
|
|
}
|
|
|
|
|
2019-06-03 23:04:18 -06:00
|
|
|
func (h *nativeFileHandle) Read(ctx context.Context) ([]byte, string, error) {
|
2019-05-30 07:03:31 -06:00
|
|
|
//TODO: this should fail if the version is not the same as the handle
|
2019-06-06 11:51:00 -06:00
|
|
|
data, err := ioutil.ReadFile(h.identity.URI.Filename())
|
2019-06-03 23:04:18 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|
2019-06-03 23:04:18 -06:00
|
|
|
return data, hashContents(data), nil
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|