From 93adb5da13a91aefdacfe8b05f63132d011de299 Mon Sep 17 00:00:00 2001 From: Felicitas Pojtinger Date: Sat, 20 Nov 2021 16:57:11 +0100 Subject: [PATCH] fix: Enable seeking through filemarks (`tar ivf`) for tar backend --- cmd/stbak/cmd/list.go | 32 ++++++++++++++++---------------- cmd/stcache/cmd/index.go | 32 ++++++++++++++++---------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/cmd/stbak/cmd/list.go b/cmd/stbak/cmd/list.go index 55f1721..4e52249 100644 --- a/cmd/stbak/cmd/list.go +++ b/cmd/stbak/cmd/list.go @@ -51,13 +51,26 @@ var listCmd = &cobra.Command{ record := viper.GetInt64(recordFlag) block := viper.GetInt64(blockFlag) - firstRecordOfArchive := int64(0) for { hdr, err := tr.Next() if err != nil { // Seek right after the next two blocks to skip the trailer - if _, err := f.Seek((int64(viper.GetInt(recordSizeFlag))*controllers.BlockSize*record)+(block+1)*controllers.BlockSize, io.SeekStart); err == nil { + if _, err := f.Seek((controllers.BlockSize * 2), io.SeekCurrent); err == nil { + curr, err := f.Seek(0, io.SeekCurrent) + if err != nil { + return err + } + + nextTotalBlocks := curr / controllers.BlockSize + record = nextTotalBlocks / int64(viper.GetInt(recordSizeFlag)) + block = nextTotalBlocks - (record * int64(viper.GetInt(recordSizeFlag))) + + if block > int64(viper.GetInt(recordSizeFlag)) { + record++ + block = 0 + } + tr = tar.NewReader(f) hdr, err = tr.Next() @@ -68,14 +81,6 @@ var listCmd = &cobra.Command{ return err } - - block++ - if block > int64(viper.GetInt(recordSizeFlag)) { - record++ - block = 0 - } - - firstRecordOfArchive = record } else { return err } @@ -98,12 +103,7 @@ var listCmd = &cobra.Command{ nextTotalBlocks := (curr + hdr.Size) / controllers.BlockSize record = nextTotalBlocks / int64(viper.GetInt(recordSizeFlag)) - - if record == 0 && block == 0 || record == firstRecordOfArchive { - block = nextTotalBlocks - (record * int64(viper.GetInt(recordSizeFlag))) // For the first record of the file or archive, the offset of one is not needed - } else { - block = nextTotalBlocks - (record * int64(viper.GetInt(recordSizeFlag))) + 1 // +1 because we need to start reading right after the last block - } + block = nextTotalBlocks - (record * int64(viper.GetInt(recordSizeFlag))) if block > int64(viper.GetInt(recordSizeFlag)) { record++ diff --git a/cmd/stcache/cmd/index.go b/cmd/stcache/cmd/index.go index 91945b1..b91e758 100644 --- a/cmd/stcache/cmd/index.go +++ b/cmd/stcache/cmd/index.go @@ -59,13 +59,26 @@ var indexCmd = &cobra.Command{ record := viper.GetInt64(recordFlag) block := viper.GetInt64(blockFlag) - firstRecordOfArchive := int64(0) for { hdr, err := tr.Next() if err != nil { // Seek right after the next two blocks to skip the trailer - if _, err := f.Seek((int64(viper.GetInt(recordSizeFlag))*controllers.BlockSize*record)+(block+1)*controllers.BlockSize, io.SeekStart); err == nil { + if _, err := f.Seek((controllers.BlockSize * 2), io.SeekCurrent); err == nil { + curr, err := f.Seek(0, io.SeekCurrent) + if err != nil { + return err + } + + nextTotalBlocks := curr / controllers.BlockSize + record = nextTotalBlocks / int64(viper.GetInt(recordSizeFlag)) + block = nextTotalBlocks - (record * int64(viper.GetInt(recordSizeFlag))) + + if block > int64(viper.GetInt(recordSizeFlag)) { + record++ + block = 0 + } + tr = tar.NewReader(f) hdr, err = tr.Next() @@ -76,14 +89,6 @@ var indexCmd = &cobra.Command{ return err } - - block++ - if block > int64(viper.GetInt(recordSizeFlag)) { - record++ - block = 0 - } - - firstRecordOfArchive = record } else { return err } @@ -106,12 +111,7 @@ var indexCmd = &cobra.Command{ nextTotalBlocks := (curr + hdr.Size) / controllers.BlockSize record = nextTotalBlocks / int64(viper.GetInt(recordSizeFlag)) - - if record == 0 && block == 0 || record == firstRecordOfArchive { - block = nextTotalBlocks - (record * int64(viper.GetInt(recordSizeFlag))) // For the first record of the file or archive, the offset of one is not needed - } else { - block = nextTotalBlocks - (record * int64(viper.GetInt(recordSizeFlag))) + 1 // +1 because we need to start reading right after the last block - } + block = nextTotalBlocks - (record * int64(viper.GetInt(recordSizeFlag))) if block > int64(viper.GetInt(recordSizeFlag)) { record++