-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.c
102 lines (94 loc) · 2.54 KB
/
lib.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
#include "lib.h"
extern int do_init(struct objfs_state *objfs)
{
struct stat sb;
char *ptr;
char buf[1024];
ptr = getcwd(buf, 1024);
if(!ptr){
perror("getcwd");
return -1;
}
if(stat(ptr, &objfs->def_dirstat)){
perror("stat");
return -1;
}
objfs->blkdev = open("disk.img", O_RDWR | O_DIRECT);
if(objfs->blkdev < 0){
perror("blkdev open");
return -1;
}
if (fstat(objfs->blkdev, &sb) < 0) {
perror("blkdev open");
return -1;
}
objfs->disksize = (sb.st_size) >> 12; // # of blocks
printf("diskblocks = %ld\n", objfs->disksize);
objfs->logfd = fopen("objfs.log", "w+");
if(objfs->logfd == NULL){
perror("logfile open");
return -1;
}
if(stat("objfs.log", &objfs->def_fstat)){
perror("stat");
return -1;
}
setvbuf(objfs->logfd, NULL, _IOLBF, 0);
objfs->cache = mmap(NULL, CACHE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if(objfs->cache == MAP_FAILED){
perror("mmap");
return -1;
}
objfs->cachesize = CACHE_SIZE;
return 0;
}
int read_block(struct objfs_state *objfs, long block_offset, char *buf)
{
unsigned long addr = (unsigned long) buf;
if(((addr >> BLOCK_SHIFT) << BLOCK_SHIFT) != addr){
dprintf("Buffer is not at block boundary\n");
return -1;
}
block_offset <<= BLOCK_SHIFT;
#if 0
if(lseek(objfs->blkdev, block_offset, SEEK_SET) < 0){
dprintf("%s: lseek error\n", __func__);
return -1;
}
if(read(objfs->blkdev, buf, BLOCK_SIZE) < 0){
dprintf("%s: read error\n", __func__);
return -1;
}
#else
if (pread(objfs->blkdev, buf, BLOCK_SIZE, block_offset) < 0) {
dprintf("%s: read error\n", __func__);
return -1;
}
#endif
return 0;
}
int write_block(struct objfs_state *objfs, long block_offset, char *buf)
{
unsigned long addr = (unsigned long) buf;
if(((addr >> BLOCK_SHIFT) << BLOCK_SHIFT) != addr){
dprintf("Buffer is not at block boundary\n");
return -1;
}
block_offset <<= BLOCK_SHIFT;
#if 0
if(lseek(objfs->blkdev, block_offset, SEEK_SET) < 0){
dprintf("%s: lseek error\n", __func__);
return -1;
}
if(write(objfs->blkdev, buf, BLOCK_SIZE) < 0){
dprintf("%s: write error\n", __func__);
return -1;
}
#else
if (pwrite(objfs->blkdev, buf, BLOCK_SIZE, block_offset) < 0) {
dprintf("%s: write error\n", __func__);
return -1;
}
#endif
return 0;
}