-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlibdisksimul.c
125 lines (108 loc) · 2.84 KB
/
libdisksimul.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "libdisksimul.h"
/* Simple library to simul read/write access to disk sectors. */
static FILE* simulfile = NULL;
/**
* @brief Disk Simulator Init.
*
* Create or open (if it already exist) the simulation file.
*
* @param filename Name of the input/output file.
* @param sector_size Sector size in number of bytes.
* @param number_sector Total number of sectors.
* @param format Force create new file.
* @return Return 0 on success, otherwise error.
*/
int ds_init(char* filename, int sector_size, int number_sectors, int format){
struct stat b;
if(format == 0){
/* Check if the file already exists */
if( stat(filename, &b) == 0){
/* File exists, open for read/write. */
if( (simulfile = fopen(filename, "r+b")) == NULL){
/* error openning the file */
perror("fopen: ");
return 1;
}
return 0;
}
return 1;
}
/* File doesn't exist initialize it. */
/* Create file */
if( (simulfile = fopen(filename, "w")) == NULL){
/* error openning the file */
perror("fopen: ");
return 1;
}
/* Set file size */
ftruncate(fileno(simulfile), (sector_size*number_sectors));
fclose(simulfile);
/* Reopen the file for input/output */
if( (simulfile = fopen(filename, "r+b")) == NULL){
/* error openning the file */
perror("fopen: ");
return 1;
}
return 0;
}
/**
* Disk Simulator Read Sector.
*
* Read a sector and load the data to the memory in data.
*
* @param sector_number Number of the sector.
* @param data Pointer to buffer to store the data.
* @param sector_size Sector size in bytes.
* @return 0 if success, otherwise error.
*/
int ds_read_sector(int sector_number, void *data, int sector_size){
int ret;
/* locate the sector .*/
if ( (ret = fseek(simulfile, sector_number*sector_size, SEEK_SET)) != 0){
return ret;
}
/* read the sector the memory buffer pointed by data. */
if ( (ret = fread(data, sizeof(char), sector_size, simulfile)) == 0){
return ret;
}
return 0;
}
/**
* Disk Simulator Write Sector.
*
* Write a sector from data in memory.
*
* @param sector_number Number of the sector.
* @param data Pointer to buffer to store the data.
* @param sector_size Sector size in bytes.
* @return 0 if success, otherwise error.
*/
int ds_write_sector(int sector_number, void *data, int sector_size){
int ret;
/* locate the sector .*/
if ( (ret = fseek(simulfile, sector_number*sector_size, SEEK_SET)) != 0){
return ret;
}
/* load the sector to the memory buffer pointed by data. */
if ( (ret = fwrite(data, sizeof(char), sector_size, simulfile)) == 0){
return ret;
}
return 0;
}
/**
* Disk Simulator Stop.
*
* Stop disk simulation.
*
* @param fp File pointer to the I/O file.
*/
void ds_stop(){
fclose(simulfile);
simulfile = NULL;
}