summaryrefslogtreecommitdiff
path: root/src/stats.c
diff options
context:
space:
mode:
authorDaniel Wagner <daniel.wagner@bmw-carit.de>2010-09-23 16:04:36 +0200
committerMarcel Holtmann <marcel@holtmann.org>2010-09-23 23:35:22 +0900
commit1aa99694d8d03882a26a6526774ee49a26778457 (patch)
treee339b9be601633bd75896bc561fb1ece7b008cf1 /src/stats.c
parent357d60c6502ed5219400603f3274369d3a786a57 (diff)
downloadconnman-1aa99694d8d03882a26a6526774ee49a26778457.tar.gz
connman-1aa99694d8d03882a26a6526774ee49a26778457.tar.bz2
connman-1aa99694d8d03882a26a6526774ee49a26778457.zip
Fix "invalid file header"
Use stat before open the file to find out if the file already exists. Only if file exists and the header is invalid trigger connman_error
Diffstat (limited to 'src/stats.c')
-rw-r--r--src/stats.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/stats.c b/src/stats.c
index 6f6e2c78..b290c131 100644
--- a/src/stats.c
+++ b/src/stats.c
@@ -250,11 +250,12 @@ static int stats_open_file(struct connman_service *service,
struct stats_file *file,
connman_bool_t roaming)
{
- struct stat stat;
+ struct stat st;
char *name;
int err;
size_t size;
struct stats_file_header *hdr;
+ connman_bool_t new_file = FALSE;
if (roaming == FALSE) {
name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
@@ -265,9 +266,16 @@ static int stats_open_file(struct connman_service *service,
}
file->name = name;
+
+ err = stat(file->name, &st);
+ if (err < 0) {
+ /* according documentation the only possible error is ENOENT */
+ new_file = TRUE;
+ }
+
file->fd = open(name, O_RDWR | O_CREAT, 0644);
- if (file->fd == -1) {
+ if (file->fd < 0) {
connman_error("open error %s for %s",
strerror(errno), file->name);
return -errno;
@@ -275,14 +283,10 @@ static int stats_open_file(struct connman_service *service,
file->max_len = STATS_MAX_FILE_SIZE;
- err = fstat(file->fd, &stat);
- if (err < 0)
- return -errno;
-
- if (stat.st_size < sysconf(_SC_PAGESIZE))
+ if (st.st_size < sysconf(_SC_PAGESIZE))
size = sysconf(_SC_PAGESIZE);
else
- size = (size_t)stat.st_size;
+ size = st.st_size;
err = stats_file_remap(file, size);
if (err < 0)
@@ -295,7 +299,15 @@ static int stats_open_file(struct connman_service *service,
hdr->end < sizeof(struct stats_file_header) ||
hdr->begin > file->len ||
hdr->end > file->len) {
- connman_warn("invalid file header for %s", file->name);
+ if (new_file == FALSE) {
+ /*
+ * A newly created file can't have a correct
+ * header so we only warn if the file already
+ * existed and doesn't have a proper
+ * header.
+ */
+ connman_warn("invalid file header for %s", file->name);
+ }
hdr->magic = MAGIC;
hdr->begin = sizeof(struct stats_file_header);