-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetransfer.go
138 lines (115 loc) · 3.73 KB
/
filetransfer.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
package dockerhelper
import (
"archive/tar"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
docker "github.com/fsouza/go-dockerclient"
stitar "github.com/openshift/source-to-image/pkg/tar"
)
// removeLeadingDirectoryAdapter wraps a tar.Reader and strips the first leading
// directory name of all of the files in the archive. An error is returned in
// the case that files with differing first leading directory names are
// encountered.
type removeLeadingDirectoryAdapter struct {
stitar.Reader
leadingDir string
setLeadingDir bool
}
func (adapter removeLeadingDirectoryAdapter) Next() (*tar.Header, error) {
for {
header, err := adapter.Reader.Next()
if err != nil {
return nil, err
}
trimmedName := strings.Trim(header.Name, "/")
paths := strings.SplitN(trimmedName, "/", 2)
// ensure leading directory is consistent throughout the tar file
if !adapter.setLeadingDir {
adapter.setLeadingDir = true
adapter.leadingDir = paths[0]
}
if adapter.leadingDir != paths[0] {
return nil, fmt.Errorf("inconsistent leading directory at %s, type %c", header.Name, header.Typeflag)
}
if len(paths) == 1 {
if header.Typeflag == tar.TypeDir {
// this is the leading directory itself: drop it
_, err = io.Copy(ioutil.Discard, adapter)
if err != nil {
return nil, err
}
continue
}
return nil, fmt.Errorf("unexpected non-directory %s, type %c", header.Name, header.Typeflag)
}
header.Name = paths[1]
return header, err
}
}
func newContainerDownloader(client *docker.Client, container, path string) io.ReadCloser {
r, w := io.Pipe()
go func() {
opts := docker.DownloadFromContainerOptions{
Path: path,
OutputStream: w,
}
w.CloseWithError(client.DownloadFromContainer(container, opts))
}()
return r
}
func newContainerUploader(client *docker.Client, container, path string) (io.WriteCloser, <-chan error) {
r, w := io.Pipe()
errch := make(chan error, 1)
go func() {
opts := docker.UploadToContainerOptions{
Path: path,
InputStream: r,
}
errch <- client.UploadToContainer(container, opts)
}()
return w, errch
}
type readCloser struct {
io.Reader
io.Closer
}
// StreamFileFromContainer returns an io.ReadCloser from which the contents of a
// file in a remote container can be read.
func StreamFileFromContainer(client *docker.Client, container, src string) (io.ReadCloser, error) {
downloader := newContainerDownloader(client, container, src)
tarReader := tar.NewReader(downloader)
header, err := tarReader.Next()
if err != nil {
return nil, err
}
if header.Name != filepath.Base(src) || (header.Typeflag != tar.TypeReg && header.Typeflag != tar.TypeRegA) {
return nil, fmt.Errorf("unexpected tar file content %s, type %c", header.Name, header.Typeflag)
}
return readCloser{Reader: tarReader, Closer: downloader}, nil
}
// DownloadDirFromContainer downloads an entire directory of files from a remote
// container.
func DownloadDirFromContainer(client *docker.Client, container, src, dst string) error {
downloader := newContainerDownloader(client, container, src)
defer downloader.Close()
tarReader := &removeLeadingDirectoryAdapter{Reader: tar.NewReader(downloader)}
t := stitar.New()
return t.ExtractTarStreamFromTarReader(dst, tarReader, nil)
}
// UploadFileToContainer uploads a file to a remote container.
func UploadFileToContainer(client *docker.Client, container, src, dest string) error {
uploader, errch := newContainerUploader(client, container, path.Dir(dest))
nullWalkFunc := func(path string, info os.FileInfo, err error) error { return err }
t := stitar.New()
err := t.StreamFileAsTarWithCallback(src, path.Base(dest), uploader, nullWalkFunc, false)
uploader.Close()
if err != nil {
return err
}
return <-errch
}