summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2005-07-03 10:44:52 +0000
committerMarcel Holtmann <marcel@holtmann.org>2005-07-03 10:44:52 +0000
commitf05a85d0eac39614d5c75e58ddbc8de3ba641f00 (patch)
tree760aaf673709d5da71b5503f735fac909205f667
parentbee17a8b3e9f220f4064069e3cc9a485663ce057 (diff)
Use textfile library for device name storage
-rw-r--r--hcid/Makefile.am4
-rw-r--r--hcid/storage.c140
2 files changed, 18 insertions, 126 deletions
diff --git a/hcid/Makefile.am b/hcid/Makefile.am
index 90136f28..875de7cd 100644
--- a/hcid/Makefile.am
+++ b/hcid/Makefile.am
@@ -23,10 +23,12 @@ dbus_hcid_cflags =
endif
hcid_SOURCES = main.c security.c storage.c hcid.h lib.c lib.h parser.h parser.y lexer.l kword.h kword.c glib-ectomy.h glib-ectomy.c $(dbus_hcid_sources)
-hcid_LDADD = $(dbus_hcid_libs) @BLUEZ_LIBS@
+hcid_LDADD = $(dbus_hcid_libs) @BLUEZ_LIBS@ $(top_builddir)/common/libtextfile.a
AM_CFLAGS = @BLUEZ_CFLAGS@ @DBUS_CFLAGS@ $(dbus_hcid_cflags)
+INCLUDES = -I$(top_srcdir)/common
+
man_MANS = hcid.8 hcid.conf.5
AM_YFLAGS = -d
diff --git a/hcid/storage.c b/hcid/storage.c
index c5f5eaed..284952f4 100644
--- a/hcid/storage.c
+++ b/hcid/storage.c
@@ -45,6 +45,7 @@
#include <bluetooth/bluetooth.h>
+#include "textfile.h"
#include "hcid.h"
struct list {
@@ -163,59 +164,8 @@ static int create_dirs(const char *filename, mode_t mode)
int write_device_name(const bdaddr_t *local, const bdaddr_t *peer, const char *name)
{
- struct list *temp, *list = NULL;
- char filename[PATH_MAX + 1], addr[18], str[249], *buf, *ptr;
- bdaddr_t bdaddr;
- struct stat st;
- int i, fd, pos, err = 0;
-
- ba2str(local, addr);
- snprintf(filename, PATH_MAX, "%s/%s/names", STORAGEDIR, addr);
-
- umask(S_IWGRP | S_IWOTH);
- create_dirs(filename, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
-
- fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- if (fd < 0)
- return -errno;
-
- if (flock(fd, LOCK_EX) < 0) {
- err = -errno;
- goto close;
- }
-
- if (fstat(fd, &st) < 0) {
- err = -errno;
- goto unlock;
- }
-
- buf = malloc(st.st_size + 300);
- if (!buf) {
- err = -ENOMEM;
- goto unlock;
- }
-
- if (st.st_size > 0) {
- read(fd, buf, st.st_size);
-
- ptr = buf;
-
- memset(str, 0, sizeof(str));
- while (sscanf(ptr, "%17s %[^\n]\n%n", addr, str, &pos) != EOF) {
- str2ba(addr, &bdaddr);
- str[sizeof(str) - 1] = '\0';
-
- list = list_add(list, &bdaddr, str, sizeof(str));
-
- memset(str, 0, sizeof(str));
- ptr += pos;
- if (ptr - buf >= st.st_size)
- break;
- };
-
- lseek(fd, 0, SEEK_SET);
- ftruncate(fd, 0);
- }
+ char filename[PATH_MAX + 1], addr[18], str[249];
+ int i;
memset(str, 0, sizeof(str));
strncpy(str, name, 248);
@@ -223,89 +173,29 @@ int write_device_name(const bdaddr_t *local, const bdaddr_t *peer, const char *n
if (!isprint(str[i]))
str[i] = '.';
- list = list_add(list, peer, str, strlen(str) + 1);
- if (!list) {
- err = -EIO;
- goto unlock;
- }
-
- list_foreach(list, temp) {
- ba2str(&temp->bdaddr, addr);
- if (temp->data && temp->size > 0) {
- memset(buf, 0, 300);
- snprintf(buf, 299, "%s %s\n", addr, temp->data);
- write(fd, buf, strlen(buf));
- }
- }
-
-unlock:
- flock(fd, LOCK_UN);
+ ba2str(local, addr);
+ snprintf(filename, PATH_MAX, "%s/%s/names", STORAGEDIR, addr);
-close:
- close(fd);
- list_free(list);
- return err;
+ ba2str(peer, addr);
+ return textfile_put(filename, addr, str);
}
int read_device_name(const bdaddr_t *local, const bdaddr_t *peer, char *name)
{
- char filename[PATH_MAX + 1], addr[18], str[249], *buf, *ptr;
- bdaddr_t bdaddr;
- struct stat st;
- int fd, pos, err = -ENOENT;
+ char filename[PATH_MAX + 1], addr[18], *str;
ba2str(local, addr);
snprintf(filename, PATH_MAX, "%s/%s/names", STORAGEDIR, addr);
- fd = open(filename, O_RDONLY);
- if (fd < 0)
- return -errno;
-
- if (flock(fd, LOCK_SH) < 0) {
- err = -errno;
- goto close;
- }
-
- if (fstat(fd, &st) < 0) {
- err = -errno;
- goto unlock;
- }
-
- buf = malloc(st.st_size);
- if (!buf) {
- err = -ENOMEM;
- goto unlock;
- }
-
- if (st.st_size > 0) {
- read(fd, buf, st.st_size);
-
- ptr = buf;
-
- memset(str, 0, sizeof(str));
- while (sscanf(ptr, "%17s %[^\n]\n%n", addr, str, &pos) != EOF) {
- str2ba(addr, &bdaddr);
- str[sizeof(str) - 1] = '\0';
-
- if (!bacmp(&bdaddr, peer)) {
- snprintf(name, 249, "%s", str);
- err = 0;
- break;
- }
-
- memset(str, 0, sizeof(str));
- ptr += pos;
- if (ptr - buf >= st.st_size)
- break;
- };
- }
+ ba2str(peer, addr);
+ str = textfile_get(filename, addr);
+ if (!str)
+ return -ENOENT;
-unlock:
- flock(fd, LOCK_UN);
+ memset(name, 0, 249);
+ strncpy(name, str, 248);
-close:
- close(fd);
- return err;
+ return 0;
}
int write_version_info(const bdaddr_t *local, const bdaddr_t *peer, const uint16_t manufacturer, const uint8_t lmp_ver, const uint16_t lmp_subver)