diff --git a/src/control/cmd/ddb/commands_wrapper.go b/src/control/cmd/ddb/commands_wrapper.go index 1ba453d082d..d9751dbd8e5 100644 --- a/src/control/cmd/ddb/commands_wrapper.go +++ b/src/control/cmd/ddb/commands_wrapper.go @@ -37,6 +37,15 @@ func freeString(s *C.char) { C.free(unsafe.Pointer(s)) } +func SetCString(out **C.char, s string) func() { + cstr := C.CString(s) + *out = cstr + + return func() { + C.free(unsafe.Pointer(cstr)) + } +} + // InitDdb initializes the ddb context and returns a closure to finalize it. func InitDdb(log *logging.LeveledLogger) (*DdbContext, func(), error) { // Must lock to OS thread because vos init/fini uses ABT init and finalize which must be called on the same thread diff --git a/src/control/cmd/ddb/ddb_commands.go b/src/control/cmd/ddb/ddb_commands.go index 7273ac94b52..d515499264c 100644 --- a/src/control/cmd/ddb/ddb_commands.go +++ b/src/control/cmd/ddb/ddb_commands.go @@ -51,6 +51,10 @@ pool shard. Part of the path is used to determine what the pool uuid is.`, a.String("path", "Path to the vos file to open.") }, Run: func(c *grumble.Context) error { + if c.Flags.String("db_path") != "" { + cleanup := SetCString(&ctx.ctx.dc_db_path, c.Flags.String("db_path")) + defer cleanup() + } return ddbOpen(ctx, c.Args.String("path"), c.Flags.Bool("write_mode")) }, Completer: openCompleter, diff --git a/src/control/cmd/ddb/main.go b/src/control/cmd/ddb/main.go index f61903827b8..9cf5bb2089d 100644 --- a/src/control/cmd/ddb/main.go +++ b/src/control/cmd/ddb/main.go @@ -16,7 +16,6 @@ import ( "path/filepath" "runtime/debug" "strings" - "unsafe" "github.com/desertbit/columnize" "github.com/desertbit/go-shlex" @@ -31,11 +30,6 @@ import ( "github.com/daos-stack/daos/src/control/server/engine" ) -/* - #include -*/ -import "C" - func exitWithError(err error) { cmdName := path.Base(os.Args[0]) fmt.Fprintf(os.Stderr, "ERROR: %s: %v\n", cmdName, err) @@ -307,13 +301,13 @@ func parseOpts(args []string, opts *cliOptions) error { app := createGrumbleApp(ctx) if opts.SysdbPath != "" { - ctx.ctx.dc_db_path = C.CString(string(opts.SysdbPath)) - defer C.free(unsafe.Pointer(ctx.ctx.dc_db_path)) + cleanup := SetCString(&ctx.ctx.dc_db_path, string(opts.SysdbPath)) + defer cleanup() } if opts.VosPath != "" { - ctx.ctx.dc_pool_path = C.CString(string(opts.VosPath)) - defer C.free(unsafe.Pointer(ctx.ctx.dc_pool_path)) + cleanup := SetCString(&ctx.ctx.dc_pool_path, string(opts.VosPath)) + defer cleanup() if !strings.HasPrefix(string(opts.Args.RunCmd), "feature") && !strings.HasPrefix(string(opts.Args.RunCmd), "open") &&