From 9d1c6fb731645c1de6ec509f22fd7a29e8b237eb Mon Sep 17 00:00:00 2001 From: Felicitas Pojtinger Date: Mon, 13 Dec 2021 22:20:07 +0100 Subject: [PATCH] refactor: Add automatic indexing for update cmd --- cmd/stbak/cmd/update.go | 69 +++++++-------------------------------- pkg/operations/archive.go | 2 +- pkg/operations/update.go | 57 +++++++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 64 deletions(-) diff --git a/cmd/stbak/cmd/update.go b/cmd/stbak/cmd/update.go index 9afc4f2..52f8c10 100644 --- a/cmd/stbak/cmd/update.go +++ b/cmd/stbak/cmd/update.go @@ -1,17 +1,13 @@ package cmd import ( - "archive/tar" - "context" "fmt" "github.com/pojntfx/stfs/internal/compression" "github.com/pojntfx/stfs/internal/keys" "github.com/pojntfx/stfs/internal/logging" - "github.com/pojntfx/stfs/internal/persisters" "github.com/pojntfx/stfs/pkg/config" "github.com/pojntfx/stfs/pkg/operations" - "github.com/pojntfx/stfs/pkg/recovery" "github.com/pojntfx/stfs/pkg/tape" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -37,16 +33,6 @@ var updateCmd = &cobra.Command{ return keys.CheckKeyAccessible(viper.GetString(signatureFlag), viper.GetString(identityFlag)) }, RunE: func(cmd *cobra.Command, args []string) error { - metadataPersister := persisters.NewMetadataPersister(viper.GetString(metadataFlag)) - if err := metadataPersister.Open(); err != nil { - return err - } - - lastIndexedRecord, lastIndexedBlock, err := metadataPersister.GetLastIndexedRecordAndBlock(context.Background(), viper.GetInt(recordSizeFlag)) - if err != nil { - return err - } - pubkey, err := keys.ReadKey(viper.GetString(encryptionFlag), viper.GetString(recipientFlag)) if err != nil { return err @@ -84,39 +70,16 @@ var updateCmd = &cobra.Command{ } defer reader.Close() - hdrs, err := operations.Update( + if _, err := operations.Update( config.DriveWriterConfig{ Drive: writer, DriveIsRegular: writerIsRegular, }, - config.PipeConfig{ - Compression: viper.GetString(compressionFlag), - Encryption: viper.GetString(encryptionFlag), - Signature: viper.GetString(signatureFlag), - }, - config.CryptoConfig{ - Recipient: recipient, - Identity: identity, - Password: viper.GetString(passwordFlag), - }, - - viper.GetInt(recordSizeFlag), - viper.GetString(fromFlag), - viper.GetBool(overwriteFlag), - viper.GetString(compressionLevelFlag), - - logging.NewLogger().PrintHeader, - ) - if err != nil { - return err - } - - return recovery.Index( - config.DriveReaderConfig{ + config.DriveConfig{ Drive: reader, DriveIsRegular: readerIsRegular, }, - config.DriveConfig{ + config.DriveReaderConfig{ Drive: reader, DriveIsRegular: readerIsRegular, }, @@ -135,26 +98,16 @@ var updateCmd = &cobra.Command{ }, viper.GetInt(recordSizeFlag), - int(lastIndexedRecord), - int(lastIndexedBlock), - false, - 1, // Ignore the first header, which is the last header which we already indexed - - func(hdr *tar.Header, i int) error { - if len(hdrs) <= i { - return config.ErrTarHeaderMissing - } - - *hdr = *hdrs[i] - - return nil - }, - func(hdr *tar.Header, isRegular bool) error { - return nil // We sign above, no need to verify - }, + viper.GetString(fromFlag), + viper.GetBool(overwriteFlag), + viper.GetString(compressionLevelFlag), logging.NewLogger().PrintHeader, - ) + ); err != nil { + return err + } + + return nil }, } diff --git a/pkg/operations/archive.go b/pkg/operations/archive.go index c0ebeda..d72f7ec 100644 --- a/pkg/operations/archive.go +++ b/pkg/operations/archive.go @@ -60,7 +60,7 @@ func Archive( lastIndexedRecord := int64(0) lastIndexedBlock := int64(0) - if overwrite { + if !overwrite { lastIndexedRecord, lastIndexedBlock, err = metadataPersister.GetLastIndexedRecordAndBlock(context.Background(), recordSize) if err != nil { return []*tar.Header{}, err diff --git a/pkg/operations/update.go b/pkg/operations/update.go index 7b40264..d1a9479 100644 --- a/pkg/operations/update.go +++ b/pkg/operations/update.go @@ -2,6 +2,7 @@ package operations import ( "archive/tar" + "context" "io" "io/fs" "os" @@ -14,16 +15,21 @@ import ( "github.com/pojntfx/stfs/internal/encryption" "github.com/pojntfx/stfs/internal/ioext" "github.com/pojntfx/stfs/internal/mtio" + "github.com/pojntfx/stfs/internal/persisters" "github.com/pojntfx/stfs/internal/records" "github.com/pojntfx/stfs/internal/signature" "github.com/pojntfx/stfs/internal/statext" "github.com/pojntfx/stfs/internal/suffix" "github.com/pojntfx/stfs/internal/tarext" "github.com/pojntfx/stfs/pkg/config" + "github.com/pojntfx/stfs/pkg/recovery" ) func Update( writer config.DriveWriterConfig, + drive config.DriveConfig, + reader config.DriveReaderConfig, + metadata config.MetadataConfig, pipes config.PipeConfig, crypto config.CryptoConfig, @@ -41,8 +47,18 @@ func Update( } defer cleanup(&dirty) - headers := []*tar.Header{} - return headers, filepath.Walk(from, func(path string, info fs.FileInfo, err error) error { + metadataPersister := persisters.NewMetadataPersister(metadata.Metadata) + if err := metadataPersister.Open(); err != nil { + return []*tar.Header{}, err + } + + lastIndexedRecord, lastIndexedBlock, err := metadataPersister.GetLastIndexedRecordAndBlock(context.Background(), recordSize) + if err != nil { + return []*tar.Header{}, err + } + + hdrs := []*tar.Header{} + if err := filepath.Walk(from, func(path string, info fs.FileInfo, err error) error { if err != nil { return err } @@ -163,7 +179,7 @@ func Update( } hdrToAppend := *hdr - headers = append(headers, &hdrToAppend) + hdrs = append(hdrs, &hdrToAppend) if err := signature.SignHeader(hdr, writer.DriveIsRegular, pipes.Signature, crypto.Identity); err != nil { return err @@ -242,7 +258,7 @@ func Update( } hdrToAppend := *hdr - headers = append(headers, &hdrToAppend) + hdrs = append(hdrs, &hdrToAppend) if err := signature.SignHeader(hdr, writer.DriveIsRegular, pipes.Signature, crypto.Identity); err != nil { return err @@ -260,5 +276,36 @@ func Update( dirty = true return nil - }) + }); err != nil { + return []*tar.Header{}, err + } + + return hdrs, recovery.Index( + reader, + drive, + metadata, + pipes, + crypto, + + recordSize, + int(lastIndexedRecord), + int(lastIndexedBlock), + false, + 1, // Ignore the first header, which is the last header which we already indexed + + func(hdr *tar.Header, i int) error { + if len(hdrs) <= i { + return config.ErrTarHeaderMissing + } + + *hdr = *hdrs[i] + + return nil + }, + func(hdr *tar.Header, isRegular bool) error { + return nil // We sign above, no need to verify + }, + + onHeader, + ) }