From 7b22b426b9f5e68a0ff51799e04f193dc74586d0 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sat, 2 Jul 2016 00:12:36 +0200 Subject: [PATCH] contentenc: rename PlaintextRange and CiphertextRange The name could be misunderstood and actually caused a bug: doWrite used to always preallocate 4128 instead of the actual data length. --- internal/contentenc/content.go | 6 ++++++ internal/contentenc/intrablock.go | 14 ++++++-------- internal/fusefrontend/file.go | 12 ++++++------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/internal/contentenc/content.go b/internal/contentenc/content.go index 2298c5e..6ac766f 100644 --- a/internal/contentenc/content.go +++ b/internal/contentenc/content.go @@ -40,10 +40,16 @@ func New(cc *cryptocore.CryptoCore, plainBS uint64) *ContentEnc { } } +// PlainBS returns the plaintext block size func (be *ContentEnc) PlainBS() uint64 { return be.plainBS } +// CipherBS returns the ciphertext block size +func (be *ContentEnc) CipherBS() uint64 { + return be.cipherBS +} + // DecryptBlocks - Decrypt a number of blocks func (be *ContentEnc) DecryptBlocks(ciphertext []byte, firstBlockNo uint64, fileId []byte) ([]byte, error) { cBuf := bytes.NewBuffer(ciphertext) diff --git a/internal/contentenc/intrablock.go b/internal/contentenc/intrablock.go index 330b980..ec9562d 100644 --- a/internal/contentenc/intrablock.go +++ b/internal/contentenc/intrablock.go @@ -16,16 +16,14 @@ func (ib *intraBlock) IsPartial() bool { return false } -// CiphertextRange - get byte range in ciphertext file corresponding to BlockNo -// (complete block) -func (ib *intraBlock) CiphertextRange() (offset uint64, length uint64) { - return ib.fs.BlockNoToCipherOff(ib.BlockNo), ib.fs.cipherBS +// BlockCipherOff returns the ciphertext offset corresponding to BlockNo +func (ib *intraBlock) BlockCipherOff() (offset uint64) { + return ib.fs.BlockNoToCipherOff(ib.BlockNo) } -// PlaintextRange - get byte range in plaintext corresponding to BlockNo -// (complete block) -func (ib *intraBlock) PlaintextRange() (offset uint64, length uint64) { - return ib.fs.BlockNoToPlainOff(ib.BlockNo), ib.fs.plainBS +// BlockPlainOff returns the plaintext offset corresponding to BlockNo +func (ib *intraBlock) BlockPlainOff() (offset uint64) { + return ib.fs.BlockNoToPlainOff(ib.BlockNo) } // CropBlock - crop a potentially larger plaintext block down to the relevant part diff --git a/internal/fusefrontend/file.go b/internal/fusefrontend/file.go index 8f13553..b5775c2 100644 --- a/internal/fusefrontend/file.go +++ b/internal/fusefrontend/file.go @@ -244,7 +244,7 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) { // Incomplete block -> Read-Modify-Write if b.IsPartial() { // Read - o, _ := b.PlaintextRange() + o := b.BlockPlainOff() var oldData []byte oldData, status = f.doRead(o, f.contentEnc.PlainBS()) if status != fuse.OK { @@ -257,13 +257,13 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) { } // Encrypt - blockOffset, blockLen := b.CiphertextRange() + blockOffset := b.BlockCipherOff() blockData = f.contentEnc.EncryptBlock(blockData, b.BlockNo, f.header.Id) tlog.Debug.Printf("ino%d: Writing %d bytes to block #%d", f.ino, uint64(len(blockData))-f.contentEnc.BlockOverhead(), b.BlockNo) // Prevent partially written (=corrupt) blocks by preallocating the space beforehand - err := prealloc(int(f.fd.Fd()), int64(blockOffset), int64(blockLen)) + err := prealloc(int(f.fd.Fd()), int64(blockOffset), int64(len(blockData))) if err != nil { tlog.Warn.Printf("ino%d fh%d: doWrite: prealloc failed: %s", f.ino, f.intFd(), err.Error()) status = fuse.ToStatus(err) @@ -410,12 +410,12 @@ func (f *file) Truncate(newSize uint64) fuse.Status { } lastBlock := addBlocks[len(addBlocks)-1] if lastBlock.IsPartial() { - off, _ := lastBlock.PlaintextRange() + off := lastBlock.BlockPlainOff() _, status := f.doWrite(make([]byte, lastBlock.Length), int64(off+lastBlock.Skip)) return status } else { - off, length := lastBlock.CiphertextRange() - err = syscall.Ftruncate(f.intFd(), int64(off+length)) + off := lastBlock.BlockCipherOff() + err = syscall.Ftruncate(f.intFd(), int64(off+f.contentEnc.CipherBS())) if err != nil { tlog.Warn.Printf("Truncate: grow Ftruncate returned error: %v", err) }