-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibDisk.c
More file actions
150 lines (130 loc) · 3 KB
/
Copy pathLibDisk.c
File metadata and controls
150 lines (130 loc) · 3 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "LibDisk.h"
typedef struct sector {
char data[SECTOR_SIZE];
} sector_t;
// used to see what happened w/ disk ops
int diskErrno;
// the disk in memory (static makes it private to the file)
static sector_t* disk;
// used for statistics
// static int lastSector = 0;
// static int seekCount = 0;
/*
* Disk_Init
*
* Initializes the disk area (really just some memory for now).
*
* THIS FUNCTION MUST BE CALLED BEFORE ANY OTHER FUNCTION IN HERE CAN BE USED!
*
*/
int Disk_Init()
{
// create the disk image and fill every sector with zeroes
disk = (sector_t *) calloc(TOTAL_SECTORS, sizeof(sector_t));
if(disk == NULL) {
diskErrno = E_MEM_OP;
return -1;
}
return 0;
}
/*
* Disk_Save
*
* Makes sure the current disk image gets saved to memory - this
* will overwrite an existing file with the same name so be careful
*/
int Disk_Save(char* file)
{
FILE* diskFile;
// error check
if (file == NULL) {
diskErrno = E_INVALID_PARAM;
return -1;
}
// open the diskFile
if ((diskFile = fopen(file, "w")) == NULL) {
diskErrno = E_OPENING_FILE;
return -1;
}
// actually write the disk image to a file
if ((fwrite(disk, sizeof(sector_t), TOTAL_SECTORS, diskFile)) != TOTAL_SECTORS) {
fclose(diskFile);
diskErrno = E_WRITING_FILE;
return -1;
}
// clean up and return
fclose(diskFile);
return 0;
}
/*
* Disk_Load
*
* Loads a current disk image from disk into memory - requires that
* the disk be created first.
*/
int Disk_Load(char* file)
{
FILE* diskFile;
// error check
if (file == NULL) {
diskErrno = E_INVALID_PARAM;
return -1;
}
// open the diskFile
if ((diskFile = fopen(file, "r")) == NULL) {
diskErrno = E_OPENING_FILE;
return -1;
}
// actually read the disk image into memory
if ((fread(disk, sizeof(sector_t), TOTAL_SECTORS, diskFile)) != TOTAL_SECTORS) {
fclose(diskFile);
diskErrno = E_READING_FILE;
return -1;
}
// clean up and return
fclose(diskFile);
return 0;
}
/*
* Disk_Read
*
* Reads a single sector from "disk" and puts it into a buffer provided
* by the user.
*/
int Disk_Read(int sector, char* buffer)
{
// quick error checks
if ((sector < 0) || (sector >= TOTAL_SECTORS) || (buffer == NULL)) {
diskErrno = E_INVALID_PARAM;
return -1;
}
// copy the memory for the user
if((memcpy((void*)buffer, (void*)(disk + sector), sizeof(sector_t))) == NULL) {
diskErrno = E_MEM_OP;
return -1;
}
return 0;
}
/*
* Disk_Write
*
* Writes a single sector from memory to "disk".
*/
int Disk_Write(int sector, char* buffer)
{
// quick error checks
if((sector < 0) || (sector >= TOTAL_SECTORS) || (buffer == NULL)) {
diskErrno = E_INVALID_PARAM;
return -1;
}
// copy the memory for the user
if((memcpy((void*)(disk + sector), (void*)buffer, sizeof(sector_t))) == NULL) {
diskErrno = E_MEM_OP;
return -1;
}
return 0;
}