forked from frapples/fulfs-filesystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_io.c
128 lines (100 loc) · 3.12 KB
/
device_io.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
#include "device_io.h"
#include<stdio.h>
#include<string.h>
#include"memory/alloc.h"
#include"utils/sys.h"
#include"utils/log.h"
struct _device_s{
char* path;
FILE* fp;
sector_no_t section_count;
};
#define MAX_DEVICE_COUNT 1024
struct _device_s* device_handle[MAX_DEVICE_COUNT] = {NULL};
int device_add(const char* path)
{
/* 已经挂载的文件就不允许别人挂载了 */
for (int i = 0; i < MAX_DEVICE_COUNT; i++) {
if (device_handle[i] != NULL && strcmp(device_handle[i]->path, path) == 0) {
log_info("%s has been mounted as a device, please uninstall it first!", path);
return DEVICE_IO_ERROR;
}
}
for (int i = 0; i < MAX_DEVICE_COUNT; i++) {
if (device_handle[i] == NULL) {
device_handle[i] = FT_NEW(struct _device_s, 1);
device_handle[i]->path = FT_NEW(char, strlen(path) + 1);
strcpy(device_handle[i]->path, path);
device_handle[i]->fp = fopen(path, "r+b");
device_handle[i]->section_count = ft_filesize_from_fp(device_handle[i]->fp) / BYTES_PER_SECTOR;
return i;
}
}
return DEVICE_IO_ERROR;
}
void device_del(device_handle_t handle)
{
if (handle < MAX_DEVICE_COUNT && handle >= 0 && device_handle[handle] != NULL) {
fclose(device_handle[handle]->fp);
ft_free(device_handle[handle]->path);
ft_free(device_handle[handle]);
device_handle[handle] = NULL;
}
}
static struct _device_s* handle_to_struct(device_handle_t handle)
{
if (!(handle < MAX_DEVICE_COUNT && handle >= 0 && device_handle[handle] != NULL)) {
return NULL;
} else {
return device_handle[handle];
}
}
int device_read(device_handle_t handle, sector_no_t sector_no, int count, char* buf)
{
struct _device_s* device = handle_to_struct(handle);
if (device == NULL) {
return DEVICE_IO_ERROR;
}
size_t offset = sector_no * BYTES_PER_SECTOR;
if (!(sector_no + count <= device->section_count)) {
count = device->section_count - sector_no;
}
if(fseek(device->fp, offset, SEEK_SET) != 0) {
return DEVICE_IO_ERROR;
}
fread(buf, BYTES_PER_SECTOR, count, device->fp);
if (ferror(device->fp)) {
return DEVICE_IO_ERROR;
} else {
return count;
}
}
int device_write(device_handle_t handle, sector_no_t sector_no, int count, const char* buf)
{
struct _device_s* device = handle_to_struct(handle);
if (device == NULL) {
return DEVICE_IO_ERROR;
}
size_t offset = sector_no * BYTES_PER_SECTOR;
if (!(sector_no + count <= device->section_count)) {
count = device->section_count - sector_no;
}
if(fseek(device->fp, offset, SEEK_SET) != 0) {
return DEVICE_IO_ERROR;
}
fwrite(buf, BYTES_PER_SECTOR, count, device->fp);
if (ferror(device->fp)) {
return DEVICE_IO_ERROR;
} else {
return count;
}
}
int device_section_count(device_handle_t handle)
{
struct _device_s* device = handle_to_struct(handle);
if (device == NULL) {
return 0;
} else {
return device->section_count;
}
}