-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys.c
289 lines (238 loc) · 7.32 KB
/
sys.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
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include "string.h"
#include "inode.h"
#include "disk_op.h"
#include "spblk.h"
#include "sbfs.h"
#include "dbg.h"
#include <time.h>
#include "datablock.h"
#include "dir.h"
#include <libgen.h>
size_t sys_write(uint32_t fd, const char *buf, size_t count, off_t offset){
log_info("SYS WRITE RECEIVED OFFSET OF %d", (int) offset);
buf = (char *) buf;
//sbfs_core_inode *c_inode = (sbfs_core_inode*) fd;
sbfs_core_inode *c_inode = iget(fd);
uint32_t file_offset;
log_info("Giving bmap with inode: %d, offset: %d", c_inode->i_nmbr, (int)offset);
int blk_nmbr = bmap(c_inode, offset, &file_offset);
log_info("File offset from bmap: %d", (int) file_offset);
//naive method, assuming everyting fits
// if(!blk_nmbr){
// c_inode->d_inode.dt_blocks[blk_nmbr] = balloc();
// }
log_info("BLOCK NMBR FROM BMAP: %d", blk_nmbr);
int i;
int number=0;
int write_count = 0;
while(number < count){
// if(number >= SBFS_BLOCK_SIZE){
// number=0;
// write_block(block_buf, blk_nmbr);
// free(block_buf);
// block_buf = calloc(1, SBFS_BLOCK_SIZE);
// blk_nmbr = bmap(c_inode, offset+write_count, &file_offset);
// read_block(block_buf, blk_nmbr);
// }
blk_nmbr = bmap(c_inode, offset+write_count, &file_offset);
char * block_buf=calloc(1, SBFS_BLOCK_SIZE);
read_block(block_buf, blk_nmbr);
for(i = file_offset; i < SBFS_BLOCK_SIZE; i++){
if(number >= count){
break;
}
block_buf[i]=buf[number];
number++;
}
write_block(block_buf, blk_nmbr);
free(block_buf);
}
//file_offset er offset innenfor blokken
//offset er totalt offset
//count er hvor langt du skal skrive fra offset
//total størrelse er gitt
if(offset+count > (c_inode->d_inode.size)){
c_inode->d_inode.size = offset+count;
log_info("Updating size of inode to: %d", (int) (offset+count));
}
c_inode->status = 1;
c_inode->d_inode.a_time = time(NULL);
c_inode->d_inode.c_time = time(NULL);
iput(c_inode);
free(c_inode);
return number;
}
size_t sys_read(uint32_t fd, char *buf, size_t count, off_t offset){
// log_info("inode_number passed to iget from read: %d", fd);
// sbfs_core_inode *c_node = iget(fd);
// int number=0;
// uint8_t file_offset;
// if(!buf){
// return number;
// }
// int blk_nmbr = bmap(c_node, offset, &file_offset);
// number = file_offset;
// char block_buf[SBFS_BLOCK_SIZE];
// read_block(block_buf, blk_nmbr);
// int read_count = 0;
// while(read_count < count && read_count < c_node->d_inode.size){
// if(number >= SBFS_BLOCK_SIZE){
// blk_nmbr = bmap(c_node, offset+read_count, &file_offset);
// number = file_offset;
// read_block(block_buf, blk_nmbr);
// }
// buf[read_count] = block_buf[file_offset+number];
// number++;
// read_count++;
// }
// return read_count;
sbfs_core_inode* inode = iget(fd);
uint32_t file_offset;
uint32_t blk_nmr;
if (!inode)
return -1;
int read_count = 0;
while (read_count < count) {
if (offset < inode->d_inode.size) {
blk_nmr = bmap(inode, offset, &file_offset);
if (blk_nmr <= 0)
break;
} else
break;
char * block_buf=calloc(1, SBFS_BLOCK_SIZE);
read_block(block_buf, blk_nmr);
int i;
for (i = file_offset; i < SBFS_BLOCK_SIZE; i++)
{
if (read_count >= count)
break;
buf[read_count] = block_buf[i];
read_count++;
}
offset += i - file_offset;
free(block_buf);
}
free(inode);
return count;
}
uint32_t sys_open(const char *path, int flags, mode_t mode){
sbfs_core_inode *inode = namei(path);
if(!inode){
free(inode);
return -ENOENT;
}else {
int i_nmbr = inode->i_nmbr;
free(inode);
return (i_nmbr);
}
}
uint32_t sys_mknod(const char *path, uint8_t file_t, mode_t mode){
log_info("sys_mknod FILE TYPE: %d", file_t);
sbfs_core_inode *inode = namei(path);
if(inode != NULL){
free(inode);
return -1; //file already exists
}
char *parentc, *parent, *filenamec, *filename;
parentc = strdup(path);
filenamec = strdup(path);
filename = basename(filenamec);
parent = dirname(parentc);
if(filename != NULL ){
inode = namei(parent);
if(inode == NULL) {
free(parentc);
free(filenamec);
return -1;
}
}
//struct dir_entry root_entries[1];
struct dir_entry *entry = calloc(1, sizeof(struct dir_entry));
sbfs_core_inode *new_inode = ialloc();
strcpy(entry->name,filename);
entry->inode_number = new_inode->i_nmbr;
log_info("MKNOD NEW ENTRY: %s, with i_nmbr: %d, with parent: %s with inmbr: %d and size: %d", path, (int) entry->inode_number, parent, inode->i_nmbr, (int)inode->d_inode.size);
entry->offset = sizeof(struct dir_entry);
entry->file_t = file_t;
//root_entries[0] = *entry;
inode->status = 1;
inode->d_inode.m_time = time(NULL);
inode->d_inode.a_time = time(NULL);
//sys_write(inode->i_nmbr, (char * )root_entries, sizeof(root_entries), inode->d_inode.size);
log_info("NEW INODE NMBR: %d", new_inode->i_nmbr);
add_dir(inode->i_nmbr, entry);
new_inode->d_inode.type = file_t;
new_inode->d_inode.perm = 0777;
new_inode->d_inode.dt_blocks[0] = balloc();
new_inode->status = 1;
new_inode->d_inode.m_time = time(NULL);
new_inode->d_inode.c_time = time(NULL);
new_inode->d_inode.a_time = time(NULL);
new_inode->d_inode.link_count = 2;
new_inode->d_inode.user_id = getuid();
new_inode->d_inode.grp_id = getgid();
iput(new_inode);
if(file_t == 2){
//struct dir_entry dir_entries[2];
struct dir_entry *dot_entry = malloc(sizeof(struct dir_entry));
strcpy(dot_entry->name, ".");
dot_entry->inode_number = new_inode->i_nmbr;
dot_entry->offset = sizeof(struct dir_entry);
dot_entry->file_t = 2;
add_dir(new_inode->i_nmbr, dot_entry);
struct dir_entry *dot_dot_entry = malloc(sizeof(struct dir_entry));
strcpy(dot_dot_entry->name, "..");
dot_dot_entry->inode_number = new_inode->i_nmbr;
dot_dot_entry->offset = sizeof(struct dir_entry);
dot_dot_entry->file_t = 2;
// dir_entries[0] = *dot_entry;
// dir_entries[1] = *dot_dot_entry; ;
add_dir(new_inode->i_nmbr, dot_dot_entry);
//sys_write(new_inode->i_nmbr, (char *) &dir_entries, sizeof(dir_entries), 0);
free(dot_dot_entry);
free(dot_entry);
}
free(parentc);
free(filenamec);
// free(inode);
// free(new_inode);
// free(entry->name);
// free(entry);
return 0;
}
int sys_mkdir(const char *path, mode_t mode){
const char *pathname = path;
int ret = sys_mknod(pathname, 2, mode);
return ret;
}
int sys_unlink(char *path){
char *parentc, *parent;
parentc = strdup(path);
parent = dirname(parentc);
sbfs_core_inode *p_inode = namei(parent);
sbfs_core_inode *c_inode = namei(path);
int increment = -1;
while(1){
struct dir_entry *entry = malloc(sizeof(struct dir_entry));
increment++;
entry = get_dir(p_inode, increment*(sizeof(struct dir_entry)));
if((entry == NULL)){ //end of entries for this directory
return -1;
}
if(entry->inode_number == c_inode->i_nmbr){
strcpy(entry->name, "\0");
entry->inode_number = 0;
c_inode->d_inode.link_count -= 1;
sys_write(p_inode->i_nmbr, (char *) entry, sizeof(entry), increment*(sizeof(struct dir_entry)));
//add_dir(p_inode->i_nmbr, entry);
iput(p_inode);
iput(c_inode); //iput takes care of delegating to bfree and ifree
return 0;
}
}
return 0;
}