From 889ae900814aac1c17c38a76882b1aafa5744be6 Mon Sep 17 00:00:00 2001
From: Jakob Unterwurzacher
Date: Tue, 8 Sep 2015 00:54:24 +0200
Subject: Add pathfs frontend (uses go-fuse instead of bazil-fuse), part I

Currently fails main_test.go, will be fixed in part II
---
 cryptfs/cryptfs_content.go | 39 ++++++++++++++++++++++++++++++++++++++-
 cryptfs/log.go             |  2 +-
 2 files changed, 39 insertions(+), 2 deletions(-)

(limited to 'cryptfs')

diff --git a/cryptfs/cryptfs_content.go b/cryptfs/cryptfs_content.go
index 4658529..e23d9b5 100644
--- a/cryptfs/cryptfs_content.go
+++ b/cryptfs/cryptfs_content.go
@@ -77,7 +77,7 @@ func (be *CryptFS) EncryptBlock(plaintext []byte) []byte {
 	return ciphertext
 }
 
-// Split a plaintext byte range into (possible partial) blocks
+// Split a plaintext byte range into (possibly partial) blocks
 func (be *CryptFS) SplitRange(offset uint64, length uint64) []intraBlock {
 	var b intraBlock
 	var parts []intraBlock
@@ -113,6 +113,22 @@ func (be *CryptFS) minu64(x uint64, y uint64) uint64 {
 	return y
 }
 
+// CiphertextRange - Get byte range in backing ciphertext corresponding
+// to plaintext range. Returns a range aligned to ciphertext blocks.
+func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (uint64, uint64, int) {
+	// Decrypting the ciphertext will yield too many plaintext bytes. Skip this number
+	// of bytes from the front.
+	skip := offset % be.plainBS
+
+	firstBlockNo := offset / be.plainBS
+	lastBlockNo := ( offset + length - 1 ) / be.plainBS
+
+	alignedOffset := firstBlockNo * be.cipherBS
+	alignedLength := (lastBlockNo - firstBlockNo + 1) * be.cipherBS
+
+	return alignedOffset, alignedLength, int(skip)
+}
+
 // Get the byte range in the ciphertext corresponding to blocks
 // (full blocks!)
 func (be *CryptFS) JoinCiphertextRange(blocks []intraBlock) (uint64, uint64) {
@@ -138,3 +154,24 @@ func (be *CryptFS) CropPlaintext(plaintext []byte, blocks []intraBlock) []byte {
 	}
 	return cropped
 }
+
+// MergeBlocks - Merge newData into oldData at offset
+// New block may be bigger than both newData and oldData
+func (be *CryptFS) MergeBlocks(oldData []byte, newData []byte, offset int) []byte {
+
+	// Make block of maximum size
+	out := make([]byte, be.plainBS)
+
+	// Copy old and new data into it
+	copy(out, oldData)
+	l := len(newData)
+	copy(out[offset:offset + l], newData)
+
+	// Crop to length
+	outLen := len(oldData)
+	newLen := offset + len(newData)
+	if outLen < newLen {
+		outLen = newLen
+	}
+	return out[0:outLen]
+}
diff --git a/cryptfs/log.go b/cryptfs/log.go
index f9c46c8..8233529 100644
--- a/cryptfs/log.go
+++ b/cryptfs/log.go
@@ -15,5 +15,5 @@ func (l logChannel) Printf(format string, args ...interface{}) {
 }
 
 
-var Debug = logChannel{false}
+var Debug = logChannel{true}
 var Warn = logChannel{true}
-- 
cgit v1.2.3