refactor: Add automatic indexing for update cmd

This commit is contained in:
Felicitas Pojtinger
2021-12-13 22:20:07 +01:00
parent f523bda5fa
commit 9d1c6fb731
3 changed files with 64 additions and 64 deletions

View File

@@ -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
},
}

View File

@@ -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

View File

@@ -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,
)
}