2020-09-27 19:24:08 +00:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Compresses all of the files matching the given paths in the specified directory. This function
|
|
|
|
// also supports passing nested paths to only compress certain files and folders when working in
|
|
|
|
// a larger directory. This effectively creates a local backup, but rather than ignoring specific
|
|
|
|
// files and folders, it takes an allow-list of files and folders.
|
|
|
|
//
|
|
|
|
// All paths are relative to the dir that is passed in as the first argument, and the compressed
|
|
|
|
// file will be placed at that location named `archive-{date}.tar.gz`.
|
|
|
|
func (fs *Filesystem) CompressFiles(dir string, paths []string) (os.FileInfo, error) {
|
|
|
|
cleanedRootDir, err := fs.SafePath(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take all of the paths passed in and merge them together with the root directory we've gotten.
|
|
|
|
for i, p := range paths {
|
|
|
|
paths[i] = filepath.Join(cleanedRootDir, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleaned, err := fs.ParallelSafePath(paths)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-25 19:52:57 +00:00
|
|
|
a := &Archive{BasePath: cleanedRootDir, Files: cleaned}
|
|
|
|
d := path.Join(
|
|
|
|
cleanedRootDir,
|
|
|
|
fmt.Sprintf("archive-%s.tar.gz", strings.ReplaceAll(time.Now().Format(time.RFC3339), ":", "")),
|
|
|
|
)
|
2020-09-27 19:24:08 +00:00
|
|
|
|
2020-12-25 19:52:57 +00:00
|
|
|
if err := a.Create(d); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-09-27 19:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Stat(d)
|
|
|
|
if err != nil {
|
|
|
|
_ = os.Remove(d)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:17:53 +00:00
|
|
|
if err := fs.HasSpaceFor(f.Size()); err != nil {
|
2020-09-27 19:24:08 +00:00
|
|
|
_ = os.Remove(d)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.addDisk(f.Size())
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|