57c2ee8c7838a5218e81ef75c4082239ccf8a285
[bertos.git] / bertos / io / kblock_posix.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_posix.h"
43 #include <string.h>
44 #include <stdio.h>
45
46
47 static int kblockposix_load(KBlock *b, block_idx_t index)
48 {
49         KBlockPosix *f = KBLOCKPOSIX_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 kblockposix_store(struct KBlock *b, block_idx_t index)
55 {
56         KBlockPosix *f = kblockposix_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 kblockposix_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
62 {
63         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
64         memcpy(buf, (uint8_t *)f->b.priv.buf + offset, size);
65         return size;
66 }
67
68 static size_t kblockposix_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
69 {
70         KBlockPosix *f = KBLOCKPOSIX_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 kblockposix_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
76 {
77         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
78         memcpy((uint8_t *)f->b.priv.buf + offset, buf, size);
79         return size;
80 }
81
82 static size_t kblockposix_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size)
83 {
84         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
85         ASSERT(buf);
86         ASSERT(index < b->blk_cnt);
87         fseek(f->fp, index * b->blk_size + offset, SEEK_SET);
88         return fwrite(buf, 1, size, f->fp);
89 }
90
91 static int kblockposix_error(struct KBlock *b)
92 {
93         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
94         return ferror(f->fp);
95 }
96
97
98 static int kblockposix_claererr(struct KBlock *b)
99 {
100         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
101         clearerr(f->fp);
102         return 0;
103 }
104
105
106 static int kblockposix_close(struct KBlock *b)
107 {
108         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
109
110         return fflush(f->fp) | fclose(f->fp);
111 }
112
113
114 static const KBlockVTable kblockposix_hwbuffered_vt =
115 {
116         .readDirect = kblockposix_readDirect,
117
118         .readBuf = kblockposix_readBuf,
119         .writeBuf = kblockposix_writeBuf,
120         .load = kblockposix_load,
121         .store = kblockposix_store,
122
123         .error = kblockposix_error,
124         .clearerr = kblockposix_claererr,
125         .close = kblockposix_close,
126 };
127
128 static const KBlockVTable kblockposix_swbuffered_vt =
129 {
130         .readDirect = kblockposix_readDirect,
131         .writeDirect =kblockposix_writeDirect,
132
133         .readBuf = kblock_swReadBuf,
134         .writeBuf = kblock_swWriteBuf,
135         .load = kblock_swLoad,
136         .store = kblock_swStore,
137
138         .error = kblockposix_error,
139         .clearerr = kblockposix_claererr,
140         .close = kblockposix_close,
141 };
142
143 static const KBlockVTable kblockposix_unbuffered_vt =
144 {
145         .readDirect = kblockposix_readDirect,
146         .writeDirect =kblockposix_writeDirect,
147
148         .error = kblockposix_error,
149         .clearerr = kblockposix_claererr,
150         .close = kblockposix_close,
151 };
152
153
154
155 void kblockposix_init(KBlockPosix *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count)
156 {
157         ASSERT(f);
158         ASSERT(fp);
159         ASSERT(block_size);
160
161         memset(f, 0, sizeof(*f));
162
163         DB(f->b.priv.type = KBT_KBLOCKPOSIX);
164
165         f->fp = fp;
166         f->b.blk_size = block_size;
167         f->b.blk_cnt = block_count;
168
169         f->b.priv.flags |= KB_PARTIAL_WRITE;
170         if (buf)
171         {
172                 f->b.priv.flags |= KB_BUFFERED;
173                 f->b.priv.buf = buf;
174                 if (hwbuf)
175                         f->b.priv.vt = &kblockposix_hwbuffered_vt;
176                 else
177                         f->b.priv.vt = &kblockposix_swbuffered_vt;
178                 kblockposix_load(&f->b, 0);
179                 f->b.priv.curr_blk = 0;
180         }
181         else
182                 f->b.priv.vt = &kblockposix_unbuffered_vt;
183 }