const for lock file, + fix unreachable Close
Juan Batiz-Benet committed
Sep 27, 2014 at 01:56 UTC
d0380b739d5a8f5a8cf87e63c54d2ef4159365a3
2 files changed
+16
-9
daemon/daemon.go
+10
-2
@@ -6,11 +6,12 @@ import (
6
"io"
7
"net"
8
"os"
9
+ "path"
10
11
core "github.com/jbenet/go-ipfs/core"
12
"github.com/jbenet/go-ipfs/core/commands"
13
u "github.com/jbenet/go-ipfs/util"
13
- "github.com/op/go-logging"
14
+ logging "github.com/op/go-logging"
15
16
"github.com/camlistore/lock"
17
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
@@ -18,6 +19,9 @@ import (
19
20
var log = logging.MustGetLogger("daemon")
21
22
+// LockFile is the filename of the daemon lock, relative to config dir
23
+const LockFile = "daemon.lock"
24
+
25
// DaemonListener listens to an initialized IPFS node and can send it commands instead of
26
// starting up a new set of connections
27
type DaemonListener struct {
@@ -41,7 +45,7 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr *ma.Multiaddr, confdir stri
45
return nil, err
46
}
47
44
- lk, err := lock.Lock(confdir + "/daemon.lock")
48
+ lk, err := daemonLock(confdir)
49
if err != nil {
50
return nil, err
51
}
@@ -133,3 +137,7 @@ func (dl *DaemonListener) Close() error {
137
dl.closed = true
138
return dl.list.Close()
139
}
140
+
141
+func daemonLock(confdir string) (io.Closer, error) {
142
+ return lock.Lock(path.Join(confdir, LockFile))
143
+}
daemon/daemon_client.go
+6
-7
@@ -8,7 +8,6 @@ import (
8
"net"
9
"os"
10
11
- lock "github.com/camlistore/lock"
11
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
12
13
u "github.com/jbenet/go-ipfs/util"
@@ -43,10 +42,10 @@ func getDaemonAddr(confdir string) (string, error) {
42
return string(line), nil
43
}
44
46
-// SendCommand issues a command (of type daemon.Command) to the daemon, if it
47
-// is running (if not, errors out). This is done over network RPC API. The
48
-// address of the daemon is retrieved from the configuration directory, where
49
-// live daemons write their addresses to special files.
45
+// SendCommand attempts to run the command over a currently-running daemon.
46
+// If there is no running daemon, returns ErrDaemonNotRunning. This is done
47
+// over network RPC API. The address of the daemon is retrieved from the config
48
+// directory, where live daemons write their addresses to special files.
49
func SendCommand(command *Command, confdir string) error {
50
//check if daemon is running
51
log.Info("Checking if daemon is running...")
@@ -55,10 +54,10 @@ func SendCommand(command *Command, confdir string) error {
54
if err != nil {
55
return err
56
}
58
- lk, err := lock.Lock(confdir + "/daemon.lock")
57
+ lk, err := daemonLock(confdir)
58
if err == nil {
60
- return ErrDaemonNotRunning
59
lk.Close()
60
+ return ErrDaemonNotRunning
61
}
62
63
log.Info("Daemon is running! %s", err)