From bac526c5f1347ac29afc1e6ff3366e12aaf68b76 Mon Sep 17 00:00:00 2001 From: adamdunkels Date: Tue, 8 Jan 2008 14:27:06 +0000 Subject: [PATCH] Fixed handling of CFS_WRITE and CFS_APPEND --- core/cfs/cfs-eeprom.c | 14 ++++++++++++-- core/cfs/cfs-posix.c | 20 ++++++++++++++++++-- core/cfs/cfs-ram.c | 20 ++++++++++++++++---- core/cfs/cfs-xmem.c | 17 ++++++++++++----- core/cfs/cfs.h | 4 ++-- 5 files changed, 60 insertions(+), 15 deletions(-) diff --git a/core/cfs/cfs-eeprom.c b/core/cfs/cfs-eeprom.c index be3cc7675..1203bd951 100644 --- a/core/cfs/cfs-eeprom.c +++ b/core/cfs/cfs-eeprom.c @@ -30,7 +30,7 @@ * * Author: Adam Dunkels * - * $Id: cfs-eeprom.c,v 1.4 2007/11/22 11:29:13 oliverschmidt Exp $ + * $Id: cfs-eeprom.c,v 1.5 2008/01/08 14:27:06 adamdunkels Exp $ */ #include "cfs/cfs.h" @@ -57,7 +57,17 @@ cfs_open(const char *n, int f) { if(file.flag == FLAG_FILE_CLOSED) { file.flag = FLAG_FILE_OPEN; - file.fileptr = 0; + if(f & CFS_READ) { + file.fileptr = 0; + } + if(f & CFS_WRITE){ + if(f & CFS_APPEND) { + file.fileptr = file.filesize; + } else { + file.fileptr = 0; + file.filesize = 0; + } + } return 1; } else { return -1; diff --git a/core/cfs/cfs-posix.c b/core/cfs/cfs-posix.c index d0a6ae850..bd4310ae0 100644 --- a/core/cfs/cfs-posix.c +++ b/core/cfs/cfs-posix.c @@ -30,7 +30,7 @@ * * Author: Adam Dunkels * - * $Id: cfs-posix.c,v 1.8 2007/12/23 14:12:44 oliverschmidt Exp $ + * $Id: cfs-posix.c,v 1.9 2008/01/08 14:27:06 adamdunkels Exp $ */ #include @@ -43,7 +43,23 @@ int cfs_open(const char *n, int f) { - return open(n, f == CFS_READ? O_RDONLY: O_CREAT|O_TRUNC|O_RDWR); + int s = 0; + if(f == CFS_READ) { + s = O_RDONLY; + } else if(f & CFS_WRITE) { + s = O_CREAT; + if(f & CFS_READ) { + s |= O_RDWR; + } else { + s |= O_WRONLY; + } + if(f & CFS_APPEND) { + s |= O_APPEND; + } else { + s |= O_TRUNC; + } + } + return open(n, s); } /*---------------------------------------------------------------------------*/ void diff --git a/core/cfs/cfs-ram.c b/core/cfs/cfs-ram.c index 6d14bff1f..50a614cb6 100644 --- a/core/cfs/cfs-ram.c +++ b/core/cfs/cfs-ram.c @@ -30,7 +30,7 @@ * * Author: Adam Dunkels * - * $Id: cfs-ram.c,v 1.5 2007/11/22 11:29:13 oliverschmidt Exp $ + * $Id: cfs-ram.c,v 1.6 2008/01/08 14:27:06 adamdunkels Exp $ */ #include @@ -60,9 +60,16 @@ cfs_open(const char *n, int f) { if(file.flag == FLAG_FILE_CLOSED) { file.flag = FLAG_FILE_OPEN; - file.fileptr = 0; - if((f & CFS_WRITE) && !(f & CFS_APPEND)) { - file.filesize = 0; + if(f & CFS_READ) { + file.fileptr = 0; + } + if(f & CFS_WRITE){ + if(f & CFS_APPEND) { + file.fileptr = file.filesize; + } else { + file.fileptr = 0; + file.filesize = 0; + } } return 1; } else { @@ -79,6 +86,8 @@ cfs_close(int f) int cfs_read(int f, void *buf, unsigned int len) { + printf("read file.fileptr %d len %d filesize %d\n", + file.fileptr, len, file.filesize); if(file.fileptr + len > sizeof(filemem)) { len = sizeof(filemem) - file.fileptr; } @@ -99,6 +108,9 @@ cfs_read(int f, void *buf, unsigned int len) int cfs_write(int f, void *buf, unsigned int len) { + printf("write file.fileptr %d len %d filesize %d\n", + file.fileptr, len, file.filesize); + if(file.fileptr >= sizeof(filemem)) { return 0; } diff --git a/core/cfs/cfs-xmem.c b/core/cfs/cfs-xmem.c index d2216187d..9d4acb3bf 100644 --- a/core/cfs/cfs-xmem.c +++ b/core/cfs/cfs-xmem.c @@ -30,7 +30,7 @@ * * Author: Adam Dunkels * - * $Id: cfs-xmem.c,v 1.6 2007/11/22 11:29:13 oliverschmidt Exp $ + * $Id: cfs-xmem.c,v 1.7 2008/01/08 14:27:06 adamdunkels Exp $ */ #include "cfs/cfs.h" @@ -65,10 +65,17 @@ cfs_open(const char *n, int f) { if(file.flag == FLAG_FILE_CLOSED) { file.flag = FLAG_FILE_OPEN; - file.fileptr = 0; - if((f & CFS_WRITE) && !(f & CFS_APPEND)) { - file.filesize = 0; - xmem_erase(CFS_XMEM_SIZE, CFS_XMEM_OFFSET); + if(f & CFS_READ) { + file.fileptr = 0; + } + if(f & CFS_WRITE){ + if(f & CFS_APPEND) { + file.fileptr = file.filesize; + } else { + file.fileptr = 0; + file.filesize = 0; + xmem_erase(CFS_XMEM_SIZE, CFS_XMEM_OFFSET); + } } return 1; } else { diff --git a/core/cfs/cfs.h b/core/cfs/cfs.h index d44689183..f852785c6 100644 --- a/core/cfs/cfs.h +++ b/core/cfs/cfs.h @@ -54,7 +54,7 @@ * * Author: Adam Dunkels * - * $Id: cfs.h,v 1.9 2007/12/23 15:22:33 oliverschmidt Exp $ + * $Id: cfs.h,v 1.10 2008/01/08 14:27:06 adamdunkels Exp $ */ #ifndef __CFS_H__ #define __CFS_H__ @@ -115,7 +115,7 @@ struct cfs_dirent { /** * \brief Open a file. * \param name The name of the file. - * \param flags CFS_READ, or CFS_WRITE, or both. + * \param flags CFS_READ, or CFS_WRITE/CFS_APPEND, or both. * \return A file descriptor, if the file could be opened, or -1 if * the file could not be opened. *