File: copy.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (105 lines) | stat: -rw-r--r-- 3,440 bytes parent folder | download | duplicates (3)
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
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"github.com/containers/storage"
	"github.com/containers/storage/pkg/chrootarchive"
	"github.com/containers/storage/pkg/idtools"
	"github.com/containers/storage/pkg/mflag"
)

var chownOptions = ""

func copyContent(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
	var untarIDMappings *idtools.IDMappings
	var chownOpts *idtools.IDPair
	if len(args) < 1 {
		return 1, nil
	}
	if len(chownOptions) > 0 {
		uidStr, gidStr, ok := strings.Cut(chownOptions, ":")
		if !ok {
			gidStr = uidStr
		}
		uid, err := strconv.ParseUint(uidStr, 10, 32)
		if err != nil {
			return 1, fmt.Errorf("bad UID: %w", err)
		}
		gid, err := strconv.ParseUint(gidStr, 10, 32)
		if err != nil {
			return 1, fmt.Errorf("bad GID: %w", err)
		}
		chownOpts = &idtools.IDPair{UID: int(uid), GID: int(gid)}
	}
	target := args[len(args)-1]
	if strings.Contains(target, ":") {
		layer, targetRel, ok := strings.Cut(target, ":")
		if !ok {
			return 1, fmt.Errorf("error parsing target location %q: only one part", target)
		}
		targetLayer, err := m.Layer(layer)
		if err != nil {
			return 1, fmt.Errorf("error finding layer %q: %+v", layer, err)
		}
		untarIDMappings = idtools.NewIDMappingsFromMaps(targetLayer.UIDMap, targetLayer.GIDMap)
		targetMount, err := m.Mount(targetLayer.ID, targetLayer.MountLabel)
		if err != nil {
			return 1, fmt.Errorf("error mounting layer %q: %+v", targetLayer.ID, err)
		}
		target = filepath.Join(targetMount, targetRel)
		defer func() {
			if _, err := m.Unmount(targetLayer.ID, false); err != nil {
				fmt.Fprintf(os.Stderr, "error unmounting layer %q: %+v\n", targetLayer.ID, err)
				// Does not change the exit code
			}
		}()
	}
	args = args[:len(args)-1]
	for _, srcSpec := range args {
		var tarIDMappings *idtools.IDMappings
		source := srcSpec
		if layer, sourceRel, ok := strings.Cut(source, ":"); ok {
			sourceLayer, err := m.Layer(layer)
			if err != nil {
				return 1, fmt.Errorf("error finding layer %q: %+v", layer, err)
			}
			tarIDMappings = idtools.NewIDMappingsFromMaps(sourceLayer.UIDMap, sourceLayer.GIDMap)
			sourceMount, err := m.Mount(sourceLayer.ID, sourceLayer.MountLabel)
			if err != nil {
				return 1, fmt.Errorf("error mounting layer %q: %+v", sourceLayer.ID, err)
			}
			source = filepath.Join(sourceMount, sourceRel)
			defer func() {
				if _, err := m.Unmount(sourceLayer.ID, false); err != nil {
					fmt.Fprintf(os.Stderr, "error unmounting layer %q: %+v\n", sourceLayer.ID, err)
					// Does not change the exit code
				}
			}()
		}
		archiver := chrootarchive.NewArchiverWithChown(tarIDMappings, chownOpts, untarIDMappings)
		if err := archiver.CopyWithTar(source, target); err != nil {
			return 1, fmt.Errorf("error copying %q to %q: %+v", source, target, err)
		}
	}
	return 0, nil
}

func init() {
	commands = append(commands, command{
		names:       []string{"copy"},
		usage:       "Copy files or directories into a layer, possibly from another layer",
		optionsHelp: "[options [...]] [sourceLayerNameOrID:]/path [...] targetLayerNameOrID:/path",
		minArgs:     2,
		maxArgs:     -1,
		action:      copyContent,
		addFlags: func(flags *mflag.FlagSet, cmd *command) {
			flags.StringVar(&chownOptions, []string{"-chown", "o"}, chownOptions, "Set owner on new copies")
			flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
		},
	})
}