-
Notifications
You must be signed in to change notification settings - Fork 48
/
gd_cache.c
392 lines (361 loc) · 9.4 KB
/
gd_cache.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
/*
fuse-google-drive: a fuse filesystem wrapper for Google Drive
Copyright (C) 2012 James Cline
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <errno.h>
#include <stdio.h>
#include <search.h>
#include <string.h>
#include "gd_cache.h"
#include "str.h"
char filenameunsafe[] =
{
'%',
'/'
};
/** Escapes unsafe characters for filenames.
*
* @filename the string to escape
* @length length of filename
* precondition: strlen(filename)
* postcondition: strlen(escaped filename)
*
* @returns escaped, null terminated string
*/
char* filenameencode (const char *filename, size_t *length)
{
size_t i;
size_t j;
size_t count = 0;
size_t size = *length;
// Count the number of characters that need to be escaped
for(i = 0; i < size; ++i)
{
for(j = 0; j < sizeof(filenameunsafe); ++j)
{
if(filename[i] == filenameunsafe[j])
{
++count;
break;
}
}
}
// Allocate the correct amount of memory for the escaped string
char *result = (char *) malloc( sizeof(char) * (size + count*3 + 1));
if(result == NULL)
return NULL;
// Copy old string into escaped string, escaping where necessary
char *iter = result;
for(i = 0; i < size; ++i)
{
for(j = 0; j < sizeof(filenameunsafe); ++j)
{
// We found a character that needs escaping
if(filename[i] == filenameunsafe[j])
{
// Had a weird issue with sprintf(,"\%%02X,), so I do this instead
*iter = '%';
++iter;
sprintf(iter, "%02X", filenameunsafe[j]);
iter+=2;
break;
}
}
// We did not need to escape the current character in filename, so just copy it
if(j == sizeof(filenameunsafe))
{
*iter = filename[i];
++iter;
}
}
// Calculate the size of the final string, should be the same as (length+count*3)
size = iter - result;
// Make sure we null terminate
result[size] = 0;
// Save the new length
*length = size;
return result;
}
/** Creates and fills in a gd_fs_entry_t from an <entry>...</entry> in xml.
*
* @xml the xml containing the entry
* @node the node representing this <entry>...</entry> block
*
* @returns pointer to gd_fs_entry_t with fields filled in as needed
*/
struct gd_fs_entry_t* gd_fs_entry_from_xml(xmlDocPtr xml, xmlNodePtr node)
{
struct gd_fs_entry_t* entry;
entry = (struct gd_fs_entry_t*) malloc(sizeof(struct gd_fs_entry_t));
if(entry == NULL) {} // TODO: ERROR
memset(entry, 0, sizeof(struct gd_fs_entry_t));
size_t length;
xmlNodePtr c1, c2;
xmlChar *value = NULL;
for(c1 = node->children; c1 != NULL; c1 = c1->next)
{
char const *name = c1->name;
switch(*name)
{
case 'a': // 'author'
for(c2 = c1->children; c2 != NULL; c2 = c2->next)
{
name = c2->name;
value = xmlNodeListGetString(xml, c2->children, 1);
switch(*name)
{
case 'n':
str_init_create(&entry->author, value, 0);
break;
case 'e':
str_init_create(&entry->author_email, value, 0);
break;
default:
break;
}
xmlFree(value);
}
break;
case 'c':
if(strcmp(name, "content") == 0)
{
value = xmlGetProp(c1, "src");
str_init_create(&entry->src, value, 0);
xmlFree(value);
}
break;
case 'f':
if(strcmp(name, "feedlink") == 0)
{
value = xmlGetProp(c1, "rel");
if(strcmp(value, "http://schemas.google.com/acl/2007#accessControlList") == 0)
{
// Link for r/w access to ACLS for this entry
// Do we care?
// Can we expose this?
}
else if(strcmp(value, "http://schemas.google.com/docs/2007/revisions") == 0)
{
// Link for r/w access to revisions
// It would be cool if we can expose these somehow
}
}
break;
case 'l':
if(strcmp(name, "lastModifiedBy") == 0)
{
for(c2 = c1->children; c2 != NULL; c2 = c2->next)
{
name = c2->name;
value = xmlNodeListGetString(xml, c2->children, 1);
switch(*name)
{
case 'n':
str_init_create(&entry->lastModifiedBy, value, 0);
break;
case 'e':
str_init_create(&entry->lastModifiedBy_email, value, 0);
break;
default:
break;
}
xmlFree(value);
}
}
else if(strcmp(name, "link") == 0)
{
value = xmlGetProp(c1, "rel");
if(strcmp(value, "http://schemas.google.com/docs/2007#parent") == 0)
{
// This entry is inside one (or more?) collections
// These entries are the folders for this entry
}
else if(strcmp(value, "alternate") == 0)
{
// Link you can open this document in a web browser with
// Do we care?
}
else if(strcmp(value, "self") == 0)
{
// Link to XML feed for just this entry
// Might be useful for checking for updates instead of changesets
xmlChar *href = xmlGetProp(c1, "href");
str_init_create(&entry->feed, href, 0);
xmlFree(href);
}
else if(strcmp(value, "edit") == 0)
{
// For writes?
}
/*
else if(strcmp(value, "edit-media") == 0)
{
// deprecated, use 'resumeable-edit-media'
}
*/
else if(strcmp(value, "http://schemas.google.com/g/2005#resumable-edit-media") == 0)
{
// For resumeable writes?
// This may be the one we *really* want to use, rather than 'edit'
}
else if(strcmp(value, "http://schemas.google.com/docs/2007/thumbnail") == 0)
{
// Might be a useful way to expose this for GUI file managers?
}
xmlFree(value);
}
break;
case 'm':
if(strcmp(name, "md5Checksum") == 0)
{
value = xmlNodeListGetString(xml, c1->children, 1);
entry->md5set = 1;
str_init_create(&entry->md5, value, 0);
xmlFree(value);
}
break;
case 't': // 'title'
if(strcmp(name, "title") == 0)
{
value = xmlNodeListGetString(xml, c1->children, 1);
str_init_create(&entry->filename, value, 0);
entry->filename.str = filenameencode(value, &entry->filename.len);
entry->filename.reserved = entry->filename.len;
xmlFree(value);
}
break;
case 's':
if(strcmp(name, "size") == 0)
{
value = xmlNodeListGetString(xml, c1->children, 1);
length = xmlStrlen(value);
// TODO: errors?
entry->size = strtol((char*)value, NULL, 10);
xmlFree(value);
}
break;
default:
break;
}
}
return entry;
}
/** Extracts the md5sum from XML containing only an <entry>.
*
* @xml struct str_t* the string containing the XML
*
* @return a string containing the extracted md5sum.
*/
struct str_t* xml_get_md5sum(const struct str_t* xml)
{
size_t length;
xmlNodePtr c1;
xmlChar *value = NULL;
struct str_t* ret = NULL;
const char* iter = strstr(xml->str, "<entry");
xmlDocPtr xmldoc = xmlParseMemory(iter, xml->len - (iter - xml->str));
xmlNodePtr node;
if(xmldoc == NULL || xmldoc->children == NULL || xmldoc->children->children == NULL)
return NULL;
for(node = xmldoc->children->children; node != NULL; node = node->next)
{
char const *name = node->name;
switch(*name)
{
case 'm':
if(strcmp(name, "md5Checksum") == 0)
{
ret = (struct str_t*) malloc(sizeof(struct str_t));
if(!ret)
break;
value = xmlNodeListGetString(xmldoc, node->children, 1);
str_init_create(ret, value, 0);
xmlFree(value);
}
break;
default:
break;
}
}
return ret;
}
/** Cleanup an entry.
*
* @entry struct gd_fs_entry_t* the entry to uninitialize members for
*/
void gd_fs_entry_destroy(struct gd_fs_entry_t* entry)
{
str_destroy(&entry->author);
str_destroy(&entry->author_email);
str_destroy(&entry->lastModifiedBy);
str_destroy(&entry->lastModifiedBy_email);
str_destroy(&entry->filename);
str_destroy(&entry->src);
str_destroy(&entry->feed);
str_destroy(&entry->cache);
str_destroy(&entry->md5);
}
/** Searches hash table for a filename.
*
* @key the name of the file to find
*
* @returns the gd_fs_entry_t representing that file
*/
struct gd_fs_entry_t* gd_fs_entry_find(const char* key)
{
ENTRY keyentry;
keyentry.key = key;
keyentry.data = NULL;
ENTRY* entry = hsearch(keyentry, FIND);
if(entry == NULL)
return NULL;
return (struct gd_fs_entry_t*) entry->data;
}
/** Creates the hash table for speeding up file finding.
*
* @size the size we want to make this hash table
* @head the first gd_fs_entry_t in the list of files
*
* @returns 0 on success, 1 on failure
*/
int create_hash_table(size_t size, const struct gd_fs_entry_t* head)
{
int ret = hcreate(size);
if(!ret)
{
fprintf(stderr, "hcreate failed\n");
return 1;
}
ENTRY entry;
struct gd_fs_entry_t *iter = head;
while(iter != NULL)
{
entry.key = iter->filename.str;
entry.data = iter;
ENTRY* entered = hsearch(entry, ENTER);
if(0 && !entered)
{
fprintf(stderr, "hsearch: %s\n", strerror(errno));
destroy_hash_table();
return 1;
}
iter = iter->next;
}
return 0;
}
/** Destroys the global hash table.
*/
void destroy_hash_table()
{
hdestroy();
}