1/* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15*/ 16 17package local 18 19import ( 20 "os" 21 22 "github.com/pkg/errors" 23 24 "github.com/containerd/containerd/content" 25 "github.com/containerd/containerd/errdefs" 26) 27 28// readerat implements io.ReaderAt in a completely stateless manner by opening 29// the referenced file for each call to ReadAt. 30type sizeReaderAt struct { 31 size int64 32 fp *os.File 33} 34 35// OpenReader creates ReaderAt from a file 36func OpenReader(p string) (content.ReaderAt, error) { 37 fi, err := os.Stat(p) 38 if err != nil { 39 if !os.IsNotExist(err) { 40 return nil, err 41 } 42 43 return nil, errors.Wrap(errdefs.ErrNotFound, "blob not found") 44 } 45 46 fp, err := os.Open(p) 47 if err != nil { 48 if !os.IsNotExist(err) { 49 return nil, err 50 } 51 52 return nil, errors.Wrap(errdefs.ErrNotFound, "blob not found") 53 } 54 55 return sizeReaderAt{size: fi.Size(), fp: fp}, nil 56} 57 58func (ra sizeReaderAt) ReadAt(p []byte, offset int64) (int, error) { 59 return ra.fp.ReadAt(p, offset) 60} 61 62func (ra sizeReaderAt) Size() int64 { 63 return ra.size 64} 65 66func (ra sizeReaderAt) Close() error { 67 return ra.fp.Close() 68} 69