fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

1// Copyright 2016 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15//go:build linux || darwin 16 17package zoekt 18 19import ( 20 "fmt" 21 "log" 22 "os" 23 24 "golang.org/x/sys/unix" 25) 26 27type mmapedIndexFile struct { 28 name string 29 size uint32 30 data []byte 31} 32 33func (f *mmapedIndexFile) Read(off, sz uint32) ([]byte, error) { 34 if off > off+sz || off+sz > uint32(len(f.data)) { 35 return nil, fmt.Errorf("out of bounds: %d, len %d, name %s", off+sz, len(f.data), f.name) 36 } 37 return f.data[off : off+sz], nil 38} 39 40func (f *mmapedIndexFile) Name() string { 41 return f.name 42} 43 44func (f *mmapedIndexFile) Size() (uint32, error) { 45 return f.size, nil 46} 47 48func (f *mmapedIndexFile) Close() { 49 if err := unix.Munmap(f.data); err != nil { 50 log.Printf("WARN failed to Munmap %s: %v", f.name, err) 51 } 52} 53 54// NewIndexFile returns a new index file. The index file takes 55// ownership of the passed in file, and may close it. 56func NewIndexFile(f *os.File) (IndexFile, error) { 57 defer f.Close() 58 59 fi, err := f.Stat() 60 if err != nil { 61 return nil, err 62 } 63 64 sz := fi.Size() 65 if sz >= maxUInt32 { 66 return nil, fmt.Errorf("file %s too large: %d", f.Name(), sz) 67 } 68 r := &mmapedIndexFile{ 69 name: f.Name(), 70 size: uint32(sz), 71 } 72 73 rounded := (r.size + 4095) &^ 4095 74 r.data, err = unix.Mmap(int(f.Fd()), 0, int(rounded), unix.PROT_READ, unix.MAP_SHARED) 75 if err != nil { 76 return nil, err 77 } 78 79 return r, err 80}