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"
|
2019-07-09 18:16:21 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-05-17 10:15:22 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-05-17 10:15:22 -06:00
|
|
|
)
|
|
|
|
|
2019-08-15 06:54:27 -06:00
|
|
|
// ioLimit limits the number of parallel file reads per process.
|
|
|
|
var ioLimit = make(chan struct{}, 128)
|
|
|
|
|
2019-05-17 10:15:22 -06:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-01-06 16:08:39 -07:00
|
|
|
func (fs *nativeFileSystem) GetFile(uri span.URI) source.FileHandle {
|
2019-11-12 15:58:37 -07:00
|
|
|
identifier := "DOES NOT EXIST"
|
2019-06-26 13:24:04 -06:00
|
|
|
if fi, err := os.Stat(uri.Filename()); err == nil {
|
2019-11-12 15:58:37 -07:00
|
|
|
identifier = fi.ModTime().String()
|
2019-06-21 15:00:02 -06:00
|
|
|
}
|
2020-01-06 16:08:39 -07:00
|
|
|
kind := source.DetectLanguage("", uri.Filename())
|
2019-05-31 17:41:39 -06:00
|
|
|
return &nativeFileHandle{
|
|
|
|
fs: fs,
|
|
|
|
identity: source.FileIdentity{
|
2019-11-12 15:58:37 -07:00
|
|
|
URI: uri,
|
|
|
|
Identifier: identifier,
|
|
|
|
Kind: kind,
|
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-03 23:04:18 -06:00
|
|
|
func (h *nativeFileHandle) Read(ctx context.Context) ([]byte, string, error) {
|
2019-07-09 18:16:21 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "cache.nativeFileHandle.Read", telemetry.File.Of(h.identity.URI.Filename()))
|
2019-10-02 12:45:55 -06:00
|
|
|
_ = ctx
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
2019-09-23 16:18:01 -06:00
|
|
|
|
2019-08-15 06:54:27 -06:00
|
|
|
ioLimit <- struct{}{}
|
|
|
|
defer func() { <-ioLimit }()
|
2019-09-18 23:21:54 -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
|
|
|
}
|