2020-09-27 19:24:08 +00:00
|
|
|
package filesystem
|
2020-07-16 04:16:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2020-09-20 18:51:12 +00:00
|
|
|
"strings"
|
2020-07-16 04:16:08 +00:00
|
|
|
"sync/atomic"
|
2021-01-16 19:48:30 +00:00
|
|
|
|
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/mholt/archiver/v3"
|
2020-07-16 04:16:08 +00:00
|
|
|
)
|
|
|
|
|
2021-01-16 19:48:30 +00:00
|
|
|
// SpaceAvailableForDecompression looks through a given archive and determines
|
|
|
|
// if decompressing it would put the server over its allocated disk space limit.
|
|
|
|
func (fs *Filesystem) SpaceAvailableForDecompression(dir string, file string) error {
|
2020-07-16 04:16:08 +00:00
|
|
|
// Don't waste time trying to determine this if we know the server will have the space for
|
|
|
|
// it since there is no limit.
|
2020-09-27 19:24:08 +00:00
|
|
|
if fs.MaxDisk() <= 0 {
|
2021-01-16 19:48:30 +00:00
|
|
|
return nil
|
2020-07-16 04:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
source, err := fs.SafePath(filepath.Join(dir, file))
|
|
|
|
if err != nil {
|
2021-01-16 19:48:30 +00:00
|
|
|
return err
|
2020-07-16 04:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the cached size in a parallel process so that if it is not cached we are not
|
|
|
|
// waiting an unnecessary amount of time on this call.
|
2020-09-01 03:45:51 +00:00
|
|
|
dirSize, err := fs.DiskUsage(false)
|
2020-07-16 04:16:08 +00:00
|
|
|
|
|
|
|
var size int64
|
2020-09-01 03:45:51 +00:00
|
|
|
// Walk over the archive and figure out just how large the final output would be from unarchiving it.
|
2020-09-18 03:37:34 +00:00
|
|
|
err = archiver.Walk(source, func(f archiver.File) error {
|
2020-09-27 19:24:08 +00:00
|
|
|
if atomic.AddInt64(&size, f.Size())+dirSize > fs.MaxDisk() {
|
2020-12-16 04:51:13 +00:00
|
|
|
return &Error{code: ErrCodeDiskSpace}
|
2020-09-18 03:37:34 +00:00
|
|
|
}
|
2020-09-01 03:45:51 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-09-20 18:51:12 +00:00
|
|
|
if err != nil {
|
|
|
|
if strings.HasPrefix(err.Error(), "format ") {
|
2021-01-16 19:48:30 +00:00
|
|
|
return &Error{code: ErrCodeUnknownArchive}
|
2020-09-20 18:51:12 +00:00
|
|
|
}
|
2021-01-16 19:48:30 +00:00
|
|
|
return err
|
2020-09-20 18:51:12 +00:00
|
|
|
}
|
2021-01-16 19:48:30 +00:00
|
|
|
return err
|
2020-07-16 04:16:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 19:48:30 +00:00
|
|
|
// DecompressFile will decompress a file in a given directory by using the
|
|
|
|
// archiver tool to infer the file type and go from there. This will walk over
|
|
|
|
// all of the files within the given archive and ensure that there is not a
|
|
|
|
// zip-slip attack being attempted by validating that the final path is within
|
|
|
|
// the server data directory.
|
2020-07-16 04:16:08 +00:00
|
|
|
func (fs *Filesystem) DecompressFile(dir string, file string) error {
|
|
|
|
source, err := fs.SafePath(filepath.Join(dir, file))
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-07-16 04:16:08 +00:00
|
|
|
}
|
2021-01-16 19:48:30 +00:00
|
|
|
// Ensure that the source archive actually exists on the system.
|
2020-07-31 22:01:32 +00:00
|
|
|
if _, err := os.Stat(source); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-07-31 22:01:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 04:16:08 +00:00
|
|
|
// 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
|
|
|
|
// encounters an error the entire process will be stopped.
|
2020-09-20 18:51:12 +00:00
|
|
|
err = archiver.Walk(source, func(f archiver.File) error {
|
2020-07-16 04:16:08 +00:00
|
|
|
// 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.
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-24 00:18:40 +00:00
|
|
|
var name string
|
|
|
|
switch s := f.Sys().(type) {
|
|
|
|
case *tar.Header:
|
|
|
|
name = s.Name
|
|
|
|
case *gzip.Header:
|
|
|
|
name = s.Name
|
|
|
|
case *zip.FileHeader:
|
|
|
|
name = s.Name
|
|
|
|
default:
|
2021-01-16 19:48:30 +00:00
|
|
|
return &Error{
|
|
|
|
code: ErrCodeUnknownError,
|
|
|
|
resolved: filepath.Join(dir, f.Name()),
|
|
|
|
err: errors.New(fmt.Sprintf("could not parse underlying data source with type: %s", reflect.TypeOf(s).String())),
|
|
|
|
}
|
2020-08-24 00:18:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 00:33:39 +00:00
|
|
|
p := filepath.Join(dir, name)
|
2021-01-11 01:01:41 +00:00
|
|
|
// If it is ignored, just don't do anything with the file and skip over it.
|
2021-01-11 00:33:39 +00:00
|
|
|
if err := fs.IsIgnored(p); err != nil {
|
2021-01-11 01:01:41 +00:00
|
|
|
return nil
|
2020-09-13 02:25:29 +00:00
|
|
|
}
|
2021-01-16 19:48:30 +00:00
|
|
|
if err := fs.Writefile(p, f); err != nil {
|
|
|
|
return &Error{code: ErrCodeUnknownError, err: err, resolved: source}
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-16 04:16:08 +00:00
|
|
|
})
|
2020-09-20 18:51:12 +00:00
|
|
|
if err != nil {
|
|
|
|
if strings.HasPrefix(err.Error(), "format ") {
|
2020-12-16 04:51:13 +00:00
|
|
|
return &Error{code: ErrCodeUnknownArchive}
|
2020-09-20 18:51:12 +00:00
|
|
|
}
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-09-20 18:51:12 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-07-16 04:16:08 +00:00
|
|
|
}
|