aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Unterwurzacher2016-01-21 22:59:11 +0100
committerJakob Unterwurzacher2016-01-21 22:59:11 +0100
commitd1631696556cb6460db1d6eedf9fbc3015433b1c (patch)
treea2665626d35a55d82cc41bdc0dce28710e1fa01f
parent9bab220a1b5a10c00c314b15f4c9f879f1473707 (diff)
Wrap Logger to make disabling output more efficient
Instead of using SetOutput(ioutil.Discard), which means that Printf is still called for every debug message, use a simple and fast boolean check. Streaming write performance improves from 86 to 93 MB/s.
-rw-r--r--cryptfs/config_test.go3
-rw-r--r--cryptfs/cryptfs.go2
-rw-r--r--cryptfs/log.go42
-rw-r--r--daemonize.go14
-rw-r--r--main.go33
5 files changed, 50 insertions, 44 deletions
diff --git a/cryptfs/config_test.go b/cryptfs/config_test.go
index 5cf04bf..0dedbd9 100644
--- a/cryptfs/config_test.go
+++ b/cryptfs/config_test.go
@@ -2,7 +2,6 @@ package cryptfs
import (
"fmt"
- "io/ioutil"
"os"
"testing"
"time"
@@ -35,7 +34,7 @@ func TestLoadV2(t *testing.T) {
func TestLoadV2PwdError(t *testing.T) {
if !testing.Verbose() {
- Warn.SetOutput(ioutil.Discard)
+ Warn.Enabled = false
}
_, _, err := LoadConfFile("config_test/v2.conf", "wrongpassword")
Warn.SetOutput(os.Stderr)
diff --git a/cryptfs/cryptfs.go b/cryptfs/cryptfs.go
index 232129f..5b3f074 100644
--- a/cryptfs/cryptfs.go
+++ b/cryptfs/cryptfs.go
@@ -9,6 +9,8 @@ import (
)
const (
+ PROGRAM_NAME = "gocryptfs"
+
DEFAULT_PLAINBS = 4096
KEY_LEN = 32 // AES-256
AUTH_TAG_LEN = 16
diff --git a/cryptfs/log.go b/cryptfs/log.go
index 32fe228..a5b5115 100644
--- a/cryptfs/log.go
+++ b/cryptfs/log.go
@@ -2,8 +2,8 @@ package cryptfs
import (
"encoding/json"
- "io/ioutil"
"log"
+ "log/syslog"
"os"
)
@@ -16,19 +16,45 @@ func JSONDump(obj interface{}) string {
}
}
-// As defined by http://elinux.org/Debugging_by_printing#Log_Levels
+// toggledLogger - a Logger than can be enabled and disabled
+type toggledLogger struct {
+ Enabled bool
+ *log.Logger
+}
+
+func (l *toggledLogger) Printf(format string, v ...interface{}) {
+ if !l.Enabled {
+ return
+ }
+ l.Logger.Printf(format, v...)
+}
+func (l *toggledLogger) Println(v ...interface{}) {
+ if !l.Enabled {
+ return
+ }
+ l.Logger.Println(v...)
+}
+func (l *toggledLogger) SwitchToSyslog(p syslog.Priority) {
+ w, err := syslog.New(p, PROGRAM_NAME)
+ if err != nil {
+ Warn.Printf("Cannot switch 0x%02x to syslog: %v", p, err)
+ } else {
+ l.SetOutput(w)
+ }
+}
+// As defined by http://elinux.org/Debugging_by_printing#Log_Levels
// Debug messages
-var Debug *log.Logger
+var Debug *toggledLogger
// Informational message e.g. startup information
-var Info *log.Logger
+var Info *toggledLogger
// A warning, meaning nothing serious by itself but might indicate problems
-var Warn *log.Logger
+var Warn *toggledLogger
func init() {
- Debug = log.New(ioutil.Discard, "", 0)
- Info = log.New(os.Stdout, "", 0)
- Warn = log.New(os.Stderr, "", 0)
+ Debug = &toggledLogger{false, log.New(os.Stdout, "", 0)}
+ Info = &toggledLogger{true, log.New(os.Stdout, "", 0)}
+ Warn = &toggledLogger{true, log.New(os.Stderr, "", 0)}
}
diff --git a/daemonize.go b/daemonize.go
index 7d00288..e227942 100644
--- a/daemonize.go
+++ b/daemonize.go
@@ -2,14 +2,10 @@ package main
import (
"fmt"
- "log"
- "log/syslog"
"os"
"os/exec"
"os/signal"
"syscall"
-
- "github.com/rfjakob/gocryptfs/cryptfs"
)
// The child sends us USR1 if the mount was successful
@@ -50,13 +46,3 @@ func forkChild() {
// The child exited with 0 - let's do the same.
os.Exit(0)
}
-
-// Switch one Logger to syslog
-func switchToSyslog(l *log.Logger, p syslog.Priority) {
- w, err := syslog.New(p, PROGRAM_NAME)
- if err != nil {
- cryptfs.Warn.Printf("Cannot switch 0x%02x to syslog: %v", p, err)
- } else {
- l.SetOutput(w)
- }
-}
diff --git a/main.go b/main.go
index 0e66b1c..f9570e0 100644
--- a/main.go
+++ b/main.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"flag"
"fmt"
- "io/ioutil"
"log/syslog"
"os"
"os/exec"
@@ -26,8 +25,6 @@ import (
)
const (
- PROGRAM_NAME = "gocryptfs"
-
// Exit codes
ERREXIT_USAGE = 1
ERREXIT_MOUNT = 3
@@ -77,15 +74,15 @@ func initDir(args *argContainer) {
cryptfs.Info.Printf(colorGreen + "The filesystem has been created successfully." + colorReset)
cryptfs.Info.Printf(colorGrey+"You can now mount it using: %s %s MOUNTPOINT"+colorReset,
- PROGRAM_NAME, args.cipherdir)
+ cryptfs.PROGRAM_NAME, args.cipherdir)
os.Exit(0)
}
func usageText() {
printVersion()
fmt.Printf("\n")
- fmt.Printf("Usage: %s -init|-passwd [OPTIONS] CIPHERDIR\n", PROGRAM_NAME)
- fmt.Printf(" or %s [OPTIONS] CIPHERDIR MOUNTPOINT\n", PROGRAM_NAME)
+ fmt.Printf("Usage: %s -init|-passwd [OPTIONS] CIPHERDIR\n", cryptfs.PROGRAM_NAME)
+ fmt.Printf(" or %s [OPTIONS] CIPHERDIR MOUNTPOINT\n", cryptfs.PROGRAM_NAME)
fmt.Printf("\nOptions:\n")
flagSet.PrintDefaults()
}
@@ -101,9 +98,9 @@ func loadConfig(args *argContainer) (masterkey []byte, confFile *cryptfs.ConfFil
fmt.Printf("Password: ")
pw := readPassword(args.extpass)
cryptfs.Info.Printf("Decrypting master key... ")
- cryptfs.Warn.SetOutput(ioutil.Discard) // Silence DecryptBlock() error messages on incorrect password
+ cryptfs.Warn.Enabled = false // Silence DecryptBlock() error messages on incorrect password
masterkey, confFile, err = cryptfs.LoadConfFile(args.config, pw)
- cryptfs.Warn.SetOutput(os.Stderr)
+ cryptfs.Warn.Enabled = true
if err != nil {
fmt.Println(err)
fmt.Println(colorRed + "Wrong password." + colorReset)
@@ -132,7 +129,7 @@ func changePassword(args *argContainer) {
// printVersion - print a version string like
// "gocryptfs v0.3.1-31-g6736212-dirty; on-disk format 2"
func printVersion() {
- fmt.Printf("%s %s; on-disk format %d\n", PROGRAM_NAME, GitVersion, cryptfs.HEADER_CURRENT_VERSION)
+ fmt.Printf("%s %s; on-disk format %d\n", cryptfs.PROGRAM_NAME, GitVersion, cryptfs.HEADER_CURRENT_VERSION)
}
func main() {
@@ -142,7 +139,7 @@ func main() {
setupColors()
// Parse command line arguments
- flagSet = flag.NewFlagSet(PROGRAM_NAME, flag.ExitOnError)
+ flagSet = flag.NewFlagSet(cryptfs.PROGRAM_NAME, flag.ExitOnError)
flagSet.Usage = usageText
flagSet.BoolVar(&args.debug, "debug", false, "Enable debug output")
flagSet.BoolVar(&args.fusedebug, "fusedebug", false, "Enable fuse library debug output")
@@ -195,7 +192,7 @@ func main() {
}
// "-q"
if args.quiet {
- cryptfs.Info.SetOutput(ioutil.Discard)
+ cryptfs.Info.Enabled = false
}
// "-config"
if args.config != "" {
@@ -226,7 +223,7 @@ func main() {
// "-init"
if args.init {
if flagSet.NArg() > 1 {
- fmt.Printf("Usage: %s -init [OPTIONS] CIPHERDIR\n", PROGRAM_NAME)
+ fmt.Printf("Usage: %s -init [OPTIONS] CIPHERDIR\n", cryptfs.PROGRAM_NAME)
os.Exit(ERREXIT_USAGE)
}
initDir(&args) // does not return
@@ -234,7 +231,7 @@ func main() {
// "-passwd"
if args.passwd {
if flagSet.NArg() > 1 {
- fmt.Printf("Usage: %s -passwd [OPTIONS] CIPHERDIR\n", PROGRAM_NAME)
+ fmt.Printf("Usage: %s -passwd [OPTIONS] CIPHERDIR\n", cryptfs.PROGRAM_NAME)
os.Exit(ERREXIT_USAGE)
}
changePassword(&args) // does not return
@@ -281,13 +278,9 @@ func main() {
if args.notifypid > 0 {
sendUsr1(args.notifypid)
- if !args.quiet {
- switchToSyslog(cryptfs.Info, syslog.LOG_USER|syslog.LOG_INFO)
- }
- if args.debug {
- switchToSyslog(cryptfs.Debug, syslog.LOG_USER|syslog.LOG_DEBUG)
- }
- switchToSyslog(cryptfs.Warn, syslog.LOG_USER|syslog.LOG_WARNING)
+ cryptfs.Info.SwitchToSyslog(syslog.LOG_USER | syslog.LOG_INFO)
+ cryptfs.Info.SwitchToSyslog(syslog.LOG_USER | syslog.LOG_DEBUG)
+ cryptfs.Info.SwitchToSyslog(syslog.LOG_USER | syslog.LOG_WARNING)
}
// Wait for SIGINT in the background and unmount ourselves if we get it.
// This prevents a dangling "Transport endpoint is not connected" mountpoint.