-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.go
250 lines (191 loc) · 6.94 KB
/
pack.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package onearchiver
import (
"fmt"
"github.com/ganeshrvel/archiver"
ignore "github.com/sabhiram/go-gitignore"
"github.com/wesovilabs/koazee"
"os"
"path/filepath"
"strings"
)
func (arc zipArchive) doPack(ph *ProgressHandler) error {
_fileList := arc.pack.FileList
commonParentPath := GetCommonParentPath(os.PathSeparator, _fileList...)
if indexExists(&_fileList, 0) && commonParentPath == _fileList[0] {
commonParentPathSplitted := strings.Split(_fileList[0], PathSep)
commonParentPath = strings.Join(commonParentPathSplitted[:len(commonParentPathSplitted)-1], PathSep)
}
if err := createZipFile(&arc, _fileList, commonParentPath, ph); err != nil {
return err
}
return nil
}
func (arc commonArchive) doPack(ph *ProgressHandler) error {
_filename := arc.meta.Filename
_fileList := arc.pack.FileList
arcFileObj, err := archiver.ByExtension(_filename)
if err != nil {
return err
}
err = archiveFormat(&arcFileObj, "", OverwriteExisting)
if err != nil {
return err
}
commonParentPath := GetCommonParentPath(os.PathSeparator, _fileList...)
if indexExists(&_fileList, 0) && commonParentPath == _fileList[0] {
commonParentPathSplitted := strings.Split(_fileList[0], PathSep)
commonParentPath = strings.Join(commonParentPathSplitted[:len(commonParentPathSplitted)-1], PathSep)
}
switch archValue := arcFileObj.(type) {
case *archiver.Tar:
err = packTarballs(&arc, archValue, &_fileList, commonParentPath, ph)
case *archiver.TarGz:
err = packTarballs(&arc, archValue, &_fileList, commonParentPath, ph)
case *archiver.TarBz2:
err = packTarballs(&arc, archValue, &_fileList, commonParentPath, ph)
case *archiver.TarBrotli:
err = packTarballs(&arc, archValue, &_fileList, commonParentPath, ph)
case *archiver.TarLz4:
err = packTarballs(&arc, archValue, &_fileList, commonParentPath, ph)
case *archiver.TarSz:
err = packTarballs(&arc, archValue, &_fileList, commonParentPath, ph)
case *archiver.TarXz:
err = packTarballs(&arc, archValue, &_fileList, commonParentPath, ph)
case *archiver.TarZstd:
err = packTarballs(&arc, archValue, &_fileList, commonParentPath, ph)
// Todo: parking the development of file compressors for now.
// It requires a different logic for listing, compressing and uncompressing
//case *archiver.Gz:
// err = packCompressFile(&arc, archValue, &_fileList)
//case *archiver.Brotli:
// err = packCompressFile(&arc, archValue, &_fileList)
//case *archiver.Bz2:
// err = packCompressFile(&arc, archValue, &_fileList)
//case *archiver.Lz4:
// err = packCompressFile(&arc, archValue, &_fileList)
//case *archiver.Snappy:
// err = packCompressFile(&arc, archValue, &_fileList)
//case *archiver.Xz:
// err = packCompressFile(&arc, archValue, &_fileList)
//case *archiver.Zstd:
// err = packCompressFile(&arc, archValue, &_fileList)
default:
return fmt.Errorf("archive file format is not supported")
}
if err != nil {
return err
}
return nil
}
func StartPacking(meta *ArchiveMeta, pack *ArchivePack, ph *ProgressHandler) error {
_meta := *meta
_pack := *pack
var arcPackObj ArchivePacker
ext := filepath.Ext(_meta.Filename)
if OverwriteExisting && FileExists(_meta.Filename) {
if err := os.Remove(_meta.Filename); err != nil {
return err
}
}
switch ext {
case ".zip":
arcPackObj = zipArchive{meta: _meta, pack: _pack}
break
default:
arcPackObj = commonArchive{meta: _meta, pack: _pack}
break
}
return arcPackObj.doPack(ph)
}
func getArchiveFilesRelativePath(absFilepath string, commonParentPath string) string {
splittedFilepath := strings.Split(absFilepath, commonParentPath)
_koazeeStream := koazee.StreamOf(splittedFilepath)
lastItem := _koazeeStream.Last()
return lastItem.String()
}
func processFilesForPacking(zipFilePathListMap *map[string]createArchiveFileInfo, fileList *[]string, commonParentPath string, gitIgnorePattern *[]string) error {
_zipFilePathListMap := *zipFilePathListMap
_fileList := *fileList
var ignoreList []string
ignoreList = append(ignoreList, GlobalPatternDenylist...)
ignoreList = append(ignoreList, *gitIgnorePattern...)
ignoreMatches := ignore.CompileIgnoreLines(ignoreList...)
for _, item := range _fileList {
err := filepath.Walk(item, func(absFilepath string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if isSymlink(fileInfo) {
return nil
}
absFilepath = filepath.ToSlash(absFilepath)
relativeFilePath := absFilepath
if commonParentPath != "" {
// if there is only one filepath in [_fileList]
if len(_fileList) < 2 && _fileList[0] == commonParentPath {
splittedFilepath := strings.Split(_fileList[0], PathSep)
_koazeeStream := koazee.StreamOf(splittedFilepath)
lastItem := _koazeeStream.Last()
lastPartOfFilename := lastItem.String()
// then the selected folder name should be the root directory in the archive
if isDirectory(_fileList[0]) {
archiveFilesRelativePath := getArchiveFilesRelativePath(absFilepath, commonParentPath)
relativeFilePath = filepath.Join(lastPartOfFilename, archiveFilesRelativePath)
} else {
// then the selected file should be in the root directory in the archive
relativeFilePath = lastPartOfFilename
}
} else {
relativeFilePath = getArchiveFilesRelativePath(absFilepath, commonParentPath)
}
}
isFileADir := fileInfo.IsDir()
relativeFilePath = fixDirSlash(isFileADir, relativeFilePath)
relativeFilePath = strings.TrimLeft(relativeFilePath, PathSep)
// ignore the files if pattern matches
if ignoreMatches.MatchesPath(relativeFilePath) {
return nil
}
// when the commonpath is used to construct the relative path, the parent directories in the filepath list doesnt get written into the archive file
if commonParentPath != "" && absFilepath != commonParentPath {
if item == absFilepath {
splittedPaths := strings.Split(relativeFilePath, PathSep)
for pathIndex := range splittedPaths {
_relativeFilePath := strings.Join(splittedPaths[:pathIndex+1], PathSep)
// skip if filename is blank
if _relativeFilePath == "" {
continue
}
_absFilepath := filepath.Join(commonParentPath, _relativeFilePath)
_fileInfo, err := os.Lstat(_absFilepath)
if err != nil {
return err
}
isDir := _fileInfo.IsDir()
_absFilepath = fixDirSlash(isDir, _absFilepath)
_relativeFilePath = fixDirSlash(isDir, _relativeFilePath)
_zipFilePathListMap[_absFilepath] = createArchiveFileInfo{
absFilepath: _absFilepath,
relativeFilePath: _relativeFilePath,
isDir: isDir,
fileInfo: &_fileInfo,
}
}
return nil
}
}
absFilepath = fixDirSlash(isFileADir, absFilepath)
_zipFilePathListMap[absFilepath] = createArchiveFileInfo{
absFilepath: absFilepath,
relativeFilePath: relativeFilePath,
isDir: isFileADir,
fileInfo: &fileInfo,
}
return nil
})
if err != nil {
return err
}
}
return nil
}