cfs-xmem.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "cfs/cfs.h"
00037 #include "dev/xmem.h"
00038
00039 struct filestate {
00040 int flag;
00041 #define FLAG_FILE_CLOSED 0
00042 #define FLAG_FILE_OPEN 1
00043 unsigned int fileptr;
00044 unsigned int filesize;
00045 };
00046
00047 #ifdef CFS_XMEM_CONF_OFFSET
00048 #define CFS_XMEM_OFFSET CFS_XMEM_CONF_OFFSET
00049 #else
00050 #define CFS_XMEM_OFFSET 0
00051 #endif
00052
00053
00054 #ifdef CFS_XMEM_CONF_SIZE
00055 #define CFS_XMEM_SIZE CFS_XMEM_CONF_SIZE
00056 #else
00057 #define CFS_XMEM_SIZE XMEM_ERASE_UNIT_SIZE
00058 #endif
00059
00060 static struct filestate file;
00061
00062
00063 int
00064 cfs_open(const char *n, int f)
00065 {
00066 if(file.flag == FLAG_FILE_CLOSED) {
00067 file.flag = FLAG_FILE_OPEN;
00068 if(f & CFS_READ) {
00069 file.fileptr = 0;
00070 }
00071 if(f & CFS_WRITE){
00072 if(f & CFS_APPEND) {
00073 file.fileptr = file.filesize;
00074 } else {
00075 file.fileptr = 0;
00076 file.filesize = 0;
00077 xmem_erase(CFS_XMEM_SIZE, CFS_XMEM_OFFSET);
00078 }
00079 }
00080 return 1;
00081 } else {
00082 return -1;
00083 }
00084 }
00085
00086 void
00087 cfs_close(int f)
00088 {
00089 file.flag = FLAG_FILE_CLOSED;
00090 }
00091
00092 int
00093 cfs_read(int f, void *buf, unsigned int len)
00094 {
00095 if(file.fileptr + len > CFS_XMEM_SIZE) {
00096 len = CFS_XMEM_SIZE - file.fileptr;
00097 }
00098
00099 if(file.fileptr + len > file.filesize) {
00100 len = file.filesize - file.fileptr;
00101 }
00102
00103 if(f == 1) {
00104 xmem_pread(buf, len, CFS_XMEM_OFFSET + file.fileptr);
00105 file.fileptr += len;
00106 return len;
00107 } else {
00108 return -1;
00109 }
00110 }
00111
00112 int
00113 cfs_write(int f, const void *buf, unsigned int len)
00114 {
00115 if(file.fileptr >= CFS_XMEM_SIZE) {
00116 return 0;
00117 }
00118 if(file.fileptr + len > CFS_XMEM_SIZE) {
00119 len = CFS_XMEM_SIZE - file.fileptr;
00120 }
00121
00122 if(file.fileptr + len > file.filesize) {
00123
00124 file.filesize = file.fileptr + len;
00125 }
00126
00127 if(f == 1) {
00128 xmem_pwrite(buf, len, CFS_XMEM_OFFSET + file.fileptr);
00129 file.fileptr += len;
00130 return len;
00131 } else {
00132 return -1;
00133 }
00134 }
00135
00136 cfs_offset_t
00137 cfs_seek(int f, cfs_offset_t o, int w)
00138 {
00139 if(w == CFS_SEEK_SET && f == 1) {
00140 if(o > file.filesize) {
00141 o = file.filesize;
00142 }
00143 file.fileptr = o;
00144 return o;
00145 }
00146 return -1;
00147 }
00148
00149 int
00150 cfs_remove(const char *name)
00151 {
00152 file.flag = FLAG_FILE_CLOSED;
00153 file.fileptr = 0;
00154 file.filesize = 0;
00155 xmem_erase(CFS_XMEM_SIZE, CFS_XMEM_OFFSET);
00156 return 0;
00157 }
00158
00159 int
00160 cfs_opendir(struct cfs_dir *p, const char *n)
00161 {
00162 return -1;
00163 }
00164
00165 int
00166 cfs_readdir(struct cfs_dir *p, struct cfs_dirent *e)
00167 {
00168 return -1;
00169 }
00170
00171 void
00172 cfs_closedir(struct cfs_dir *p)
00173 {
00174 }
00175