refactor: Use explicit reader/writer instead of filepath for drive

This commit is contained in:
Felix Pojtinger
2021-12-13 00:26:30 +01:00
parent 734df25820
commit 58b407cd4f
34 changed files with 506 additions and 340 deletions
+37 -8
View File
@@ -12,6 +12,7 @@ import (
"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"
)
@@ -30,7 +31,7 @@ var archiveCmd = &cobra.Command{
Use: "archive",
Aliases: []string{"arc", "a", "c"},
Short: "Archive a file or directory to tape or tar file",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
@@ -83,10 +84,27 @@ var archiveCmd = &cobra.Command{
return err
}
writer, writerIsRegular, err := tape.OpenTapeWriteOnly(
viper.GetString(driveFlag),
viper.GetInt(recordSizeFlag),
viper.GetBool(overwriteFlag),
)
if err != nil {
return nil
}
defer writer.Close()
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
hdrs, err := operations.Archive(
config.StateConfig{
Drive: viper.GetString(driveFlag),
Metadata: viper.GetString(metadataFlag),
config.DriveWriterConfig{
Drive: writer,
DriveIsRegular: writerIsRegular,
},
config.PipeConfig{
Compression: viper.GetString(compressionFlag),
@@ -101,7 +119,6 @@ var archiveCmd = &cobra.Command{
viper.GetInt(recordSizeFlag),
viper.GetString(fromFlag),
viper.GetBool(overwriteFlag),
viper.GetString(compressionLevelFlag),
logging.NewLogger().PrintHeader,
@@ -110,9 +127,21 @@ var archiveCmd = &cobra.Command{
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.StateConfig{
Drive: viper.GetString(driveFlag),
config.DriveReaderConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.MetadataConfig{
Metadata: viper.GetString(metadataFlag),
},
config.PipeConfig{
@@ -130,7 +159,7 @@ var archiveCmd = &cobra.Command{
int(lastIndexedRecord),
int(lastIndexedBlock),
viper.GetBool(overwriteFlag),
1, // Ignore the first header, which is the last header which we already indexed
index,
func(hdr *tar.Header, i int) error {
if len(hdrs) <= i {
+32 -3
View File
@@ -5,6 +5,7 @@ import (
"github.com/pojntfx/stfs/internal/logging"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/operations"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -17,7 +18,7 @@ var deleteCmd = &cobra.Command{
Use: "delete",
Aliases: []string{"del", "d", "rm"},
Short: "Delete a file or directory from tape or tar file",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
@@ -49,9 +50,37 @@ var deleteCmd = &cobra.Command{
return err
}
writer, writerIsRegular, err := tape.OpenTapeWriteOnly(
viper.GetString(driveFlag),
viper.GetInt(recordSizeFlag),
false,
)
if err != nil {
return nil
}
defer writer.Close()
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
return operations.Delete(
config.StateConfig{
Drive: viper.GetString(driveFlag),
config.DriveWriterConfig{
Drive: writer,
DriveIsRegular: writerIsRegular,
},
config.DriveReaderConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.MetadataConfig{
Metadata: viper.GetString(metadataFlag),
},
config.PipeConfig{
+13 -2
View File
@@ -1,7 +1,9 @@
package cmd
import (
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/hardware"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -14,9 +16,18 @@ var driveEjectCmd = &cobra.Command{
return err
}
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
return hardware.Eject(
hardware.DriveConfig{
Drive: viper.GetString(driveFlag),
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
)
},
+13 -2
View File
@@ -3,7 +3,9 @@ package cmd
import (
"fmt"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/hardware"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -16,9 +18,18 @@ var driveTellCmd = &cobra.Command{
return err
}
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
currentRecord, err := hardware.Tell(
hardware.DriveConfig{
Drive: viper.GetString(driveFlag),
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
)
if err != nil {
+2 -1
View File
@@ -2,6 +2,7 @@ package cmd
import (
"github.com/pojntfx/stfs/internal/logging"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/inventory"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -21,7 +22,7 @@ var findCmd = &cobra.Command{
}
if _, err := inventory.Find(
inventory.MetadataConfig{
config.MetadataConfig{
Metadata: viper.GetString(metadataFlag),
},
+1 -1
View File
@@ -26,7 +26,7 @@ var keygenCmd = &cobra.Command{
Encryption: viper.GetString(encryptionFlag),
Signature: viper.GetString(signatureFlag),
},
utility.PasswordConfig{
config.PasswordConfig{
Password: viper.GetString(passwordFlag),
},
)
+2 -1
View File
@@ -2,6 +2,7 @@ package cmd
import (
"github.com/pojntfx/stfs/internal/logging"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/inventory"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -17,7 +18,7 @@ var listCmd = &cobra.Command{
}
if _, err := inventory.List(
inventory.MetadataConfig{
config.MetadataConfig{
Metadata: viper.GetString(metadataFlag),
},
+32 -3
View File
@@ -5,6 +5,7 @@ import (
"github.com/pojntfx/stfs/internal/logging"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/operations"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -13,7 +14,7 @@ var moveCmd = &cobra.Command{
Use: "move",
Aliases: []string{"mov", "m", "mv"},
Short: "Move a file or directory on tape or tar file",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
@@ -45,9 +46,37 @@ var moveCmd = &cobra.Command{
return err
}
writer, writerIsRegular, err := tape.OpenTapeWriteOnly(
viper.GetString(driveFlag),
viper.GetInt(recordSizeFlag),
false,
)
if err != nil {
return nil
}
defer writer.Close()
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
return operations.Move(
config.StateConfig{
Drive: viper.GetString(driveFlag),
config.DriveWriterConfig{
Drive: writer,
DriveIsRegular: writerIsRegular,
},
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.DriveReaderConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.MetadataConfig{
Metadata: viper.GetString(metadataFlag),
},
config.PipeConfig{
+17 -4
View File
@@ -4,8 +4,8 @@ import (
"github.com/pojntfx/stfs/internal/keys"
"github.com/pojntfx/stfs/internal/logging"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/hardware"
"github.com/pojntfx/stfs/pkg/recovery"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -20,7 +20,7 @@ const (
var recoveryFetchCmd = &cobra.Command{
Use: "fetch",
Short: "Fetch a file or directory from tape or tar file by record and block without the index",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
@@ -52,9 +52,22 @@ var recoveryFetchCmd = &cobra.Command{
return err
}
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
return recovery.Fetch(
hardware.DriveConfig{
Drive: viper.GetString(driveFlag),
config.DriveReaderConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.PipeConfig{
Compression: viper.GetString(compressionFlag),
+19 -3
View File
@@ -9,6 +9,7 @@ import (
"github.com/pojntfx/stfs/internal/signature"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/recovery"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -16,7 +17,7 @@ import (
var recoveryIndexCmd = &cobra.Command{
Use: "index",
Short: "Index contents of tape or tar file",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
@@ -48,9 +49,24 @@ var recoveryIndexCmd = &cobra.Command{
return err
}
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
return recovery.Index(
config.StateConfig{
Drive: viper.GetString(driveFlag),
config.DriveReaderConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.MetadataConfig{
Metadata: viper.GetString(metadataFlag),
},
config.PipeConfig{
+13 -4
View File
@@ -4,8 +4,8 @@ import (
"github.com/pojntfx/stfs/internal/keys"
"github.com/pojntfx/stfs/internal/logging"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/hardware"
"github.com/pojntfx/stfs/pkg/recovery"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -13,7 +13,7 @@ import (
var recoveryQueryCmd = &cobra.Command{
Use: "query",
Short: "Query contents of tape or tar file without the index",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
@@ -45,9 +45,18 @@ var recoveryQueryCmd = &cobra.Command{
return err
}
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
if _, err := recovery.Query(
hardware.DriveConfig{
Drive: viper.GetString(driveFlag),
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.PipeConfig{
Compression: viper.GetString(compressionFlag),
+19 -3
View File
@@ -5,6 +5,7 @@ import (
"github.com/pojntfx/stfs/internal/logging"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/operations"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -17,7 +18,7 @@ var restoreCmd = &cobra.Command{
Use: "restore",
Aliases: []string{"res", "r", "x"},
Short: "Restore a file or directory from tape or tar file",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
@@ -49,9 +50,24 @@ var restoreCmd = &cobra.Command{
return err
}
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
return operations.Restore(
config.StateConfig{
Drive: viper.GetString(driveFlag),
config.DriveReaderConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.MetadataConfig{
Metadata: viper.GetString(metadataFlag),
},
config.PipeConfig{
+31 -6
View File
@@ -12,6 +12,7 @@ import (
"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"
)
@@ -20,7 +21,7 @@ var updateCmd = &cobra.Command{
Use: "update",
Aliases: []string{"upd", "u"},
Short: "Update a file or directory's content and metadata on tape or tar file",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
@@ -66,10 +67,27 @@ var updateCmd = &cobra.Command{
return err
}
writer, writerIsRegular, err := tape.OpenTapeWriteOnly(
viper.GetString(driveFlag),
viper.GetInt(recordSizeFlag),
false,
)
if err != nil {
return nil
}
defer writer.Close()
reader, readerIsRegular, err := tape.OpenTapeReadOnly(
viper.GetString(driveFlag),
)
if err != nil {
return nil
}
defer reader.Close()
hdrs, err := operations.Update(
config.StateConfig{
Drive: viper.GetString(driveFlag),
Metadata: viper.GetString(metadataFlag),
config.DriveWriterConfig{
Drive: writer,
DriveIsRegular: writerIsRegular,
},
config.PipeConfig{
Compression: viper.GetString(compressionFlag),
@@ -94,8 +112,15 @@ var updateCmd = &cobra.Command{
}
return recovery.Index(
config.StateConfig{
Drive: viper.GetString(driveFlag),
config.DriveReaderConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.MetadataConfig{
Metadata: viper.GetString(metadataFlag),
},
config.PipeConfig{