-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocation.c
195 lines (166 loc) · 4.08 KB
/
allocation.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#define NUM_BLOCKS 50
/* The name of the file that contains our block allocation table
* that simulates the used disk.
* We make the variable static to hide it from other C files.
*/
static char* file_name = NULL;
void set_block_allocation_table_name( char* str )
{
if( file_name != NULL )
{
fprintf( stderr, "Cannot set %s as block allocation table name.\n"
"The name of the block_allocation_table has already been set to %s\n",
str, file_name );
exit( -1 );
}
file_name = strdup( str );
}
void release_block_allocation_table_name( )
{
if( file_name )
{
free( file_name );
}
}
static char* read_table( )
{
if( file_name == NULL )
{
fprintf( stderr, "Failed to set the name of the block allocation table file.\n" );
exit( -1 );
}
char* table = malloc( NUM_BLOCKS );
if( table == NULL )
{
fprintf( stderr, "Failed to allocate %d bytes\n", NUM_BLOCKS );
return NULL;
}
FILE* f = fopen( file_name, "r" );
if( !f )
{
fprintf( stderr, "Failed to open file %s for reading\n", file_name );
perror("reason:");
return NULL;
}
int num_read = fread( table, 1, NUM_BLOCKS, f );
if( num_read != NUM_BLOCKS )
{
fprintf( stderr, "Failed to load %d block entries from disk\n", NUM_BLOCKS );
perror("reason:");
fclose(f);
return NULL;
}
fclose( f );
return table;
}
static int write_table( char* table )
{
if( file_name == NULL )
{
fprintf( stderr, "Failed to set the name of the block allocation table file.\n" );
exit( -1 );
}
FILE* f = fopen( file_name, "w" );
if( !f )
{
fprintf( stderr, "Failed to open file %s for writing\n", file_name );
perror("reason:");
return -1;
}
int num = fwrite( table, 1, NUM_BLOCKS, f );
if( num != NUM_BLOCKS )
{
fprintf( stderr, "Failed to write %d bytes to %s\n", NUM_BLOCKS, file_name);
fprintf( stderr, "fwrite returned %d\n", num );
perror("reason:");
return -1;
}
fclose( f );
return 0;
}
int format_disk()
{
if( file_name == NULL )
{
fprintf( stderr, "Failed to set the name of the block allocation table file.\n" );
exit( -1 );
}
int error = unlink( file_name );
if( error == 0 || errno == ENOENT )
{
/* We want to set all NUM_BLOCK chars to 0, convenient to use
* calloc.
*/
char* table = calloc( NUM_BLOCKS, 1 );
if( table == NULL )
{
fprintf( stderr, "Failed to allocate %d bytes\n", NUM_BLOCKS );
return -1;
}
int retval = write_table( table );
free( table );
return retval;
}
fprintf( stderr, "Failed to remove existing file %s, code %d\n", file_name, errno );
perror("reason:");
return -1;
}
int allocate_block( )
{
char* table = read_table( );
if( table == NULL )
{
return -1;
}
for( int i=0; i<NUM_BLOCKS; i++ )
{
if( table[i] == 0 )
{
/* Found an unused block */
table[i] = 1;
write_table( table );
free( table );
return i;
}
}
free( table );
return -1;
}
int free_block(int block)
{
if( block < 0 || block >= NUM_BLOCKS )
{
fprintf( stderr, "Block number %d is not valid\n", block );
return -1;
}
char* table = read_table();
if( table == NULL )
{
return -1;
}
if( table[block] != 1 )
{
fprintf( stderr, "Block %d was not allocated\n", block );
free( table );
return -1;
}
table[block] = 0;
write_table( table );
free( table );
return 0;
}
void debug_disk( )
{
char* table = read_table( );
printf("Disk:\n");
for( int i=0; i<NUM_BLOCKS; i++ )
printf("%d", table[i] );
printf("\n");
free(table);
}