Run go fmt

This commit is contained in:
Jakob Unterwurzacher 2015-10-04 14:36:20 +02:00
parent 5bd08abf40
commit 89fef80d32
15 changed files with 120 additions and 121 deletions

View File

@ -1,8 +1,8 @@
package cryptfs package cryptfs
import ( import (
"io/ioutil"
"encoding/json" "encoding/json"
"io/ioutil"
) )
import "os" import "os"

View File

@ -1,8 +1,8 @@
package cryptfs package cryptfs
import ( import (
"testing"
"fmt" "fmt"
"testing"
) )
type testRange struct { type testRange struct {
@ -22,9 +22,9 @@ func TestSplitRange(t *testing.T) {
key := make([]byte, 16) key := make([]byte, 16)
f := NewCryptFS(key, true) f := NewCryptFS(key, true)
for _, r := range(ranges) { for _, r := range ranges {
parts := f.SplitRange(r.offset, r.length) parts := f.SplitRange(r.offset, r.length)
for _, p := range(parts) { for _, p := range parts {
if p.Length > DEFAULT_PLAINBS || p.Skip >= DEFAULT_PLAINBS { if p.Length > DEFAULT_PLAINBS || p.Skip >= DEFAULT_PLAINBS {
fmt.Printf("Test fail: n=%d, length=%d, offset=%d\n", p.BlockNo, p.Length, p.Skip) fmt.Printf("Test fail: n=%d, length=%d, offset=%d\n", p.BlockNo, p.Length, p.Skip)
t.Fail() t.Fail()
@ -45,15 +45,15 @@ func TestCiphertextRange(t *testing.T) {
key := make([]byte, 16) key := make([]byte, 16)
f := NewCryptFS(key, true) f := NewCryptFS(key, true)
for _, r := range(ranges) { for _, r := range ranges {
alignedOffset, alignedLength, skipBytes := f.CiphertextRange(r.offset, r.length) alignedOffset, alignedLength, skipBytes := f.CiphertextRange(r.offset, r.length)
if alignedLength < r.length { if alignedLength < r.length {
t.Fail() t.Fail()
} }
if alignedOffset % f.cipherBS != 0 { if alignedOffset%f.cipherBS != 0 {
t.Fail() t.Fail()
} }
if r.offset % f.plainBS != 0 && skipBytes == 0 { if r.offset%f.plainBS != 0 && skipBytes == 0 {
t.Fail() t.Fail()
} }
} }

View File

@ -3,9 +3,9 @@ package cryptfs
// CryptFS is the crypto backend of GoCryptFS // CryptFS is the crypto backend of GoCryptFS
import ( import (
"fmt"
"crypto/cipher"
"crypto/aes" "crypto/aes"
"crypto/cipher"
"fmt"
) )
const ( const (

View File

@ -4,11 +4,11 @@ package cryptfs
import ( import (
"bytes" "bytes"
"os"
"errors"
"crypto/cipher" "crypto/cipher"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"errors"
"os"
) )
const ( const (
@ -113,7 +113,7 @@ func (be *CryptFS) SplitRange(offset uint64, length uint64) []intraBlock {
for length > 0 { for length > 0 {
b.BlockNo = offset / be.plainBS b.BlockNo = offset / be.plainBS
b.Skip = offset % be.plainBS b.Skip = offset % be.plainBS
b.Length = be.minu64(length, be.plainBS - b.Skip) b.Length = be.minu64(length, be.plainBS-b.Skip)
parts = append(parts, b) parts = append(parts, b)
offset += b.Length offset += b.Length
length -= b.Length length -= b.Length
@ -131,7 +131,7 @@ func (be *CryptFS) PlainSize(size uint64) uint64 {
overhead := be.cipherBS - be.plainBS overhead := be.cipherBS - be.plainBS
nBlocks := (size + be.cipherBS - 1) / be.cipherBS nBlocks := (size + be.cipherBS - 1) / be.cipherBS
if nBlocks * overhead > size { if nBlocks*overhead > size {
Warn.Printf("PlainSize: Negative size, returning 0 instead\n") Warn.Printf("PlainSize: Negative size, returning 0 instead\n")
return 0 return 0
} }
@ -164,7 +164,7 @@ func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (alignedOffset
skip := offset % be.plainBS skip := offset % be.plainBS
firstBlockNo := offset / be.plainBS firstBlockNo := offset / be.plainBS
lastBlockNo := ( offset + length - 1 ) / be.plainBS lastBlockNo := (offset + length - 1) / be.plainBS
alignedOffset = firstBlockNo * be.cipherBS alignedOffset = firstBlockNo * be.cipherBS
alignedLength = (lastBlockNo - firstBlockNo + 1) * be.cipherBS alignedLength = (lastBlockNo - firstBlockNo + 1) * be.cipherBS
@ -191,10 +191,10 @@ func (be *CryptFS) CropPlaintext(plaintext []byte, blocks []intraBlock) []byte {
last := blocks[len(blocks)-1] last := blocks[len(blocks)-1]
length := (last.BlockNo - blocks[0].BlockNo + 1) * be.plainBS length := (last.BlockNo - blocks[0].BlockNo + 1) * be.plainBS
var cropped []byte var cropped []byte
if offset + length > uint64(len(plaintext)) { if offset+length > uint64(len(plaintext)) {
cropped = plaintext[offset:len(plaintext)] cropped = plaintext[offset:len(plaintext)]
} else { } else {
cropped = plaintext[offset:offset+length] cropped = plaintext[offset : offset+length]
} }
return cropped return cropped
} }
@ -209,7 +209,7 @@ func (be *CryptFS) MergeBlocks(oldData []byte, newData []byte, offset int) []byt
// Copy old and new data into it // Copy old and new data into it
copy(out, oldData) copy(out, oldData)
l := len(newData) l := len(newData)
copy(out[offset:offset + l], newData) copy(out[offset:offset+l], newData)
// Crop to length // Crop to length
outLen := len(oldData) outLen := len(oldData)

View File

@ -3,12 +3,12 @@ package cryptfs
// Filename encryption / decryption function // Filename encryption / decryption function
import ( import (
"crypto/cipher"
"crypto/aes" "crypto/aes"
"fmt" "crypto/cipher"
"strings"
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt"
"strings"
) )
const ( const (
@ -30,7 +30,7 @@ func (be *CryptFS) decryptName(cipherName string) (string, error) {
return "", err return "", err
} }
if len(bin) % aes.BlockSize != 0 { if len(bin)%aes.BlockSize != 0 {
return "", errors.New(fmt.Sprintf("Name len=%d is not a multiple of 16", len(bin))) return "", errors.New(fmt.Sprintf("Name len=%d is not a multiple of 16", len(bin)))
} }
@ -120,7 +120,7 @@ func (be *CryptFS) pad16(orig []byte) (padded []byte) {
if oldLen == 0 { if oldLen == 0 {
panic("Padding zero-length string makes no sense") panic("Padding zero-length string makes no sense")
} }
padLen := aes.BlockSize - oldLen % aes.BlockSize padLen := aes.BlockSize - oldLen%aes.BlockSize
if padLen == 0 { if padLen == 0 {
padLen = aes.BlockSize padLen = aes.BlockSize
} }
@ -137,11 +137,11 @@ func (be *CryptFS) pad16(orig []byte) (padded []byte) {
// unPad16 - remove padding // unPad16 - remove padding
func (be *CryptFS) unPad16(orig []byte) ([]byte, error) { func (be *CryptFS) unPad16(orig []byte) ([]byte, error) {
oldLen := len(orig) oldLen := len(orig)
if oldLen % aes.BlockSize != 0 { if oldLen%aes.BlockSize != 0 {
return nil, errors.New("Unaligned size") return nil, errors.New("Unaligned size")
} }
// The last byte is always a padding byte // The last byte is always a padding byte
padByte := orig[oldLen -1] padByte := orig[oldLen-1]
// The padding byte's value is the padding length // The padding byte's value is the padding length
padLen := int(padByte) padLen := int(padByte)
// Padding must be at least 1 byte // Padding must be at least 1 byte

View File

@ -29,9 +29,9 @@ func (ib *intraBlock) PlaintextRange() (offset uint64, length uint64) {
} }
// CropBlock - crop a potentially larger plaintext block down to the relevant part // CropBlock - crop a potentially larger plaintext block down to the relevant part
func (ib *intraBlock) CropBlock(d []byte) []byte{ func (ib *intraBlock) CropBlock(d []byte) []byte {
lenHave := len(d) lenHave := len(d)
lenWant := int(ib.Skip+ib.Length) lenWant := int(ib.Skip + ib.Length)
if lenHave < lenWant { if lenHave < lenWant {
return d[ib.Skip:lenHave] return d[ib.Skip:lenHave]
} }

View File

@ -1,8 +1,8 @@
package cryptfs package cryptfs
import ( import (
"testing"
"bytes" "bytes"
"testing"
) )
func TestTranslatePath(t *testing.T) { func TestTranslatePath(t *testing.T) {
@ -14,7 +14,7 @@ func TestTranslatePath(t *testing.T) {
key := make([]byte, 16) key := make([]byte, 16)
fs := NewCryptFS(key, true) fs := NewCryptFS(key, true)
for _, n := range(s) { for _, n := range s {
c := fs.EncryptPath(n) c := fs.EncryptPath(n)
d, err := fs.DecryptPath(c) d, err := fs.DecryptPath(c)
if err != nil { if err != nil {
@ -36,13 +36,13 @@ func TestPad16(t *testing.T) {
key := make([]byte, 16) key := make([]byte, 16)
fs := NewCryptFS(key, true) fs := NewCryptFS(key, true)
for i := range(s) { for i := range s {
orig := s[i] orig := s[i]
padded := fs.pad16(orig) padded := fs.pad16(orig)
if len(padded) <= len(orig) { if len(padded) <= len(orig) {
t.Errorf("Padded length not bigger than orig: %d", len(padded)) t.Errorf("Padded length not bigger than orig: %d", len(padded))
} }
if len(padded) % 16 != 0 { if len(padded)%16 != 0 {
t.Errorf("Length is not aligend: %d", len(padded)) t.Errorf("Length is not aligend: %d", len(padded))
} }
unpadded, err := fs.unPad16(padded) unpadded, err := fs.unPad16(padded)
@ -52,7 +52,7 @@ func TestPad16(t *testing.T) {
if len(unpadded) != len(orig) { if len(unpadded) != len(orig) {
t.Errorf("Size mismatch: orig=%d unpadded=%d", len(s[i]), len(unpadded)) t.Errorf("Size mismatch: orig=%d unpadded=%d", len(s[i]), len(unpadded))
} }
if ! bytes.Equal(orig, unpadded) { if !bytes.Equal(orig, unpadded) {
t.Error("Content mismatch orig vs unpadded") t.Error("Content mismatch orig vs unpadded")
} }
} }

View File

@ -1,10 +1,10 @@
package cryptfs package cryptfs
import ( import (
"crypto/rand"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"sync" "sync"
"crypto/rand"
) )
type nonce96 struct { type nonce96 struct {

View File

@ -66,8 +66,8 @@ func (be opensslGCM) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
} }
l := len(ciphertext) l := len(ciphertext)
tag := ciphertext[l-AUTH_TAG_LEN:l] tag := ciphertext[l-AUTH_TAG_LEN : l]
ciphertext = ciphertext[0:l-AUTH_TAG_LEN] ciphertext = ciphertext[0 : l-AUTH_TAG_LEN]
plainBuf := bytes.NewBuffer(dst) plainBuf := bytes.NewBuffer(dst)
dctx, err := openssl.NewGCMDecryptionCipherCtx(128, nil, be.key[:], nonce[:]) dctx, err := openssl.NewGCMDecryptionCipherCtx(128, nil, be.key[:], nonce[:])

14
main.go
View File

@ -1,18 +1,18 @@
package main package main
import ( import (
"runtime/pprof" "encoding/hex"
"io/ioutil"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"time"
"encoding/hex"
"runtime" "runtime"
"runtime/pprof"
"time"
"github.com/rfjakob/gocryptfs/pathfs_frontend"
"github.com/rfjakob/gocryptfs/cryptfs" "github.com/rfjakob/gocryptfs/cryptfs"
"github.com/rfjakob/gocryptfs/pathfs_frontend"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
@ -206,9 +206,9 @@ func pathfsFrontend(key []byte, cipherdir string, mountpoint string, debug bool)
mOpts.AllowOther = false mOpts.AllowOther = false
// Set values shown in "df -T" and friends // Set values shown in "df -T" and friends
// First column, "Filesystem" // First column, "Filesystem"
mOpts.Options = append(mOpts.Options, "fsname=" + cipherdir) mOpts.Options = append(mOpts.Options, "fsname="+cipherdir)
// Second column, "Type", will be shown as "fuse." + Name // Second column, "Type", will be shown as "fuse." + Name
mOpts.Name="gocryptfs" mOpts.Name = "gocryptfs"
state, err := fuse.NewServer(conn.RawFS(), mountpoint, &mOpts) state, err := fuse.NewServer(conn.RawFS(), mountpoint, &mOpts)
if err != nil { if err != nil {

View File

@ -227,12 +227,12 @@ func BenchmarkStreamRead(t *testing.B) {
if t.N > mb { if t.N > mb {
// Grow file so we can satisfy the test // Grow file so we can satisfy the test
fmt.Printf("Growing file to %d MB... ", t.N) fmt.Printf("Growing file to %d MB... ", t.N)
f2, err := os.OpenFile(fn, os.O_WRONLY | os.O_APPEND, 0666) f2, err := os.OpenFile(fn, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
t.FailNow() t.FailNow()
} }
for h := 0; h < t.N - mb ; h++ { for h := 0; h < t.N-mb; h++ {
_, err = f2.Write(buf) _, err = f2.Write(buf)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)

View File

@ -7,10 +7,10 @@ package benchmark
import ( import (
"bytes" "bytes"
"testing"
"github.com/spacemonkeygo/openssl"
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"github.com/spacemonkeygo/openssl"
"testing"
) )
func BenchmarkAESGCMSeal4K(b *testing.B) { func BenchmarkAESGCMSeal4K(b *testing.B) {
@ -86,7 +86,7 @@ func BenchmarkOpensslGCMenc4K(b *testing.B) {
func BenchmarkOpensslGCMdec4K(b *testing.B) { func BenchmarkOpensslGCMdec4K(b *testing.B) {
buf := makeOpensslCiphertext() buf := makeOpensslCiphertext()
b.SetBytes(int64(1024*4)) b.SetBytes(int64(1024 * 4))
tag := buf[4096:] tag := buf[4096:]
buf = buf[0:4096] buf = buf[0:4096]

View File

@ -1,9 +1,9 @@
package pathfs_frontend package pathfs_frontend
import ( import (
"io"
"bytes" "bytes"
"fmt" "fmt"
"io"
"os" "os"
"sync" "sync"
"syscall" "syscall"
@ -101,7 +101,7 @@ func (f *file) doRead(off uint64, length uint64) ([]byte, fuse.Status) {
lenHave := len(plaintext) lenHave := len(plaintext)
lenWant := skip + int(length) lenWant := skip + int(length)
if lenHave > lenWant { if lenHave > lenWant {
out = plaintext[skip:skip + int(length)] out = plaintext[skip : skip+int(length)]
} else if lenHave > skip { } else if lenHave > skip {
out = plaintext[skip:lenHave] out = plaintext[skip:lenHave]
} else { } else {
@ -139,7 +139,7 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) {
status := fuse.OK status := fuse.OK
dataBuf := bytes.NewBuffer(data) dataBuf := bytes.NewBuffer(data)
blocks := f.cfs.SplitRange(uint64(off), uint64(len(data))) blocks := f.cfs.SplitRange(uint64(off), uint64(len(data)))
for _, b := range(blocks) { for _, b := range blocks {
blockData := dataBuf.Next(int(b.Length)) blockData := dataBuf.Next(int(b.Length))
@ -180,7 +180,7 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) {
// Write - FUSE call // Write - FUSE call
func (f *file) Write(data []byte, off int64) (uint32, fuse.Status) { func (f *file) Write(data []byte, off int64) (uint32, fuse.Status) {
cryptfs.Debug.Printf("ino%d: FUSE Write %s: offset=%d length=%d\n", f.ino, off, len(data)) cryptfs.Debug.Printf("ino%d: FUSE Write: offset=%d length=%d\n", f.ino, off, len(data))
fi, err := f.fd.Stat() fi, err := f.fd.Stat()
if err != nil { if err != nil {
@ -248,8 +248,8 @@ func (f *file) Truncate(newSize uint64) fuse.Status {
// File grows // File grows
if newSize > oldSize { if newSize > oldSize {
blocks := f.cfs.SplitRange(oldSize, newSize - oldSize) blocks := f.cfs.SplitRange(oldSize, newSize-oldSize)
for _, b := range(blocks) { for _, b := range blocks {
// First and last block may be partial // First and last block may be partial
if b.IsPartial() { if b.IsPartial() {
off, _ := b.PlaintextRange() off, _ := b.PlaintextRange()
@ -261,7 +261,7 @@ func (f *file) Truncate(newSize uint64) fuse.Status {
} else { } else {
off, length := b.CiphertextRange() off, length := b.CiphertextRange()
f.lock.Lock() f.lock.Lock()
err := syscall.Ftruncate(int(f.fd.Fd()), int64(off + length)) err := syscall.Ftruncate(int(f.fd.Fd()), int64(off+length))
f.lock.Unlock() f.lock.Unlock()
if err != nil { if err != nil {
cryptfs.Warn.Printf("grow Ftruncate returned error: %v", err) cryptfs.Warn.Printf("grow Ftruncate returned error: %v", err)

View File

@ -1,10 +1,10 @@
package pathfs_frontend package pathfs_frontend
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"fmt"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/fuse/nodefs"
@ -24,7 +24,6 @@ func NewFS(key []byte, backing string, useOpenssl bool) *FS {
CryptFS: cryptfs.NewCryptFS(key, useOpenssl), CryptFS: cryptfs.NewCryptFS(key, useOpenssl),
FileSystem: pathfs.NewLoopbackFileSystem(backing), FileSystem: pathfs.NewLoopbackFileSystem(backing),
backing: backing, backing: backing,
} }
} }
@ -52,7 +51,7 @@ func (fs *FS) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Stat
func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) { func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
cryptfs.Debug.Printf("OpenDir(%s)\n", dirName) cryptfs.Debug.Printf("OpenDir(%s)\n", dirName)
cipherEntries, status := fs.FileSystem.OpenDir(fs.EncryptPath(dirName), context); cipherEntries, status := fs.FileSystem.OpenDir(fs.EncryptPath(dirName), context)
var plain []fuse.DirEntry var plain []fuse.DirEntry
if cipherEntries != nil { if cipherEntries != nil {
for i := range cipherEntries { for i := range cipherEntries {
@ -76,7 +75,7 @@ func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, f
// We always need read access to do read-modify-write cycles // We always need read access to do read-modify-write cycles
func (fs *FS) mangleOpenFlags(flags uint32) (newFlags int, writeOnly bool) { func (fs *FS) mangleOpenFlags(flags uint32) (newFlags int, writeOnly bool) {
newFlags = int(flags) newFlags = int(flags)
if newFlags & os.O_WRONLY > 0 { if newFlags&os.O_WRONLY > 0 {
writeOnly = true writeOnly = true
newFlags = newFlags ^ os.O_WRONLY | os.O_RDWR newFlags = newFlags ^ os.O_WRONLY | os.O_RDWR
} }