Skip to content

Commit 594ded1

Browse files
committed
BUG FIXES
1 parent 35f7ab9 commit 594ded1

File tree

4 files changed

+36
-67
lines changed

4 files changed

+36
-67
lines changed

file_operation.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int create_file(char *name)
105105
{
106106
string filename = string(name);
107107
//check if file already exist in disk
108-
if (dir_map.find(filename) != dir_map.end())
108+
if (file_to_inode_map.find(filename) != file_to_inode_map.end())
109109
{
110110
cout << string(RED) << "Create File Error : File already present !!!" << string(DEFAULT) << endl;
111111
return -1;
@@ -135,7 +135,7 @@ int create_file(char *name)
135135
inode_arr[next_avl_inode].pointer[0] = next_avl_datablock;
136136
inode_arr[next_avl_inode].filesize = 0;
137137

138-
dir_map[filename] = next_avl_inode;
138+
file_to_inode_map[filename] = next_avl_inode;
139139
inode_to_file_map[next_avl_inode] = filename;
140140

141141
file_inode_mapping_arr[next_avl_inode].inode_num = next_avl_inode;
@@ -150,14 +150,14 @@ int delete_file(char *name)
150150
string filename = string(name);
151151

152152
//check if file exist or not
153-
if (dir_map.find(filename) == dir_map.end())
153+
if (file_to_inode_map.find(filename) == file_to_inode_map.end())
154154
{
155155
cout << string(RED) << "Delete File Error : File doesn't exist !!!" << string(DEFAULT) << endl;
156156
return -1;
157157
}
158158

159159
//getting inode of file
160-
int cur_inode = dir_map[filename];
160+
int cur_inode = file_to_inode_map[filename];
161161

162162
for (int i = 0; i < NO_OF_FILE_DESCRIPTORS; i++)
163163
{
@@ -175,8 +175,8 @@ int delete_file(char *name)
175175
strcpy(file_inode_mapping_arr[cur_inode].file_name, emptyname);
176176
file_inode_mapping_arr[cur_inode].inode_num = -1;
177177

178-
inode_to_file_map.erase(dir_map[filename]);
179-
dir_map.erase(filename);
178+
inode_to_file_map.erase(file_to_inode_map[filename]);
179+
file_to_inode_map.erase(filename);
180180

181181
cout << string(GREEN) << "File Deleted successfully :) " << string(DEFAULT) << endl;
182182

@@ -186,7 +186,7 @@ int delete_file(char *name)
186186
int open_file(char *name)
187187
{
188188
string filename = string(name);
189-
if (dir_map.find(filename) == dir_map.end())
189+
if (file_to_inode_map.find(filename) == file_to_inode_map.end())
190190
{
191191
cout << string(RED) << "Open File Error : File not found !!!" << string(DEFAULT) << endl;
192192
return -1;
@@ -209,7 +209,7 @@ int open_file(char *name)
209209
}
210210
} while (file_mode < 0 || file_mode > 2);
211211

212-
int cur_inode = dir_map[filename];
212+
int cur_inode = file_to_inode_map[filename];
213213

214214
/* checking if file is already open in write or append mode. */
215215
if (file_mode == 1 || file_mode == 2)

file_read_write.cpp

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,6 @@ string user_input()
1717
return s;
1818
}
1919

20-
int _block_write(int block, char *buf, int size, int start_position)
21-
{
22-
23-
if ((block < 0) || (block >= DISK_BLOCKS))
24-
{
25-
fprintf(stderr, "block_write: block index out of bounds\n");
26-
return -1;
27-
}
28-
29-
if (fseek(diskptr, (block * BLOCK_SIZE) + start_position, SEEK_SET) < 0)
30-
{
31-
perror("block_write: failed to lseek");
32-
return -1;
33-
}
34-
35-
if (fwrite(buf, sizeof(char), size, diskptr) < 0)
36-
{
37-
perror("block_write: failed to write");
38-
return -1;
39-
}
40-
41-
return 0;
42-
}
43-
4420
int _write_into_file(int fd, char *buff, int len, int *bytes_written)
4521
{
4622

@@ -55,7 +31,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
5531
if (filled_data_block < 10)
5632
{
5733
int data_block_to_write = inode_arr[file_inode].pointer[filled_data_block];
58-
_block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
34+
block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
5935
inode_arr[file_inode].filesize += len;
6036
file_descriptor_map[fd].second += len; //updating the cur pos in the file
6137
*bytes_written += len;
@@ -70,7 +46,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
7046
memcpy(block_pointers, read_buf, sizeof(read_buf));
7147

7248
int data_block_to_write = block_pointers[filled_data_block - 10];
73-
_block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
49+
block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
7450
inode_arr[file_inode].filesize += len;
7551
file_descriptor_map[fd].second += len; //updating the cur pos in the file
7652
*bytes_written += len;
@@ -90,7 +66,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
9066
memcpy(block_pointers2, read_buf, sizeof(read_buf));
9167

9268
int data_block_to_write = block_pointers[(filled_data_block - 1034) / 1024];
93-
_block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
69+
block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
9470
inode_arr[file_inode].filesize += len;
9571
file_descriptor_map[fd].second += len; //updating the cur pos in the file
9672
*bytes_written += len;
@@ -102,7 +78,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
10278
/* if filesize = 0 means file is empty then start writting into file from first direct block. */
10379
if (inode_arr[file_descriptor_map[fd].first].filesize == 0)
10480
{
105-
_block_write(inode_arr[file_inode].pointer[0], buff, len, 0);
81+
block_write(inode_arr[file_inode].pointer[0], buff, len, 0);
10682
inode_arr[file_inode].filesize += len;
10783
file_descriptor_map[fd].second += len;
10884
*bytes_written += len;
@@ -126,7 +102,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
126102
free_data_block_vector.pop_back();
127103
inode_arr[file_inode].pointer[0] = next_avl_datablock;
128104
}
129-
_block_write(inode_arr[file_inode].pointer[0], buff, len, 0);
105+
block_write(inode_arr[file_inode].pointer[0], buff, len, 0);
130106
inode_arr[file_inode].filesize += len;
131107
file_descriptor_map[fd].second += len;
132108
*bytes_written += len;
@@ -145,7 +121,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
145121
free_data_block_vector.pop_back();
146122
inode_arr[file_inode].pointer[filled_data_block] = data_block_to_write;
147123

148-
_block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
124+
block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
149125
inode_arr[file_inode].filesize += len;
150126
file_descriptor_map[fd].second += len; //updating the cur pos in the file
151127
*bytes_written += len;
@@ -172,7 +148,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
172148
char temp_buf[BLOCK_SIZE];
173149
memcpy(temp_buf, block_pointers, BLOCK_SIZE);
174150

175-
_block_write(data_block_single_indirect, temp_buf, BLOCK_SIZE, 0);
151+
block_write(data_block_single_indirect, temp_buf, BLOCK_SIZE, 0);
176152
}
177153

178154
int block = inode_arr[file_inode].pointer[10];
@@ -194,10 +170,10 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
194170
block_pointers[filled_data_block - 10] = data_block_to_write;
195171
char temp_buf[BLOCK_SIZE];
196172
memcpy(temp_buf, block_pointers, BLOCK_SIZE);
197-
_block_write(block, temp_buf, BLOCK_SIZE, 0);
173+
block_write(block, temp_buf, BLOCK_SIZE, 0);
198174

199175
//write data into DB
200-
_block_write(data_block_to_write, buff, len, 0);
176+
block_write(data_block_to_write, buff, len, 0);
201177
inode_arr[file_inode].filesize += len;
202178
file_descriptor_map[fd].second += len; //updating the cur pos in the file
203179
*bytes_written += len;
@@ -223,7 +199,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
223199
char temp_buf[BLOCK_SIZE];
224200
memcpy(temp_buf, block_pointers, BLOCK_SIZE);
225201

226-
_block_write(data_block_double_indirect, temp_buf, BLOCK_SIZE, 0);
202+
block_write(data_block_double_indirect, temp_buf, BLOCK_SIZE, 0);
227203
}
228204
if ((filled_data_block - 1034) % 1024 == 0) //i.e if filled_data_block is multiple of 1024 means need new DB to be assigned
229205
{
@@ -248,10 +224,10 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
248224
block_pointers[(filled_data_block - 1034) / 1024] = data_block_double_indirect2;
249225
char temp_buf[BLOCK_SIZE];
250226
memcpy(temp_buf, block_pointers2, BLOCK_SIZE);
251-
_block_write(data_block_double_indirect2, temp_buf, BLOCK_SIZE, 0);
227+
block_write(data_block_double_indirect2, temp_buf, BLOCK_SIZE, 0);
252228

253229
memcpy(temp_buf, block_pointers, BLOCK_SIZE);
254-
_block_write(block, temp_buf, BLOCK_SIZE, 0);
230+
block_write(block, temp_buf, BLOCK_SIZE, 0);
255231
}
256232

257233
int block = inode_arr[file_inode].pointer[11];
@@ -275,12 +251,12 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
275251
int data_block_to_write = free_data_block_vector.back(); //to store block_pointers[1024] into db_for_double_indirect
276252
free_data_block_vector.pop_back();
277253
block_pointers2[(filled_data_block - 1034) % 1024] = data_block_to_write;
278-
_block_write(data_block_to_write, buff, len, 0); //writing data into db_to_write DB
254+
block_write(data_block_to_write, buff, len, 0); //writing data into db_to_write DB
279255

280256
//now restore block_pointers2 back to the block2
281257
char temp_buf[BLOCK_SIZE];
282258
memcpy(temp_buf, block_pointers2, BLOCK_SIZE);
283-
_block_write(block2, temp_buf, BLOCK_SIZE, 0);
259+
block_write(block2, temp_buf, BLOCK_SIZE, 0);
284260

285261
//updating the filesize
286262
inode_arr[file_inode].filesize += len;

inode.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct super_block sb;
88
struct file_to_inode_mapping file_inode_mapping_arr[NO_OF_INODES];
99
struct inode inode_arr[NO_OF_INODES];
1010

11-
map<string, int> dir_map; //file name as key maps to inode (value)
11+
map<string, int> file_to_inode_map; //file name as key maps to inode (value)
1212
vector<int> free_inode_vector; //denote free inodes
1313
vector<int> free_data_block_vector; //denote free data blocks
1414
vector<int> free_filedescriptor_vector; //denote free filedescriptor.
@@ -152,7 +152,7 @@ int mount_disk(char *name)
152152
for (int i = NO_OF_INODES - 1; i >= 0; --i)
153153
if (sb.inode_freelist[i] == true)
154154
{
155-
dir_map[string(file_inode_mapping_arr[i].file_name)] = file_inode_mapping_arr[i].inode_num;
155+
file_to_inode_map[string(file_inode_mapping_arr[i].file_name)] = file_inode_mapping_arr[i].inode_num;
156156
inode_to_file_map[file_inode_mapping_arr[i].inode_num] = string(file_inode_mapping_arr[i].file_name);
157157
}
158158
else
@@ -236,7 +236,7 @@ int unmount_disk()
236236
free_filedescriptor_vector.clear();
237237
file_descriptor_mode_map.clear();
238238
file_descriptor_map.clear();
239-
dir_map.clear();
239+
file_to_inode_map.clear();
240240
inode_to_file_map.clear();
241241

242242
cout << string(GREEN) << "Disk Unmounted!!!" << string(DEFAULT) << endl;
@@ -280,43 +280,33 @@ int block_read(int block, char *buf)
280280
return 0;
281281
}
282282

283-
int block_write(int block, char *buf)
283+
int block_write(int block, char *buf, int size, int start_position)
284284
{
285-
cout << string(RED);
286-
if (!active)
287-
{
288-
fprintf(stderr, "block_write: disk not active\n");
289-
cout << string(DEFAULT);
290-
return -1;
291-
}
292285

293286
if ((block < 0) || (block >= DISK_BLOCKS))
294287
{
295288
fprintf(stderr, "block_write: block index out of bounds\n");
296-
cout << string(DEFAULT);
297289
return -1;
298290
}
299291

300-
if (fseek(diskptr, block * BLOCK_SIZE, SEEK_SET) < 0)
292+
if (fseek(diskptr, (block * BLOCK_SIZE) + start_position, SEEK_SET) < 0)
301293
{
302294
perror("block_write: failed to lseek");
303-
cout << string(DEFAULT);
304295
return -1;
305296
}
306297

307-
if (fwrite(buf, sizeof(char), BLOCK_SIZE, diskptr) < 0)
298+
if (fwrite(buf, sizeof(char), size, diskptr) < 0)
308299
{
309300
perror("block_write: failed to write");
310-
cout << string(DEFAULT);
311301
return -1;
312302
}
313-
cout << string(DEFAULT);
303+
314304
return 0;
315305
}
316306

317307
void print_list_open_files()
318308
{
319-
cout << string(BOLD) << "### List of opened files ###" << string(DEFAULT) << endl;
309+
cout << string(GREEN) << "List of opened files " << string(DEFAULT) << endl;
320310
for (auto i : file_descriptor_map)
321311
{
322312
int fd = i.first;
@@ -334,8 +324,8 @@ void print_list_open_files()
334324

335325
void print_list_files()
336326
{
337-
cout << string(BOLD) << "### List of All files ###" << string(DEFAULT) << endl;
338-
for (auto i : dir_map)
327+
cout << string(GREEN) << "List of All files" << string(DEFAULT) << endl;
328+
for (auto i : file_to_inode_map)
339329
{
340330
cout << i.first << " with inode : " << i.second << endl;
341331
}
@@ -425,6 +415,7 @@ int user_handle()
425415
}
426416
}
427417
}
418+
428419
int main()
429420
{
430421
int choice;

inode.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extern struct inode inode_arr[NO_OF_INODES];
7575

7676
extern FILE *diskptr;
7777
extern int openfile_count; // keeps track of number of files opened.
78-
extern map<string, int> dir_map; // filename->inode file name as key maps to inode (value)
78+
extern map<string, int> file_to_inode_map; // filename->inode file name as key maps to inode (value)
7979
extern map<int, string> inode_to_file_map; // indoe-> filename inode to file mapping
8080

8181
extern vector<int> free_inode_vector; // denote free inodes
@@ -97,6 +97,8 @@ int unmount_disk(); /* close a previously opened disk (file)
9797
int open_file(char *name); /* open file to get its file descriptor */
9898
int close_file(int fd); /* close the file */
9999
int block_read(int block, char *buf); /* read block */
100+
int block_write(int block, char *buf,
101+
int size, int start_position); /* Write block */
100102
int create_file(char *name); /* to create file */
101103
int delete_file(char *name); /* to delete file */
102104
int write_into_file(int fd, int mode); /* to write content into file */

0 commit comments

Comments
 (0)