feat: Allow seeking to specific location in tape before starting count in list command

This commit is contained in:
Felicitas Pojtinger
2021-11-19 16:41:07 +01:00
parent 8e822c39f8
commit be65e19d89
3 changed files with 24 additions and 59 deletions

View File

@@ -19,7 +19,7 @@ import (
const (
tapeFlag = "tape"
recordSizeFlag = "recordSize"
recordSizeFlag = "record-size"
srcFlag = "src"
overwriteFlag = "overwrite"
)

View File

@@ -42,10 +42,15 @@ var listCmd = &cobra.Command{
defer f.Close()
if fileDescription.Mode().IsRegular() {
// Seek to record and block
if _, err := f.Seek(int64((viper.GetInt(recordSizeFlag)*controllers.BlockSize*viper.GetInt(recordFlag))+viper.GetInt(blockFlag)*controllers.BlockSize), 0); err != nil {
return err
}
tr := tar.NewReader(f)
record := int64(0)
block := int64(0)
record := viper.GetInt64(recordFlag)
block := viper.GetInt64(blockFlag)
firstRecordOfArchive := int64(0)
for {
@@ -106,15 +111,24 @@ var listCmd = &cobra.Command{
}
}
} else {
// Seek to record
if err := controllers.SeekToRecordOnTape(f, int32(viper.GetInt(recordFlag))); err != nil {
return err
}
// Seek to block
br := bufio.NewReaderSize(f, controllers.BlockSize*viper.GetInt(recordSizeFlag))
if _, err := br.Read(make([]byte, viper.GetInt(blockFlag)*controllers.BlockSize)); err != nil {
return err
}
counter := &readers.Counter{Reader: br}
lastBytesRead := 0
record := viper.GetInt64(recordFlag)
block := viper.GetInt64(blockFlag)
lastBytesRead := (viper.GetInt(recordSizeFlag) * controllers.BlockSize * viper.GetInt(recordFlag)) + (viper.GetInt(blockFlag) * controllers.BlockSize)
counter := &readers.Counter{Reader: br, BytesRead: lastBytesRead}
dirty := false
record := int64(0)
block := int64(0)
for {
tr := tar.NewReader(counter)
hdr, err := tr.Next()
@@ -180,6 +194,8 @@ var listCmd = &cobra.Command{
func init() {
listCmd.PersistentFlags().StringP(tapeFlag, "t", "/dev/nst0", "Tape or tar file to read from")
listCmd.PersistentFlags().IntP(recordSizeFlag, "e", 20, "Amount of 512-bit blocks per record")
listCmd.PersistentFlags().IntP(recordFlag, "r", 0, "Record to seek too before counting")
listCmd.PersistentFlags().IntP(blockFlag, "b", 0, "Block in record to seek too before counting")
viper.AutomaticEnv()

View File

@@ -1,51 +0,0 @@
package cmd
import (
"bufio"
"os"
"github.com/pojntfx/stfs/pkg/controllers"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var seekCmd = &cobra.Command{
Use: "seek",
Aliases: []string{"s"},
Short: "Seek to a record and block (tape only)",
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
f, err := os.OpenFile(viper.GetString(tapeFlag), os.O_RDONLY, os.ModeCharDevice)
if err != nil {
return err
}
defer f.Close()
// Seek to record
if err := controllers.SeekToRecordOnTape(f, int32(viper.GetInt(recordFlag))); err != nil {
return err
}
// Seek to block
br := bufio.NewReaderSize(f, controllers.BlockSize*viper.GetInt(recordSizeFlag))
if _, err := br.Read(make([]byte, viper.GetInt(blockFlag)*controllers.BlockSize)); err != nil {
return err
}
return nil
},
}
func init() {
seekCmd.PersistentFlags().StringP(tapeFlag, "t", "/dev/nst0", "Tape drive to seek on")
seekCmd.PersistentFlags().IntP(recordSizeFlag, "e", 20, "Amount of 512-bit blocks per record")
seekCmd.PersistentFlags().IntP(recordFlag, "r", 0, "Record to seek too")
seekCmd.PersistentFlags().IntP(blockFlag, "b", 0, "Block in record to seek too")
viper.AutomaticEnv()
rootCmd.AddCommand(seekCmd)
}