-
Notifications
You must be signed in to change notification settings - Fork 0
/
fatdev.c
102 lines (97 loc) · 2.59 KB
/
fatdev.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
// fatdev.c : fat device operate source file
#include "fatdev.h"
fat_dev_t* fat_dev_open(const char* path, int sector_size)
{
fat_dev_t* device = NULL;
struct stat file_stat = { 0x00 };
if ((path == NULL) || (sector_size == 0))
{
printf("fatdev args is null.\r\n");
return NULL;
}
// file is exist.
if (stat(path, &file_stat) < 0)
{
printf("file %s is not exist.\r\n", path);
return NULL;
}
if (!(file_stat.st_mode & S_IFREG))
{
printf("file %s is not a file.\r\n", path);
return NULL;
}
// create fat device.
device = (fat_dev_t*)calloc(1, sizeof(fat_dev_t));
if (device == NULL)
{
printf("fatdev build failed.\r\n");
return NULL;
}
// get file size.
device->file_size = file_stat.st_size;
// open file.
device->file_hand = open(path, O_RDONLY | O_BINARY);
if (device->file_hand < 0)
{
printf("file %s open failed.\r\n", path);
free(device);
return NULL;
}
device->sector_size = sector_size;
return device;
}
int fat_dev_read(fat_dev_t* device, size_t offset, uint8_t *buff, size_t size)
{
int result = 0;
if ((device == NULL) || (device->file_hand < 0) || (buff == NULL) || (size <= 0))
{
printf("fat device read failed, parameter is null.\r\n");
return -1;
}
result = lseek(device->file_hand, offset, SEEK_SET);
if (result < 0)
{
printf("fat device read lseek offset %d failed.\r\n", offset);
return -1;
}
result = read(device->file_hand, buff, size);
if (result != size)
{
printf("fat device read failed.\r\n");
return -1;
}
return result;
}
int fat_dev_write(fat_dev_t* device, size_t offset, uint8_t *buff, size_t size)
{
int result = 0;
if ((device == NULL) || (device->file_hand < 0) || (buff == NULL) || (size <= 0))
{
printf("fat device write failed, parameter is null.\r\n");
return -1;
}
result = lseek(device->file_hand, offset, SEEK_SET);
if (result < 0)
{
printf("fat device write lseek to %d failed.\r\n", offset);
return -1;
}
result = write(device->file_hand, buff, size);
if (result != size)
{
printf("fat device write failed.\r\n");
return -1;
}
return result;
}
int fat_dev_close(fat_dev_t* device)
{
int result = 0;
if ((device == NULL) || (device->file_hand < 0))
{
printf("fat device close failed, parameter is null.\r\n");
return -1;
}
result = close(device->file_hand);
return result;
}