forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
170 lines (137 loc) · 4.7 KB
/
writer.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Copyright IBM Corp. 2016 All Rights Reserved.
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 util
import (
"archive/tar"
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/op/go-logging"
)
var vmLogger = logging.MustGetLogger("container")
// These filetypes are excluded while creating the tar package sent to Docker
// Generated .class and other temporary files can be excluded
var javaExcludeFileTypes = map[string]bool{
".class": true,
}
func WriteFolderToTarPackage(tw *tar.Writer, srcPath string, excludeDir string, includeFileTypeMap map[string]bool, excludeFileTypeMap map[string]bool) error {
rootDirectory := srcPath
vmLogger.Infof("rootDirectory = %s", rootDirectory)
//append "/" if necessary
if excludeDir != "" && strings.LastIndex(excludeDir, "/") < len(excludeDir)-1 {
excludeDir = excludeDir + "/"
}
rootDirLen := len(rootDirectory)
walkFn := func(path string, info os.FileInfo, err error) error {
// If path includes .git, ignore
if strings.Contains(path, ".git") {
return nil
}
if info.Mode().IsDir() {
return nil
}
//exclude any files with excludeDir prefix. They should already be in the tar
if excludeDir != "" && strings.Index(path, excludeDir) == rootDirLen+1 {
//1 for "/"
return nil
}
// Because of scoping we can reference the external rootDirectory variable
if len(path[rootDirLen:]) == 0 {
return nil
}
ext := filepath.Ext(path)
if includeFileTypeMap != nil {
// we only want 'fileTypes' source files at this point
if _, ok := includeFileTypeMap[ext]; ok != true {
return nil
}
}
//exclude the given file types
if excludeFileTypeMap != nil {
if exclude, ok := excludeFileTypeMap[ext]; ok && exclude {
return nil
}
}
newPath := fmt.Sprintf("src%s", path[rootDirLen:])
//newPath := path[len(rootDirectory):]
err = WriteFileToPackage(path, newPath, tw)
if err != nil {
return fmt.Errorf("Error writing file to package: %s", err)
}
return nil
}
if err := filepath.Walk(rootDirectory, walkFn); err != nil {
vmLogger.Infof("Error walking rootDirectory: %s", err)
return err
}
return nil
}
//Package Java project to tar file from the source path
func WriteJavaProjectToPackage(tw *tar.Writer, srcPath string) error {
vmLogger.Debugf("Packaging Java project from path %s", srcPath)
if err := WriteFolderToTarPackage(tw, srcPath, "", nil, javaExcludeFileTypes); err != nil {
vmLogger.Errorf("Error writing folder to tar package %s", err)
return err
}
// Write the tar file out
if err := tw.Close(); err != nil {
return err
}
return nil
}
//WriteFileToPackage writes a file to the tarball
func WriteFileToPackage(localpath string, packagepath string, tw *tar.Writer) error {
fd, err := os.Open(localpath)
if err != nil {
return fmt.Errorf("%s: %s", localpath, err)
}
defer fd.Close()
is := bufio.NewReader(fd)
return WriteStreamToPackage(is, localpath, packagepath, tw)
}
//WriteStreamToPackage writes bytes (from a file reader) to the tarball
func WriteStreamToPackage(is io.Reader, localpath string, packagepath string, tw *tar.Writer) error {
info, err := os.Stat(localpath)
if err != nil {
return fmt.Errorf("%s: %s", localpath, err)
}
header, err := tar.FileInfoHeader(info, localpath)
if err != nil {
return fmt.Errorf("Error getting FileInfoHeader: %s", err)
}
//Let's take the variance out of the tar, make headers identical by using zero time
oldname := header.Name
var zeroTime time.Time
header.AccessTime = zeroTime
header.ModTime = zeroTime
header.ChangeTime = zeroTime
header.Name = packagepath
if err = tw.WriteHeader(header); err != nil {
return fmt.Errorf("Error write header for (path: %s, oldname:%s,newname:%s,sz:%d) : %s", localpath, oldname, packagepath, header.Size, err)
}
if _, err := io.Copy(tw, is); err != nil {
return fmt.Errorf("Error copy (path: %s, oldname:%s,newname:%s,sz:%d) : %s", localpath, oldname, packagepath, header.Size, err)
}
return nil
}
func WriteBytesToPackage(name string, payload []byte, tw *tar.Writer) error {
//Make headers identical by using zero time
var zeroTime time.Time
tw.WriteHeader(&tar.Header{Name: name, Size: int64(len(payload)), ModTime: zeroTime, AccessTime: zeroTime, ChangeTime: zeroTime})
tw.Write(payload)
return nil
}