lib: fix parse commnad line arguments

Adds a placeholder string at argv[0] position because Ceph will start
parsing the provided arguments at position argv[1] (skipping what would
normally be the executable name).

Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
This commit is contained in:
Noah Watkins 2014-08-30 10:30:16 -07:00
parent 728afd56be
commit b8609f826c
2 changed files with 17 additions and 8 deletions

14
conn.go
View File

@ -179,12 +179,20 @@ func (c *Conn) GetClusterStats() (stat ClusterStat, err error) {
// ParseCmdLineArgs configures the connection from command line arguments.
func (c *Conn) ParseCmdLineArgs(args []string) error {
argc := C.int(len(args))
// add an empty element 0 -- Ceph treats the array as the actual contents
// of argv and skips the first element (the executable name)
argc := C.int(len(args) + 1)
argv := make([]*C.char, argc)
// make the first element a string just in case it is ever examined
argv[0] = C.CString("placeholder")
defer C.free(unsafe.Pointer(argv[0]))
for i, arg := range args {
argv[i] = C.CString(arg)
defer C.free(unsafe.Pointer(argv[i]))
argv[i+1] = C.CString(arg)
defer C.free(unsafe.Pointer(argv[i+1]))
}
ret := C.rados_conf_parse_argv(c.cluster, argc, &argv[0])
if ret < 0 {
return RadosError(int(ret))

View File

@ -56,16 +56,17 @@ func TestParseDefaultConfigEnv(t *testing.T) {
func TestParseCmdLineArgs(t *testing.T) {
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
log_file_val, _ := conn.GetConfigOption("mon_host")
assert.NotEqual(t, log_file_val, "127.0.0.1")
mon_host_val, _ := conn.GetConfigOption("mon_host")
assert.NotEqual(t, mon_host_val, "1.1.1.1")
args := []string{ "--mon-host 127.0.0.1" }
args := []string{ "--mon-host", "1.1.1.1" }
err := conn.ParseCmdLineArgs(args)
assert.NoError(t, err)
log_file_val, _ = conn.GetConfigOption("mon_host")
assert.Equal(t, log_file_val, "127.0.0.1")
mon_host_val, _ = conn.GetConfigOption("mon_host")
assert.Equal(t, mon_host_val, "1.1.1.1")
}
func TestGetClusterStats(t *testing.T) {