Update in order to use the new KBlock API.
[bertos.git] / bertos / io / kblock_file.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Francesco Sacchi <batt@develer.com>
34  *
35  * \brief KBlock interface over libc files.
36  *
37  * notest: avr
38  * notest: arm
39  */
40
41
42 #include "kblock_file.h"
43 #include <string.h>
44 #include <stdio.h>
45
46
47 static int kblockfile_load(KBlock *b, block_idx_t index)
48 {
49         KBlockFile *f = KBLOCKFILE_CAST(b);
50         fseek(f->fp, index * b->blk_size, SEEK_SET);
51         return (fread(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
52 }
53
54 static int kblockfile_store(struct KBlock *b, block_idx_t index)
55 {
56         KBlockFile *f = KBLOCKFILE_CAST(b);
57         fseek(f->fp, index * b->blk_size, SEEK_SET);
58         return (fwrite(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
59 }
60
61 static size_t kblockfile_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
62 {
63         KBlockFile *f = KBLOCKFILE_CAST(b);
64         memcpy(buf, (uint8_t *)f->b.priv.buf + offset, size);
65         return size;
66 }
67
68 static size_t kblockfile_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
69 {
70         KBlockFile *f = KBLOCKFILE_CAST(b);
71         fseek(f->fp, index * b->blk_size + offset, SEEK_SET);
72         return fread(buf, 1, size, f->fp);
73 }
74
75 static size_t kblockfile_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
76 {
77         KBlockFile *f = KBLOCKFILE_CAST(b);
78         memcpy((uint8_t *)f->b.priv.buf + offset, buf, size);
79         return size;
80 }
81
82 static int kblockfile_writeBlock(struct KBlock *b, block_idx_t index, const void *buf)
83 {
84         KBlockFile *f = KBLOCKFILE_CAST(b);
85         ASSERT(buf);
86         ASSERT(index < b->blk_cnt);
87         fseek(f->fp, index * b->blk_size, SEEK_SET);
88         return (fwrite(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
89 }
90
91 static int kblockfile_readBlock(struct KBlock *b, block_idx_t index, void *buf)
92 {
93         KBlockFile *f = KBLOCKFILE_CAST(b);
94         ASSERT(buf);
95         ASSERT(index < b->blk_cnt);
96         fseek(f->fp, index * b->blk_size, SEEK_SET);
97         return (fread(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
98 }
99
100 static int kblockfile_error(struct KBlock *b)
101 {
102         KBlockFile *f = KBLOCKFILE_CAST(b);
103         return ferror(f->fp);
104 }
105
106
107 static int kblockfile_claererr(struct KBlock *b)
108 {
109         KBlockFile *f = KBLOCKFILE_CAST(b);
110         clearerr(f->fp);
111         return 0;
112 }
113
114
115 static int kblockfile_close(struct KBlock *b)
116 {
117         KBlockFile *f = KBLOCKFILE_CAST(b);
118
119         return fflush(f->fp) | fclose(f->fp);
120 }
121
122
123 static const KBlockVTable kblockfile_hwbuffered_vt =
124 {
125         .readDirect = kblockfile_readDirect,
126         .readBuf = kblockfile_readBuf,
127         .writeBuf = kblockfile_writeBuf,
128         .load = kblockfile_load,
129         .store = kblockfile_store,
130         
131         .readBlock = kblock_swReadBlock,
132         .writeBlock = kblock_swWriteBlock,
133
134         .error = kblockfile_error,
135         .clearerr = kblockfile_claererr,
136         .close = kblockfile_close,
137 };
138
139 static const KBlockVTable kblockfile_swbuffered_vt =
140 {
141         .readDirect = kblock_swReadDirect,
142         .readBuf = kblock_swReadBuf,
143         .writeBuf = kblock_swWriteBuf,
144         .load = kblock_swLoad,
145         .store = kblock_swStore,
146         
147         .readBlock = kblockfile_readBlock,
148         .writeBlock =kblockfile_writeBlock,
149
150         .error = kblockfile_error,
151         .clearerr = kblockfile_claererr,
152         .close = kblockfile_close,
153 };
154
155 static const KBlockVTable kblockfile_unbuffered_vt =
156 {
157         .readBlock = kblockfile_readBlock,
158         .writeBlock =kblockfile_writeBlock,
159
160         .error = kblockfile_error,
161         .clearerr = kblockfile_claererr,
162         .close = kblockfile_close,
163 };
164
165
166
167 void kblockfile_init(KBlockFile *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count)
168 {
169         ASSERT(f);
170         ASSERT(fp);
171         ASSERT(block_size);
172
173         memset(f, 0, sizeof(*f));
174
175         DB(f->b.priv.type = KBT_KBLOCKFILE);
176
177         f->fp = fp;
178         f->b.blk_size = block_size;
179         f->b.blk_cnt = block_count;
180         
181         if (buf)
182         {
183                 f->b.priv.flags |= KB_BUFFERED;
184                 f->b.priv.buf = buf;
185                 if (hwbuf)
186                         f->b.priv.vt = &kblockfile_hwbuffered_vt;
187                 else
188                         f->b.priv.vt = &kblockfile_swbuffered_vt;
189                 kblockfile_load(&f->b, 0);
190                 f->b.priv.curr_blk = 0;
191                 f->b.priv.cache_dirty = false;
192         }
193         else
194                 f->b.priv.vt = &kblockfile_unbuffered_vt;
195 }