Skip to content

Commit a0e5e3f

Browse files
ForestEckhardtryanmoran
authored andcommitted
Adds documentation around the symlink sorting function and adds example input and output
1 parent db37853 commit a0e5e3f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

vacation/vacation.go

+28
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ func (ta TarArchive) Decompress(destination string) error {
7676
// metadata.
7777
directories := map[string]interface{}{}
7878

79+
// Struct and slice to collect symlinks and create them after all files have
80+
// been created
7981
type header struct {
8082
name string
8183
linkname string
@@ -166,6 +168,18 @@ func (ta TarArchive) Decompress(destination string) error {
166168
}
167169
}
168170

171+
// Sort the symlinks so that symlinks of symlinks have their base link
172+
// created before they are created.
173+
//
174+
// For example:
175+
// b-sym -> a-sym/file
176+
// a-sym -> dir
177+
// c-sym -> a-sym/other-file
178+
//
179+
// Will sort to:
180+
// a-sym -> dir
181+
// b-sym -> a-sym/file
182+
// c-sym -> a-sym/other-file
169183
sort.Slice(symlinkHeaders, func(i, j int) bool {
170184
return filepath.Clean(symlinkHeaders[i].name) < filepath.Clean(filepath.Join(filepath.Dir(symlinkHeaders[j].name), symlinkHeaders[j].linkname))
171185
})
@@ -313,6 +327,8 @@ func NewZipArchive(inputReader io.Reader) ZipArchive {
313327
// Decompress reads from ZipArchive and writes files into the destination
314328
// specified.
315329
func (z ZipArchive) Decompress(destination string) error {
330+
// Struct and slice to collect symlinks and create them after all files have
331+
// been created
316332
type header struct {
317333
name string
318334
linkname string
@@ -400,6 +416,18 @@ func (z ZipArchive) Decompress(destination string) error {
400416
}
401417
}
402418

419+
// Sort the symlinks so that symlinks of symlinks have their base link
420+
// created before they are created.
421+
//
422+
// For example:
423+
// b-sym -> a-sym/file
424+
// a-sym -> dir
425+
// c-sym -> a-sym/other-file
426+
//
427+
// Will sort to:
428+
// a-sym -> dir
429+
// b-sym -> a-sym/file
430+
// c-sym -> a-sym/other-file
403431
sort.Slice(symlinkHeaders, func(i, j int) bool {
404432
return filepath.Clean(symlinkHeaders[i].name) < filepath.Clean(filepath.Join(filepath.Dir(symlinkHeaders[j].name), symlinkHeaders[j].linkname))
405433
})

0 commit comments

Comments
 (0)