-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathtar_image.go
150 lines (125 loc) · 3.13 KB
/
tar_image.go
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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package image
import (
"archive/tar"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
)
type TarImage struct {
files []string
excludePaths []string
infoLog io.Writer
}
func NewTarImage(files []string, excludePaths []string, infoLog io.Writer) *TarImage {
return &TarImage{files, excludePaths, infoLog}
}
func (i *TarImage) AsFileImage(labels map[string]string) (*FileImage, error) {
tmpFile, err := ioutil.TempFile("", "imgpkg-tar-image")
if err != nil {
return nil, err
}
defer tmpFile.Close()
err = i.createTarball(tmpFile, i.files)
if err != nil {
_ = os.Remove(tmpFile.Name())
return nil, err
}
fileImg, err := NewFileImage(tmpFile.Name(), labels)
if err != nil {
_ = os.Remove(tmpFile.Name())
return nil, err
}
return fileImg, nil
}
func (i *TarImage) createTarball(file *os.File, filePaths []string) error {
tarWriter := tar.NewWriter(file)
defer tarWriter.Close()
for _, path := range filePaths {
info, err := os.Stat(path)
if err != nil {
return err
}
if info.IsDir() {
// Walk is deterministic according to https://golang.org/pkg/path/filepath/#Walk
err := filepath.Walk(path, func(walkedPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(path, walkedPath)
if err != nil {
return err
}
if info.IsDir() {
if i.isExcluded(relPath) {
return filepath.SkipDir
}
return i.addDirToTar(relPath, info, tarWriter)
}
if (info.Mode() & os.ModeType) != 0 {
return fmt.Errorf("Expected file '%s' to be a regular file", walkedPath)
}
return i.addFileToTar(walkedPath, relPath, info, tarWriter)
})
if err != nil {
return fmt.Errorf("Adding file '%s' to tar: %s", path, err)
}
} else {
err := i.addFileToTar(path, filepath.Base(path), info, tarWriter)
if err != nil {
return err
}
}
}
return nil
}
func (i *TarImage) addDirToTar(relPath string, info os.FileInfo, tarWriter *tar.Writer) error {
if i.isExcluded(relPath) {
panic("Unreachable") // directories excluded above
}
i.infoLog.Write([]byte(fmt.Sprintf("dir: %s\n", relPath)))
header := &tar.Header{
Name: relPath,
Size: info.Size(),
Mode: 0700, // static
ModTime: time.Time{}, // static
Typeflag: tar.TypeDir,
}
return tarWriter.WriteHeader(header)
}
func (i *TarImage) addFileToTar(fullPath, relPath string, info os.FileInfo, tarWriter *tar.Writer) error {
if i.isExcluded(relPath) {
return nil
}
i.infoLog.Write([]byte(fmt.Sprintf("file: %s\n", relPath)))
file, err := os.Open(fullPath)
if err != nil {
return err
}
defer file.Close()
header := &tar.Header{
Name: relPath,
Size: info.Size(),
Mode: 0600, // static
ModTime: time.Time{}, // static
Typeflag: tar.TypeReg,
}
err = tarWriter.WriteHeader(header)
if err != nil {
return err
}
_, err = io.Copy(tarWriter, file)
return err
}
func (i *TarImage) isExcluded(relPath string) bool {
for _, path := range i.excludePaths {
if path == relPath {
return true
}
}
return false
}