-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDisk.cpp
More file actions
73 lines (65 loc) · 2.12 KB
/
Disk.cpp
File metadata and controls
73 lines (65 loc) · 2.12 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
#include "Disk.h"
#include <fstream>
#include <iostream>
#include "../define/constants.h"
/*
* Used to make a temporary copy of the disk contents before the starting of a new session.
* This ensures that if the system has a forced shutdown during the course of the session,
* the previous state of the disk is not lost.
*/
Disk::Disk() {
/* An efficient method to copy files */
/* Copy Disk to Disk Run Copy */
std::ifstream src(DISK_PATH, std::ios::binary);
std::ofstream dst(DISK_RUN_COPY_PATH, std::ios::binary);
dst << src.rdbuf();
src.close();
dst.close();
}
/*
* Used to update the changes made to the disk on graceful termination of the latest session.
* This ensures that these changes are visible in future sessions.
*/
Disk::~Disk() {
/* An efficient method to copy files */
/* Copy Disk Run Copy to Disk */
std::ifstream src(DISK_RUN_COPY_PATH, std::ios::binary);
std::ofstream dst(DISK_PATH, std::ios::binary);
dst << src.rdbuf();
src.close();
dst.close();
}
/*
* Used to Read a specified block from disk
* block - Memory pointer of the buffer to which the block contents is to be loaded/read.
* (MUST be Allocated by caller)
* blockNum - Block number of the disk block to be read.
*/
int Disk::readBlock(unsigned char *block, int blockNum) {
FILE *disk = fopen(DISK_RUN_COPY_PATH, "rb");
if (blockNum < 0 || blockNum > DISK_BLOCKS - 1) {
return E_OUTOFBOUND;
}
const int offset = blockNum * BLOCK_SIZE;
fseek(disk, offset, SEEK_SET);
fread(block, BLOCK_SIZE, 1, disk);
fclose(disk);
return SUCCESS;
}
/*
* Used to Write a specified block from disk
* block - Memory pointer of the buffer to which contain the contents to be written.
* (MUST be Allocated by caller)
* blockNum - Block number of the disk block to be written into.
*/
int Disk::writeBlock(unsigned char *block, int blockNum) {
FILE *disk = fopen(DISK_RUN_COPY_PATH, "rb+");
if (blockNum < 0 || blockNum > DISK_BLOCKS - 1) {
return E_OUTOFBOUND;
}
const int offset = blockNum * BLOCK_SIZE;
fseek(disk, offset, SEEK_SET);
fwrite(block, BLOCK_SIZE, 1, disk);
fclose(disk);
return SUCCESS;
}