forked from yutianzeabc/File-System-Design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_open_close.c
More file actions
117 lines (100 loc) · 3.44 KB
/
Copy pathmy_open_close.c
File metadata and controls
117 lines (100 loc) · 3.44 KB
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
//
// Created by Fish on 2020/12/26.
//
#ifndef FILE_SYSTEM_DESIGN_FS_OPEN_CLOSE
#define FILE_SYSTEM_DESIGN_FS_OPEN_CLOSE
#include "fs.h"
#include "inode.h"
#include <stdio.h>
#include <string.h>
#include "c_operator.h"
int my_open(char *filename)
{
inode *curr_dir_point = NULL;
curr_dir_point = get_inode_point(curr_dir_inode);
int curr_dir_index;
curr_dir_index = curr_dir_point->direction_chart_id;
index_element *curr_dir_index_start = NULL;
curr_dir_index_start = virtualDisk + curr_dir_index * BLOCK_SIZE;
fcb *curr_dir_fcb = NULL;
curr_dir_fcb = virtualDisk + curr_dir_index_start->physical_id * BLOCK_SIZE;
int i = 0;
for (i = 0; i < MAX_OPEN_FILE; ++i) {
if ((USEROPENS[i].openlabel==1) && (strcmp(filename, USEROPENS[i].filename) == 0))
{
printf("[Error:In my_open()]:The file is already opened!\n");
return -1;
}
}
for (i = 0; i < curr_dir_point->length / sizeof(fcb); ++i)
{
if (strcmp(filename, curr_dir_fcb->filename) == 0)
{
int j = 0;
for (j = 0; j < 10; j++)
{
if (USEROPENS[j].openlabel == 0)
{
break;
}
}
if (j == 10)
{
printf("[Error:In my_open()]:The Number of open files reached the limit!\n");
return -1;
}
inode *target_file_inode = get_inode_point(curr_dir_fcb->i_ino);
//初始化USEROPEN
memcpy(USEROPENS[j].filename, filename, sizeof(char) * 20);
USEROPENS[j].inode_id = curr_dir_fcb->i_ino;
USEROPENS[j].attribute = target_file_inode->attribute;
USEROPENS[j].access = target_file_inode->access;
USEROPENS[j].time = target_file_inode->time;
USEROPENS[j].date = target_file_inode->date;
USEROPENS[j].length = target_file_inode->length;
//memcpy(USEROPENS[j].dir, curr_dir_name);
USEROPENS[j].direction_chart_id = target_file_inode->direction_chart_id;
USEROPENS[j].count = 0;
USEROPENS[j].inodestate = 0; //索引节点打开时设置为未修改
USEROPENS[j].openlabel = 1;
printf("Open %s with fd %d.\n", filename, j);
return j;
}
curr_dir_fcb++;
}
printf("[Error:In my_open()]:No such file!\n");
return -2;
}
int my_close(int fd)
{
if ((fd < 0) || (fd > 9))
{
printf("[Error:In my_close()]:Illegal fd!\n");
return -1;
}
if (USEROPENS[fd].openlabel == 0)
{
printf("[Error:In my_close()]:fd not in use!\n");
return -2;
}
if (USEROPENS[fd].inodestate == 0)
{
USEROPENS[fd].openlabel = 0;
printf("Close %s with fd %d (Not Modified).\n", USEROPENS[fd].filename, fd);
return 0;
}
else
{
inode *target_file_inode = get_inode_point(USEROPENS[fd].inode_id);
// 更新INODE
target_file_inode->attribute = USEROPENS[fd].attribute;
target_file_inode->access = USEROPENS[fd].access;
target_file_inode->time = USEROPENS[fd].time;
target_file_inode->date = USEROPENS[fd].date;
target_file_inode->length = USEROPENS[fd].length;
USEROPENS[fd].openlabel = 0;
printf("Close %s with fd %d (Modified).\n", USEROPENS[fd].filename, fd);
return 1;
}
}
#endif