67a4a779b2d37aba2ad79e3e9d8f922d794494b2
[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->pagebuf, 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->pagebuf, 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, f->pagebuf + 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(f->pagebuf + offset, buf, size);
79         return size;
80 }
81
82 static int kblockfile_error(struct KBlock *b)
83 {
84         KBlockFile *f = KBLOCKFILE_CAST(b);
85         return ferror(f->fp);
86 }
87
88
89 static int kblockfile_claererr(struct KBlock *b)
90 {
91         KBlockFile *f = KBLOCKFILE_CAST(b);
92         clearerr(f->fp);
93         return 0;
94 }
95
96
97 static int kblockfile_close(struct KBlock *b)
98 {
99         KBlockFile *f = KBLOCKFILE_CAST(b);
100
101         return fflush(f->fp) | fclose(f->fp);
102 }
103
104
105 static KBlockVTable kblockfile_vt =
106 {
107         .readDirect = kblockfile_readDirect,
108         .readBuf = kblockfile_readBuf,
109         .writeBuf = kblockfile_writeBuf,
110         .load = kblockfile_load,
111         .store = kblockfile_store,
112
113         .error = kblockfile_error,
114         .clearerr = kblockfile_claererr,
115         .close = kblockfile_close,
116 };
117
118
119 void kblockfile_init(KBlockFile *f, FILE *fp, void *buf, size_t block_size, block_idx_t block_count)
120 {
121         ASSERT(f);
122         ASSERT(fp);
123         ASSERT(buf);
124         ASSERT(block_size);
125
126         memset(f, 0, sizeof(*f));
127
128         DB(f->b.priv.type = KBT_KBLOCKFILE);
129
130         f->fp = fp;
131         f->pagebuf = buf;
132         f->b.blk_size = block_size;
133         f->b.blk_cnt = block_count;
134         f->b.priv.vt = &kblockfile_vt;
135         kblockfile_load(&f->b, 0);
136         f->b.priv.curr_blk = 0;
137         f->b.priv.cache_dirty = false;
138 }