Skip to content

Commit e1b7a98

Browse files
committed
Use min/max to simplify the code
Should not change functionality. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent c800027 commit e1b7a98

File tree

5 files changed

+6
-24
lines changed

5 files changed

+6
-24
lines changed

pkg/chrootarchive/archive_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,7 @@ type slowEmptyTarReader struct {
496496
// Read is a slow reader of an empty tar (like the output of "tar c --files-from /dev/null")
497497
func (s *slowEmptyTarReader) Read(p []byte) (int, error) {
498498
time.Sleep(100 * time.Millisecond)
499-
count := s.chunkSize
500-
if len(p) < s.chunkSize {
501-
count = len(p)
502-
}
499+
count := min(len(p), s.chunkSize)
503500
for i := 0; i < count; i++ {
504501
p[i] = 0
505502
}

pkg/chunked/compressor/compressor.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ func (rc *rollingChecksumReader) Read(b []byte) (bool, int, error) {
142142
rc.IsLastChunkZeros = false
143143

144144
if rc.pendingHole > 0 {
145-
toCopy := int64(len(b))
146-
if rc.pendingHole < toCopy {
147-
toCopy = rc.pendingHole
148-
}
145+
toCopy := min(rc.pendingHole, int64(len(b)))
149146
rc.pendingHole -= toCopy
150147
for i := int64(0); i < toCopy; i++ {
151148
b[i] = 0

pkg/chunked/storage_linux.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -698,18 +698,12 @@ func (c *chunkedDiffer) prepareCompressedStreamToFile(partCompression compressed
698698

699699
// hashHole writes SIZE zeros to the specified hasher
700700
func hashHole(h hash.Hash, size int64, copyBuffer []byte) error {
701-
count := int64(len(copyBuffer))
702-
if size < count {
703-
count = size
704-
}
701+
count := min(size, int64(len(copyBuffer)))
705702
for i := int64(0); i < count; i++ {
706703
copyBuffer[i] = 0
707704
}
708705
for size > 0 {
709-
count = int64(len(copyBuffer))
710-
if size < count {
711-
count = size
712-
}
706+
count = min(size, int64(len(copyBuffer)))
713707
if _, err := h.Write(copyBuffer[:count]); err != nil {
714708
return err
715709
}

pkg/ioutils/bytespipe.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ loop0:
9393
}
9494

9595
// add new byte slice to the buffers slice and continue writing
96-
nextCap := b.Cap() * 2
97-
if nextCap > maxCap {
98-
nextCap = maxCap
99-
}
96+
nextCap := min(b.Cap()*2, maxCap)
10097
bp.buf = append(bp.buf, getBuffer(nextCap))
10198
}
10299
bp.wait.Broadcast()

userns.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,7 @@ func (s *store) getAutoUserNS(options *types.AutoUserNsOptions, image *Image, rl
276276
// bigger than s.autoNsMaxSize.
277277
// This is a best effort heuristic.
278278
if requestedSize == 0 {
279-
size = initialSize
280-
if s.autoNsMinSize > size {
281-
size = s.autoNsMinSize
282-
}
279+
size = max(s.autoNsMinSize, initialSize)
283280
if image != nil {
284281
sizeFromImage, err := s.getMaxSizeFromImage(image, rlstore, lstores, options.PasswdFile, options.GroupFile)
285282
if err != nil {

0 commit comments

Comments
 (0)