2005-04-22 20:15:36 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2009-01-02 02:33:20 +08:00
|
|
|
* Copyright (C) 2004-2009 Marcel Holtmann <marcel@holtmann.org>
|
2005-04-22 20:15:36 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2005-10-30 06:36:31 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2005-04-22 20:15:36 +08:00
|
|
|
*
|
2005-10-30 06:36:31 +08:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2005-04-22 20:15:36 +08:00
|
|
|
*
|
2005-10-30 06:36:31 +08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-04-22 20:15:36 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2006-08-31 16:31:39 +08:00
|
|
|
#define _GNU_SOURCE
|
2005-04-22 20:15:36 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2007-11-26 19:52:16 +08:00
|
|
|
#include <ctype.h>
|
2005-04-22 20:15:36 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2008-06-11 21:20:50 +08:00
|
|
|
#include <stdlib.h>
|
2005-04-22 20:15:36 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
2005-08-05 16:43:42 +08:00
|
|
|
#include <sys/param.h>
|
2005-04-22 20:15:36 +08:00
|
|
|
|
2006-05-01 04:40:25 +08:00
|
|
|
#include "textfile.h"
|
|
|
|
|
2006-02-11 20:12:27 +08:00
|
|
|
int create_dirs(const char *filename, const mode_t mode)
|
2005-08-30 08:35:39 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-02-11 20:12:27 +08:00
|
|
|
int create_file(const char *filename, const mode_t mode)
|
2005-08-30 08:35:39 +08:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
umask(S_IWGRP | S_IWOTH);
|
2007-11-26 19:52:16 +08:00
|
|
|
create_dirs(filename, S_IRUSR | S_IWUSR | S_IXUSR |
|
|
|
|
S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
2005-08-30 08:35:39 +08:00
|
|
|
|
|
|
|
fd = open(filename, O_RDWR | O_CREAT, mode);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-26 21:03:13 +08:00
|
|
|
int create_name(char *buf, size_t size, const char *path, const char *address, const char *name)
|
2006-07-26 20:58:09 +08:00
|
|
|
{
|
2006-07-26 21:03:13 +08:00
|
|
|
return snprintf(buf, size, "%s/%s/%s", path, address, name);
|
2006-07-26 20:58:09 +08:00
|
|
|
}
|
|
|
|
|
2007-11-26 19:52:16 +08:00
|
|
|
static inline char *find_key(char *map, size_t size, const char *key, size_t len, int icase)
|
2006-02-07 17:10:07 +08:00
|
|
|
{
|
2007-11-26 19:52:16 +08:00
|
|
|
char *ptr = map;
|
|
|
|
size_t ptrlen = size;
|
|
|
|
|
|
|
|
while (ptrlen > len + 1) {
|
|
|
|
int cmp = (icase) ? strncasecmp(ptr, key, len) : strncmp(ptr, key, len);
|
|
|
|
if (cmp == 0) {
|
|
|
|
if (ptr == map)
|
|
|
|
return ptr;
|
|
|
|
|
|
|
|
if ((*(ptr - 1) == '\r' || *(ptr - 1) == '\n') &&
|
|
|
|
*(ptr + len) == ' ')
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (icase) {
|
|
|
|
char *p1 = memchr(ptr + 1, tolower(*key), ptrlen - 1);
|
|
|
|
char *p2 = memchr(ptr + 1, toupper(*key), ptrlen - 1);
|
|
|
|
|
|
|
|
if (!p1)
|
|
|
|
ptr = p2;
|
|
|
|
else if (!p2)
|
|
|
|
ptr = p1;
|
|
|
|
else
|
|
|
|
ptr = (p1 < p2) ? p1 : p2;
|
|
|
|
} else
|
|
|
|
ptr = memchr(ptr + 1, *key, ptrlen - 1);
|
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ptrlen = size - (ptr - map);
|
2006-08-31 16:31:39 +08:00
|
|
|
}
|
2006-02-07 17:10:07 +08:00
|
|
|
|
2007-11-26 19:52:16 +08:00
|
|
|
return NULL;
|
2006-02-07 17:10:07 +08:00
|
|
|
}
|
|
|
|
|
2006-02-11 20:12:27 +08:00
|
|
|
static inline int write_key_value(int fd, const char *key, const char *value)
|
2005-05-07 03:56:46 +08:00
|
|
|
{
|
|
|
|
char *str;
|
2005-08-06 14:27:05 +08:00
|
|
|
size_t size;
|
|
|
|
int err = 0;
|
2005-08-05 16:43:42 +08:00
|
|
|
|
2005-05-07 03:56:46 +08:00
|
|
|
size = strlen(key) + strlen(value) + 2;
|
|
|
|
|
2005-08-06 14:27:05 +08:00
|
|
|
str = malloc(size + 1);
|
2005-05-07 03:56:46 +08:00
|
|
|
if (!str)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
sprintf(str, "%s %s\n", key, value);
|
|
|
|
|
|
|
|
if (write(fd, str, size) < 0)
|
|
|
|
err = errno;
|
|
|
|
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-08-31 16:31:39 +08:00
|
|
|
static int write_key(const char *pathname, const char *key, const char *value, int icase)
|
2005-05-07 03:56:46 +08:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *map, *off, *end, *str;
|
2008-12-23 10:53:31 +08:00
|
|
|
off_t size, pos; size_t base;
|
|
|
|
int fd, len, err = 0;
|
2005-05-07 03:56:46 +08:00
|
|
|
|
|
|
|
fd = open(pathname, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (flock(fd, LOCK_EX) < 0) {
|
|
|
|
err = errno;
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0) {
|
|
|
|
err = errno;
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = st.st_size;
|
|
|
|
|
|
|
|
if (!size) {
|
2006-03-24 22:36:27 +08:00
|
|
|
if (value) {
|
|
|
|
pos = lseek(fd, size, SEEK_SET);
|
|
|
|
err = write_key_value(fd, key, value);
|
|
|
|
}
|
2005-05-07 03:56:46 +08:00
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
2007-11-26 19:52:16 +08:00
|
|
|
map = mmap(NULL, size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_LOCKED, fd, 0);
|
2005-11-09 08:04:15 +08:00
|
|
|
if (!map || map == MAP_FAILED) {
|
2005-05-07 03:56:46 +08:00
|
|
|
err = errno;
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
2007-01-22 02:01:39 +08:00
|
|
|
len = strlen(key);
|
2007-11-26 19:52:16 +08:00
|
|
|
off = find_key(map, size, key, len, icase);
|
2005-05-07 03:56:46 +08:00
|
|
|
if (!off) {
|
2006-02-07 17:10:07 +08:00
|
|
|
if (value) {
|
|
|
|
munmap(map, size);
|
|
|
|
pos = lseek(fd, size, SEEK_SET);
|
|
|
|
err = write_key_value(fd, key, value);
|
|
|
|
}
|
2005-05-07 03:56:46 +08:00
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
base = off - map;
|
|
|
|
|
|
|
|
end = strpbrk(off, "\r\n");
|
|
|
|
if (!end) {
|
|
|
|
err = EILSEQ;
|
|
|
|
goto unmap;
|
|
|
|
}
|
|
|
|
|
2009-01-30 00:32:08 +08:00
|
|
|
if (value && ((ssize_t) strlen(value) == end - off - len - 1) &&
|
2007-01-22 02:01:39 +08:00
|
|
|
!strncmp(off + len + 1, value, end - off - len - 1))
|
|
|
|
goto unmap;
|
|
|
|
|
2005-05-07 03:56:46 +08:00
|
|
|
len = strspn(end, "\r\n");
|
|
|
|
end += len;
|
|
|
|
|
|
|
|
len = size - (end - map);
|
2005-09-08 01:31:56 +08:00
|
|
|
if (!len) {
|
|
|
|
munmap(map, size);
|
2006-07-26 20:53:48 +08:00
|
|
|
if (ftruncate(fd, base) < 0) {
|
|
|
|
err = errno;
|
|
|
|
goto unlock;
|
|
|
|
}
|
2005-09-08 01:31:56 +08:00
|
|
|
pos = lseek(fd, base, SEEK_SET);
|
2006-02-07 17:10:07 +08:00
|
|
|
if (value)
|
|
|
|
err = write_key_value(fd, key, value);
|
2006-07-26 20:53:48 +08:00
|
|
|
|
2005-09-08 01:31:56 +08:00
|
|
|
goto unlock;
|
|
|
|
}
|
2005-05-07 03:56:46 +08:00
|
|
|
|
2005-09-08 01:31:56 +08:00
|
|
|
if (len < 0 || len > size) {
|
2005-08-05 16:43:42 +08:00
|
|
|
err = EILSEQ;
|
|
|
|
goto unmap;
|
|
|
|
}
|
|
|
|
|
2005-05-07 03:56:46 +08:00
|
|
|
str = malloc(len);
|
|
|
|
if (!str) {
|
|
|
|
err = errno;
|
|
|
|
goto unmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(str, end, len);
|
|
|
|
|
2005-09-08 01:31:56 +08:00
|
|
|
munmap(map, size);
|
2006-07-26 20:53:48 +08:00
|
|
|
if (ftruncate(fd, base) < 0) {
|
|
|
|
err = errno;
|
2006-09-13 22:58:58 +08:00
|
|
|
free(str);
|
2006-07-26 20:53:48 +08:00
|
|
|
goto unlock;
|
|
|
|
}
|
2005-05-07 03:56:46 +08:00
|
|
|
pos = lseek(fd, base, SEEK_SET);
|
2006-02-07 17:10:07 +08:00
|
|
|
if (value)
|
|
|
|
err = write_key_value(fd, key, value);
|
2005-05-07 03:56:46 +08:00
|
|
|
|
2006-07-26 20:53:48 +08:00
|
|
|
if (write(fd, str, len) < 0)
|
|
|
|
err = errno;
|
2005-05-07 03:56:46 +08:00
|
|
|
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
goto unlock;
|
|
|
|
|
|
|
|
unmap:
|
|
|
|
munmap(map, size);
|
|
|
|
|
|
|
|
unlock:
|
|
|
|
flock(fd, LOCK_UN);
|
|
|
|
|
|
|
|
close:
|
2008-09-02 04:48:28 +08:00
|
|
|
fdatasync(fd);
|
|
|
|
|
2005-05-07 03:56:46 +08:00
|
|
|
close(fd);
|
|
|
|
errno = err;
|
|
|
|
|
|
|
|
return -err;
|
|
|
|
}
|
|
|
|
|
2006-08-31 16:31:39 +08:00
|
|
|
static char *read_key(const char *pathname, const char *key, int icase)
|
2005-04-22 20:15:36 +08:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *map, *off, *end, *str = NULL;
|
|
|
|
off_t size; size_t len;
|
2005-05-07 03:56:46 +08:00
|
|
|
int fd, err = 0;
|
2005-04-22 20:15:36 +08:00
|
|
|
|
|
|
|
fd = open(pathname, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2005-05-07 03:56:46 +08:00
|
|
|
if (flock(fd, LOCK_SH) < 0) {
|
|
|
|
err = errno;
|
2005-04-22 20:15:36 +08:00
|
|
|
goto close;
|
2005-05-07 03:56:46 +08:00
|
|
|
}
|
2005-04-22 20:15:36 +08:00
|
|
|
|
2005-05-07 03:56:46 +08:00
|
|
|
if (fstat(fd, &st) < 0) {
|
|
|
|
err = errno;
|
2005-04-22 20:15:36 +08:00
|
|
|
goto unlock;
|
2005-05-07 03:56:46 +08:00
|
|
|
}
|
2005-04-22 20:15:36 +08:00
|
|
|
|
|
|
|
size = st.st_size;
|
|
|
|
|
2005-05-07 03:56:46 +08:00
|
|
|
map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
2005-11-09 08:04:15 +08:00
|
|
|
if (!map || map == MAP_FAILED) {
|
2005-05-07 03:56:46 +08:00
|
|
|
err = errno;
|
|
|
|
goto unlock;
|
|
|
|
}
|
2005-04-22 20:15:36 +08:00
|
|
|
|
2005-09-10 17:44:19 +08:00
|
|
|
len = strlen(key);
|
2007-11-26 19:52:16 +08:00
|
|
|
off = find_key(map, size, key, len, icase);
|
2005-05-07 03:56:46 +08:00
|
|
|
if (!off) {
|
|
|
|
err = EILSEQ;
|
2005-04-22 20:15:36 +08:00
|
|
|
goto unmap;
|
2005-05-07 03:56:46 +08:00
|
|
|
}
|
2005-04-22 20:15:36 +08:00
|
|
|
|
|
|
|
end = strpbrk(off, "\r\n");
|
2005-05-07 03:56:46 +08:00
|
|
|
if (!end) {
|
|
|
|
err = EILSEQ;
|
2005-04-22 20:15:36 +08:00
|
|
|
goto unmap;
|
2005-05-07 03:56:46 +08:00
|
|
|
}
|
2005-04-22 20:15:36 +08:00
|
|
|
|
|
|
|
str = malloc(end - off - len);
|
2005-05-07 03:56:46 +08:00
|
|
|
if (!str) {
|
|
|
|
err = EILSEQ;
|
2005-04-22 20:15:36 +08:00
|
|
|
goto unmap;
|
2005-05-07 03:56:46 +08:00
|
|
|
}
|
2005-04-22 20:15:36 +08:00
|
|
|
|
2005-05-07 03:56:46 +08:00
|
|
|
memset(str, 0, end - off - len);
|
2005-04-22 20:15:36 +08:00
|
|
|
strncpy(str, off + len + 1, end - off - len - 1);
|
|
|
|
|
|
|
|
unmap:
|
|
|
|
munmap(map, size);
|
|
|
|
|
|
|
|
unlock:
|
|
|
|
flock(fd, LOCK_UN);
|
|
|
|
|
|
|
|
close:
|
|
|
|
close(fd);
|
2005-05-07 03:56:46 +08:00
|
|
|
errno = err;
|
2005-04-22 20:15:36 +08:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2006-02-08 13:38:38 +08:00
|
|
|
|
2006-08-31 16:31:39 +08:00
|
|
|
int textfile_put(const char *pathname, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
return write_key(pathname, key, value, 0);
|
|
|
|
}
|
|
|
|
|
2007-02-27 04:48:15 +08:00
|
|
|
int textfile_caseput(const char *pathname, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
return write_key(pathname, key, value, 1);
|
|
|
|
}
|
|
|
|
|
2006-08-31 16:31:39 +08:00
|
|
|
int textfile_del(const char *pathname, const char *key)
|
|
|
|
{
|
|
|
|
return write_key(pathname, key, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2006-11-08 04:34:46 +08:00
|
|
|
int textfile_casedel(const char *pathname, const char *key)
|
|
|
|
{
|
|
|
|
return write_key(pathname, key, NULL, 1);
|
|
|
|
}
|
|
|
|
|
2006-08-31 16:31:39 +08:00
|
|
|
char *textfile_get(const char *pathname, const char *key)
|
|
|
|
{
|
|
|
|
return read_key(pathname, key, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *textfile_caseget(const char *pathname, const char *key)
|
|
|
|
{
|
|
|
|
return read_key(pathname, key, 1);
|
|
|
|
}
|
|
|
|
|
2006-02-11 20:12:27 +08:00
|
|
|
int textfile_foreach(const char *pathname,
|
2006-02-08 13:38:38 +08:00
|
|
|
void (*func)(char *key, char *value, void *data), void *data)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *map, *off, *end, *key, *value;
|
|
|
|
off_t size; size_t len;
|
|
|
|
int fd, err = 0;
|
|
|
|
|
|
|
|
fd = open(pathname, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = st.st_size;
|
|
|
|
|
|
|
|
map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (!map || map == MAP_FAILED) {
|
|
|
|
err = errno;
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
off = map;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
end = strpbrk(off, " ");
|
|
|
|
if (!end) {
|
|
|
|
err = EILSEQ;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = end - off;
|
|
|
|
|
|
|
|
key = malloc(len + 1);
|
|
|
|
if (!key) {
|
|
|
|
err = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(key, 0, len + 1);
|
|
|
|
memcpy(key, off, len);
|
|
|
|
|
|
|
|
off = end + 1;
|
|
|
|
|
|
|
|
end = strpbrk(off, "\r\n");
|
|
|
|
if (!end) {
|
|
|
|
err = EILSEQ;
|
2006-09-13 22:58:58 +08:00
|
|
|
free(key);
|
2006-02-08 13:38:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = end - off;
|
|
|
|
|
|
|
|
value = malloc(len + 1);
|
|
|
|
if (!value) {
|
|
|
|
err = errno;
|
2006-09-13 22:58:58 +08:00
|
|
|
free(key);
|
2006-02-08 13:38:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(value, 0, len + 1);
|
|
|
|
memcpy(value, off, len);
|
|
|
|
|
|
|
|
func(key, value, data);
|
|
|
|
|
|
|
|
free(key);
|
|
|
|
free(value);
|
|
|
|
|
|
|
|
off = end + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
munmap(map, size);
|
|
|
|
|
|
|
|
unlock:
|
|
|
|
flock(fd, LOCK_UN);
|
|
|
|
|
|
|
|
close:
|
|
|
|
close(fd);
|
|
|
|
errno = err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|