Skip to content

Commit

Permalink
Implement StripComponents to rar
Browse files Browse the repository at this point in the history
  • Loading branch information
illiliti authored and AJ ONeal committed Oct 13, 2020
1 parent 2e9f979 commit b0ffd5a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/arc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func getFormat(subcommand string) (interface{}, error) {
v.OverwriteExisting = overwriteExisting
v.MkdirAll = mkdirAll
v.ImplicitTopLevelFolder = implicitTopLevelFolder
v.StripComponents = stripComponents
v.ContinueOnError = continueOnError
v.Password = os.Getenv("ARCHIVE_PASSWORD")
case *archiver.Tar:
Expand Down
15 changes: 15 additions & 0 deletions rar.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type Rar struct {
// especially on extraction.
ImplicitTopLevelFolder bool

// Strip number of leading paths. This feature is available
// only during unpacking of the entire archive.
StripComponents int

// If true, errors encountered during reading
// or writing a single file will be logged and
// the operation will continue on remaining files.
Expand Down Expand Up @@ -168,6 +172,17 @@ func (r *Rar) unrarNext(to string) error {
return fmt.Errorf("checking path traversal attempt: %v", errPath)
}

if r.StripComponents > 0 {
if strings.Count(header.Name, "/") < r.StripComponents {
return nil // skip path with fewer components
}

for i := 0; i < r.StripComponents; i++ {
slash := strings.Index(header.Name, "/")
header.Name = header.Name[slash+1:]
}
}

return r.unrarFile(f, filepath.Join(to, header.Name))
}

Expand Down

0 comments on commit b0ffd5a

Please sign in to comment.