-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.c
464 lines (368 loc) · 7.8 KB
/
interface.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "interface.h"
#include <stdlib.h>
/*
添加联系人数据指针索引
*/
bool AddDataIndex(DTYPE *fp, void *data, size_t size)
{
int nDataLength = 0;
bool isTrue = false;
isTrue = device_read(fp, OFFSETHEAD_INDEX, &nDataLength, sizeof(int));
if (false == isTrue)
{
return false;
}
isTrue = device_write(fp, OFFSETHEAD_INDEX + sizeof(int) + nDataLength * size, data, size);
if (false == isTrue)
{
return false;
}
nDataLength = nDataLength + 1;
isTrue = device_write(fp, OFFSETHEAD_INDEX, &nDataLength, sizeof(int));
if (false == isTrue)
{
return false;
}
fflush(fp);
return true;
}
/*
添加可回收垃圾数据指针索引
*/
bool AddRecoverableDataIndex(DTYPE *fp, void *data, size_t size)
{
int nDataLength = 0;
bool isTrue = false;
//读取索引计数
isTrue = device_read(fp, OFFSETHEAD_RECOVERABLE, &nDataLength, sizeof(int));
if (false == isTrue)
{
return false;
}
isTrue = device_write(fp, OFFSETHEAD_RECOVERABLE + sizeof(nDataLength) + nDataLength * size, data, size);
if (false == isTrue)
{
return false;
}
nDataLength = nDataLength + 1;
//写入索引计数
isTrue = device_write(fp, OFFSETHEAD_RECOVERABLE, &nDataLength, sizeof(int));
if (false == isTrue)
{
return false;
}
fflush(fp);
return true;
}
/*
获取索引数量
*/
int GetDataIndexCount(DTYPE *fp)
{
int nDataLength = 0;
bool isTrue = false;
isTrue = device_read(fp, OFFSETHEAD_INDEX, &nDataLength, sizeof(int));
if (false == isTrue)
{
nDataLength = -1;
}
return nDataLength;
}
/*
获取可回收垃圾索引数量
*/
int GetRecoverableDataIndexCount(DTYPE *fp)
{
int nDataLength = 0;
bool isTrue = false;
isTrue = device_read(fp, OFFSETHEAD_RECOVERABLE, &nDataLength, sizeof(int));
if (false == isTrue)
{
nDataLength = -1;
}
return nDataLength;
}
/*
获取索引数据项
*/
bool GetDataIndex(DTYPE *fp, int index, PTAGLINKMAN data)
{
bool isTrue = false;
isTrue = device_read(fp, OFFSETHEAD_INDEX + 4 + index * sizeof(TAGLINKMAN), data, sizeof(TAGLINKMAN));
if (false == isTrue)
{
return false;
}
return true;
}
/*
获取索引数据项
*/
int GetDataBinSize(DTYPE *fp)
{
int size = 0;
bool isTrue = false;
isTrue = device_read(fp, OFFSETTAIL_DATA, &size, sizeof(int));
if (false == isTrue)
{
return 0;
}
return size;
}
//更新分配表信息
bool UpdateDateIndex(DTYPE *fp, int index, void *pdata, size_t size)
{
bool isTrue = false;
isTrue = device_write(fp, OFFSETHEAD_INDEX + sizeof(int) + index * size, pdata, size);
if (false == isTrue)
{
return false;
}
fflush(fp);
return true;
}
//更新数据
bool UpdateDateBin(DTYPE *fp, int pos, void *pdata, size_t size)
{
bool isTrue = false;
isTrue = device_write(fp, pos, pdata, size);
if (false == isTrue)
{
return false;
}
fflush(fp);
return true;
}
/*
获取可回收垃圾索引数据项
*/
bool GetRecoverableDataIndex(DTYPE *fp, int index, PTAGLINKMAN data)
{
bool isTrue = false;
isTrue = device_read(fp, OFFSETHEAD_RECOVERABLE + 4 + index * sizeof(TAGLINKMAN), data, sizeof(TAGLINKMAN));
if (false == isTrue)
{
return false;
}
return true;
}
/*
获取可回收垃圾项
*/
bool GetRecoverableItemIndex(DTYPE *fp, int index, PTAGITEM data)
{
bool isTrue = false;
isTrue = device_read(fp, OFFSETHEAD_RECOVERABLE + 4 + index * sizeof(TAGITEM), data, sizeof(TAGITEM));
if (false == isTrue)
{
return false;
}
return true;
}
/*
删除碎片索引
*/
bool DeleteRecoverableByIndex(DTYPE *fp, int index)
{
int nBegin = OFFSETHEAD_RECOVERABLE + sizeof(int) + index * sizeof(TAGLINKMAN);
int nLen = GetRecoverableDataIndexCount(fp);
int nTotol = OFFSETHEAD_RECOVERABLE + sizeof(int) + nLen * sizeof(TAGLINKMAN);
int nSize = nTotol - nBegin;
char *buf = (char *)malloc(nSize);
if (buf == NULL)
{
goto EXIT_PRO;
}
TAGLINKMAN temp = { 0 };
//获取数据
GetRecoverableDataIndex(fp, index, &temp);
//移除索引
device_read(fp, nBegin, buf, nSize);
device_write(fp, OFFSETHEAD_RECOVERABLE + sizeof(int) + index * sizeof(TAGLINKMAN), buf, nSize);
nLen = nLen - 1;
if (device_write(fp, OFFSETHEAD_RECOVERABLE, &nLen, sizeof(nLen)) == false)
{
printf("Delete Recoverable Error!");
}
fflush(fp);
EXIT_PRO:
if (buf != NULL)
{
free(buf);
buf = NULL;
}
return false;
}
/*
是否在回收站
*/
bool IsRecoverable(DTYPE *fp, int nOffset)
{
int nRecCount = GetRecoverableDataIndexCount(fp);
for (int i = 0; i < (nRecCount * 2); i++)
{
TAGITEM tagTemp = { 0 };
if (GetRecoverableItemIndex(fp, i, &tagTemp) == true)
{
if (nOffset >= tagTemp.nOffset && nOffset <= (tagTemp.nOffset + tagTemp.nLen))
{
return true;
}
}
else
{
return false;
}
}
return false;
}
/*
添加联系人二进制数据到磁盘
返回磁盘中的offset
*/
int AddDataBin(DTYPE *fp, char *data, size_t size)
{
int nOffset = 0;
int ret = 0;
bool isTrue = false;
isTrue = device_read(fp, OFFSETTAIL_DATA, &nOffset, sizeof(nOffset));
if (false == isTrue)
{
return -1;
}
isTrue = device_write(fp, OFFSETSTART_DATA + nOffset, data, size);
if (false == isTrue)
{
return -1;
}
ret = nOffset;
nOffset = nOffset + size;
isTrue = device_write(fp, OFFSETTAIL_DATA, &nOffset, sizeof(int));
if (false == isTrue)
{
return -1;
}
fflush(fp);
return ret;
}
/*
读取二进制数据
*/
bool GetDataBin(DTYPE *fp, int nOffset, size_t size, void *data)
{
bool isTrue = false;
isTrue = device_read(fp, OFFSETSTART_DATA + nOffset, data, size);
if (false == isTrue)
{
return false;
}
return true;
}
/*
删除分配表索引数据项
*/
bool DeleteDataIndex(DTYPE *fp, int index)
{
int nBegin = OFFSETHEAD_INDEX + sizeof(int) + (index + 1) * sizeof(TAGLINKMAN);
int nLen = GetDataIndexCount(fp);
int nTotol = OFFSETHEAD_INDEX + sizeof(int) + nLen * sizeof(TAGLINKMAN);
int nSize = nTotol - nBegin;
char *buf = (char *)malloc(nSize);
if (buf == NULL)
{
goto EXIT_PRO;
}
TAGLINKMAN temp = { 0 };
//获取数据
GetDataIndex(fp, index, &temp);
//放入回收站
AddRecoverableDataIndex(fp, &temp, sizeof(temp));
//移除索引
device_read(fp, nBegin, buf, nSize);
device_write(fp, OFFSETHEAD_INDEX + sizeof(int) + index * sizeof(TAGLINKMAN), buf, nSize);
nLen = nLen - 1;
if (device_write(fp, OFFSETHEAD_INDEX, &nLen, sizeof(nLen)) == false)
{
printf("Delete Linkman Error!");
}
EXIT_PRO:
if (buf != NULL)
{
free(buf);
buf = NULL;
}
return false;
}
/*
初始化设备
*/
bool InitDevice(DTYPE **dOffset, DTYPE **dData)
{
//打开偏移索引文件
*dOffset = device_open(OFFSET_DEVICE_NAME);
if (NULL == *dOffset)
{
//失败表示文件不存在 则创建文件
puts("Not found data offset file,Is create a file,Please input 'y' or 'Y' to confirm,Otherwise it will be exit (y or Y): ");
char ch = 0;
ch = getchar();
getchar();
if ('y' == ch || 'Y' == ch)
{
if (false == create_offset_bin())
{
puts("Create file is fail,program quit!");
return false;
}
else
{
*dOffset = device_open(OFFSET_DEVICE_NAME);
}
}
else
{
return false;
}
}
//打开数据文件
*dData = device_open(DEVICE_NAME);
if (NULL == *dData)
{
//失败表示文件不存在 则创建文件
puts("Not found data file,Is create a file,Please input 'y' or 'Y' to confirm,Otherwise it will be exit (y or Y): ");
char ch = 0;
ch = getchar();
getchar();
if ('y' == ch || 'Y' == ch)
{
if (false == create_data_bin())
{
puts("Create file is fail,program quit!");
return false;
}
else
{
*dData = device_open(DEVICE_NAME);
}
}
else
{
return false;
}
}
return true;
}
/*
关闭设备
*/
void CloseDevice(DTYPE *dOffset, DTYPE *dData)
{
if (NULL != dOffset)
{
device_close(dOffset);
}
if (NULL != dData)
{
device_close(dData);
}
}