Skip to content

Commit 391aaed

Browse files
committed
chore: sync with go1.19.13
1 parent 0c947a4 commit 391aaed

File tree

8 files changed

+158
-0
lines changed

8 files changed

+158
-0
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ issues:
223223
- test/testdata_etc # test files
224224
- internal/cache # extracted from Go code
225225
- internal/robustio # extracted from Go code
226+
- internal/mmap # extracted from Go code
226227

227228
run:
228229
timeout: 5m

internal/cache/cache.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323
"time"
2424

25+
"github.com/golangci/golangci-lint/internal/mmap"
2526
"github.com/rogpeppe/go-internal/lockedfile"
2627
)
2728

@@ -258,6 +259,28 @@ func (c *Cache) GetBytes(id ActionID) ([]byte, Entry, error) {
258259
return data, entry, nil
259260
}
260261

262+
// GetMmap looks up the action ID in the cache and returns
263+
// the corresponding output bytes.
264+
// GetMmap should only be used for data that can be expected to fit in memory.
265+
func (c *Cache) GetMmap(id ActionID) ([]byte, Entry, error) {
266+
entry, err := c.Get(id)
267+
if err != nil {
268+
return nil, entry, err
269+
}
270+
outputFile, err := c.OutputFile(entry.OutputID)
271+
if err != nil {
272+
return nil, entry, err
273+
}
274+
md, err := mmap.Mmap(outputFile)
275+
if err != nil {
276+
return nil, Entry{}, err
277+
}
278+
if int64(len(md.Data)) != entry.Size {
279+
return nil, Entry{}, &entryNotFoundError{Err: errors.New("file incomplete")}
280+
}
281+
return md.Data, entry, nil
282+
}
283+
261284
// OutputFile returns the name of the cache file storing output with the given OutputID.
262285
func (c *Cache) OutputFile(out OutputID) (string, error) {
263286
file := c.fileName(out, "d")

internal/cache/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Extracted from `go/src/cmd/go/internal/cache/`.
44

5+
- sync with go1.19.13
56
- sync with go1.18.10
67
- sync with go1.17.13
78
- sync with go1.16.15

internal/mmap/mmap.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2011 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// This package is a lightly modified version of the mmap code
6+
// in github.com/google/codesearch/index.
7+
8+
// The mmap package provides an abstraction for memory mapping files
9+
// on different platforms.
10+
package mmap
11+
12+
import (
13+
"os"
14+
)
15+
16+
// Data is mmap'ed read-only data from a file.
17+
// The backing file is never closed, so Data
18+
// remains valid for the lifetime of the process.
19+
type Data struct {
20+
f *os.File
21+
Data []byte
22+
}
23+
24+
// Mmap maps the given file into memory.
25+
func Mmap(file string) (Data, error) {
26+
f, err := os.Open(file)
27+
if err != nil {
28+
return Data{}, err
29+
}
30+
return mmapFile(f)
31+
}

internal/mmap/mmap_other.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build plan9 || solaris
6+
7+
package mmap
8+
9+
import (
10+
"io"
11+
"os"
12+
)
13+
14+
// mmapFile on other systems doesn't mmap the file. It just reads everything.
15+
func mmapFile(f *os.File) (Data, error) {
16+
b, err := io.ReadAll(f)
17+
if err != nil {
18+
return Data{}, err
19+
}
20+
return Data{f, b}, nil
21+
}

internal/mmap/mmap_unix.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2011 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build unix && !solaris
6+
7+
package mmap
8+
9+
import (
10+
"fmt"
11+
"io/fs"
12+
"os"
13+
"syscall"
14+
)
15+
16+
func mmapFile(f *os.File) (Data, error) {
17+
st, err := f.Stat()
18+
if err != nil {
19+
return Data{}, err
20+
}
21+
size := st.Size()
22+
pagesize := int64(os.Getpagesize())
23+
if int64(int(size+(pagesize-1))) != size+(pagesize-1) {
24+
return Data{}, fmt.Errorf("%s: too large for mmap", f.Name())
25+
}
26+
n := int(size)
27+
if n == 0 {
28+
return Data{f, nil}, nil
29+
}
30+
mmapLength := int(((size + pagesize - 1) / pagesize) * pagesize) // round up to page size
31+
data, err := syscall.Mmap(int(f.Fd()), 0, mmapLength, syscall.PROT_READ, syscall.MAP_SHARED)
32+
if err != nil {
33+
return Data{}, &fs.PathError{Op: "mmap", Path: f.Name(), Err: err}
34+
}
35+
return Data{f, data[:n]}, nil
36+
}

internal/mmap/mmap_windows.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2011 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package mmap
6+
7+
import (
8+
"fmt"
9+
"internal/syscall/windows"
10+
"os"
11+
"syscall"
12+
"unsafe"
13+
)
14+
15+
func mmapFile(f *os.File) (Data, error) {
16+
st, err := f.Stat()
17+
if err != nil {
18+
return Data{}, err
19+
}
20+
size := st.Size()
21+
if size == 0 {
22+
return Data{f, nil}, nil
23+
}
24+
h, err := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, 0, 0, nil)
25+
if err != nil {
26+
return Data{}, fmt.Errorf("CreateFileMapping %s: %w", f.Name(), err)
27+
}
28+
29+
addr, err := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, 0)
30+
if err != nil {
31+
return Data{}, fmt.Errorf("MapViewOfFile %s: %w", f.Name(), err)
32+
}
33+
var info windows.MemoryBasicInformation
34+
err = windows.VirtualQuery(addr, &info, unsafe.Sizeof(info))
35+
if err != nil {
36+
return Data{}, fmt.Errorf("VirtualQuery %s: %w", f.Name(), err)
37+
}
38+
data := unsafe.Slice((*byte)(unsafe.Pointer(addr)), int(info.RegionSize))
39+
return Data{f, data}, nil
40+
}

internal/mmap/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# mmap
2+
3+
Extracted from `go/src/cmd/go/internal/mmap/` (related to `cache`).
4+
5+
- sync with go1.19.13

0 commit comments

Comments
 (0)