github.com/uber/kraken@v0.1.4/lib/store/base/file_readwriter.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 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 package base 15 16 import ( 17 "io" 18 "os" 19 ) 20 21 // FileReader provides read operation on a file. 22 type FileReader interface { 23 io.Reader 24 io.ReaderAt 25 io.Seeker 26 io.Closer 27 Size() int64 28 } 29 30 // FileReadWriter provides read/write operation on a file. 31 type FileReadWriter interface { 32 FileReader 33 io.Writer 34 io.WriterAt 35 36 Cancel() error // required by docker registry. 37 Commit() error // required by docker registry. 38 } 39 40 // LocalFileReadWriter implements FileReadWriter interface, provides read/write 41 // operation on a local file. 42 type localFileReadWriter struct { 43 entry *localFileEntry 44 descriptor *os.File 45 } 46 47 func (readWriter *localFileReadWriter) close() error { 48 return readWriter.descriptor.Close() 49 } 50 51 // Close closes underlying OS.File object. 52 func (readWriter localFileReadWriter) Close() error { 53 return readWriter.close() 54 } 55 56 // Write writes up to len(b) bytes to the File. 57 func (readWriter localFileReadWriter) Write(p []byte) (int, error) { 58 return readWriter.descriptor.Write(p) 59 } 60 61 // WriteAt writes len(p) bytes from p to the underlying data stream at offset. 62 func (readWriter localFileReadWriter) WriteAt(p []byte, offset int64) (int, error) { 63 return readWriter.descriptor.WriteAt(p, offset) 64 } 65 66 // Read reads up to len(b) bytes from the File. 67 func (readWriter localFileReadWriter) Read(p []byte) (int, error) { 68 return readWriter.descriptor.Read(p) 69 } 70 71 // ReadAt reads len(b) bytes from the File starting at byte offset off. 72 func (readWriter localFileReadWriter) ReadAt(p []byte, offset int64) (int, error) { 73 return readWriter.descriptor.ReadAt(p, offset) 74 } 75 76 // Seek sets the offset for the next Read or Write on file to offset, 77 // interpreted according to whence: 78 // 0 means relative to the origin of the file; 79 // 1 means relative to the current offset; 80 // 2 means relative to the end. 81 func (readWriter localFileReadWriter) Seek(offset int64, whence int) (int64, error) { 82 return readWriter.descriptor.Seek(offset, whence) 83 } 84 85 // Size returns the size of the file. 86 func (readWriter localFileReadWriter) Size() int64 { 87 // Use file entry instead of descriptor, because descriptor could have been closed. 88 fileInfo, err := readWriter.entry.GetStat() 89 90 if err != nil { 91 return 0 92 } 93 return fileInfo.Size() 94 } 95 96 // Cancel is supposed to remove any written content. 97 // In this implementation file is not actually removed, and it's fine since there won't be name 98 // collision between upload files. 99 func (readWriter localFileReadWriter) Cancel() error { 100 return readWriter.close() 101 } 102 103 // Commit is supposed to flush all content for buffered writer. 104 // In this implementation all writes write to the file directly through syscall. 105 func (readWriter localFileReadWriter) Commit() error { 106 return readWriter.close() 107 }