Don't cause a race condition
This commit is contained in:
parent
a635cdd6b2
commit
b2d34cf8e7
|
@ -4,11 +4,9 @@ import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mholt/archiver/v3"
|
"github.com/mholt/archiver/v3"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"golang.org/x/sync/errgroup"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -75,37 +73,18 @@ func (fs *Filesystem) DecompressFile(dir string, file string) error {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
g, ctx := errgroup.WithContext(context.Background())
|
|
||||||
|
|
||||||
// Walk over all of the files spinning up an additional go-routine for each file we've encountered
|
// Walk over all of the files spinning up an additional go-routine for each file we've encountered
|
||||||
// and then extract that file from the archive and write it to the disk. If any part of this process
|
// and then extract that file from the archive and write it to the disk. If any part of this process
|
||||||
// encounters an error the entire process will be stopped.
|
// encounters an error the entire process will be stopped.
|
||||||
archiver.Walk(source, func(f archiver.File) error {
|
return archiver.Walk(source, func(f archiver.File) error {
|
||||||
// Don't waste time with directories, we don't need to create them if they have no contents, and
|
// Don't waste time with directories, we don't need to create them if they have no contents, and
|
||||||
// we will ensure the directory exists when opening the file for writing anyways.
|
// we will ensure the directory exists when opening the file for writing anyways.
|
||||||
if f.IsDir() {
|
if f.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fi := f
|
return fs.extractFileFromArchive(f)
|
||||||
g.Go(func() error {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
default:
|
|
||||||
return fs.extractFileFromArchive(fi)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Wait until everything is done, and then return.
|
|
||||||
if err := g.Wait(); err != nil {
|
|
||||||
return errors.WithStack(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extracts a single file from the archive and writes it to the disk after verifying that it will end
|
// Extracts a single file from the archive and writes it to the disk after verifying that it will end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user