-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.c
69 lines (53 loc) · 1.52 KB
/
disk.c
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
/*
Interface to access disk file.
*/
#include "disk.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
BLOCK *disk;
/* Reads an entire block from fileBlockNumber on the disk to virtBlockNumber on the memory copy of the disk */
int readFromDisk(int virtBlockNumber, int fileBlockNumber)
{
int fd = openDiskFile(O_RDONLY);
lseek(fd, sizeof(BLOCK) * fileBlockNumber, SEEK_SET);
read(fd, &disk[virtBlockNumber], sizeof(BLOCK));
close(fd);
return XFS_SUCCESS;
}
/* Writes an entire block to fileBlocknumber on the disk from virtBlockNumber on the memory copy of the disk */
int writeToDisk(int virtBlockNumber, int fileBlockNumber)
{
int fd = openDiskFile(O_WRONLY);
lseek(fd, 0, SEEK_SET);
lseek(fd, sizeof(BLOCK) * fileBlockNumber, SEEK_CUR);
write(fd, &disk[virtBlockNumber], sizeof(BLOCK));
close(fd);
return XFS_SUCCESS;
}
/* Opens the disk file and returns the file descriptor */
int openDiskFile(int access)
{
int fd = open(DISK_NAME, access, 0666);
if (fd < 0)
exception_throwException(EXCEPTION_CANT_OPEN_DISK);
return fd;
}
/* Creates the disk file */
void createDiskFile(int format)
{
int fd;
if (format == DISK_FORMAT)
fd = open(DISK_NAME, O_CREAT | O_TRUNC | O_SYNC, 0666);
else
fd = open(DISK_NAME, O_CREAT, 0666);
if (fd < 0)
exception_throwException(EXCEPTION_CANT_CREATE_DISK);
close(fd);
}
/* Tries to open the disk */
void diskCheckFileExists()
{
close(openDiskFile(O_RDONLY));
}