-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibFS.cpp
166 lines (135 loc) · 3.13 KB
/
LibFS.cpp
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
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string>
#include "LibFS.h"
#include "LibDisk.h"
#include <sys/stat.h>
// global errno value here
int osErrno;
// Define Helper functions
// This will check if the given file name currently exists on disk
bool doesExist(const char* fileName);
int FS::FS_Boot(char *path)
{
printf("FS_Boot %s\n", path);
std::cout << "Super Block Size = " << sizeof(superBlock) << "\n";
std::cout << "bitset Size = " << sizeof(std::bitset<1000>) << "\n";
// oops, check for errors
if (Disk_Init() == -1) {
printf("Disk_Init() failed\n");
osErrno = E_GENERAL;
return -1;
}
if (doesExist(path))
{
if (Disk_Load(path))
{
osErrno = E_GENERAL;
return -1;
}
void * sector = new Sector;
Disk_Read(0, (char*)sector);
if (((superBlock*)(sector))->magic_num != MAGIC_NUM)
{
osErrno = E_GENERAL;
return -1;
}
memcpy(inodeBitmap,((char*)sector + sizeof(superBlock)), sizeof(std::bitset<INODE_NUM>));
memcpy(dataBitmap, ((char*)sector + sizeof(superBlock) + sizeof(std::bitset<INODE_NUM>)), sizeof(std::bitset<DATA_NUM>));
delete (Sector*)sector;
}
else
{
FILE *fp = fopen(path, "ab+");
if (fp == NULL)
{
osErrno = E_GENERAL;
return -1;
}
// Write the superblock into the new disk
void * buffer = new Sector();
((superBlock*)buffer)->magic_num = MAGIC_NUM;
memcpy(((char*)buffer + sizeof(superBlock)), inodeBitmap, sizeof(std::bitset<INODE_NUM>));
memcpy(((char*)buffer + sizeof(superBlock) + sizeof(std::bitset<INODE_NUM>)), dataBitmap, sizeof(std::bitset<DATA_NUM>));
Disk_Write(0, (char *)buffer);
Disk_Save(path);
delete (Sector*)buffer;
fclose(fp);
}
current_path = path;
return 0;
}
int FS::FS_Sync()
{
// Saves to disk in the file that the disk was booted from
printf("FS_Sync\n");
Disk_Save(current_path);
return 0;
}
int FS::File_Create(char *file)
{
// Needs to check the file bitmap in the second section to see if the file exists
printf("FS_Create\n");
int status = Disk_Save(file);
if (status == -1)
{
std::cout << diskErrno << "\n";
}
return 0;
}
int FS::File_Open(char *file)
{
printf("FS_Open\n");
return 0;
}
int FS::File_Read(int fd, void *buffer, int size)
{
printf("FS_Read\n");
return 0;
}
int FS::File_Write(int fd, void *buffer, int size)
{
printf("FS_Write\n");
return 0;
}
int FS::File_Seek(int fd, int offset)
{
printf("FS_Seek\n");
return 0;
}
int FS::File_Close(int fd)
{
printf("FS_Close\n");
return 0;
}
int FS::File_Unlink(char *file)
{
printf("FS_Unlink\n");
return 0;
}
// directory ops
int FS::Dir_Create(char *path)
{
printf("Dir_Create %s\n", path);
return 0;
}
int FS::Dir_Size(char *path)
{
printf("Dir_Size\n");
return 0;
}
int FS::Dir_Read(char *path, void *buffer, int size)
{
printf("Dir_Read\n");
return 0;
}
int FS::Dir_Unlink(char *path)
{
printf("Dir_Unlink\n");
return 0;
}
// Define Helper functions
bool doesExist(const char* fileName) {
struct stat buffer;
return (stat(fileName, &buffer) == 0);
}