-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.c
175 lines (147 loc) · 3.4 KB
/
index.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
#include "archiver.h"
#include <unistd.h>
/*
* Index file format
*
* This index is a text file using the format:
* path/to/file1\0numOfAddrs[2]addr1len1addr2len2...
* path/to/file1\0numOfAddrs[2]addr1len1addr2len2...
* where addr is the position in the archive file
*/
int readIndex()
{
idx = fopen(INDEXFILE, "rb");
if (!idx) {
puts("Error opening index file");
return -1;
}
/* Read the whole index */
short fileLen;
char c;
for (;;) {
/* Print the file name */
do {
c = fgetc(idx);
if (c < 0) {
exit(0);
}
printf("%c", c);
} while (c);
/* Discard first and second values */
fseek(idx, 10, SEEK_CUR);
/* Read now to get length */
fread(&fileLen, sizeof(long), 1, idx);
printf("\tlength: %d bytes\n", fileLen);
}
exit(0);
}
int grepIndex(char *name)
{
idx = fopen(INDEXFILE, "rb");
if (!idx) {
puts("Error opening index file");
return -1;
}
/* Read the whole index searching for name */
// FIXME: Loop is not ok
long addr, len = 0;
char c;
while ((c = fgetc(idx)) > -1) {
/* Check if the file name matches */
while (c > 0) {
if (c == *name++) {
/* File was found, save content location */
puts(" file found");
fseek(idx, 2, SEEK_CUR);
fread(&addr, sizeof(long), 1, idx);
fread(&len, sizeof(long), 1, idx);
break;
} else {
/* jump to next file */
while (fgetc(idx) > 0) {
;
}
fseek(idx, 18, SEEK_CUR);
}
c = fgetc(idx);
}
}
/* Print file contents if found */
if (len > 0) {
readArchive(addr, len);
}
/* Close all files */
fclose(idx);
return 0;
}
// int writeIndex(struct item *index)
// {
// idx = fopen(INDEXFILE, "wb");
// if (!idx) {
// puts("Error opening index file");
// exit(-1);
// }
// /* Discover length of index */
// short entries = 0, len = 0;
// for (struct item *ind = index; ind; ind++) {
// entries++;
// }
// /* Write item length */
// fprintf(idx, "%hd\n", entries);
// /* Read the whole index */
// for (short i = 0; i < entries; i++) {
// /* Get list of addresses length */
// for (short ind = 0; (index + i)->lengths; ind++) {
// len++;
// }
// fwrite(&len, 2, 1, idx);
// int nameLen = strlen((index + 1)->fileName);
// fwrite(&nameLen, 2, 1, idx);
// /* Write address intervals */
// for (int n = 0; n < len; n++) {
// fwrite((index + i)->lengths + n, sizeof(long), 1,
// idx);
// }
// }
// /* Close index file */
// fclose(idx);
// return entries;
// }
int writeIndex(struct index index)
{
idx = fopen(INDEXFILE, "wb");
if (!idx) {
puts("Error opening index file");
exit(-1);
}
/* Write whole index to file */
return fwrite(&index, sizeof(struct index), 1, idx);
}
int addToIndex(struct item item)
{
idx = fopen(INDEXFILE, "ab");
if (!idx) {
puts("Error opening index file");
exit(-1);
}
/* Write the file name */
fwrite(item.fileName, strlen(item.fileName) + 1, 1, idx);
/* Write the number of addresses */
// For now it's only 2
// short len = 0;
// for (long *ind = item.lengths; ind != NULL; ind++) {
// len++;
// }
short temp = 2;
fwrite(&temp, sizeof(short), 1, idx);
printf("addrs lens: %ld %ld\n", item.lengths[0], item.lengths[1]);
/* Write address intervals */
// For now there are just 2
// for (int n = 0; n < len; n++) {
// fwrite(item.lengths + n, sizeof(long), 1, idx);
// }
fwrite(&item.lengths[0], sizeof(long), 1, idx);
fwrite(&item.lengths[1], sizeof(long), 1, idx);
fclose(idx);
return 0;
}