From d0380b739d5a8f5a8cf87e63c54d2ef4159365a3 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sat, 27 Sep 2014 01:56:59 -0700 Subject: [PATCH] const for lock file, + fix unreachable Close --- daemon/daemon.go | 12 ++++++++++-- daemon/daemon_client.go | 13 ++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/daemon/daemon.go b/daemon/daemon.go index 4c011d15b..dd317bc7d 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -6,11 +6,12 @@ import ( "io" "net" "os" + "path" core "github.com/jbenet/go-ipfs/core" "github.com/jbenet/go-ipfs/core/commands" u "github.com/jbenet/go-ipfs/util" - "github.com/op/go-logging" + logging "github.com/op/go-logging" "github.com/camlistore/lock" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" @@ -18,6 +19,9 @@ import ( var log = logging.MustGetLogger("daemon") +// LockFile is the filename of the daemon lock, relative to config dir +const LockFile = "daemon.lock" + // DaemonListener listens to an initialized IPFS node and can send it commands instead of // starting up a new set of connections type DaemonListener struct { @@ -41,7 +45,7 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr *ma.Multiaddr, confdir stri return nil, err } - lk, err := lock.Lock(confdir + "/daemon.lock") + lk, err := daemonLock(confdir) if err != nil { return nil, err } @@ -133,3 +137,7 @@ func (dl *DaemonListener) Close() error { dl.closed = true return dl.list.Close() } + +func daemonLock(confdir string) (io.Closer, error) { + return lock.Lock(path.Join(confdir, LockFile)) +} diff --git a/daemon/daemon_client.go b/daemon/daemon_client.go index c804ba440..5ca0e3a37 100644 --- a/daemon/daemon_client.go +++ b/daemon/daemon_client.go @@ -8,7 +8,6 @@ import ( "net" "os" - lock "github.com/camlistore/lock" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" u "github.com/jbenet/go-ipfs/util" @@ -43,10 +42,10 @@ func getDaemonAddr(confdir string) (string, error) { return string(line), nil } -// SendCommand issues a command (of type daemon.Command) to the daemon, if it -// is running (if not, errors out). This is done over network RPC API. The -// address of the daemon is retrieved from the configuration directory, where -// live daemons write their addresses to special files. +// SendCommand attempts to run the command over a currently-running daemon. +// If there is no running daemon, returns ErrDaemonNotRunning. This is done +// over network RPC API. The address of the daemon is retrieved from the config +// directory, where live daemons write their addresses to special files. func SendCommand(command *Command, confdir string) error { //check if daemon is running log.Info("Checking if daemon is running...") @@ -55,10 +54,10 @@ func SendCommand(command *Command, confdir string) error { if err != nil { return err } - lk, err := lock.Lock(confdir + "/daemon.lock") + lk, err := daemonLock(confdir) if err == nil { - return ErrDaemonNotRunning lk.Close() + return ErrDaemonNotRunning } log.Info("Daemon is running! %s", err)