Move create_dirs to storage.c, add file read/write utilities

This commit is contained in:
Andrzej Zaborowski 2009-09-05 03:12:19 +02:00 committed by Denis Kenzior
parent 36d0c8451f
commit 17cb0ce3c4
4 changed files with 166 additions and 66 deletions

View File

@ -158,7 +158,7 @@ src_ofonod_SOURCES = $(gdbus_sources) $(builtin_sources) \
src/call-meter.c src/smsutil.h src/smsutil.c \
src/ssn.c src/call-barring.c src/sim.c \
src/phonebook.c src/history.c src/message-waiting.c \
src/simutil.h src/simutil.c
src/simutil.h src/simutil.c src/storage.h src/storage.c
src_ofonod_LDADD = $(builtin_libadd) \
@GLIB_LIBS@ @GTHREAD_LIBS@ @DBUS_LIBS@ -ldl

View File

@ -41,12 +41,7 @@
#include "util.h"
#include "smsutil.h"
#include "simutil.h"
#ifdef TEMP_FAILURE_RETRY
#define TFR TEMP_FAILURE_RETRY
#else
#define TFR
#endif
#include "storage.h"
#define SIM_MANAGER_INTERFACE "org.ofono.SimManager"
@ -617,34 +612,6 @@ static void sim_retrieve_imsi(struct ofono_sim *sim)
sim->driver->read_imsi(sim, sim_imsi_cb, sim);
}
static int create_dirs(const char *filename, const mode_t mode)
{
struct stat st;
char *dir;
const char *prev, *next;
int err;
err = stat(filename, &st);
if (!err && S_ISREG(st.st_mode))
return 0;
dir = g_malloc(strlen(filename) + 1);
strcpy(dir, "/");
for (prev = filename; (next = strchr(prev + 1, '/')); prev = next)
if (next > prev + 1) {
strncat(dir, prev + 1, next - prev);
if (mkdir(dir, mode) && errno != EEXIST) {
g_free(dir);
return -1;
}
}
g_free(dir);
return 0;
}
static void sim_op_error(struct ofono_sim *sim)
{
struct sim_file_op *op = g_queue_pop_head(sim->simop_q);
@ -769,33 +736,6 @@ static gboolean sim_op_retrieve_next(gpointer user)
return FALSE;
}
static gboolean cache_info(const char *path, const unsigned char *info, int len)
{
int fd;
int r;
if (create_dirs(path, SIM_CACHE_MODE | S_IXUSR) != 0)
return FALSE;
fd = TFR(open(path, O_WRONLY | O_CREAT, SIM_CACHE_MODE));
if (fd == -1) {
ofono_debug("Error %i creating cache file %s",
errno, path);
return FALSE;
}
r = TFR(write(fd, info, len));
TFR(close(fd));
if (r < len) {
unlink(path);
return FALSE;
}
return TRUE;
}
static void sim_op_info_cb(const struct ofono_error *error, int length,
enum ofono_sim_file_structure structure,
int record_length,
@ -838,7 +778,6 @@ static void sim_op_info_cb(const struct ofono_error *error, int length,
sim->simop_source = g_timeout_add(0, sim_op_retrieve_next, sim);
if (op->cache && imsi) {
char *path = g_strdup_printf(SIM_CACHE_PATH, imsi, op->id);
unsigned char fileinfo[6];
fileinfo[0] = error->type;
@ -848,9 +787,9 @@ static void sim_op_info_cb(const struct ofono_error *error, int length,
fileinfo[4] = record_length >> 8;
fileinfo[5] = record_length & 0xff;
op->cache = cache_info(path, fileinfo, 6);
g_free(path);
if (write_file(fileinfo, 6, SIM_CACHE_MODE,
SIM_CACHE_PATH, imsi, op->id) != 6)
op->cache = FALSE;
}
}

125
src/storage.c Normal file
View File

@ -0,0 +1,125 @@
/*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2008-2009 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*
* 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
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <glib.h>
#include <ofono/types.h>
#include "storage.h"
int create_dirs(const char *filename, const mode_t mode)
{
struct stat st;
char *dir;
const char *prev, *next;
int err;
err = stat(filename, &st);
if (!err && S_ISREG(st.st_mode))
return 0;
dir = g_malloc(strlen(filename) + 1);
strcpy(dir, "/");
for (prev = filename; (next = strchr(prev + 1, '/')); prev = next)
if (next > prev + 1) {
strncat(dir, prev + 1, next - prev);
if (mkdir(dir, mode) && errno != EEXIST) {
g_free(dir);
return -1;
}
}
g_free(dir);
return 0;
}
ssize_t read_file(unsigned char *buffer, size_t len,
const char *path_fmt, ...) {
va_list ap;
char *path;
ssize_t r;
int fd;
va_start(ap, path_fmt);
path = g_strdup_vprintf(path_fmt, ap);
va_end(ap);
fd = TFR(open(path, O_RDONLY));
g_free(path);
if (fd == -1)
return -1;
r = TFR(read(fd, buffer, len));
TFR(close(fd));
return r;
}
ssize_t write_file(const unsigned char *buffer, size_t len, mode_t mode,
const char *path_fmt, ...) {
va_list ap;
char *path;
ssize_t r;
int fd;
va_start(ap, path_fmt);
path = g_strdup_vprintf(path_fmt, ap);
va_end(ap);
if (create_dirs(path, mode | S_IXUSR) != 0) {
g_free(path);
return -1;
}
fd = TFR(open(path, O_WRONLY | O_CREAT | O_TRUNC, mode));
if (fd == -1) {
g_free(path);
return -1;
}
r = TFR(write(fd, buffer, len));
TFR(close(fd));
if (r != (ssize_t) len) {
unlink(path);
r = -1;
}
g_free(path);
return r;
}

36
src/storage.h Normal file
View File

@ -0,0 +1,36 @@
/*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2008-2009 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*
* 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
*
*/
#ifdef TEMP_FAILURE_RETRY
#define TFR TEMP_FAILURE_RETRY
#else
#define TFR
#endif
#include <fcntl.h>
int create_dirs(const char *filename, const mode_t mode);
ssize_t read_file(unsigned char *buffer, size_t len,
const char *path_fmt, ...);
ssize_t write_file(const unsigned char *buffer, size_t len, mode_t mode,
const char *path_fmt, ...);