Skip to content

Commit

Permalink
Adds documentation around the symlink sorting function and adds examp…
Browse files Browse the repository at this point in the history
…le input and output
  • Loading branch information
ForestEckhardt authored and ryanmoran committed Apr 13, 2021
1 parent db37853 commit a0e5e3f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions vacation/vacation.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (ta TarArchive) Decompress(destination string) error {
// metadata.
directories := map[string]interface{}{}

// Struct and slice to collect symlinks and create them after all files have
// been created
type header struct {
name string
linkname string
Expand Down Expand Up @@ -166,6 +168,18 @@ func (ta TarArchive) Decompress(destination string) error {
}
}

// Sort the symlinks so that symlinks of symlinks have their base link
// created before they are created.
//
// For example:
// b-sym -> a-sym/file
// a-sym -> dir
// c-sym -> a-sym/other-file
//
// Will sort to:
// a-sym -> dir
// b-sym -> a-sym/file
// c-sym -> a-sym/other-file
sort.Slice(symlinkHeaders, func(i, j int) bool {
return filepath.Clean(symlinkHeaders[i].name) < filepath.Clean(filepath.Join(filepath.Dir(symlinkHeaders[j].name), symlinkHeaders[j].linkname))
})
Expand Down Expand Up @@ -313,6 +327,8 @@ func NewZipArchive(inputReader io.Reader) ZipArchive {
// Decompress reads from ZipArchive and writes files into the destination
// specified.
func (z ZipArchive) Decompress(destination string) error {
// Struct and slice to collect symlinks and create them after all files have
// been created
type header struct {
name string
linkname string
Expand Down Expand Up @@ -400,6 +416,18 @@ func (z ZipArchive) Decompress(destination string) error {
}
}

// Sort the symlinks so that symlinks of symlinks have their base link
// created before they are created.
//
// For example:
// b-sym -> a-sym/file
// a-sym -> dir
// c-sym -> a-sym/other-file
//
// Will sort to:
// a-sym -> dir
// b-sym -> a-sym/file
// c-sym -> a-sym/other-file
sort.Slice(symlinkHeaders, func(i, j int) bool {
return filepath.Clean(symlinkHeaders[i].name) < filepath.Clean(filepath.Join(filepath.Dir(symlinkHeaders[j].name), symlinkHeaders[j].linkname))
})
Expand Down

0 comments on commit a0e5e3f

Please sign in to comment.