-
Notifications
You must be signed in to change notification settings - Fork 6
/
freedb.cpp
172 lines (151 loc) · 5.68 KB
/
freedb.cpp
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
/***************************************************************************
freedb.cpp - free spaces database support
-------------------
begin : Sun Apr 29 2001
copyright : (C) 2001 by Alexander Bilichenko
email : [email protected]
***************************************************************************/
#include "basetypes.h"
#include "freedb.h"
#define unlock_and_freedb_io_error1() do { unlock_file(ffb); errnum = FREEDBFILE_ERROR_IO_ERROR; return FREEDBFILE_ERROR_IO_ERROR; } while (0)
#define freedb_io_error1() do { errnum = FREEDBFILE_ERROR_IO_ERROR; return FREEDBFILE_ERROR_IO_ERROR; } while (0)
/* mark free space block with size bsize and index bIndex
* if successfull return FREEDB_ERROR_ALLOK
*/
DWORD CFreeDBFile::MarkFreeSpace(DWORD bIndex, DWORD bsize)
{
SFreeDBEntry fs;
DWORD rr;
WCFILE *ffb;
if(!init) {
errnum = FREEDBFILE_ERROR_NOT_INITIALIZED;
return errnum;
}
// ignore wasted blocks
if(bsize < wasted_block) bsize = 0;
if((ffb = wcfopen(fname, FILE_ACCESS_MODES_RW)) == NULL) {
if((ffb = wcfopen(fname, FILE_ACCESS_MODES_CW)) == NULL) {
freedb_io_error1();
}
else {
wcfclose(ffb);
if((ffb = wcfopen(fname, FILE_ACCESS_MODES_RW)) == NULL) {
freedb_io_error1();
}
}
}
/* seek at begining of WCFILE */
if(wcfseek(ffb, 0, SEEK_SET) != 0) {
freedb_io_error1();
}
lock_file(ffb);
int alreadyfind = 0;
while(!wcfeof(ffb)) {
if((rr = (DWORD)wcfread(&fs, 1, sizeof(SFreeDBEntry), ffb)) != sizeof(SFreeDBEntry)) {
if(rr == 0) break;
unlock_and_freedb_io_error1();
}
if(fs.size == 0) {
alreadyfind = 1;
rr = wcftell(ffb) - sizeof(SFreeDBEntry);
if(wcfseek(ffb, rr, SEEK_SET) != 0) {
unlock_and_freedb_io_error1();
}
fs.size = bsize;
fs.index = bIndex;
if(!fCheckedWrite(&fs, sizeof(SFreeDBEntry), ffb)) {
unlock_and_freedb_io_error1();
}
break;
}
}
if(!alreadyfind) {
if(wcfseek(ffb, 0, SEEK_END) != 0) {
unlock_and_freedb_io_error1();
}
fs.size = bsize;
fs.index = bIndex;
if(!fCheckedWrite(&fs, sizeof(SFreeDBEntry), ffb)) {
unlock_and_freedb_io_error1();
}
}
unlock_file(ffb);
wcfclose(ffb);
errnum = FREEDBFILE_ERROR_ALLOK;
return FREEDBFILE_ERROR_ALLOK;
}
/* allocate size bytes of free space, and return index of it
* otherwise return 0xFFFFFFFF and set errnum with error code
*/
DWORD CFreeDBFile::AllocFreeSpace(DWORD size)
{
SFreeDBEntry fs;
DWORD rr;
WCFILE *ffb;
if(!init) {
errnum = FREEDBFILE_ERROR_NOT_INITIALIZED;
return 0xFFFFFFFF;
}
if((ffb = wcfopen(fname, FILE_ACCESS_MODES_RW)) == NULL) {
if((ffb = wcfopen(fname, FILE_ACCESS_MODES_CW)) == NULL) {
freedb_io_error1();
}
else {
wcfclose(ffb);
if((ffb = wcfopen(fname, FILE_ACCESS_MODES_RW)) == NULL) {
freedb_io_error1();
}
}
}
/* seek at begining of WCFILE */
if(wcfseek(ffb, 0, SEEK_SET) != 0) {
freedb_io_error1();
}
lock_file(ffb);
while(!wcfeof(ffb)) {
if((rr = (DWORD)wcfread(&fs, 1, sizeof(SFreeDBEntry), ffb)) != sizeof(SFreeDBEntry)) {
if(rr == 0) {
break;
}
unlock_and_freedb_io_error1();
}
if(fs.size >= size) {
rr = wcftell(ffb) - sizeof(SFreeDBEntry);
if(wcfseek(ffb, rr, SEEK_SET) != 0) {
unlock_and_freedb_io_error1();
}
fs.size = fs.size - size;
fs.index = fs.index + size;
// make wasted space, if block too small
if(fs.size < wasted_block) fs.size = 0;
if(!fCheckedWrite(&fs, sizeof(SFreeDBEntry), ffb)) {
unlock_and_freedb_io_error1();
}
unlock_file(ffb);
wcfclose(ffb);
// unlock FFB semaphore
return fs.index - size;
}
}
unlock_file(ffb);
wcfclose(ffb);
return 0xFFFFFFFF;
}
CFreeDBFile::CFreeDBFile(const char *ifname, DWORD wasted_b)
{
init = 0;
if(ifname == NULL || strlen(ifname) < 1) {
errnum = FREEDBFILE_ERROR_INVALID_FILE;
return;
}
fname = strdup(ifname);
wasted_block = wasted_b;
errnum = FREEDBFILE_ERROR_ALLOK;
init = 1;
}
CFreeDBFile::~CFreeDBFile()
{
if(init) {
free(fname);
}
}