diff --git a/cmd/stbak/cmd/archive.go b/cmd/stbak/cmd/archive.go index ee38491..68a8195 100644 --- a/cmd/stbak/cmd/archive.go +++ b/cmd/stbak/cmd/archive.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" @@ -47,23 +43,6 @@ var archiveCmd = &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 := int64(0) - lastIndexedBlock := int64(0) - if !viper.GetBool(overwriteFlag) { - r, b, err := metadataPersister.GetLastIndexedRecordAndBlock(context.Background(), viper.GetInt(recordSizeFlag)) - if err != nil { - return err - } - - lastIndexedRecord = r - lastIndexedBlock = b - } - pubkey, err := keys.ReadKey(viper.GetString(encryptionFlag), viper.GetString(recipientFlag)) if err != nil { return err @@ -101,43 +80,16 @@ var archiveCmd = &cobra.Command{ } defer reader.Close() - hdrs, err := operations.Archive( + if _, err := operations.Archive( 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.GetString(compressionLevelFlag), - - logging.NewLogger().PrintHeader, - ) - if err != nil { - return err - } - - index := 1 // Ignore the first header, which is the last header which we already indexed - if viper.GetBool(overwriteFlag) { - index = 0 // If we are starting fresh, index from start - } - - return recovery.Index( - config.DriveReaderConfig{ + config.DriveConfig{ Drive: reader, DriveIsRegular: readerIsRegular, }, - config.DriveConfig{ + config.DriveReaderConfig{ Drive: reader, DriveIsRegular: readerIsRegular, }, @@ -156,26 +108,16 @@ var archiveCmd = &cobra.Command{ }, viper.GetInt(recordSizeFlag), - int(lastIndexedRecord), - int(lastIndexedBlock), + viper.GetString(fromFlag), + viper.GetString(compressionLevelFlag), viper.GetBool(overwriteFlag), - index, - - 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 - }, logging.NewLogger().PrintHeader, - ) + ); err != nil { + return err + } + + return nil }, } diff --git a/pkg/operations/archive.go b/pkg/operations/archive.go index 62c684d..c0ebeda 100644 --- a/pkg/operations/archive.go +++ b/pkg/operations/archive.go @@ -2,6 +2,7 @@ package operations import ( "archive/tar" + "context" "errors" "io" "io/fs" @@ -16,12 +17,14 @@ 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" ) var ( @@ -30,12 +33,16 @@ var ( func Archive( writer config.DriveWriterConfig, + drive config.DriveConfig, + reader config.DriveReaderConfig, + metadata config.MetadataConfig, pipes config.PipeConfig, crypto config.CryptoConfig, recordSize int, from string, compressionLevel string, + overwrite bool, onHeader func(hdr *models.Header), ) ([]*tar.Header, error) { @@ -46,8 +53,22 @@ func Archive( } 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 := int64(0) + lastIndexedBlock := int64(0) + if overwrite { + 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 } @@ -165,7 +186,7 @@ func Archive( } 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 @@ -235,5 +256,41 @@ func Archive( } return nil - }) + }); err != nil { + return []*tar.Header{}, err + } + + index := 1 // Ignore the first header, which is the last header which we already indexed + if overwrite { + index = 0 // If we are starting fresh, index from start + } + + return hdrs, recovery.Index( + reader, + drive, + metadata, + pipes, + crypto, + + recordSize, + int(lastIndexedRecord), + int(lastIndexedBlock), + overwrite, + index, + + 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, + ) }