diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 66bdaf671..daab7f36f 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -35,6 +35,17 @@ var initCmd = &cmds.Command{ // name of the file? // TODO cmds.StringOption("event-logs", "l", "Location for machine-readable event logs"), }, + PreRun: func(req cmds.Request) error { + daemonLocked := fsrepo.LockedByOtherProcess(req.Context().ConfigRoot) + + log.Info("checking if daemon is running...") + if daemonLocked { + e := "ipfs daemon is running. please stop it to run this command" + return cmds.ClientError(e) + } + + return nil + }, Run: func(req cmds.Request, res cmds.Response) { force, _, err := req.Option("f").Bool() // if !found, it's okay force == false diff --git a/test/sharness/t0020-init.sh b/test/sharness/t0020-init.sh index 002b9865e..5ee134a36 100755 --- a/test/sharness/t0020-init.sh +++ b/test/sharness/t0020-init.sh @@ -44,4 +44,16 @@ test_expect_success "ipfs init output looks good" ' test_cmp expected actual_init ' +test_init_ipfs + +test_launch_ipfs_daemon + +test_expect_success "ipfs init should not run while daemon is running" ' + test_must_fail ipfs init 2> daemon_running_err && + EXPECT="Error: ipfs daemon is running. please stop it to run this command" && + grep "$EXPECT" daemon_running_err +' + +test_kill_ipfs_daemon + test_done