-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefragmenter.c
45 lines (43 loc) · 1.01 KB
/
defragmenter.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
#include <stdint.h>
#include <fastdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
node_t *addDEFNode(node_t *node, FILE *db, int depth)
{
node_t *nn = createRNode(node->name);
nn->size = 0;
nn->linked = node->linked;
writeNode(nn, db);
if (node->hasChild == true && !node->isPointer)
{
for (int i = 0; i < node->size; i++)
{
node_t *cnode = getNodeFromAddr(node->children[i], db_file);
node_t *cn = addDEFNode(cnode, db, depth + 1);
linkNode(nn, cn, db);
if (cnode->hasChild == false)
{
char *content = readContent(cnode, db_file);
writeContent(cn, content, db);
}
else
free(cn->children);
free(cn);
}
}
return nn;
}
void defragment()
{
printf("\nInfo: defragmenting...");
remove("./temp.dat");
FILE *tempsFile = fopen("./temp.dat", "wb+");
fseek(tempsFile, 0, SEEK_SET);
addDEFNode(db_node, tempsFile, 0);
fclose(db_file);
fclose(tempsFile);
remove(input_file_path);
rename("./temp.dat", input_file_path);
printf("\nInfo: Success : defragmenting done");
}