-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisk.cc
More file actions
103 lines (86 loc) · 1.67 KB
/
Copy pathdisk.cc
File metadata and controls
103 lines (86 loc) · 1.67 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
#include "disk.h"
#include <unistd.h>
Disk::Disk(const char *filename, int n)
{
diskfile = fopen(filename, "r+");
if (!diskfile)
diskfile = fopen(filename, "w+");
if (!diskfile)
{
cout << "Error when opening the file " << filename << "\n";
return;
}
ftruncate(fileno(diskfile), n * DISK_BLOCK_SIZE);
nblocks = n;
nreads = 0;
nwrites = 0;
}
int Disk::size()
{
return nblocks;
}
void Disk::sanity_check(int blocknum, const void *data)
{
if (blocknum < 0)
{
cout << "ERROR: blocknum (" << blocknum << ") is negative!\n";
abort();
}
if (blocknum >= nblocks)
{
cout << "ERROR: blocknum (" << blocknum << ") is too big!\n";
abort();
}
if (!data)
{
cout << "ERROR: null data pointer!\n";
abort();
}
}
void Disk::read(int blocknum, char *data)
{
sanity_check(blocknum, data);
fseek(diskfile, blocknum * DISK_BLOCK_SIZE, SEEK_SET);
if (fread(data, DISK_BLOCK_SIZE, 1, diskfile) == 1)
{
nreads++;
}
else
{
cout << "ERROR: couldn't access simulated disk\n";
abort();
}
}
void Disk::write(int blocknum, const char *data)
{
sanity_check(blocknum, data);
fseek(diskfile, blocknum * DISK_BLOCK_SIZE, SEEK_SET);
if (fwrite(data, DISK_BLOCK_SIZE, 1, diskfile) == 1)
{
nwrites++;
}
else
{
cout << "ERROR: couldn't access simulated disk\n";
abort();
}
}
void Disk::close()
{
if (diskfile)
{
cout << nreads << " disk block reads\n";
cout << nwrites << " disk block writes\n";
fclose(diskfile);
diskfile = 0;
}
}
void Disk::setBitMap()
{
/* Sets the size of bitmap = number of blocks */
bitmap.resize(nblocks + 1);
/* TODO:
1. first block is the superblock. Therefore it's being used, therefore it's a 1.
2. FOr the rest is zero.
*/
}