mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-03 04:37:30 +08:00
const for lock file, + fix unreachable Close
This commit is contained in:
@ -6,11 +6,12 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
core "github.com/jbenet/go-ipfs/core"
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
"github.com/jbenet/go-ipfs/core/commands"
|
"github.com/jbenet/go-ipfs/core/commands"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
"github.com/op/go-logging"
|
logging "github.com/op/go-logging"
|
||||||
|
|
||||||
"github.com/camlistore/lock"
|
"github.com/camlistore/lock"
|
||||||
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
||||||
@ -18,6 +19,9 @@ import (
|
|||||||
|
|
||||||
var log = logging.MustGetLogger("daemon")
|
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
|
// DaemonListener listens to an initialized IPFS node and can send it commands instead of
|
||||||
// starting up a new set of connections
|
// starting up a new set of connections
|
||||||
type DaemonListener struct {
|
type DaemonListener struct {
|
||||||
@ -41,7 +45,7 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr *ma.Multiaddr, confdir stri
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lk, err := lock.Lock(confdir + "/daemon.lock")
|
lk, err := daemonLock(confdir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -133,3 +137,7 @@ func (dl *DaemonListener) Close() error {
|
|||||||
dl.closed = true
|
dl.closed = true
|
||||||
return dl.list.Close()
|
return dl.list.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func daemonLock(confdir string) (io.Closer, error) {
|
||||||
|
return lock.Lock(path.Join(confdir, LockFile))
|
||||||
|
}
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
lock "github.com/camlistore/lock"
|
|
||||||
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
||||||
|
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
@ -43,10 +42,10 @@ func getDaemonAddr(confdir string) (string, error) {
|
|||||||
return string(line), nil
|
return string(line), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendCommand issues a command (of type daemon.Command) to the daemon, if it
|
// SendCommand attempts to run the command over a currently-running daemon.
|
||||||
// is running (if not, errors out). This is done over network RPC API. The
|
// If there is no running daemon, returns ErrDaemonNotRunning. This is done
|
||||||
// address of the daemon is retrieved from the configuration directory, where
|
// over network RPC API. The address of the daemon is retrieved from the config
|
||||||
// live daemons write their addresses to special files.
|
// directory, where live daemons write their addresses to special files.
|
||||||
func SendCommand(command *Command, confdir string) error {
|
func SendCommand(command *Command, confdir string) error {
|
||||||
//check if daemon is running
|
//check if daemon is running
|
||||||
log.Info("Checking 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
lk, err := lock.Lock(confdir + "/daemon.lock")
|
lk, err := daemonLock(confdir)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return ErrDaemonNotRunning
|
|
||||||
lk.Close()
|
lk.Close()
|
||||||
|
return ErrDaemonNotRunning
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Daemon is running! %s", err)
|
log.Info("Daemon is running! %s", err)
|
||||||
|
Reference in New Issue
Block a user