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
|
// Copyright 2018 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 main
import (
"fmt"
"io"
"os"
"regexp"
"github.com/urfave/cli"
)
var isimg = regexp.MustCompile(`\.(mender|sdimg|uefiimg)`)
func Cat(c *cli.Context) (err error) {
if c.NArg() != 1 {
return cli.NewExitError(fmt.Sprintf("Got %d arguments, wants one", c.NArg()), 1)
}
if !isimg.MatchString(c.Args().First()) {
return cli.NewExitError("The input image does not seem to be a valid image", 1)
}
r, err := NewPartitionFile(c.Args().First(), c.String("key"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to open the partition reader: err: %v", err), 1)
}
var w io.WriteCloser = os.Stdout
if _, err = io.Copy(w, r); err != nil {
return cli.NewExitError(fmt.Sprintf("failed to copy from: %s to stdout: err: %v", c.Args().First(), err), 1)
}
return r.Close()
}
func Copy(c *cli.Context) (err error) {
var r io.ReadCloser
var w io.WriteCloser
switch parseCLIOptions(c) {
case copyin:
r, err = os.OpenFile(c.Args().First(), os.O_CREATE|os.O_RDONLY, 0655)
defer r.Close()
if err != nil {
return cli.NewExitError(fmt.Sprintf("%v", err), 1)
}
w, err = NewPartitionFile(c.Args().Get(1), c.String("key"))
if err != nil {
w.Close()
return cli.NewExitError(fmt.Sprintf("%v", err), 1)
}
case copyinstdin:
r = os.Stdin
w, err = NewPartitionFile(c.Args().First(), c.String("key"))
if err != nil {
w.Close()
return cli.NewExitError(fmt.Sprintf("%v", err), 1)
}
case copyout:
r, err = NewPartitionFile(c.Args().First(), c.String("key"))
defer r.Close()
if err != nil {
return cli.NewExitError(fmt.Sprintf("%v", err), 1)
}
w, err = os.OpenFile(c.Args().Get(1), os.O_CREATE|os.O_WRONLY, 0655)
if err != nil {
w.Close()
return cli.NewExitError(fmt.Sprintf("%v", err), 1)
}
case parseError:
return cli.NewExitError(fmt.Sprintln("no artifact or sdimage provided"), 1)
case argerror:
return cli.NewExitError(fmt.Sprintf("got %d arguments, wants two", c.NArg()), 1)
default:
return cli.NewExitError("critical error", 1)
}
_, err = io.Copy(w, r)
// Ignore the close error in case of an io.Copy error.
if cerr := w.Close(); cerr != nil && err != nil {
err = cerr
}
return err
}
// Install installs a file from the host filesystem onto either
// a mender artifact, or an sdimg.
func Install(c *cli.Context) (err error) {
var r io.ReadCloser
var w io.WriteCloser
switch parseCLIOptions(c) {
case copyin:
var perm os.FileMode
if c.Int("mode") == 0 {
return cli.NewExitError("File permissions needs to be set, if you are simply copying, the cp command should fit your needs", 1)
}
perm = os.FileMode(c.Int("mode"))
r, err = os.OpenFile(c.Args().First(), os.O_RDWR, perm)
defer r.Close()
if err != nil {
return cli.NewExitError(fmt.Sprintf("%v", err), 1)
}
w, err = NewPartitionFile(c.Args().Get(1), c.String("key"))
defer w.Close()
if err != nil {
return cli.NewExitError(fmt.Sprintf("%v", err), 1)
}
if _, err = io.Copy(w, r); err != nil {
return cli.NewExitError(fmt.Sprintf("%v", err), 1)
}
return nil
case parseError:
return cli.NewExitError("No artifact or sdimg provided", 1)
case argerror:
return cli.NewExitError(fmt.Sprintf("got %d arguments, wants two", c.NArg()), 1)
default:
return cli.NewExitError("Unrecognized parse error", 1)
}
}
// enumerate cli-options
const (
copyin = iota
copyinstdin
copyout
parseError
argerror
criterror
)
func parseCLIOptions(c *cli.Context) int {
finfo, err := os.Stdin.Stat()
if err != nil {
return criterror
}
// no data on stdin
if finfo.Mode()&os.ModeNamedPipe == 0 {
if c.NArg() != 2 {
return argerror
}
switch {
case isimg.MatchString(c.Args().First()):
return copyout
case isimg.MatchString(c.Args().Get(1)):
return copyin
default:
return parseError
}
} else {
// data on stdin
if isimg.MatchString(c.Args().First()) {
return copyinstdin
}
return parseError
}
}
|