Fixed handling of CFS_WRITE and CFS_APPEND

This commit is contained in:
adamdunkels 2008-01-08 14:27:06 +00:00
parent c55f320bd5
commit bac526c5f1
5 changed files with 60 additions and 15 deletions

View file

@ -30,7 +30,7 @@
* *
* Author: Adam Dunkels <adam@sics.se> * Author: Adam Dunkels <adam@sics.se>
* *
* $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" #include "cfs/cfs.h"
@ -57,7 +57,17 @@ cfs_open(const char *n, int f)
{ {
if(file.flag == FLAG_FILE_CLOSED) { if(file.flag == FLAG_FILE_CLOSED) {
file.flag = FLAG_FILE_OPEN; 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; return 1;
} else { } else {
return -1; return -1;

View file

@ -30,7 +30,7 @@
* *
* Author: Adam Dunkels <adam@sics.se> * Author: Adam Dunkels <adam@sics.se>
* *
* $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 <stdio.h> #include <stdio.h>
@ -43,7 +43,23 @@
int int
cfs_open(const char *n, int f) 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 void

View file

@ -30,7 +30,7 @@
* *
* Author: Adam Dunkels <adam@sics.se> * Author: Adam Dunkels <adam@sics.se>
* *
* $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 <string.h> #include <string.h>
@ -60,9 +60,16 @@ cfs_open(const char *n, int f)
{ {
if(file.flag == FLAG_FILE_CLOSED) { if(file.flag == FLAG_FILE_CLOSED) {
file.flag = FLAG_FILE_OPEN; file.flag = FLAG_FILE_OPEN;
file.fileptr = 0; if(f & CFS_READ) {
if((f & CFS_WRITE) && !(f & CFS_APPEND)) { file.fileptr = 0;
file.filesize = 0; }
if(f & CFS_WRITE){
if(f & CFS_APPEND) {
file.fileptr = file.filesize;
} else {
file.fileptr = 0;
file.filesize = 0;
}
} }
return 1; return 1;
} else { } else {
@ -79,6 +86,8 @@ cfs_close(int f)
int int
cfs_read(int f, void *buf, unsigned int len) 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)) { if(file.fileptr + len > sizeof(filemem)) {
len = sizeof(filemem) - file.fileptr; len = sizeof(filemem) - file.fileptr;
} }
@ -99,6 +108,9 @@ cfs_read(int f, void *buf, unsigned int len)
int int
cfs_write(int f, void *buf, unsigned int len) 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)) { if(file.fileptr >= sizeof(filemem)) {
return 0; return 0;
} }

View file

@ -30,7 +30,7 @@
* *
* Author: Adam Dunkels <adam@sics.se> * Author: Adam Dunkels <adam@sics.se>
* *
* $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" #include "cfs/cfs.h"
@ -65,10 +65,17 @@ cfs_open(const char *n, int f)
{ {
if(file.flag == FLAG_FILE_CLOSED) { if(file.flag == FLAG_FILE_CLOSED) {
file.flag = FLAG_FILE_OPEN; file.flag = FLAG_FILE_OPEN;
file.fileptr = 0; if(f & CFS_READ) {
if((f & CFS_WRITE) && !(f & CFS_APPEND)) { file.fileptr = 0;
file.filesize = 0; }
xmem_erase(CFS_XMEM_SIZE, CFS_XMEM_OFFSET); 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; return 1;
} else { } else {

View file

@ -54,7 +54,7 @@
* *
* Author: Adam Dunkels <adam@sics.se> * Author: Adam Dunkels <adam@sics.se>
* *
* $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__ #ifndef __CFS_H__
#define __CFS_H__ #define __CFS_H__
@ -115,7 +115,7 @@ struct cfs_dirent {
/** /**
* \brief Open a file. * \brief Open a file.
* \param name The name of the 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 * \return A file descriptor, if the file could be opened, or -1 if
* the file could not be opened. * the file could not be opened.
* *