diff options
-rw-r--r-- | common/textfile.c | 50 | ||||
-rw-r--r-- | common/textfile.h | 3 | ||||
-rw-r--r-- | hcid/storage.c | 50 | ||||
-rw-r--r-- | hidd/sdp.c | 8 |
4 files changed, 55 insertions, 56 deletions
diff --git a/common/textfile.c b/common/textfile.c index a5d0a58b..26cbbea7 100644 --- a/common/textfile.c +++ b/common/textfile.c @@ -41,6 +41,56 @@ #include <sys/mman.h> #include <sys/param.h> +int create_dirs(char *filename, mode_t mode) +{ + struct stat st; + char dir[PATH_MAX + 1], *prev, *next; + int err; + + err = stat(filename, &st); + if (!err && S_ISREG(st.st_mode)) + return 0; + + memset(dir, 0, PATH_MAX + 1); + strcat(dir, "/"); + + prev = strchr(filename, '/'); + + while (prev) { + next = strchr(prev + 1, '/'); + if (!next) + break; + + if (next - prev == 1) { + prev = next; + continue; + } + + strncat(dir, prev + 1, next - prev); + mkdir(dir, mode); + + prev = next; + } + + return 0; +} + +int create_file(char *filename, mode_t mode) +{ + int fd; + + 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, mode); + if (fd < 0) + return fd; + + close(fd); + + return 0; +} + static inline int write_key_value(int fd, char *key, char *value) { char *str; diff --git a/common/textfile.h b/common/textfile.h index 4f3cf3d4..bd7c7470 100644 --- a/common/textfile.h +++ b/common/textfile.h @@ -26,5 +26,8 @@ * $Id$ */ +int create_dirs(char *filename, mode_t mode); +int create_file(char *filename, mode_t mode); + int textfile_put(char *pathname, char *key, char *value); char *textfile_get(char *pathname, char *key); diff --git a/hcid/storage.c b/hcid/storage.c index eed3e694..e270cbd8 100644 --- a/hcid/storage.c +++ b/hcid/storage.c @@ -48,56 +48,6 @@ #include "textfile.h" #include "hcid.h" -static int create_dirs(char *filename, mode_t mode) -{ - struct stat st; - char dir[PATH_MAX + 1], *prev, *next; - int err; - - err = stat(filename, &st); - if (!err && S_ISREG(st.st_mode)) - return 0; - - memset(dir, 0, PATH_MAX + 1); - strcat(dir, "/"); - - prev = strchr(filename, '/'); - - while (prev) { - next = strchr(prev + 1, '/'); - if (!next) - break; - - if (next - prev == 1) { - prev = next; - continue; - } - - strncat(dir, prev + 1, next - prev); - mkdir(dir, mode); - - prev = next; - } - - return 0; -} - -static inline int create_file(char *filename, mode_t mode) -{ - int fd; - - 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, mode); - if (fd < 0) - return fd; - - close(fd); - - return 0; -} - int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name) { char filename[PATH_MAX + 1], addr[18], str[249]; @@ -77,7 +77,7 @@ static void epox_endian_quirk(unsigned char *data, int size) static int store_device_info(const bdaddr_t *src, const bdaddr_t *dst, struct hidp_connadd_req *req) { char filename[PATH_MAX + 1], addr[18], *str, *desc; - int i, fd, size; + int i, size; ba2str(src, addr); snprintf(filename, PATH_MAX, "%s/%s/hidd", STORAGEDIR, addr); @@ -102,11 +102,7 @@ static int store_device_info(const bdaddr_t *src, const bdaddr_t *dst, struct hi req->subclass, req->country, req->parser, desc, req->flags, req->name); - fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (fd < 0) - return -errno; - - close(fd); + create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); ba2str(dst, addr); return textfile_put(filename, addr, str); |