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
|
// Copyright 2022 Northern.tech AS
//
// 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 handlers
import (
"archive/tar"
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
"github.com/mendersoftware/mender-artifact/artifact"
)
// DataFile represents the minimum set of attributes each update file
// must contain. Some of those might be empty though for specific update types.
type DataFile struct {
// name of the update file
Name string
// size of the update file
Size int64
// last modification time
Date time.Time
// checksum of the update file
Checksum []byte
}
type ComposeHeaderArgs struct {
TarWriter *tar.Writer
No int
Version int
Augmented bool
TypeInfoV3 *artifact.TypeInfoV3
MetaData map[string]interface{} // Generic JSON
Files []string
}
type ArtifactUpdateHeaders interface {
GetVersion() int
// Return type of this update, which could be augmented.
GetUpdateType() *string
// Return type of original (non-augmented) update, if any.
GetUpdateOriginalType() *string
// Returns merged data of non-augmented and augmented data, where the
// latter overrides the former. Returns error if they cannot be merged.
GetUpdateDepends() (artifact.TypeInfoDepends, error)
GetUpdateProvides() (artifact.TypeInfoProvides, error)
GetUpdateMetaData() (map[string]interface{}, error) // Generic JSON
GetUpdateClearsProvides() []string
// Returns non-augmented (original) data.
GetUpdateOriginalDepends() artifact.TypeInfoDepends
GetUpdateOriginalProvides() artifact.TypeInfoProvides
GetUpdateOriginalMetaData() map[string]interface{} // Generic JSON
GetUpdateOriginalClearsProvides() []string
// Returns augmented data.
GetUpdateAugmentDepends() artifact.TypeInfoDepends
GetUpdateAugmentProvides() artifact.TypeInfoProvides
GetUpdateAugmentMetaData() map[string]interface{} // Generic JSON
GetUpdateAugmentClearsProvides() []string
GetUpdateOriginalTypeInfoWriter() io.Writer
GetUpdateAugmentTypeInfoWriter() io.Writer
}
type ArtifactUpdate interface {
ArtifactUpdateHeaders
// Operates on non-augmented files.
GetUpdateFiles() [](*DataFile)
SetUpdateFiles(files [](*DataFile)) error
// Operates on augmented files.
GetUpdateAugmentFiles() [](*DataFile)
SetUpdateAugmentFiles(files [](*DataFile)) error
// Gets both augmented and non-augmented files.
GetUpdateAllFiles() [](*DataFile)
}
type Composer interface {
ArtifactUpdate
ComposeHeader(args *ComposeHeaderArgs) error
}
type UpdateStorer interface {
// Called as soon as all headers all collected.
Initialize(artifactHeaders,
artifactAugmentedHeaders artifact.HeaderInfoer,
payloadHeaders ArtifactUpdateHeaders) error
// Called before storing any file for this UpdateStorer
PrepareStoreUpdate() error
// Called once for each file to store
StoreUpdate(r io.Reader, info os.FileInfo) error
// Called after all files have been stored, even if there was an error
FinishStoreUpdate() error
}
type UpdateStorerProducer interface {
NewUpdateStorer(updateType *string, payloadNum int) (UpdateStorer, error)
}
type Installer interface {
ArtifactUpdate
UpdateStorerProducer
ReadHeader(r io.Reader, path string, version int, augmented bool) error
SetUpdateStorerProducer(producer UpdateStorerProducer)
NewInstance() Installer
NewAugmentedInstance(orig ArtifactUpdate) (Installer, error)
}
type installerBase struct {
updateStorerProducer UpdateStorerProducer
}
type devNullUpdateStorer struct {
}
func (i *installerBase) SetUpdateStorerProducer(producer UpdateStorerProducer) {
i.updateStorerProducer = producer
}
func (i *installerBase) NewUpdateStorer(updateType *string, payloadNum int) (UpdateStorer, error) {
if i.updateStorerProducer != nil {
return i.updateStorerProducer.NewUpdateStorer(updateType, payloadNum)
} else {
return &devNullUpdateStorer{}, nil
}
}
func (s *devNullUpdateStorer) Initialize(artifactHeaders,
artifactAugmentedHeaders artifact.HeaderInfoer,
payloadHeaders ArtifactUpdateHeaders) error {
return nil
}
func (s *devNullUpdateStorer) PrepareStoreUpdate() error {
return nil
}
func (s *devNullUpdateStorer) StoreUpdate(r io.Reader, info os.FileInfo) error {
_, err := io.Copy(ioutil.Discard, r)
return err
}
func (s *devNullUpdateStorer) FinishStoreUpdate() error {
return nil
}
func parseFiles(r io.Reader) (*artifact.Files, error) {
files := new(artifact.Files)
if _, err := io.Copy(files, r); err != nil {
return nil, errors.Wrap(err, "Payload: error reading files")
}
if err := files.Validate(); err != nil {
return nil, err
}
return files, nil
}
func match(pattern, name string) bool {
match, err := filepath.Match(pattern, name)
if err != nil {
return false
}
return match
}
func writeFiles(tw *tar.Writer, updFiles []string, dir string) error {
if tw == nil {
return errors.New("writer: tar-writer is nil")
}
files := new(artifact.Files)
files.FileList = append(files.FileList, updFiles...)
sa := artifact.NewTarWriterStream(tw)
stream, err := artifact.ToStream(files)
if err != nil {
return errors.Wrap(err, "writeFiles: ")
}
if err := sa.Write(stream,
filepath.Join(dir, "files")); err != nil {
return errors.Wrapf(err, "writer: can not tar files")
}
return nil
}
func writeTypeInfo(tw *tar.Writer, updateType string, dir string) error {
tInfo := artifact.TypeInfo{Type: updateType}
info, err := json.Marshal(&tInfo)
if err != nil {
return errors.Wrapf(err, "Payload: can not create type-info")
}
w := artifact.NewTarWriterStream(tw)
if err := w.Write(info, filepath.Join(dir, "type-info")); err != nil {
return errors.Wrapf(err, "Payload: can not tar type-info")
}
return nil
}
type WriteInfoArgs struct {
tarWriter *tar.Writer
dir string
typeinfov3 *artifact.TypeInfoV3
}
func writeTypeInfoV3(args *WriteInfoArgs) error {
info, err := json.Marshal(args.typeinfov3)
if err != nil {
return errors.Wrapf(err, "Payload: can not create type-info")
}
w := artifact.NewTarWriterStream(args.tarWriter)
if err := w.Write(info, filepath.Join(args.dir, "type-info")); err != nil {
return errors.Wrapf(err, "Payload: can not tar type-info")
}
return nil
}
|