-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
177 lines (157 loc) · 4.91 KB
/
builder.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
171
172
173
174
175
176
177
package core
import (
"fmt"
"hash"
"io"
"log"
"path/filepath"
"strings"
"github.com/smarty/satisfy/contracts"
)
type PackageBuilder interface {
Build() error
Contents() []contracts.ArchiveItem
}
type DirectoryPackageBuilderFileSystem interface {
contracts.PathLister
contracts.FileOpener
contracts.RootPath
}
type DirectoryPackageBuilder struct {
storage DirectoryPackageBuilderFileSystem
archive contracts.ArchiveWriter
hasher hash.Hash
contents []contracts.ArchiveItem
showProgress bool
}
func NewDirectoryPackageBuilder(storage DirectoryPackageBuilderFileSystem, archive contracts.ArchiveWriter, hasher hash.Hash, showProgress bool) PackageBuilder {
return &DirectoryPackageBuilder{
storage: storage,
archive: archive,
hasher: hasher,
showProgress: showProgress,
}
}
func (this *DirectoryPackageBuilder) Build() error {
if fileInfo, ok := this.fileOnly(); ok == true {
err := this.add(fileInfo, true)
if err != nil {
return err
}
} else {
for _, file := range this.storage.Listing() {
err := this.add(file, false)
if err != nil {
return err
}
}
}
return this.archive.Close()
}
func (this *DirectoryPackageBuilder) add(file contracts.FileInfo, fileOnly bool) error {
log.Printf("Adding \"%s\" to archive.", file.Path())
header, err := this.buildHeader(file, fileOnly)
if err != nil {
return err
}
this.archive.WriteHeader(header)
err = this.archiveContents(file, header.LinkName)
if err != nil {
return err
}
this.contents = append(this.contents, this.buildManifestEntry(file, header.LinkName))
return err
}
func (this *DirectoryPackageBuilder) archiveContents(file contracts.FileInfo, symlinkSourcePath string) error {
if symlinkSourcePath != "" {
_, _ = io.WriteString(this.hasher, symlinkSourcePath)
return nil
}
progressWriter := newArchiveProgressCounter(file.Size(), func(archived, total string, done bool) {
if this.showProgress {
if done {
fmt.Printf("\nArchived %s of %s.\n", archived, total)
} else {
fmt.Printf("\033[2K\rArchived %s of %s.", archived, total)
}
}
})
defer func() {
fmt.Printf("\n")
}()
defer closeResource(progressWriter)
writer := io.MultiWriter(this.hasher, this.archive, progressWriter)
reader := this.storage.Open(file.Path())
defer closeResource(reader)
_, err := io.Copy(writer, reader)
return err
}
func (this *DirectoryPackageBuilder) buildHeader(file contracts.FileInfo, fileOnly bool) (header contracts.ArchiveHeader, err error) {
if fileOnly {
header.Name = filepath.Base(file.Path())
} else {
header.Name = strings.TrimPrefix(file.Path(), this.storage.RootPath()+"/")
}
header.Size = file.Size()
header.ModTime = file.ModTime()
header.Executable = contracts.IsExecutable(file.Mode())
if file.Symlink() == "" {
return header, nil
}
if this.outOfBounds(file) {
return header, this.symlinkOutOfBoundError(file)
}
header.LinkName, err = this.relativeLinkSourcePath(file)
return header, err
}
func (this *DirectoryPackageBuilder) relativeLinkSourcePath(file contracts.FileInfo) (string, error) {
path := file.Symlink()
if this.isAbsolute(path) {
return filepath.Rel(filepath.Dir(file.Path()), path)
}
joined := filepath.Join(filepath.Dir(file.Path()), path)
path = filepath.Clean(joined)
return filepath.Rel(filepath.Dir(file.Path()), path)
}
func (this *DirectoryPackageBuilder) symlinkOutOfBoundError(file contracts.FileInfo) error {
return fmt.Errorf(
"the file \"%s\" is a symlink that refers to \"%s\" which is outside of the configured root directory: \"%s\"",
file.Path(),
file.Symlink(),
this.storage.RootPath())
}
func (this *DirectoryPackageBuilder) buildManifestEntry(file contracts.FileInfo, symlinkSourcePath string) contracts.ArchiveItem {
defer this.hasher.Reset()
return contracts.ArchiveItem{
Path: strings.TrimPrefix(file.Path(), this.storage.RootPath()+"/"),
Size: this.determineFileSize(file, symlinkSourcePath),
MD5Checksum: this.hasher.Sum(nil),
}
}
func (this *DirectoryPackageBuilder) determineFileSize(file contracts.FileInfo, symlinkSourcePath string) int64 {
if symlinkSourcePath == "" {
return file.Size()
}
return int64(len(symlinkSourcePath))
}
func (this *DirectoryPackageBuilder) Contents() []contracts.ArchiveItem {
return this.contents
}
func (this *DirectoryPackageBuilder) outOfBounds(info contracts.FileInfo) bool {
if this.isAbsolute(info.Symlink()) {
return !strings.HasPrefix(info.Symlink(), this.storage.RootPath())
}
cleaned := filepath.Clean(filepath.Join(filepath.Dir(info.Path()), info.Symlink()))
return !strings.HasPrefix(cleaned, this.storage.RootPath())
}
func (this *DirectoryPackageBuilder) isAbsolute(path string) bool {
return strings.HasPrefix(path, "/")
}
func (this *DirectoryPackageBuilder) fileOnly() (contracts.FileInfo, bool) {
if len(this.storage.Listing()) == 1 {
if this.storage.Listing()[0].Mode().IsRegular() {
return this.storage.Listing()[0], true
}
}
return nil, false
}