-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.c
29 lines (26 loc) · 778 Bytes
/
block.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
#include "image.h"
#include "block.h"
#include "free.h"
#include <unistd.h>
#include <stdio.h>
int alloc(void) {
unsigned char data_map[BLOCK_SIZE] = {0};
bread(DATA_BLOCK, data_map);
int lowest_free = find_free(data_map);
if (lowest_free != -1) {
set_free(data_map, lowest_free, SET_BIT);
bwrite(DATA_BLOCK, data_map);
}
return lowest_free;
}
unsigned char *bread(int block_num, unsigned char *block) {
int byte_offset = block_num * BLOCK_SIZE;
lseek(image_fd, byte_offset, SEEK_SET);
read(image_fd, block, BLOCK_SIZE);
return block;
}
void bwrite(int block_num, unsigned char *block) {
int byte_offset = block_num * BLOCK_SIZE;
lseek(image_fd, byte_offset, SEEK_SET);
write(image_fd, block, BLOCK_SIZE);
}