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
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-03-10 21:09:39 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-05-17 10:15:22 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
2020-01-23 10:34:02 -07:00
|
|
|
errors "golang.org/x/xerrors"
|
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-05-31 17:41:39 -06:00
|
|
|
return &nativeFileHandle{
|
|
|
|
fs: fs,
|
|
|
|
identity: source.FileIdentity{
|
2019-11-12 15:58:37 -07:00
|
|
|
URI: uri,
|
2020-01-23 10:34:02 -07:00
|
|
|
Identifier: identifier(uri.Filename()),
|
|
|
|
Kind: source.DetectLanguage("", uri.Filename()),
|
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) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "cache.nativeFileHandle.Read", tag.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 }()
|
2020-01-23 10:34:02 -07:00
|
|
|
|
|
|
|
if id := identifier(h.identity.URI.Filename()); id != h.identity.Identifier {
|
|
|
|
return nil, "", errors.Errorf("%s: file has been modified", h.identity.URI.Filename())
|
|
|
|
}
|
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
|
|
|
}
|
2020-01-23 10:34:02 -07:00
|
|
|
|
|
|
|
func identifier(filename string) string {
|
|
|
|
if fi, err := os.Stat(filename); err == nil {
|
|
|
|
return fi.ModTime().String()
|
|
|
|
}
|
|
|
|
return "DOES NOT EXIST"
|
|
|
|
}
|