-
Notifications
You must be signed in to change notification settings - Fork 38
/
file.c
338 lines (291 loc) · 10.5 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/****************************************************************************
**
** Copyright (C) ${COPYRIGHT_YEAR} MikroElektronika d.o.o.
** Contact: https://www.mikroe.com/contact
**
** This file is part of the mikroSDK package
**
** Commercial License Usage
**
** Licensees holding valid commercial NECTO compilers AI licenses may use this
** file in accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The MikroElektronika Company.
** For licensing terms and conditions see
** https://www.mikroe.com/legal/software-license-agreement.
** For further information use the contact form at
** https://www.mikroe.com/contact.
**
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used for
** non-commercial projects under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** OF MERCHANTABILITY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
** TO THE WARRANTIES FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
** OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
/*!
* @file file.c
* @brief File System File Access and Management.
*/
#include "file.h"
#include "fs_common.h"
#include "fatfs.h"
fs_status_t file_open(file_t * file, fs_file_t fs_specific_file, const char * __generic_ptr path, int flags)
{
uint8_t vol_id;
logical_drive_t * logical_drive;
fs_status_t res;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return res;
}
res = FILE_SYSTEM_VALIDATE_FILE(fs_specific_file);
if (FSS_OK != res) {
// error: the fs_specific_file parameter is NULL
return res;
}
// check if the file can be opened
res = file_system_validate_object_opening(file);
if (FSS_OK != res) {
// error: the file is already open, or the registry is full
return res;
}
res = file_system_get_drive_id(path, &vol_id);
if (res != FSS_OK) {
// error: given path is not valid
return res;
}
// validate the logical drive
logical_drive = file_system_get_registred_volume(vol_id);
res = FILE_SYSTEM_VALIDATE_LOGICAL_DRIVE(logical_drive);
if (res != FSS_OK) {
// error: logical drive is either nullptr or is not initialized
return res;
}
// open fs_specific_file on the logical_drive
res = logical_drive->vtptr->f_open(logical_drive, fs_specific_file, path, flags);
if (FSS_OK == res) {
// initialize the file structure
file->logical_drive = logical_drive;
file->fs_file = fs_specific_file;
// register the file wtth the logical drive
++logical_drive->object_count;
// register the file wtth the file system
file_system_register_object(file);
}
return res;
}
fs_status_t file_close(file_t * file)
{
logical_drive_t * logical_drive;
fs_status_t res;
uint8_t found_object_index;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return res;
}
// check if the file is opened
res = file_system_validate_object_closing(file, &found_object_index);
if (res != FSS_OK) {
// error: the file is not open
return res;
}
// close the file->fs_file on the logical_drive
logical_drive = file->logical_drive;
res = logical_drive->vtptr->f_close(logical_drive, file->fs_file);
if (FSS_OK == res) {
// deinitialize the file_t structure
file->logical_drive = (logical_drive_t *)0;
file->fs_file = (fs_file_t)0;
// unregister the file from the logical drive
--logical_drive->object_count;
// unregister the file from the file system
file_system_unregister_object(file, found_object_index);
}
return res;
}
fs_status_t file_read(file_t * file, void * buffer, uint32_t bytes_to_read)
{
logical_drive_t * logical_drive;
fs_status_t res;
uint8_t found_object_index;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return res;
}
res = FILE_SYSTEM_VALIDATE_FILE(buffer);
if (FSS_OK != res) {
// error: the buffer parameter is NULL
return res;
}
// check if the file is open
found_object_index = file_system_check_object_registration(file);
if (FILE_SYSTEM_OBJECT_NOT_FOUND == found_object_index) {
// error: the file must be opened first
return FSS_GENERAL_ERROR;
}
// read from the file->fs_file on the logical_drive
logical_drive = file->logical_drive;
return logical_drive->vtptr->f_read(logical_drive, file->fs_file, buffer, bytes_to_read);
}
fs_status_t file_write(file_t * file, void * buffer, uint32_t bytes_to_write)
{
logical_drive_t * logical_drive;
fs_status_t res;
uint8_t found_object_index;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return res;
}
res = FILE_SYSTEM_VALIDATE_FILE(buffer);
if (FSS_OK != res) {
// error: the buffer parameter is NULL
return res;
}
// check if the file is open
found_object_index = file_system_check_object_registration(file);
if (FILE_SYSTEM_OBJECT_NOT_FOUND == found_object_index) {
// error: the file must be opened first
return FSS_GENERAL_ERROR;
}
// write to the file->fs_file on the logical_drive
logical_drive = file->logical_drive;
return logical_drive->vtptr->f_write(logical_drive, file->fs_file, buffer, bytes_to_write);
}
fs_status_t file_sync(file_t * file)
{
logical_drive_t * logical_drive;
fs_status_t res;
uint8_t found_object_index;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return res;
}
// check if the file is open
found_object_index = file_system_check_object_registration(file);
if (FILE_SYSTEM_OBJECT_NOT_FOUND == found_object_index) {
// error: the file must be opened first
return FSS_GENERAL_ERROR;
}
// sync the file->fs_file on the logical drive
logical_drive = file->logical_drive;
return logical_drive->vtptr->f_sync(logical_drive, file->fs_file);
}
fs_status_t file_seek(file_t * file, int32_t offset, logical_drive_type_t starting_position)
{
logical_drive_t * logical_drive;
fs_status_t res;
uint8_t found_object_index;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return res;
}
// check if the file is open
found_object_index = file_system_check_object_registration(file);
if (FILE_SYSTEM_OBJECT_NOT_FOUND == found_object_index) {
// error: the file must be opened first
return FSS_GENERAL_ERROR;
}
// seek position in the file->fs_file in the logical_drive
logical_drive = file->logical_drive;
return logical_drive->vtptr->f_seek(logical_drive, file->fs_file, offset, starting_position);
}
uint32_t file_tell(file_t * file)
{
logical_drive_t * logical_drive;
uint32_t res;
uint8_t found_object_index;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return 0;
}
// check if the file is open
found_object_index = file_system_check_object_registration(file);
if (FILE_SYSTEM_OBJECT_NOT_FOUND == found_object_index) {
// error: the file must be opened first
return FSS_GENERAL_ERROR; // TODO must be 0, but it manifests PIC compiler bug
}
// get current read/write pointer in the file->fs_file on the logical_drive
logical_drive = file->logical_drive;
return logical_drive->vtptr->f_tell(logical_drive, file->fs_file);
}
fs_status_t file_rewind(file_t * file)
{
logical_drive_t * logical_drive;
fs_status_t res;
uint8_t found_object_index;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return res;
}
// check if the file is open
found_object_index = file_system_check_object_registration(file);
if (FILE_SYSTEM_OBJECT_NOT_FOUND == found_object_index) {
// error: the file must be opened first
return FSS_GENERAL_ERROR;
}
// move read/write pointer to the beginning of the file->fs_file on the logical_drive
logical_drive = file->logical_drive;
return logical_drive->vtptr->f_rewind(logical_drive, file->fs_file);
}
uint32_t file_size(file_t * file)
{
logical_drive_t * logical_drive;
uint32_t res;
uint8_t found_object_index;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return 0;
}
// check if the file is open
found_object_index = file_system_check_object_registration(file);
if (FILE_SYSTEM_OBJECT_NOT_FOUND == found_object_index) {
// error: the file must be opened first
return 0;
}
// get size of the file->fs_file on the logical_drive
logical_drive = file->logical_drive;
return logical_drive->vtptr->f_size(logical_drive, file->fs_file);
}
fs_status_t file_truncate(file_t * file, uint32_t truncated_size)
{
logical_drive_t * logical_drive;
fs_status_t res;
uint8_t found_object_index;
res = FILE_SYSTEM_VALIDATE_FILE(file);
if (FSS_OK != res) {
// error: the file parameter is NULL
return res;
}
// check if the file is open
found_object_index = file_system_check_object_registration(file);
if (FILE_SYSTEM_OBJECT_NOT_FOUND == found_object_index) {
// error: the file must be opened first
return FSS_GENERAL_ERROR;
}
// truncate the file->fs_file on the logical_drive
logical_drive = file->logical_drive;
return logical_drive->vtptr->f_truncate(logical_drive, file->fs_file, truncated_size);
}