fork of https://github.com/sourcegraph/zoekt
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// +build !linux,!darwin
17
18package zoekt
19
20import (
21 "fmt"
22 "os"
23)
24
25// NewIndexFile returns a new index file. The index file takes
26// ownership of the passed in file, and may close it.
27func NewIndexFile(f *os.File) (IndexFile, error) {
28 return &indexFileFromOS{f}, nil
29}
30
31type indexFileFromOS struct {
32 f *os.File
33}
34
35func (f *indexFileFromOS) Read(off, sz uint32) ([]byte, error) {
36 r := make([]byte, sz)
37 _, err := f.f.ReadAt(r, int64(off))
38 return r, err
39}
40
41func (f indexFileFromOS) Size() (uint32, error) {
42 fi, err := f.f.Stat()
43 if err != nil {
44 return 0, err
45 }
46
47 sz := fi.Size()
48
49 if sz >= maxUInt32 {
50 return 0, fmt.Errorf("overflow")
51 }
52
53 return uint32(sz), nil
54}
55
56func (f indexFileFromOS) Close() {
57 f.f.Close()
58}
59
60func (f indexFileFromOS) Name() string {
61 return f.f.Name()
62}