Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
Fixed files getting trashed when extraction is canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
martinplaner committed Mar 11, 2017
1 parent f6c8247 commit 911b950
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 10 additions & 3 deletions archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
package archive

import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"time"

"github.com/martinplaner/gunarchiver/debug"
"github.com/martinplaner/gunarchiver/progress"
)

var ErrCanceled = errors.New("the process was canceled by user request")

func IsCanceled(err error) bool {
return err == ErrCanceled
}

// Archive represent an archive and provides methods for iterating through its files.
type Archive interface {
// Basename returns the base name of the archive file, without file extension or path.
Expand All @@ -43,7 +49,8 @@ type File interface {
}

// Extract extracts the Archive a to the specified path and reports progress to the supplied progressChan.
// If the supplied shouldCancel func returns true, the extraction will get canceled as soon as possible.
// If the supplied shouldCancel func returns true, the extraction will get canceled as soon as possible
// and return ErrCanceled. Callers should check using IsCanceled.
func Extract(a Archive, path string, progressChan chan progress.Progress, shouldCancel func() bool) error {
progressChan <- progress.Progress{CurrentFile: "Starting extraction..."}

Expand All @@ -53,7 +60,7 @@ func Extract(a Archive, path string, progressChan chan progress.Progress, should
if shouldCancel() {
// Aborting extraction as per user's request
progressChan <- progress.Progress{CurrentFile: "Canceled extraction!", Percentage: 100}
return nil
return ErrCanceled
}
file, err := a.Next()
if err == io.EOF {
Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ func main() {
}

func extractArchiveAndDelete(path string, progressChan chan progress.Progress, shouldCancel func() bool) error {
if err := extractArchive(path, progressChan, shouldCancel); err != nil {

err := extractArchive(path, progressChan, shouldCancel)
if archive.IsCanceled(err) {
return nil
} else if err != nil {
return err
}

Expand Down Expand Up @@ -94,7 +98,10 @@ func extractArchive(path string, progressChan chan progress.Progress, shouldCanc
archive.CreateDir(baseDir)
}

if err := archive.Extract(a, baseDir, progressChan, shouldCancel); err != nil {
err = archive.Extract(a, baseDir, progressChan, shouldCancel)
if archive.IsCanceled(err) {
return err
} else if err != nil {
return fmt.Errorf("could not extract archive: %v", err)
}

Expand Down

0 comments on commit 911b950

Please sign in to comment.