Skip to content

Commit 17dc5a8

Browse files
committed
2 parents b0d2e11 + 5914516 commit 17dc5a8

File tree

4 files changed

+119
-86
lines changed

4 files changed

+119
-86
lines changed

file_operation.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,21 @@ int create_file(char *name)
107107
//check if file already exist in disk
108108
if (dir_map.find(filename) != dir_map.end())
109109
{
110-
cout << "Create File Error : File already present !!!" << endl;
110+
cout << string(RED) << "Create File Error : File already present !!!" << string(DEFAULT) << endl;
111111
return -1;
112112
}
113113

114114
//check if inode are available
115115
if (free_inode_vector.size() == 0)
116116
{
117-
cout << "Create File Error : No more Inodes available" << endl;
117+
cout << string(RED) << "Create File Error : No more Inodes available" << string(DEFAULT) << endl;
118118
return -1;
119119
}
120120

121121
//check if datablock are available
122122
if (free_data_block_vector.size() == 0)
123123
{
124-
cout << "Create File Error : No more DataBlock available" << endl;
124+
cout << string(RED) << "Create File Error : No more DataBlock available" << string(DEFAULT) << endl;
125125
return -1;
126126
}
127127

@@ -141,7 +141,7 @@ int create_file(char *name)
141141
file_inode_mapping_arr[next_avl_inode].inode_num = next_avl_inode;
142142
strcpy(file_inode_mapping_arr[next_avl_inode].file_name, name);
143143

144-
cout << "File Successfully Created :) " << endl;
144+
cout << string(GREEN) << "File Successfully Created :) " << string(DEFAULT) << endl;
145145
return 1;
146146
}
147147

@@ -152,7 +152,7 @@ int delete_file(char *name)
152152
//check if file exist or not
153153
if (dir_map.find(filename) == dir_map.end())
154154
{
155-
cout << "Delete File Error : File doesn't exist !!!" << endl;
155+
cout << string(RED) << "Delete File Error : File doesn't exist !!!" << string(DEFAULT) << endl;
156156
return -1;
157157
}
158158

@@ -163,7 +163,7 @@ int delete_file(char *name)
163163
{
164164
if (file_descriptor_map.find(i) != file_descriptor_map.end() && file_descriptor_map[i].first == cur_inode)
165165
{
166-
cout << "Delete File Error : File is opened, Can not delete an opened file !!!" << endl;
166+
cout << string(RED) << "Delete File Error : File is opened, Can not delete an opened file !!!" << string(DEFAULT) << endl;
167167
return -1;
168168
}
169169
}
@@ -178,8 +178,7 @@ int delete_file(char *name)
178178
inode_to_file_map.erase(dir_map[filename]);
179179
dir_map.erase(filename);
180180

181-
182-
cout << "File Deleted successfully :) " << endl;
181+
cout << string(GREEN) << "File Deleted successfully :) " << string(DEFAULT) << endl;
183182

184183
return 0;
185184
}
@@ -189,24 +188,24 @@ int open_file(char *name)
189188
string filename = string(name);
190189
if (dir_map.find(filename) == dir_map.end())
191190
{
192-
cout << "Open File Error : File not found !!!" << endl;
191+
cout << string(RED) << "Open File Error : File not found !!!" << string(DEFAULT) << endl;
193192
return -1;
194193
}
195194

196195
if (free_filedescriptor_vector.size() == 0)
197196
{
198-
cout << "Open File Error : File descriptor not available !!!" << endl;
197+
cout << string(RED) << "Open File Error : File descriptor not available !!!" << string(DEFAULT) << endl;
199198
return -1;
200199
}
201200
/* asking for mode of file */
202201
int file_mode = -1;
203202
do
204203
{
205-
cout << "0: read mode\n1: write mode\n2: append mode\n";
204+
cout << "0 : read mode\n1 : write mode\n2 : append mode\n";
206205
cin >> file_mode;
207206
if (file_mode < 0 || file_mode > 2)
208207
{
209-
cout << "Please make valid choice" << endl;
208+
cout << string(RED) << "Please make valid choice" << string(DEFAULT) << endl;
210209
}
211210
} while (file_mode < 0 || file_mode > 2);
212211

@@ -221,7 +220,7 @@ int open_file(char *name)
221220
file_descriptor_map[i].first == cur_inode &&
222221
(file_descriptor_mode_map[i] == 1 || file_descriptor_mode_map[i] == 2))
223222
{
224-
cout << "File is already in use with file descriptor : " << i << endl;
223+
cout << string(RED) << "File is already in use with file descriptor : " << i << string(DEFAULT) << endl;
225224
return -1;
226225
}
227226
}
@@ -235,7 +234,7 @@ int open_file(char *name)
235234
file_descriptor_mode_map[fd] = file_mode;
236235
openfile_count++;
237236

238-
cout << "File " << filename << " opened with file descriptor : " << fd << endl;
237+
cout << string(GREEN) << "File " << filename << " opened with file descriptor : " << fd << string(DEFAULT) << endl;
239238

240239
return fd;
241240
}
@@ -244,35 +243,34 @@ int close_file(int fd)
244243
{
245244
if (file_descriptor_map.find(fd) == file_descriptor_map.end())
246245
{
247-
cout << "close File Error : file is not opened yet !!!" << endl;
246+
cout << string(RED) << "close File Error : file is not opened yet !!!" << string(DEFAULT) << endl;
248247
return -1;
249248
}
250249

251250
file_descriptor_map.erase(fd);
252251
file_descriptor_mode_map.erase(fd);
253252
openfile_count--;
254253
free_filedescriptor_vector.push_back(fd);
255-
cout << "File closed successfully :) " << endl;
254+
cout << string(GREEN) << "File closed successfully :) " << string(DEFAULT) << endl;
256255
return 1;
257256
}
258257

259258
int read_file(int fd)
260259
{
261-
262-
260+
263261
if (file_descriptor_map.find(fd) == file_descriptor_map.end())
264262
{
265-
cout << "Read File Error : file is not opened yet !!!" << endl;
263+
cout << string(RED) << "Read File Error : file is not opened yet !!!" << string(DEFAULT) << endl;
266264
return -1;
267265
}
268266

269267
if (file_descriptor_mode_map[fd] != 0)
270268
{
271-
cout << "Read File Error : file with descriptor " << fd << " is not opened in read mode !!!" << endl;
269+
cout << string(RED) << "Read File Error : file with descriptor " << fd << " is not opened in read mode !!!" << string(DEFAULT) << endl;
272270
return -1;
273271
}
274272

275-
int bytes_read=0;
273+
int bytes_read = 0;
276274
bool partial_read = false;
277275
int fs = file_descriptor_map[fd].second;
278276

@@ -281,7 +279,7 @@ int read_file(int fd)
281279
int filesize = in.filesize;
282280
char *buf;
283281
buf = new char[filesize];
284-
char *initial_buf_pos=buf;
282+
char *initial_buf_pos = buf;
285283

286284
int noOfBlocks = ceil(((float)inode_arr[cur_inode].filesize) / BLOCK_SIZE);
287285
int tot_block = noOfBlocks; // tot_block = numner of blocks to read and noOfBlocks = blocks left to read
@@ -307,9 +305,10 @@ int read_file(int fd)
307305
partial_read = true;
308306
bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
309307
}
310-
else{
311-
312-
memcpy(buf, read_buf, BLOCK_SIZE );
308+
else
309+
{
310+
311+
memcpy(buf, read_buf, BLOCK_SIZE);
313312
buf = buf + BLOCK_SIZE;
314313
bytes_read += BLOCK_SIZE;
315314
}
@@ -335,15 +334,16 @@ int read_file(int fd)
335334
{
336335
if (partial_read == false)
337336
{
338-
337+
339338
memcpy(buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
340339
buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
341340
partial_read = true;
342341
bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
343342
}
344-
else{
345-
346-
memcpy(buf, read_buf, BLOCK_SIZE );
343+
else
344+
{
345+
346+
memcpy(buf, read_buf, BLOCK_SIZE);
347347
buf = buf + BLOCK_SIZE;
348348
bytes_read += BLOCK_SIZE;
349349
}
@@ -374,19 +374,19 @@ int read_file(int fd)
374374
{
375375
if (partial_read == false)
376376
{
377-
377+
378378
memcpy(buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
379379
buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
380380
partial_read = true;
381381
bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
382382
}
383-
else{
384-
385-
memcpy(buf, read_buf, BLOCK_SIZE );
386-
buf = buf + BLOCK_SIZE;
383+
else
384+
{
385+
386+
memcpy(buf, read_buf, BLOCK_SIZE);
387+
buf = buf + BLOCK_SIZE;
387388
bytes_read += BLOCK_SIZE;
388389
}
389-
390390
}
391391
noOfBlocks--;
392392
}
@@ -402,12 +402,12 @@ int read_file(int fd)
402402
memcpy(buf, read_buf + (fs % BLOCK_SIZE), (inode_arr[cur_inode].filesize) % BLOCK_SIZE - fs % BLOCK_SIZE);
403403
bytes_read += (inode_arr[cur_inode].filesize) % BLOCK_SIZE - fs % BLOCK_SIZE;
404404
}
405-
406-
initial_buf_pos[bytes_read]='\0';
405+
406+
initial_buf_pos[bytes_read] = '\0';
407407
cout.flush();
408408
cout << initial_buf_pos << endl;
409409
cout.flush();
410-
cout << endl<<"File read successfully " << endl;
410+
cout << string(GREEN) << "File read successfully " << string(DEFAULT) << endl;
411411
return 1;
412412
}
413413

file_read_write.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
119119
//check if datablock are available
120120
if (free_data_block_vector.size() == 0)
121121
{
122-
cout << "Write File Error : No more DataBlock available" << endl;
122+
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
123123
return -1;
124124
}
125125
int next_avl_datablock = free_data_block_vector.back();
@@ -138,7 +138,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
138138
//check if datablock are available
139139
if (free_data_block_vector.size() == 0)
140140
{
141-
cout << "Write File Error : No more DataBlock available" << endl;
141+
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
142142
return -1;
143143
}
144144
int data_block_to_write = free_data_block_vector.back();
@@ -162,7 +162,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
162162
//check if datablock are available
163163
if (free_data_block_vector.size() == 0)
164164
{
165-
cout << "Write File Error : No more DataBlock available" << endl;
165+
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
166166
return -1;
167167
}
168168
int data_block_single_indirect = free_data_block_vector.back();
@@ -184,7 +184,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
184184
//check if datablock are available
185185
if (free_data_block_vector.size() == 0)
186186
{
187-
cout << "Write File Error : No more DataBlock available" << endl;
187+
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
188188
return -1;
189189
}
190190
int data_block_to_write = free_data_block_vector.back();
@@ -213,7 +213,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
213213
//check if datablock are available
214214
if (free_data_block_vector.size() == 0)
215215
{
216-
cout << "Write File Error : No more DataBlock available" << endl;
216+
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
217217
return -1;
218218
}
219219
int data_block_double_indirect = free_data_block_vector.back(); //to store block_pointers[1024] into db_for_double_indirect
@@ -239,7 +239,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
239239
//check if datablock are available
240240
if (free_data_block_vector.size() == 0)
241241
{
242-
cout << "Write File Error : No more DataBlock available" << endl;
242+
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
243243
return -1;
244244
}
245245
int data_block_double_indirect2 = free_data_block_vector.back(); //to store block_pointers[1024] into db_for_double_indirect
@@ -269,7 +269,7 @@ int _write_into_file(int fd, char *buff, int len, int *bytes_written)
269269
//check if datablock are available
270270
if (free_data_block_vector.size() == 0)
271271
{
272-
cout << "Write File Error : No more DataBlock available" << endl;
272+
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
273273
return -1;
274274
}
275275
int data_block_to_write = free_data_block_vector.back(); //to store block_pointers[1024] into db_for_double_indirect
@@ -299,7 +299,7 @@ int write_into_file(int fd, int mode)
299299
//check if file exist or not
300300
if (file_descriptor_map.find(fd) == file_descriptor_map.end())
301301
{
302-
cout << "Write File Error : File descriptor " << fd << " doesn't exist !!!" << endl;
302+
cout << string(RED) << "Write File Error : File descriptor " << fd << " doesn't exist !!!" << string(DEFAULT) << endl;
303303
return -1;
304304
}
305305

@@ -310,7 +310,7 @@ int write_into_file(int fd, int mode)
310310
/* Write */
311311
if (file_descriptor_mode_map[fd] != 1)
312312
{
313-
cout << "Write File Error : File descriptor " << fd << " is not opened in write mode!!!" << endl;
313+
cout << string(RED) << "Write File Error : File descriptor " << fd << " is not opened in write mode!!!" << string(DEFAULT) << endl;
314314
return -1;
315315
}
316316

@@ -330,7 +330,7 @@ int write_into_file(int fd, int mode)
330330
/* Append */
331331
if (file_descriptor_mode_map[fd] != 2)
332332
{
333-
cout << "Append File Error : File descriptor " << fd << " is not opened in append mode!!!" << endl;
333+
cout << string(RED) << "Append File Error : File descriptor " << fd << " is not opened in append mode!!!" << string(DEFAULT) << endl;
334334
return -1;
335335
}
336336
file_descriptor_map[fd].second = inode_arr[cur_inode].filesize;
@@ -376,7 +376,7 @@ int write_into_file(int fd, int mode)
376376
x = x.substr(len);
377377
if (_write_into_file(fd, buff, len, &bytes_written) == -1)
378378
{
379-
cout << "No Enough space. Only " << bytes_written << " bytes written." << endl;
379+
cout << string(RED) << "No Enough space. Only " << bytes_written << " bytes written." << string(DEFAULT) << endl;
380380
return -1;
381381
};
382382
}
@@ -390,10 +390,10 @@ int write_into_file(int fd, int mode)
390390
buff1[len] = '\0';
391391
if (_write_into_file(fd, buff1, len, &bytes_written) == -1)
392392
{
393-
cout << "No Enough space. Only " << bytes_written << " bytes written." << endl;
393+
cout << string(RED) << "No Enough space. Only " << bytes_written << " bytes written." << string(DEFAULT) << endl;
394394
return -1;
395395
};
396396
}
397-
cout << bytes_written << " bytes written. \nFile Written Successfully." << endl;
397+
cout << string(GREEN) << bytes_written << " bytes written. \nFile Written Successfully." << string(DEFAULT) << endl;
398398
return 0;
399399
}

0 commit comments

Comments
 (0)