1 /* 2 Copyright The ORAS Authors. 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 16 package content 17 18 import ( 19 "errors" 20 21 "github.com/opencontainers/go-digest" 22 ) 23 24 type WriterOpts struct { 25 InputHash *digest.Digest 26 OutputHash *digest.Digest 27 Blocksize int 28 MultiWriterIngester bool 29 IgnoreNoName bool 30 } 31 32 type WriterOpt func(*WriterOpts) error 33 34 func DefaultWriterOpts() WriterOpts { 35 return WriterOpts{ 36 InputHash: nil, 37 OutputHash: nil, 38 Blocksize: DefaultBlocksize, 39 IgnoreNoName: true, 40 } 41 } 42 43 // WithInputHash provide the expected input hash to a writer. Writers 44 // may suppress their own calculation of a hash on the stream, taking this 45 // hash instead. If the Writer processes the data before passing it on to another 46 // Writer layer, this is the hash of the *input* stream. 47 // 48 // To have a blank hash, use WithInputHash(BlankHash). 49 func WithInputHash(hash digest.Digest) WriterOpt { 50 return func(w *WriterOpts) error { 51 w.InputHash = &hash 52 return nil 53 } 54 } 55 56 // WithOutputHash provide the expected output hash to a writer. Writers 57 // may suppress their own calculation of a hash on the stream, taking this 58 // hash instead. If the Writer processes the data before passing it on to another 59 // Writer layer, this is the hash of the *output* stream. 60 // 61 // To have a blank hash, use WithInputHash(BlankHash). 62 func WithOutputHash(hash digest.Digest) WriterOpt { 63 return func(w *WriterOpts) error { 64 w.OutputHash = &hash 65 return nil 66 } 67 } 68 69 // WithBlocksize set the blocksize used by the processor of data. 70 // The default is DefaultBlocksize, which is the same as that used by io.Copy. 71 // Includes a safety check to ensure the caller doesn't actively set it to <= 0. 72 func WithBlocksize(blocksize int) WriterOpt { 73 return func(w *WriterOpts) error { 74 if blocksize <= 0 { 75 return errors.New("blocksize must be greater than or equal to 0") 76 } 77 w.Blocksize = blocksize 78 return nil 79 } 80 } 81 82 // WithMultiWriterIngester the passed ingester also implements MultiWriter 83 // and should be used as such. If this is set to true, but the ingester does not 84 // implement MultiWriter, calling Writer should return an error. 85 func WithMultiWriterIngester() WriterOpt { 86 return func(w *WriterOpts) error { 87 w.MultiWriterIngester = true 88 return nil 89 } 90 } 91 92 // WithErrorOnNoName some ingesters, when creating a Writer, do not return an error if 93 // the descriptor does not have a valid name on the descriptor. Passing WithErrorOnNoName 94 // tells the writer to return an error instead of passing the data to a nil writer. 95 func WithErrorOnNoName() WriterOpt { 96 return func(w *WriterOpts) error { 97 w.IgnoreNoName = false 98 return nil 99 } 100 } 101 102 // WithIgnoreNoName some ingesters, when creating a Writer, return an error if 103 // the descriptor does not have a valid name on the descriptor. Passing WithIgnoreNoName 104 // tells the writer not to return an error, but rather to pass the data to a nil writer. 105 // 106 // Deprecated: Use WithErrorOnNoName 107 func WithIgnoreNoName() WriterOpt { 108 return func(w *WriterOpts) error { 109 w.IgnoreNoName = true 110 return nil 111 } 112 } 113