MCPcopy Create free account
hub / github.com/containers/image / DetectCompressionFormat

Function DetectCompressionFormat

pkg/compression/compression.go:121–147  ·  view source on GitHub ↗

DetectCompressionFormat returns an Algorithm and DecompressorFunc if the input is recognized as a compressed format, an invalid value and nil otherwise. Because it consumes the start of input, other consumers must use the returned io.Reader instead to also read from the beginning.

(input io.Reader)

Source from the content-addressed store, hash-verified

119// value and nil otherwise.
120// Because it consumes the start of input, other consumers must use the returned io.Reader instead to also read from the beginning.
121func DetectCompressionFormat(input io.Reader) (Algorithm, DecompressorFunc, io.Reader, error) {
122 buffer := [8]byte{}
123
124 n, err := io.ReadAtLeast(input, buffer[:], len(buffer))
125 if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
126 // This is a “real” error. We could just ignore it this time, process the data we have, and hope that the source will report the same error again.
127 // Instead, fail immediately with the original error cause instead of a possibly secondary/misleading error returned later.
128 return Algorithm{}, nil, nil, err
129 }
130
131 var retAlgo Algorithm
132 var decompressor DecompressorFunc
133 for _, algo := range compressionAlgorithms {
134 prefix := internal.AlgorithmPrefix(algo)
135 if len(prefix) > 0 && bytes.HasPrefix(buffer[:n], prefix) {
136 logrus.Debugf("Detected compression format %s", algo.Name())
137 retAlgo = algo
138 decompressor = internal.AlgorithmDecompressor(algo)
139 break
140 }
141 }
142 if decompressor == nil {
143 logrus.Debugf("No compression detected")
144 }
145
146 return retAlgo, decompressor, io.MultiReader(bytes.NewReader(buffer[:n]), input), nil
147}
148
149// DetectCompression returns a DecompressorFunc if the input is recognized as a compressed format, nil otherwise.
150// Because it consumes the start of input, other consumers must use the returned io.Reader instead to also read from the beginning.

Callers 3

NewImageSourceMethod · 0.92
DetectCompressionFunction · 0.85

Calls 3

AlgorithmPrefixFunction · 0.92
AlgorithmDecompressorFunction · 0.92
NameMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…