diff --git a/cmd/restic/cmd_stats.go b/cmd/restic/cmd_stats.go index f0b9fbd75..ace2bb9f2 100644 --- a/cmd/restic/cmd_stats.go +++ b/cmd/restic/cmd_stats.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/restic/chunker" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/data" "github.com/restic/restic/internal/global" "github.com/restic/restic/internal/repository" @@ -162,10 +161,10 @@ func runStats(ctx context.Context, opts StatsOptions, gopts global.Options, args } stats.TotalSize += uint64(pbs[0].CiphertextLength()) if repo.Config().Version >= 2 { - stats.TotalUncompressedSize += uint64(crypto.CiphertextLength(int(pbs[0].PlaintextLength()))) + stats.TotalUncompressedSize += uint64(pbs[0].UncompressedCiphertextLength()) if pbs[0].IsCompressed() { stats.TotalCompressedBlobsSize += uint64(pbs[0].CiphertextLength()) - stats.TotalCompressedBlobsUncompressedSize += uint64(crypto.CiphertextLength(int(pbs[0].PlaintextLength()))) + stats.TotalCompressedBlobsUncompressedSize += uint64(pbs[0].UncompressedCiphertextLength()) } } stats.TotalBlobCount++ diff --git a/internal/backend/cache/file.go b/internal/backend/cache/file.go index 71613bfbf..38497ab76 100644 --- a/internal/backend/cache/file.go +++ b/internal/backend/cache/file.go @@ -10,7 +10,6 @@ import ( "github.com/pkg/errors" "github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend/util" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/debug" ) @@ -54,11 +53,6 @@ func (c *Cache) load(h backend.Handle, length int, offset int64) (io.ReadCloser, } size := fi.Size() - if size <= int64(crypto.CiphertextLength(0)) { - _ = f.Close() - return nil, true, errors.Errorf("cached file %v is truncated", h) - } - if size < offset+int64(length) { _ = f.Close() return nil, true, errors.Errorf("cached file %v is too short", h) @@ -101,20 +95,13 @@ func (c *Cache) save(h backend.Handle, rd io.Reader) error { return err } - n, err := io.Copy(f, rd) + _, err = io.Copy(f, rd) if err != nil { _ = f.Close() _ = os.Remove(f.Name()) return errors.Wrap(err, "Copy") } - if n <= int64(crypto.CiphertextLength(0)) { - _ = f.Close() - _ = os.Remove(f.Name()) - debug.Log("trying to cache truncated file %v, removing", h) - return nil - } - // Close, then rename. Windows doesn't like the reverse order. if err = f.Close(); err != nil { _ = os.Remove(f.Name()) diff --git a/internal/crypto/buffer.go b/internal/repository/crypto/buffer.go similarity index 100% rename from internal/crypto/buffer.go rename to internal/repository/crypto/buffer.go diff --git a/internal/crypto/crypto.go b/internal/repository/crypto/crypto.go similarity index 100% rename from internal/crypto/crypto.go rename to internal/repository/crypto/crypto.go diff --git a/internal/crypto/crypto_int_test.go b/internal/repository/crypto/crypto_int_test.go similarity index 100% rename from internal/crypto/crypto_int_test.go rename to internal/repository/crypto/crypto_int_test.go diff --git a/internal/crypto/crypto_test.go b/internal/repository/crypto/crypto_test.go similarity index 99% rename from internal/crypto/crypto_test.go rename to internal/repository/crypto/crypto_test.go index 47debe896..2959e49ff 100644 --- a/internal/crypto/crypto_test.go +++ b/internal/repository/crypto/crypto_test.go @@ -6,7 +6,7 @@ import ( "io" "testing" - "github.com/restic/restic/internal/crypto" + "github.com/restic/restic/internal/repository/crypto" rtest "github.com/restic/restic/internal/test" "github.com/restic/chunker" diff --git a/internal/crypto/doc.go b/internal/repository/crypto/doc.go similarity index 100% rename from internal/crypto/doc.go rename to internal/repository/crypto/doc.go diff --git a/internal/crypto/kdf.go b/internal/repository/crypto/kdf.go similarity index 100% rename from internal/crypto/kdf.go rename to internal/repository/crypto/kdf.go diff --git a/internal/crypto/kdf_test.go b/internal/repository/crypto/kdf_test.go similarity index 100% rename from internal/crypto/kdf_test.go rename to internal/repository/crypto/kdf_test.go diff --git a/internal/repository/debug.go b/internal/repository/debug.go index dc0a81ce2..4f422409c 100644 --- a/internal/repository/debug.go +++ b/internal/repository/debug.go @@ -17,7 +17,7 @@ import ( "github.com/klauspost/compress/zstd" "golang.org/x/sync/errgroup" - "github.com/restic/restic/internal/crypto" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/repository/index" "github.com/restic/restic/internal/repository/pack" "github.com/restic/restic/internal/restic" diff --git a/internal/repository/index/associated_data_test.go b/internal/repository/index/associated_data_test.go index 413f60990..143d3d20a 100644 --- a/internal/repository/index/associated_data_test.go +++ b/internal/repository/index/associated_data_test.go @@ -5,7 +5,7 @@ import ( "slices" "testing" - "github.com/restic/restic/internal/crypto" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/repository/pack" "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/test" diff --git a/internal/repository/index/index.go b/internal/repository/index/index.go index 9d205ba4b..bbdad92d5 100644 --- a/internal/repository/index/index.go +++ b/internal/repository/index/index.go @@ -13,8 +13,8 @@ import ( "sync" "time" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/errors" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/repository/pack" "github.com/restic/restic/internal/restic" diff --git a/internal/repository/index/master_index_test.go b/internal/repository/index/master_index_test.go index ed320cc3b..e8f04c77a 100644 --- a/internal/repository/index/master_index_test.go +++ b/internal/repository/index/master_index_test.go @@ -10,9 +10,9 @@ import ( "github.com/google/go-cmp/cmp" "github.com/restic/restic/internal/checker" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/data" "github.com/restic/restic/internal/repository" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/repository/index" "github.com/restic/restic/internal/repository/pack" "github.com/restic/restic/internal/restic" diff --git a/internal/repository/key.go b/internal/repository/key.go index 374c984fb..0f2db3e61 100644 --- a/internal/repository/key.go +++ b/internal/repository/key.go @@ -12,8 +12,8 @@ import ( "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/backend" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/debug" + "github.com/restic/restic/internal/repository/crypto" ) var ( diff --git a/internal/repository/pack/blob.go b/internal/repository/pack/blob.go index 5ec8e6680..46b51e9ee 100644 --- a/internal/repository/pack/blob.go +++ b/internal/repository/pack/blob.go @@ -3,7 +3,7 @@ package pack import ( "fmt" - "github.com/restic/restic/internal/crypto" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/restic" ) @@ -27,6 +27,10 @@ func (b Blob) DataLength() uint { return uint(crypto.PlaintextLength(int(b.Length))) } +func (b Blob) UncompressedCiphertextLength() uint { + return uint(crypto.CiphertextLength(int(b.DataLength()))) +} + func (b Blob) IsCompressed() bool { return b.UncompressedLength != 0 } diff --git a/internal/repository/pack/pack.go b/internal/repository/pack/pack.go index b23043e7f..43254b80b 100644 --- a/internal/repository/pack/pack.go +++ b/internal/repository/pack/pack.go @@ -12,7 +12,7 @@ import ( "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/restic" - "github.com/restic/restic/internal/crypto" + "github.com/restic/restic/internal/repository/crypto" ) // ErrBroken is returned by Add and Finalize after a write error. The packer diff --git a/internal/repository/pack/pack_internal_test.go b/internal/repository/pack/pack_internal_test.go index d9a15298a..8c4c37b9c 100644 --- a/internal/repository/pack/pack_internal_test.go +++ b/internal/repository/pack/pack_internal_test.go @@ -7,7 +7,7 @@ import ( "strings" "testing" - "github.com/restic/restic/internal/crypto" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" ) diff --git a/internal/repository/pack/pack_test.go b/internal/repository/pack/pack_test.go index 897aeddfe..20b5f154f 100644 --- a/internal/repository/pack/pack_test.go +++ b/internal/repository/pack/pack_test.go @@ -11,8 +11,8 @@ import ( "github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend/mem" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/errors" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/repository/pack" "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" diff --git a/internal/repository/pack/packedblob.go b/internal/repository/pack/packedblob.go index e5d3e3588..b71e2a4ce 100644 --- a/internal/repository/pack/packedblob.go +++ b/internal/repository/pack/packedblob.go @@ -8,12 +8,18 @@ type PackedBlob struct { Blob Blob } +var _ restic.PackBlob = (*PackedBlob)(nil) + func (pb *PackedBlob) PackID() restic.ID { return pb.Pack } func (pb *PackedBlob) Handle() restic.BlobHandle { return pb.Blob.BlobHandle } func (pb *PackedBlob) CiphertextLength() uint { return pb.Blob.Length } +func (pb *PackedBlob) UncompressedCiphertextLength() uint { + return pb.Blob.UncompressedCiphertextLength() +} + func (pb *PackedBlob) PlaintextLength() uint { return pb.Blob.DataLength() } func (pb *PackedBlob) IsCompressed() bool { return pb.Blob.IsCompressed() } diff --git a/internal/repository/packer_manager.go b/internal/repository/packer_manager.go index 730be79b2..ea69b6b65 100644 --- a/internal/repository/packer_manager.go +++ b/internal/repository/packer_manager.go @@ -15,9 +15,9 @@ import ( "github.com/restic/restic/internal/repository/hashing" "github.com/restic/restic/internal/restic" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/fs" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/repository/pack" ) diff --git a/internal/repository/packer_manager_test.go b/internal/repository/packer_manager_test.go index 7f3d34f1b..b91d2262f 100644 --- a/internal/repository/packer_manager_test.go +++ b/internal/repository/packer_manager_test.go @@ -7,7 +7,7 @@ import ( "sync" "testing" - "github.com/restic/restic/internal/crypto" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/test" ) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 121d8c5a1..31e27a977 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -14,9 +14,9 @@ import ( "github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend/cache" "github.com/restic/restic/internal/backend/dryrun" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/repository/index" "github.com/restic/restic/internal/repository/pack" "github.com/restic/restic/internal/restic" diff --git a/internal/repository/repository_internal_test.go b/internal/repository/repository_internal_test.go index 226c3c237..59873bdad 100644 --- a/internal/repository/repository_internal_test.go +++ b/internal/repository/repository_internal_test.go @@ -14,8 +14,8 @@ import ( "github.com/google/go-cmp/cmp" "github.com/klauspost/compress/zstd" "github.com/restic/restic/internal/backend" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/errors" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/repository/index" "github.com/restic/restic/internal/repository/pack" "github.com/restic/restic/internal/restic" diff --git a/internal/repository/repository_test.go b/internal/repository/repository_test.go index 083768a80..517cd80ea 100644 --- a/internal/repository/repository_test.go +++ b/internal/repository/repository_test.go @@ -18,9 +18,9 @@ import ( "github.com/restic/restic/internal/backend/cache" "github.com/restic/restic/internal/backend/local" "github.com/restic/restic/internal/backend/mem" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/repository" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/repository/index" "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" diff --git a/internal/repository/testing.go b/internal/repository/testing.go index 3aabc02d2..772fe7b94 100644 --- a/internal/repository/testing.go +++ b/internal/repository/testing.go @@ -11,7 +11,7 @@ import ( "github.com/restic/restic/internal/backend/local" "github.com/restic/restic/internal/backend/mem" "github.com/restic/restic/internal/backend/retry" - "github.com/restic/restic/internal/crypto" + "github.com/restic/restic/internal/repository/crypto" "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/test" diff --git a/internal/restic/blob.go b/internal/restic/blob.go index 5fad7b3f6..c19fb6592 100644 --- a/internal/restic/blob.go +++ b/internal/restic/blob.go @@ -14,6 +14,8 @@ type PackBlob interface { Handle() BlobHandle // CiphertextLength is the encrypted size stored in the pack. CiphertextLength() uint + // UncompressedCiphertextLength is the encrypted size of the uncompressed blob. + UncompressedCiphertextLength() uint // PlaintextLength is the size after decryption/decompression. PlaintextLength() uint IsCompressed() bool diff --git a/internal/restic/repository.go b/internal/restic/repository.go index b3e688271..8c68d46e8 100644 --- a/internal/restic/repository.go +++ b/internal/restic/repository.go @@ -5,7 +5,6 @@ import ( "iter" "github.com/restic/restic/internal/backend" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/ui/progress" ) @@ -20,7 +19,6 @@ type Repository interface { Connections() uint Config() Config PackSize() uint - Key() *crypto.Key LoadIndex(ctx context.Context, p TerminalCounterFactory) error diff --git a/internal/restorer/filerestorer_test.go b/internal/restorer/filerestorer_test.go index d517a741d..389604726 100644 --- a/internal/restorer/filerestorer_test.go +++ b/internal/restorer/filerestorer_test.go @@ -47,6 +47,8 @@ func (pb *testPackBlob) Handle() restic.BlobHandle { return pb.handle } func (pb *testPackBlob) CiphertextLength() uint { return pb.ciphertext } +func (pb *testPackBlob) UncompressedCiphertextLength() uint { return pb.ciphertext } + func (pb *testPackBlob) PlaintextLength() uint { return pb.plaintext } func (pb *testPackBlob) IsCompressed() bool { return pb.compressed }