Skip to contentSkip to conversation

storage: Move adapter info to adapter.conf

PATCH In review Frédéric Danis 3 participants 3 messages Oct 3, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 storage: Move adapter info to adapter.conf

Add functions to read and write to .conf files. Convert adapter "config" file to "adapter.conf".

---
 .gitignore          |    1 +
 Makefile.am         |    3 +-
 Makefile.tools      |    6 +-
 src/keyfile.c       |  282 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/keyfile.h       |   51 ++++++++++
 src/storage.c       |  100 +++++++-----------
 test/test-keyfile.c |  252 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 627 insertions(+), 68 deletions(-)
 create mode 100644 src/keyfile.c
 create mode 100644 src/keyfile.h
 create mode 100644 test/test-keyfile.c
.gitignore +1 -0
index c9f293a..2f89353 100644
@@ -82,6 +82,7 @@ unit/test-eir
 tools/btmgmt
 monitor/btmon
 emulator/btvirt
+test/test-keyfile
 
 doc/*.bak
 doc/*.stamp
Makefile.am +2 -1
index c27eb01..ba7c8d8 100644
@@ -309,7 +309,8 @@ src_bluetoothd_SOURCES = $(gdbus_sources) $(builtin_sources) \
 			src/dbus-common.c src/dbus-common.h \
 			src/event.h src/event.c \
 			src/oob.h src/oob.c src/eir.h src/eir.c \
-			src/mgmt.c src/mgmt.h
+			src/mgmt.c src/mgmt.h \
+			src/keyfile.h src/keyfile.c
 src_bluetoothd_LDADD = lib/libbluetooth-private.la @GLIB_LIBS@ @DBUS_LIBS@ \
 								-ldl -lrt
 src_bluetoothd_LDFLAGS = $(AM_LDFLAGS) -Wl,--export-dynamic \
Makefile.tools +5 -1
index 9637a9f..e122b0a 100644
@@ -157,7 +157,8 @@ noinst_PROGRAMS += test/gaptest test/sdptest test/scotest \
 			test/attest test/hstest test/avtest \
 					test/lmptest test/bdaddr test/agent \
 					test/btiotest test/test-textfile \
-					test/uuidtest test/mpris-player
+					test/uuidtest test/mpris-player \
+					test/test-keyfile
 
 test_hciemu_LDADD = lib/libbluetooth-private.la
 
@@ -195,6 +196,9 @@ test_mpris_player_LDADD = @DBUS_LIBS@ @GLIB_LIBS@
 
 test_test_textfile_SOURCES = test/test-textfile.c src/textfile.h src/textfile.c
 
+test_test_keyfile_SOURCES = test/test-keyfile.c src/keyfile.h src/keyfile.c
+test_test_keyfile_LDADD = @GLIB_LIBS@
+
 dist_man_MANS += test/rctest.1 test/hciemu.1
 
 EXTRA_DIST += test/bdaddr.8
src/keyfile.c new file +282 -0

282 lines not on this page · raw message

src/keyfile.h new file +51 -0

51 lines not on this page · raw message

src/storage.c +34 -66

208 lines not on this page · raw message

test/test-keyfile.c new file +252 -0

252 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#1 lore raw

Hi Frédéric,

Quoted 14 lines · Frédéric Danis

Add functions to read and write to .conf files.
Convert adapter "config" file to "adapter.conf".

---
 .gitignore          |    1 +
 Makefile.am         |    3 +-
 Makefile.tools      |    6 +-
 src/keyfile.c       |  282 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/keyfile.h       |   51 ++++++++++
 src/storage.c       |  100 +++++++-----------
 test/test-keyfile.c |  252 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 627 insertions(+), 68 deletions(-)
 create mode 100644 src/keyfile.c
 create mode 100644 src/keyfile.h
 create mode 100644 test/test-keyfile.c

The keyfile.[ch] files should be split into their own patch along with test-keyfile.c.

Quoted 7 lines
src/keyfile.c new file +7 -0
+int keyfile_put_string(const char *pathname, const char *group,
+				const char *key, const char *value)
+{
+	GKeyFile *key_file;
+	char *data;
+	FILE *file;
+

src/keyfile.c line 52

Quoted 10 lines
src/keyfile.c new file +10 -0
+	key_file = g_key_file_new();
+
+	g_key_file_load_from_file(key_file, pathname, 0, NULL);
+
+	g_key_file_set_string(key_file, group, key, value);
+
+	data = g_key_file_to_data(key_file, NULL, NULL);
+	file = fopen(pathname, "w");
+	fputs(data, file);
+	fclose(file);

It seems like there's some error checking missing here as both g_key_file_load_from_file and fopen could fail.

src/keyfile.c line 134

Quoted 4 lines
src/keyfile.c new file +4 -0
+}
+
+
+int keyfile_get_boolean(const char *pathname, const char *group,

Two consecutive empty lines above. Please remove one.

src/keyfile.c line 138

Quoted 4 lines
src/keyfile.c new file +4 -0
+				const char *key, gboolean *value)
+{
+	GKeyFile *key_file;
+	GError *error = NULL;

Please don't reuse the symbol name "error" since we already have that for error logging. Use gerr instead

Review

Quoted 1 line
(quoted diff) +1 -0
+	int err = 0;

This initialization upon declaration is unnecessary. Set to 0 at the end of the success path instead.

src/keyfile.c line 152

Quoted 4 lines
src/keyfile.c new file +4 -0
+	if (error) {
+		g_error_free(error);
+		err = -EINVAL;
+	}

< i.e. here >

src/keyfile.c line 128

Quoted 4 lines
src/keyfile.c new file +4 -0
+
+failed:
+	g_key_file_free(key_file);
+	errno = -err;

Don't mix errno into this. It should be considered "owned" by libc and we should only read it and not write to it. Return the error in the function return value instead.

src/storage.c int write_device_alias(const char *src, const char *dst, uint8_t dst_type, · line 122

Quoted 1 line
src/storage.c +1 -0
+	return keyfile_put_integer(filename, "General", "DiscovTO", timeout);

I really dislike this "TO" short-hand for timeout. I think we can use longer and more readable names in the INI-format files. In this case DiscoverableTimeout should be perfectly fine.

src/storage.c int write_device_alias(const char *src, const char *dst, uint8_t dst_type, · line 142

Quoted 1 line
src/storage.c +1 -0
+	return keyfile_put_integer(filename, "General", "PairTO", timeout);

Same here. PairableTimeout should be fine.

Johan

#2 lore raw
In reply to Frédéric Danis

Hi Frédéric,

src/keyfile.h line 35

Quoted 9 lines · Frédéric Danis
src/keyfile.h new file +9 -0
+int keyfile_put_string(const char *pathname, const char *group,
+                               const char *key, const char *value);
+int keyfile_put_boolean(const char *pathname, const char *group,
+                               const char *key, gboolean value);
+int keyfile_put_integer(const char *pathname, const char *group,
+                               const char *key, int value);
+
+char *keyfile_get_string(const char *pathname, const char *group,
+                               const char *key);

One suggestion (which may or may not be acked by core devs), what about being consistent here and use:

int keyfile_get_string(const char *pathname, const char *group, const char *key, char **value);

(i.e. use return value for error and value pointer on last parameter).

Quoted 6 lines
src/keyfile.h new file +6 -0
+int keyfile_get_boolean(const char *pathname, const char *group,
+                               const char *key, gboolean *value);
+int keyfile_get_integer(const char *pathname, const char *group,
+                               const char *key, int *value);
+
+int keyfile_del(const char *pathname, const char *group, const char *key);

src/keyfile.h line 51

Quoted 10 lines
src/keyfile.h new file +10 -0
+
+typedef void (*keyfile_key_cb) (char *key, char *value, void *data);
+typedef void (*keyfile_group_cb) (char *group, void *data);
+
+int keyfile_foreach_key(const char *pathname, const char *group,
+                               keyfile_key_cb func, void *data);
+int keyfile_foreach_group(const char *pathname,
+                               keyfile_group_cb func, void *data);
+
+#endif /* __KEYFILE_H */

Regards,

Signature · 3 lines
Anderson Lizardo
Instituto Nokia de Tecnologia - INdT
Manaus - Brazil