1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/*
* Copyright (c) SAS Institute, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpmutils
import (
"errors"
"fmt"
"io"
"github.com/sassoftware/go-rpmutils/cpio"
)
// TODO version 2:
// - Make PayloadReader and FileInfo regular structs
// - Promote IsLink to a method of FileInfo
// - Add Close() that must be called to clean up decompressors
// PayloadReader is used to sequentially access the file contents of a RPM payload
type PayloadReader interface {
Next() (FileInfo, error)
Read([]byte) (int, error)
IsLink() bool
}
type payloadReader struct {
stream io.Reader
cr *cpio.Reader
files []*fileInfo
fileMap map[string]int
isLink []bool
index int
}
func newPayloadReader(r io.Reader, files []FileInfo) *payloadReader {
pr := &payloadReader{
stream: r,
files: make([]*fileInfo, len(files)),
fileMap: make(map[string]int, len(files)),
isLink: make([]bool, len(files)),
}
fileSizes := make([]int64, len(files))
var lastInode uint64
for i, info := range files {
fileSt := info.(*fileInfo)
pr.files[i] = fileSt
pr.fileMap[fileSt.name] = i
switch fileSt.fileType() {
case cpio.S_ISREG:
fileSizes[i] = fileSt.Size()
// all but the last file in a link group will have no contents. flag
// them so we don't try to read the nonexistent payload.
ino := fileSt.inode64()
if ino == lastInode && ino != 0 {
pr.isLink[i-1] = true
fileSizes[i-1] = 0
}
lastInode = ino
case cpio.S_ISLNK:
fileSizes[i] = int64(len(fileSt.linkName))
}
}
pr.cr = cpio.NewReaderWithSizes(r, fileSizes)
return pr
}
// Next returns the info of the next file in the payload. After calling Next(),
// Read() can be used to read the contents of the file. Returns io.EOF when all
// files have been consumed.
func (pr *payloadReader) Next() (FileInfo, error) {
hdr, err := pr.cr.Next()
if err != nil {
// close decompressor on EOF, zstd in particular leaks goroutines otherwise
if c, ok := pr.stream.(io.Closer); ok {
c.Close()
}
return nil, err
}
var index int
if hdr.IsStripped() {
index = hdr.Index()
} else {
var ok bool
name := hdr.Filename()
if len(name) > 1 && name[0] == '.' && name[1] == '/' {
name = name[1:]
}
index, ok = pr.fileMap[name]
if !ok {
return nil, fmt.Errorf("invalid file \"%s\" in payload", name)
}
}
if index >= len(pr.files) {
return nil, errors.New("invalid file index")
}
pr.index = index
return pr.files[index], nil
}
// Read bytes from the file returned by the preceding call to Next()
func (pr *payloadReader) Read(d []byte) (int, error) {
return pr.cr.Read(d)
}
// IsLink returns true if the current file is a hard-link with no contents. A
// subsequent file with the same FileInfo.Inode and for which IsLink() returns
// false will have the contents.
func (pr *payloadReader) IsLink() bool {
return pr.isLink[pr.index]
}
|