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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*
* 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 (
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/sassoftware/go-rpmutils/cpio"
)
// Rpm is an open RPM header and payload
type Rpm struct {
Header *RpmHeader
f io.Reader
}
// RpmHeader holds the signature header and general header of a RPM
//
// Tags are drawn from both header areas, with IDs between _GENERAL_TAG_BASE and
// _SIGHEADER_TAG_BASE coming from the general header and all others coming from
// the signature header.
type RpmHeader struct {
lead []byte
sigHeader *rpmHeader
genHeader *rpmHeader
isSource bool
}
// ReadRpm reads the header from a RPM file and prepares to read payload contents
func ReadRpm(f io.Reader) (*Rpm, error) {
hdr, err := ReadHeader(f)
if err != nil {
return nil, err
}
return &Rpm{
Header: hdr,
f: f,
}, nil
}
// ExpandPayload extracts the payload of a RPM to the specified directory
func (rpm *Rpm) ExpandPayload(dest string) error {
pld, err := uncompressRpmPayloadReader(rpm.f, rpm.Header)
if err != nil {
return err
}
if c, ok := pld.(io.Closer); ok {
defer c.Close()
}
return cpio.Extract(pld, dest)
}
// PayloadReader accesses the payload cpio archive within the RPM.
//
// DEPRECATED: Use PayloadReaderExtended instead in order to handle files larger than 4GiB.
func (rpm *Rpm) PayloadReader() (*cpio.Reader, error) {
pld, err := uncompressRpmPayloadReader(rpm.f, rpm.Header)
if err != nil {
return nil, err
}
return cpio.NewReader(pld), nil
}
// PayloadReaderExtended accesses payload file contents sequentially
func (rpm *Rpm) PayloadReaderExtended() (PayloadReader, error) {
pld, err := uncompressRpmPayloadReader(rpm.f, rpm.Header)
if err != nil {
return nil, err
}
files, err := rpm.Header.GetFiles()
if err != nil {
return nil, err
}
return newPayloadReader(pld, files), nil
}
// ReadHeader reads the signature and general headers from a RPM.
//
// The stream is positioned for reading the compressed payload following the headers.
func ReadHeader(f io.Reader) (*RpmHeader, error) {
lead, sigHeader, err := readSignatureHeader(f)
if err != nil {
return nil, err
}
genHeader, err := readHeader(f, getSha1(sigHeader), sigHeader.isSource, false)
if err != nil {
return nil, err
}
return &RpmHeader{
lead: lead,
sigHeader: sigHeader,
genHeader: genHeader,
isSource: sigHeader.isSource,
}, nil
}
func readSignatureHeader(f io.Reader) ([]byte, *rpmHeader, error) {
// Read signature header
lead, err := readExact(f, 96)
if err != nil {
return nil, nil, fmt.Errorf("error reading RPM lead: %s", err.Error())
}
// Check file magic
magic := binary.BigEndian.Uint32(lead[0:4])
if magic&0xffffffff != 0xedabeedb {
return nil, nil, fmt.Errorf("file is not an RPM")
}
// Check source flag
isSource := binary.BigEndian.Uint16(lead[6:8]) == 1
// Return signature header
hdr, err := readHeader(f, "", isSource, true)
return lead, hdr, err
}
// HeaderRange indicates the byte offsets that the RPM header spans
type HeaderRange struct {
// Start is the byte offset of the signature header
Start int
// End is the byte offset of the end of the general header and start of the payload
End int
}
// GetRange returns the byte offsets that the RPM header spans within the original RPM file
func (hdr *RpmHeader) GetRange() HeaderRange {
start := 96 + hdr.sigHeader.origSize
end := start + hdr.genHeader.origSize
return HeaderRange{
Start: start,
End: end,
}
}
// HasTag returns true if the given tag exists in the header
func (hdr *RpmHeader) HasTag(tag int) bool {
h, t := hdr.getHeader(tag)
return h.HasTag(t)
}
// Get the value of a tag. Returns whichever type most closely represents how
// the tag was stored, or NoSuchTagError if the tag was not found. If tag is
// OLDFILENAMES, special handling is provided to splice together DIRNAMES and
// BASENAMES if it is not present.
func (hdr *RpmHeader) Get(tag int) (interface{}, error) {
h, t := hdr.getHeader(tag)
return h.Get(t)
}
// GetString returns the value of a tag holding a single string
func (hdr *RpmHeader) GetString(tag int) (string, error) {
vals, err := hdr.GetStrings(tag)
if err != nil {
return "", err
}
if len(vals) != 1 {
return "", fmt.Errorf("incorrect number of values")
}
return vals[0], nil
}
// GetStrings fetches the given tag holding a string or array of strings. If tag
// is OLDFILENAMES, special handling is provided to splice together DIRNAMES and
// BASENAMES if it is not present.
func (hdr *RpmHeader) GetStrings(tag int) ([]string, error) {
h, t := hdr.getHeader(tag)
return h.GetStrings(t)
}
// GetInt gets an integer using the default 'int' type.
//
// DEPRECATED: large int32s and int64s can overflow. Use GetUint32s or GetUint64s instead.
func (hdr *RpmHeader) GetInt(tag int) (int, error) {
vals, err := hdr.GetInts(tag)
if err != nil {
return -1, err
}
if len(vals) != 1 {
return -1, fmt.Errorf("incorrect number of values")
}
return vals[0], nil
}
// GetInts gets an integer array using the default 'int' type.
//
// DEPRECATED: large int32s and int64s can overflow. Use GetUint32s or GetUint64s instead.
func (hdr *RpmHeader) GetInts(tag int) ([]int, error) {
h, t := hdr.getHeader(tag)
return h.GetInts(t)
}
// GetUint32s gets an int array as a uint32 slice. This can accomodate any int
// type other than INT64. Returns an error in case of overflow.
func (hdr *RpmHeader) GetUint32s(tag int) ([]uint32, error) {
h, t := hdr.getHeader(tag)
return h.GetUint32s(t)
}
// GetUint64s gets an int array as a uint64 slice. This can accomodate all int
// types
func (hdr *RpmHeader) GetUint64s(tag int) ([]uint64, error) {
h, t := hdr.getHeader(tag)
return h.GetUint64s(t)
}
// GetUint64Fallback gets longTag if it exists, otherwise intTag, and returns
// the value as an array of uint64s. This can accomodate all int types and is
// normally used when a int32 tag was later replaced with a int64 tag.
func (hdr *RpmHeader) GetUint64Fallback(intTag, longTag int) (uint64, error) {
h, t := hdr.getHeader(longTag)
vals, err := h.GetUint64s(t)
if err == nil && len(vals) == 1 {
return vals[0], nil
}
h, t = hdr.getHeader(intTag)
vals, err = h.GetUint64s(t)
if err != nil {
return 0, err
} else if len(vals) != 1 {
return 0, errors.New("incorrect number of values")
}
return vals[0], nil
}
// GetBytes gets a tag as a byte array.
func (hdr *RpmHeader) GetBytes(tag int) ([]byte, error) {
h, t := hdr.getHeader(tag)
return h.GetBytes(t)
}
// getHeader decides whether the conventional tag ID is within the signature
// header range and returns the appropriate sub-header struct and raw tag
// identifier
func (hdr *RpmHeader) getHeader(tag int) (*rpmHeader, int) {
if tag > _SIGHEADER_TAG_BASE {
return hdr.sigHeader, tag - _SIGHEADER_TAG_BASE
}
if tag < _GENERAL_TAG_BASE {
return hdr.sigHeader, tag
}
return hdr.genHeader, tag
}
// GetNEVRA gets the name, epoch, version, release and arch of the RPM.
func (hdr *RpmHeader) GetNEVRA() (*NEVRA, error) {
return hdr.genHeader.GetNEVRA()
}
// GetFiles returns an array of FileInfo objects holding file-related attributes
// held in parallel arrays of tags
func (hdr *RpmHeader) GetFiles() ([]FileInfo, error) {
return hdr.genHeader.GetFiles()
}
// InstalledSize returns the approximate disk space needed to install the package
func (hdr *RpmHeader) InstalledSize() (int64, error) {
u, err := hdr.GetUint64Fallback(SIZE, LONGSIZE)
if err != nil {
return -1, err
}
return int64(u), nil
}
// PayloadSize returns the size of the uncompressed payload in bytes
func (hdr *RpmHeader) PayloadSize() (int64, error) {
u, err := hdr.sigHeader.GetUint64Fallback(SIG_PAYLOADSIZE-_SIGHEADER_TAG_BASE, SIG_LONGARCHIVESIZE)
if err != nil {
return -1, err
} else if len(u) != 1 {
return -1, errors.New("incorrect number of values")
}
return int64(u[0]), err
}
|