| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package signdeb |
| 18 | |
| 19 | import ( |
| 20 | "archive/tar" |
| 21 | "bufio" |
| 22 | "bytes" |
| 23 | "compress/bzip2" |
| 24 | "compress/gzip" |
| 25 | "errors" |
| 26 | "io" |
| 27 | "io/ioutil" |
| 28 | "path" |
| 29 | "strings" |
| 30 | |
| 31 | "github.com/xi2/xz" |
| 32 | ) |
| 33 | |
| 34 | type PackageInfo struct { |
| 35 | Package, Version, Arch string |
| 36 | } |
| 37 | |
| 38 | // Parse basic package info from a control.tar.gz stream |
| 39 | func parseControl(r io.Reader, ext string) (*PackageInfo, error) { |
| 40 | var err error |
| 41 | switch ext { |
| 42 | case ".gz": |
| 43 | r, err = gzip.NewReader(r) |
| 44 | case ".bz2": |
| 45 | r = bzip2.NewReader(r) |
| 46 | case ".xz": |
| 47 | r, err = xz.NewReader(r, 0) |
| 48 | case "": |
| 49 | default: |
| 50 | err = errors.New("unrecognized compression on control.tar") |
| 51 | } |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | tr := tar.NewReader(r) |
| 56 | found := false |
| 57 | for { |
| 58 | hdr, err := tr.Next() |
| 59 | if err == io.EOF { |
| 60 | break |
| 61 | } else if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | if path.Clean(hdr.Name) == "control" { |
| 65 | found = true |
| 66 | break |
| 67 | } |
| 68 | } |
| 69 | if !found { |
| 70 | return nil, errors.New("control.tar has no control file") |
| 71 | } |
| 72 | blob, err := ioutil.ReadAll(tr) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | info := new(PackageInfo) |
| 77 | scanner := bufio.NewScanner(bytes.NewReader(blob)) |
| 78 | for scanner.Scan() { |
| 79 | line := scanner.Text() |
| 80 | i := strings.IndexAny(line, " \t\r\n") |
| 81 | j := strings.Index(line, ":") |
| 82 | if j < 0 || i < j { |
| 83 | continue |
| 84 | } |
| 85 | key := line[:j] |
| 86 | value := strings.Trim(line[j+1:], " \t\r\n") |
| 87 | switch strings.ToLower(key) { |
| 88 | case "package": |
| 89 | info.Package = value |
| 90 | case "version": |
| 91 | info.Version = value |
| 92 | case "architecture": |
| 93 | info.Arch = value |
| 94 | } |
| 95 | } |
| 96 | if scanner.Err() != nil { |
| 97 | return nil, scanner.Err() |
| 98 | } |
| 99 | if info.Package == "" || info.Version == "" { |
| 100 | return nil, errors.New("control file is missing package and/or version fields") |
| 101 | } |
| 102 | return info, nil |
| 103 | } |