-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfs_api.c
40 lines (38 loc) · 1.19 KB
/
mfs_api.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
#include "mfs_api.h"
#include <assert.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* if flag contains O_CREAT, len is needed */
int32_t MfsHashOpen(SuperBlock* super_block, char* file_name,
FileInfo** file_info, int32_t flag, MfsHashBlock* hash_block, uint32_t len)
{
mfs_idx mfsidx;
uint32_t fd = -1;
if (MfsHashFind(&mfsidx, file_name, hash_block) < 0) {
if (flag & O_CREAT) {
(*file_info) = FileInfoCreate(len, super_block);
if (MfsHashPut(file_name, *file_info, hash_block) < 0)
return -1;
if ((fd = MfsOpen(
(*file_info)->file_name, file_info, super_block, flag))
>= 0) {
assert(fd > 0);
return fd;
} else
return -1;
} else {
return -1;
}
} else {
char name_buf[NAME_MAX_LEN] = { "\0" };
sprintf(name_buf, "%u-%u", GET_BLOCK_ID(mfsidx), GET_FILE_ID(mfsidx));
if ((fd = MfsOpen(name_buf, file_info, super_block, flag)) >= 0) {
assert(fd > 0);
return fd;
} else
return -1;
}
}