Skip to content

Commit

Permalink
fix decrompression of archives
Browse files Browse the repository at this point in the history
  • Loading branch information
ostempel committed Dec 3, 2024
1 parent 148ce9b commit e9ccaac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
45 changes: 38 additions & 7 deletions cmd/internal/compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,54 @@ import (

"github.com/metal-stack/backup-restore-sidecar/pkg/constants"
"github.com/mholt/archives"
"github.com/spf13/afero"
)

type Compressor struct {
fs afero.Fs
compressor *archives.CompressedArchive
extension string
}

type CompressorConfig struct {
Method string
FS afero.Fs
}

// New Returns a new Compressor
func New(method string) (*Compressor, error) {
func New(config *CompressorConfig) (*Compressor, error) {

if config.FS == nil {
config.FS = afero.NewOsFs()
}

c := &archives.CompressedArchive{}
switch method {
switch config.Method {
case "tar":
c = &archives.CompressedArchive{
Archival: archives.Tar{},
Archival: archives.Tar{},
Extraction: archives.Tar{},
}
case "targz":
c = &archives.CompressedArchive{
Compression: archives.Gz{},
Extraction: archives.Tar{},
Archival: archives.Tar{},
}
case "tarlz4":
c = &archives.CompressedArchive{
Compression: archives.Lz4{},
Extraction: archives.Tar{},
Archival: archives.Tar{},
}
default:
return nil, fmt.Errorf("unsupported compression method: %s", method)
return nil, fmt.Errorf("unsupported compression method: %s", config.Method)
}
return &Compressor{compressor: c, extension: "." + method}, nil
return &Compressor{
compressor: c,
extension: "." + config.Method,
fs: config.FS,
}, nil
}

// Compress the given backupFile and returns the full filename with the extension
Expand All @@ -57,8 +76,20 @@ func (c *Compressor) Compress(ctx context.Context, backupFilePath string, output
// Decompress the given backupFile
func (c *Compressor) Decompress(ctx context.Context, inputReader io.Reader) error {
err := c.compressor.Extract(ctx, inputReader, func(ctx context.Context, f archives.FileInfo) error {
// do something with the file here; or, if you only want a specific file or directory,
// just return until you come across the desired f.NameInArchive value(s)
// open archive file
file, err := f.Open()
if err != nil {
return err
}
defer file.Close()
// create file in restore directory
outputFile, err := c.fs.Create(constants.RestoreDir + "/" + f.Name())
if err != nil {
return err
}
defer outputFile.Close()
// copy file content
io.Copy(outputFile, file)

Check failure on line 92 in cmd/internal/compress/compress.go

View workflow job for this annotation

GitHub Actions / Integration Test

Error return value of `io.Copy` is not checked (errcheck)
return nil
})
return err
Expand Down
36 changes: 22 additions & 14 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ const (
)

var (
cfgFile string
logger *slog.Logger
db database.Database
bp providers.BackupProvider
encrypter *encryption.Encrypter
stop context.Context
cfgFile string
logger *slog.Logger
db database.Database
bp providers.BackupProvider
encrypter *encryption.Encrypter
compressor *compress.Compressor
stop context.Context
)

var rootCmd = &cobra.Command{
Expand All @@ -135,6 +136,9 @@ var startCmd = &cobra.Command{
if err := initEncrypter(); err != nil {
return err
}
if err := initCompressor(); err != nil {
return err
}
return initBackupProvider()
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -153,12 +157,6 @@ var startCmd = &cobra.Command{

logger.Info("starting backup-restore-sidecar", "version", v.V, "bind-addr", addr)

comp, err := compress.New(viper.GetString(compressionMethod))
logger.Info(comp.Extension())
if err != nil {
return err
}

metrics := metrics.New()
metrics.Start(logger.WithGroup("metrics"))

Expand All @@ -168,11 +166,11 @@ var startCmd = &cobra.Command{
DatabaseProber: db,
BackupProvider: bp,
Metrics: metrics,
Compressor: comp,
Compressor: compressor,
Encrypter: encrypter,
})

if err := initializer.New(logger.WithGroup("initializer"), addr, db, bp, comp, metrics, viper.GetString(databaseDatadirFlg), encrypter).Start(stop, backuper); err != nil {
if err := initializer.New(logger.WithGroup("initializer"), addr, db, bp, compressor, metrics, viper.GetString(databaseDatadirFlg), encrypter).Start(stop, backuper); err != nil {
return err
}

Expand Down Expand Up @@ -554,6 +552,16 @@ func initDatabase() error {
return nil
}

func initCompressor() error {
var err error
method := viper.GetString(compressionMethod)
compressor, err = compress.New(&compress.CompressorConfig{Method: method})
if err != nil {
return fmt.Errorf("unable to initialize compressor: %w", err)
}
return nil
}

func initEncrypter() error {
var err error
key := viper.GetString(encryptionKeyFlg)
Expand Down

0 comments on commit e9ccaac

Please sign in to comment.