Fix size reporting
This commit is contained in:
parent
199d3fd79f
commit
7e564f928f
@ -1,9 +1,9 @@
|
|||||||
package cryptfs
|
package cryptfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
//"fmt"
|
||||||
"os"
|
"os"
|
||||||
"io"
|
//"io"
|
||||||
"errors"
|
"errors"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
)
|
)
|
||||||
@ -11,8 +11,6 @@ import (
|
|||||||
type CryptFile struct {
|
type CryptFile struct {
|
||||||
file *os.File
|
file *os.File
|
||||||
gcm cipher.AEAD
|
gcm cipher.AEAD
|
||||||
plainBS int64
|
|
||||||
cipherBS int64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// decryptBlock - Verify and decrypt GCM block
|
// decryptBlock - Verify and decrypt GCM block
|
||||||
@ -59,6 +57,7 @@ func (be *CryptFS) EncryptBlock(plaintext []byte) []byte {
|
|||||||
return ciphertext
|
return ciphertext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// readCipherBlock - Read ciphertext block number "blockNo", decrypt,
|
// readCipherBlock - Read ciphertext block number "blockNo", decrypt,
|
||||||
// return plaintext
|
// return plaintext
|
||||||
func (be *CryptFile) readCipherBlock(blockNo int64) ([]byte, error) {
|
func (be *CryptFile) readCipherBlock(blockNo int64) ([]byte, error) {
|
||||||
@ -98,12 +97,13 @@ func (be *CryptFile) readCipherBlock(blockNo int64) ([]byte, error) {
|
|||||||
|
|
||||||
return plainBuf, nil
|
return plainBuf, nil
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// intraBlock identifies a part of a file block
|
// intraBlock identifies a part of a file block
|
||||||
type intraBlock struct {
|
type intraBlock struct {
|
||||||
BlockNo int64 // Block number in file
|
BlockNo uint64 // Block number in file
|
||||||
Offset int64 // Offset into block plaintext
|
Offset uint64 // Offset into block plaintext
|
||||||
Length int64 // Length of data from this block
|
Length uint64 // Length of data from this block
|
||||||
fs *CryptFS
|
fs *CryptFS
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,13 +117,13 @@ func (ib *intraBlock) IsPartial() bool {
|
|||||||
|
|
||||||
// CiphertextRange - get byte range in ciphertext file corresponding to BlockNo
|
// CiphertextRange - get byte range in ciphertext file corresponding to BlockNo
|
||||||
// (complete block)
|
// (complete block)
|
||||||
func (ib *intraBlock) CiphertextRange() (offset int64, length int64) {
|
func (ib *intraBlock) CiphertextRange() (offset uint64, length uint64) {
|
||||||
return ib.BlockNo * ib.fs.cipherBS, ib.fs.cipherBS
|
return ib.BlockNo * ib.fs.cipherBS, ib.fs.cipherBS
|
||||||
}
|
}
|
||||||
|
|
||||||
// PlaintextRange - get byte range in plaintext corresponding to BlockNo
|
// PlaintextRange - get byte range in plaintext corresponding to BlockNo
|
||||||
// (complete block)
|
// (complete block)
|
||||||
func (ib *intraBlock) PlaintextRange() (offset int64, length int64) {
|
func (ib *intraBlock) PlaintextRange() (offset uint64, length uint64) {
|
||||||
return ib.BlockNo * ib.fs.plainBS, ib.fs.plainBS
|
return ib.BlockNo * ib.fs.plainBS, ib.fs.plainBS
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ func (ib *intraBlock) CropBlock(d []byte) []byte{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Split a plaintext byte range into (possible partial) blocks
|
// Split a plaintext byte range into (possible partial) blocks
|
||||||
func (be *CryptFS) SplitRange(offset int64, length int64) []intraBlock {
|
func (be *CryptFS) SplitRange(offset uint64, length uint64) []intraBlock {
|
||||||
var b intraBlock
|
var b intraBlock
|
||||||
var parts []intraBlock
|
var parts []intraBlock
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ func (be *CryptFS) SplitRange(offset int64, length int64) []intraBlock {
|
|||||||
for length > 0 {
|
for length > 0 {
|
||||||
b.BlockNo = offset / be.plainBS
|
b.BlockNo = offset / be.plainBS
|
||||||
b.Offset = offset % be.plainBS
|
b.Offset = offset % be.plainBS
|
||||||
b.Length = be.min64(length, be.plainBS - b.Offset)
|
b.Length = be.minu64(length, be.plainBS - b.Offset)
|
||||||
parts = append(parts, b)
|
parts = append(parts, b)
|
||||||
offset += b.Length
|
offset += b.Length
|
||||||
length -= b.Length
|
length -= b.Length
|
||||||
@ -155,13 +155,14 @@ func (be *CryptFS) SplitRange(offset int64, length int64) []intraBlock {
|
|||||||
return parts
|
return parts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (be *CryptFS) min64(x int64, y int64) int64 {
|
func (be *CryptFS) minu64(x uint64, y uint64) uint64 {
|
||||||
if x < y {
|
if x < y {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
return y
|
return y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// writeCipherBlock - Encrypt plaintext and write it to file block "blockNo"
|
// writeCipherBlock - Encrypt plaintext and write it to file block "blockNo"
|
||||||
func (be *CryptFile) writeCipherBlock(blockNo int64, plain []byte) error {
|
func (be *CryptFile) writeCipherBlock(blockNo int64, plain []byte) error {
|
||||||
|
|
||||||
@ -186,7 +187,7 @@ func (be *CryptFile) writeCipherBlock(blockNo int64, plain []byte) error {
|
|||||||
// Perform RMW cycle on block
|
// Perform RMW cycle on block
|
||||||
// Write "data" into file location specified in "b"
|
// Write "data" into file location specified in "b"
|
||||||
func (be *CryptFile) rmwWrite(b intraBlock, data []byte, f *os.File) error {
|
func (be *CryptFile) rmwWrite(b intraBlock, data []byte, f *os.File) error {
|
||||||
if b.Length != int64(len(data)) {
|
if b.Length != uint64(len(data)) {
|
||||||
panic("Length mismatch")
|
panic("Length mismatch")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,7 +201,7 @@ func (be *CryptFile) rmwWrite(b intraBlock, data []byte, f *os.File) error {
|
|||||||
|
|
||||||
// Write goes beyond the old block and grows the file?
|
// Write goes beyond the old block and grows the file?
|
||||||
// Must create a bigger newBlock
|
// Must create a bigger newBlock
|
||||||
if newBlockLen > int64(len(oldBlock)) {
|
if newBlockLen > uint64(len(oldBlock)) {
|
||||||
newBlock = make([]byte, newBlockLen)
|
newBlock = make([]byte, newBlockLen)
|
||||||
} else {
|
} else {
|
||||||
newBlock = make([]byte, len(oldBlock))
|
newBlock = make([]byte, len(oldBlock))
|
||||||
@ -222,3 +223,4 @@ func (be *CryptFile) rmwWrite(b intraBlock, data []byte, f *os.File) error {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -22,8 +21,8 @@ const (
|
|||||||
type CryptFS struct {
|
type CryptFS struct {
|
||||||
blockCipher cipher.Block
|
blockCipher cipher.Block
|
||||||
gcm cipher.AEAD
|
gcm cipher.AEAD
|
||||||
plainBS int64
|
plainBS uint64
|
||||||
cipherBS int64
|
cipherBS uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCryptFS(key [16]byte) *CryptFS {
|
func NewCryptFS(key [16]byte) *CryptFS {
|
||||||
@ -46,6 +45,7 @@ func NewCryptFS(key [16]byte) *CryptFS {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func (fs *CryptFS) NewFile(f *os.File) *CryptFile {
|
func (fs *CryptFS) NewFile(f *os.File) *CryptFile {
|
||||||
return &CryptFile {
|
return &CryptFile {
|
||||||
file: f,
|
file: f,
|
||||||
@ -54,8 +54,9 @@ func (fs *CryptFS) NewFile(f *os.File) *CryptFile {
|
|||||||
cipherBS: fs.cipherBS,
|
cipherBS: fs.cipherBS,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
func (be *CryptFS) PlainBS() int64 {
|
func (be *CryptFS) PlainBS() uint64 {
|
||||||
return be.plainBS
|
return be.plainBS
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,18 +140,25 @@ func (be *CryptFS) DecryptPath(path string) (string, error) {
|
|||||||
return be.translatePath(path, DECRYPT)
|
return be.translatePath(path, DECRYPT)
|
||||||
}
|
}
|
||||||
|
|
||||||
// plainSize - calculate plaintext size from ciphertext size
|
// PlainSize - calculate plaintext size from ciphertext size
|
||||||
func (be *CryptFS) PlainSize(s int64) int64 {
|
func (be *CryptFS) PlainSize(size uint64) uint64 {
|
||||||
// Zero sized files stay zero-sized
|
// Zero sized files stay zero-sized
|
||||||
if s > 0 {
|
if size > 0 {
|
||||||
// Number of blocks
|
|
||||||
n := s / be.cipherBS + 1
|
|
||||||
overhead := be.cipherBS - be.plainBS
|
overhead := be.cipherBS - be.plainBS
|
||||||
s -= n * overhead
|
nBlocks := (size + be.cipherBS - 1) / be.cipherBS
|
||||||
|
size -= nBlocks * overhead
|
||||||
}
|
}
|
||||||
return s
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// plainSizeFromCipherSize - calculate plaintext size from ciphertext size
|
||||||
|
func (be *CryptFS) plainSizeFromCipherSize(size int64) int64 {
|
||||||
|
if size > 0 {
|
||||||
|
|
||||||
|
}
|
||||||
|
return size
|
||||||
|
}
|
||||||
// pad16 - pad filename to 16 byte blocks using standard PKCS#7 padding
|
// pad16 - pad filename to 16 byte blocks using standard PKCS#7 padding
|
||||||
// https://tools.ietf.org/html/rfc5652#section-6.3
|
// https://tools.ietf.org/html/rfc5652#section-6.3
|
||||||
func (be *CryptFS) pad16(orig []byte) (padded []byte) {
|
func (be *CryptFS) pad16(orig []byte) (padded []byte) {
|
||||||
|
31
frontend/checks.go
Normal file
31
frontend/checks.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package frontend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bazil.org/fuse/fs"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Compile-time interface checks.
|
||||||
|
var _ fs.FS = (*FS)(nil)
|
||||||
|
var _ fs.FSStatfser = (*FS)(nil)
|
||||||
|
|
||||||
|
var _ fs.Node = (*Dir)(nil)
|
||||||
|
var _ fs.NodeCreater = (*Dir)(nil)
|
||||||
|
var _ fs.NodeMkdirer = (*Dir)(nil)
|
||||||
|
var _ fs.NodeRemover = (*Dir)(nil)
|
||||||
|
var _ fs.NodeRenamer = (*Dir)(nil)
|
||||||
|
var _ fs.HandleReadDirAller = (*Dir)(nil)
|
||||||
|
|
||||||
|
var _ fs.HandleReader = (*File)(nil)
|
||||||
|
var _ fs.HandleWriter = (*File)(nil)
|
||||||
|
var _ fs.Node = (*File)(nil)
|
||||||
|
var _ fs.NodeOpener = (*File)(nil)
|
||||||
|
var _ fs.NodeSetattrer = (*File)(nil)
|
||||||
|
|
||||||
|
func foo(h fs.HandleReadDirAller) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var d Dir
|
||||||
|
foo(&d)
|
||||||
|
}
|
@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
type Dir struct {
|
type Dir struct {
|
||||||
*cluefs.Dir
|
*cluefs.Dir
|
||||||
|
|
||||||
crfs *cryptfs.CryptFS
|
crfs *cryptfs.CryptFS
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,18 +40,19 @@ func (d *Dir) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenRe
|
|||||||
func (d *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fusefs.Node, error) {
|
func (d *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fusefs.Node, error) {
|
||||||
fmt.Printf("Lookup %s\n", req.Name)
|
fmt.Printf("Lookup %s\n", req.Name)
|
||||||
req.Name = d.crfs.EncryptPath(req.Name)
|
req.Name = d.crfs.EncryptPath(req.Name)
|
||||||
n, err := d.Dir.Lookup(ctx, req, resp)
|
node, err := d.Dir.Lookup(ctx, req, resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
clueDir, ok := n.(*cluefs.Dir)
|
clueDir, ok := node.(*cluefs.Dir)
|
||||||
if ok {
|
if ok {
|
||||||
return &Dir {
|
return &Dir {
|
||||||
Dir: clueDir,
|
Dir: clueDir,
|
||||||
crfs: d.crfs,
|
crfs: d.crfs,
|
||||||
}, nil
|
}, nil
|
||||||
} else {
|
} else {
|
||||||
clueFile := n.(*cluefs.File)
|
resp.Attr.Size = d.crfs.PlainSize(resp.Attr.Size)
|
||||||
|
clueFile := node.(*cluefs.File)
|
||||||
return &File {
|
return &File {
|
||||||
File: clueFile,
|
File: clueFile,
|
||||||
crfs: d.crfs,
|
crfs: d.crfs,
|
||||||
|
@ -56,12 +56,12 @@ func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenR
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
func (f *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
||||||
iblocks := f.crfs.SplitRange(req.Offset, int64(req.Size))
|
iblocks := f.crfs.SplitRange(uint64(req.Offset), uint64(req.Size))
|
||||||
for _, ib := range iblocks {
|
for _, ib := range iblocks {
|
||||||
var partReq fuse.ReadRequest
|
var partReq fuse.ReadRequest
|
||||||
var partResp fuse.ReadResponse
|
var partResp fuse.ReadResponse
|
||||||
o, l := ib.CiphertextRange()
|
o, l := ib.CiphertextRange()
|
||||||
partReq.Offset = o
|
partReq.Offset = int64(o)
|
||||||
partReq.Size = int(l)
|
partReq.Size = int(l)
|
||||||
partResp.Data = make([]byte, int(l))
|
partResp.Data = make([]byte, int(l))
|
||||||
err := f.File.Read(ctx, &partReq, &partResp)
|
err := f.File.Read(ctx, &partReq, &partResp)
|
||||||
@ -82,7 +82,7 @@ func (f *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadR
|
|||||||
func (f *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
|
func (f *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
|
||||||
fmt.Printf("File.Write\n")
|
fmt.Printf("File.Write\n")
|
||||||
resp.Size = 0
|
resp.Size = 0
|
||||||
iblocks := f.crfs.SplitRange(req.Offset, int64(len(req.Data)))
|
iblocks := f.crfs.SplitRange(uint64(req.Offset), uint64(len(req.Data)))
|
||||||
var blockData []byte
|
var blockData []byte
|
||||||
for _, ib := range iblocks {
|
for _, ib := range iblocks {
|
||||||
if ib.IsPartial() {
|
if ib.IsPartial() {
|
||||||
@ -91,7 +91,7 @@ func (f *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.Wri
|
|||||||
var readReq fuse.ReadRequest
|
var readReq fuse.ReadRequest
|
||||||
var readResp fuse.ReadResponse
|
var readResp fuse.ReadResponse
|
||||||
o, l := ib.PlaintextRange()
|
o, l := ib.PlaintextRange()
|
||||||
readReq.Offset = o
|
readReq.Offset = int64(o)
|
||||||
readReq.Size = int(l)
|
readReq.Size = int(l)
|
||||||
err := f.Read(ctx, &readReq, &readResp)
|
err := f.Read(ctx, &readReq, &readResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -109,7 +109,7 @@ func (f *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.Wri
|
|||||||
var partResp fuse.WriteResponse
|
var partResp fuse.WriteResponse
|
||||||
o, _ := ib.CiphertextRange()
|
o, _ := ib.CiphertextRange()
|
||||||
partReq.Data = ciphertext
|
partReq.Data = ciphertext
|
||||||
partReq.Offset = o
|
partReq.Offset = int64(o)
|
||||||
err := f.File.Write(ctx, &partReq, &partResp)
|
err := f.File.Write(ctx, &partReq, &partResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Write failure: %s\n", err.Error())
|
fmt.Printf("Write failure: %s\n", err.Error())
|
||||||
@ -121,3 +121,13 @@ func (f *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.Wri
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *File) Attr(ctx context.Context, attr *fuse.Attr) error {
|
||||||
|
fmt.Printf("Attr\n")
|
||||||
|
err := f.File.Node.Attr(ctx, attr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
attr.Size = f.crfs.PlainSize(attr.Size)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
package frontend
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/rfjakob/cluefs/lib/cluefs"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Node struct {
|
|
||||||
*cluefs.Node
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewNode(parent string, name string, fs *FS) *Node {
|
|
||||||
fmt.Printf("NewNode\n")
|
|
||||||
return &Node{
|
|
||||||
Node: cluefs.NewNode(parent, name, fs.ClueFS),
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user