forked from jcline/fuse-google-drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gd_interface.c
745 lines (634 loc) · 17.8 KB
/
gd_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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
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 <curl/curl.h>
#include <curl/multi.h>
#include <errno.h>
#include <fcntl.h>
#include <json.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h> // mkdir
#include <unistd.h>
#include <libxml/tree.h>
#include "gd_interface.h"
#include "gd_cache.h"
#include "stack.h"
#include "functional_stack.h"
#include "str.h"
#include "curl_interface.h"
const char auth_uri[] = "https://accounts.google.com/o/oauth2/auth";
const char token_uri[] = "https://accounts.google.com/o/oauth2/token";
//const char drive_scope[] = "https://www.googleapis.com/auth/drive.file";
const char email_scope[] = "https://www.googleapis.com/auth/userinfo.email";
const char docs_scope[] = "https://docs.google.com/feeds/";
const char docsguc_scope[] = "https://docs.googleusercontent.com/";
const char spreadsheets_scope[] = "https://spreadsheets.google.com/feeds/";
const char profile_scope[] = "https://www.googleapis.com/auth/userinfo.profile";
int create_oauth_header(struct gdi_state* state)
{
union func_u func;
int ret = 0;
struct str_t oauth;
struct str_t oauth_header;
str_init(&oauth_header);
str_init_create(&oauth, "Authorization: OAuth ", 0);
const struct str_t const* concat[2] = {&oauth, &state->access_token};
str_init(&state->oauth_header);
func.func3 = str_destroy;
fstack_push(state->stack, &state->oauth_header, &func, 3);
ret = str_concat(&state->oauth_header, 2, concat);
if(ret)
fstack_pop(state->stack);
str_destroy(&oauth);
str_destroy(&oauth_header);
return ret;
}
int gdi_get_credentials()
{
return 0;
}
char* load_file(const char* path, const char* name)
{
size_t pathlen = strlen(path);
size_t namelen = strlen(name);
size_t full_name_len = sizeof(char) * (pathlen + namelen) + 1;
char *full_name = (char*) malloc(full_name_len);
if(full_name == NULL)
{
perror("malloc");
return NULL;
}
memset(full_name, 0, full_name_len);
memcpy(full_name, path, pathlen);
memcpy(full_name + pathlen, name, namelen);
FILE *f = fopen(full_name, "rb");
if(f == NULL)
{
printf("fopen(\"%s\"): %s\n", full_name, strerror(errno));
return NULL;
}
// Find the size of the file
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
rewind(f);
char *result = (char *) malloc(sizeof(char) * (size+1));
if(result == NULL)
{
perror("malloc");
return NULL;
}
// Read the file in one chunk, possible issue for larger files
fread(result, 1, size, f);
if(ferror(f))
{
printf("fread(\"%s\"): %s\n", full_name, strerror(errno));
return NULL;
}
fclose(f);
result[size] = 0;
char *to = result;
char *from = result;
for(; *from != 0; ++from)
{
*to = *from;
if(*from != '\n')
++to;
}
*to = 0;
return result;
}
/** Callback for libcurl for parsing json auth tokens.
*
*/
int curl_post_callback(struct gdi_state *state, struct request_t *request)
{
struct json_object *json = json_tokener_parse(request->response.body.str);
union func_u func;
struct json_object *tmp = json_object_object_get(json, "error");
if(tmp != NULL)
{
fprintf(stderr, "Error: %s\n", json_object_get_string(tmp));
state->callback_error = 1;
return 1;
}
tmp = json_object_object_get(json, "access_token");
const char* val = json_object_get_string(tmp);
str_init_create(&state->access_token, val, 0);
func.func1 = str_destroy;
fstack_push(state->stack, &state->access_token, &func, 1);
tmp = json_object_object_get(json, "token_type");
val = json_object_get_string(tmp);
str_init_create(&state->token_type, val, 0);
func.func1 = str_destroy;
fstack_push(state->stack, &state->token_type, &func, 1);
tmp = json_object_object_get(json, "refresh_token");
val = json_object_get_string(tmp);
str_init_create(&state->refresh_token, val, 0);
func.func1 = free;
fstack_push(state->stack, &state->refresh_token, &func, 1);
tmp = json_object_object_get(json, "id_token");
val = json_object_get_string(tmp);
str_init_create(&state->id_token, val, 0);
func.func1 = free;
fstack_push(state->stack, &state->id_token, &func, 1);
tmp = json_object_object_get(json, "expires_in");
state->token_expiration = json_object_get_int(tmp);
return 0;
}
void print_api_info(const char* path)
{
printf("If you are seeing this then fuse-google-drive was unable to ");
printf("find the files it needs in $XDG_CONFIG_HOME/fuse-google-drive/. ");
printf("You need to go to https://code.google.com/apis/console/ then login ");
printf("and create an installed application project. Select the Google Drive ");
printf("API and generate the necessary tokens. Then, place the clientsecrets ");
printf("code in $XDG_CONFIG_HOME/fuse-google-drive/clientsecrets and place ");
printf("the clientid in the same folder, but in the file 'clientid'.\n");
int ret = mkdir(path, S_IRWXU);
if(ret == -1)
{
if(errno == ENOENT)
{
char *home = getenv("HOME");
char *conf = "/.config/";
char *full_path = (char*) malloc(sizeof(char) * (strlen(home) + strlen(conf)));
memcpy(full_path, home, strlen(home));
memcpy(full_path + strlen(home), conf, strlen(conf));
printf("%s\n", full_path);
ret = mkdir(full_path, S_IRWXU);
if(ret == 0)
{
ret = mkdir(path, S_IRWXU);
if(ret)
rmdir(full_path);
}
free(full_path);
}
}
if(ret == 0)
{
printf("\nWe have created the folder %s for you to place these files.", path);
printf("Please ensure there are no newlines or whitespace before or after ");
printf("the codes in these files.\n");
}
}
/** Reads in the clientsecrets from a file.
*
*/
char* gdi_load_clientsecrets(const char *path, const char *name)
{
char *ret = load_file(path, name);
if(ret == NULL)
print_api_info(path);
return ret;
}
/** Reads in the redirection URI from a file.
*
*/
char* gdi_load_redirecturi(const char *path, const char *name)
{
return load_file(path, name);
}
/** Reads in the client id from a file.
*
*/
char* gdi_load_clientid(const char *path, const char *name)
{
return load_file(path, name);
}
/** Concatenate an urlencoded string with buf.
*
*/
size_t add_encoded_uri(char *buf, const char *uri, const size_t size)
{
size_t length = size;
char *encoded ;//= urlencode(uri, &length);
memcpy(buf, encoded, length);
free(encoded);
return length-1;
}
/** Effectively strcat() that doesn't need to call strlen(buf).
*
*/
size_t add_unencoded_str(char *buf, const char *str, const size_t size)
{
memcpy(buf, str, size);
return size-1;
}
int gdi_init(struct gdi_state* state)
{
union func_u func;
struct stack_t *estack = (struct stack_t*)malloc(sizeof(struct stack_t));
if(!estack)
return 1;
if(fstack_init(estack, 20))
return 1;
struct stack_t *gstack = (struct stack_t*)malloc(sizeof(struct stack_t));
if(!gstack)
return 1;
if(fstack_init(gstack, 20))
return 1;
state->stack = estack;
state->head = NULL;
state->tail = NULL;
state->callback_error = 0;
state->num_files = 0;
char *xdg_conf = getenv("XDG_CONFIG_HOME");
char *pname = "/fuse-google-drive/";
if(xdg_conf == NULL)
{
xdg_conf = getenv("HOME");
pname = "/.config/fuse-google-drive/";
}
char *full_path = (char*) malloc(sizeof(char) * (strlen(xdg_conf) +
strlen(pname) + 1));
if(full_path == NULL)
goto init_fail;
func.func1 = free;
fstack_push(gstack, full_path, &func, 1);
memcpy(full_path, xdg_conf, strlen(xdg_conf) + 1);
memcpy(full_path + strlen(xdg_conf), pname, strlen(pname) + 1);
state->clientsecrets = gdi_load_clientsecrets(full_path, "clientsecrets");
if(state->clientsecrets == NULL)
goto init_fail;
func.func1 = free;
fstack_push(estack, state->clientsecrets, &func, 1);
char redirecturi[] = "urn:ietf:wg:oauth:2.0:oob";
state->redirecturi = (char*) malloc(sizeof(char) * sizeof(redirecturi));
if(state->redirecturi == NULL)
goto init_fail;
func.func1 = free;
fstack_push(estack, state->redirecturi, &func, 1);
memcpy(state->redirecturi, redirecturi, sizeof(redirecturi));
state->clientid = gdi_load_clientid(full_path, "clientid");
if(state->clientid == NULL)
goto init_fail;
func.func1 = free;
fstack_push(estack, state->clientid, &func, 1);
if(curl_global_init(CURL_GLOBAL_SSL) != 0)
goto init_fail;
func.func2 = curl_global_cleanup;
fstack_push(estack, NULL, &func, 2);
state->curlmulti = curl_multi_init();
if(state->curlmulti == NULL)
goto init_fail;
func.func3 = curl_multi_cleanup;
fstack_push(estack, state->curlmulti, &func, 3);
/* Authenticate the application */
struct str_t complete_authuri;
func.func1 = str_destroy;
fstack_push(gstack, &complete_authuri, &func, 1);
struct str_t plus;
struct str_t amp;
struct str_t que;
struct str_t response_type_code;
struct str_t code_param;
struct str_t redirect_param;
struct str_t scope_param;
struct str_t client_id_param;
struct str_t client_secret_param;
struct str_t grant_type_param;
struct str_t *email_scope_str = str_urlencode_char(email_scope, 0);
struct str_t *profile_scope_str = str_urlencode_char(profile_scope, 0);
struct str_t *docs_scope_str = str_urlencode_char(docs_scope, 0);
struct str_t *docsguc_scope_str = str_urlencode_char(docsguc_scope, 0);
struct str_t *spreadsheets_scope_str = str_urlencode_char(spreadsheets_scope, 0);
struct str_t *redirecturi_str = str_urlencode_char(state->redirecturi, 0);
struct str_t *clientid_str = str_urlencode_char(state->clientid, 0);
struct str_t *clientsecret_str = str_urlencode_char(state->clientsecrets, 0);
str_init_create(&plus, "+", 0);
str_init_create(&, "&", 0);
str_init_create(&que, "?", 0);
str_init_create(&response_type_code, "response_type=code", 0);
str_init_create(&code_param, "code=", 0);
str_init_create(&redirect_param, "redirect_uri=", 0);
str_init_create(&scope_param, "scope=", 0);
str_init_create(&client_id_param, "client_id=", 0);
str_init_create(&client_secret_param, "client_secret=", 0);
str_init_create(&grant_type_param, "grant_type=authorization%5Fcode", 0);
{ // So we can reuse variable name again
const struct str_t const* uri_parts[] =
{
&que,
&response_type_code,
&,
&scope_param,
email_scope_str,
&plus,
profile_scope_str,
&plus,
docs_scope_str,
&plus,
docsguc_scope_str,
&plus,
spreadsheets_scope_str,
&,
&redirect_param,
redirecturi_str,
&,
&client_id_param,
clientid_str
};
str_init_create(&complete_authuri, auth_uri, 0);
str_concat(&complete_authuri, sizeof(uri_parts)/sizeof(const struct str_t const*), uri_parts);
}
printf("Please open this in a web browser and authorize fuse-google-drive:\n%s\n", complete_authuri.str);
printf("\n\nOnce you authenticate, Google should give you a code, please paste it here:\n");
// Read in the code from Google
size_t length = 45;
size_t i = 0;
size_t done = 0;
state->code = (char *) malloc(sizeof(char)*(length+1));
if(state->code == NULL)
goto init_fail;
func.func1 = free;
fstack_push(estack, state->code, &func, 1);
char *code = state->code;
while(i < length && !done)
{
char c = getc(stdin);
switch(c)
{
case ' ':
break;
case '\t':
break;
case '\n':
done = 1;
break;
default:
code[i] = c;
++i;
break;
}
}
code[i] = 0; // Null terminate code
if(i < 5)
{
printf("You did not enter a correct authentication code.\n");
goto init_fail;
}
// Prepare and make the request to exchange the code for an access token
str_clear(&complete_authuri);
struct str_t *code_str = str_urlencode_char(code, 0);
{
const struct str_t const* uri_parts[] =
{
&code_param,
code_str,
&,
&client_id_param,
clientid_str,
&,
&client_secret_param,
clientsecret_str,
&,
&redirect_param,
redirecturi_str,
&,
&grant_type_param
};
str_init(&complete_authuri);
if(str_concat(&complete_authuri, sizeof(uri_parts)/sizeof(const struct str_t const*), uri_parts))
goto init_fail;
printf("%s\n", complete_authuri.str);
struct str_t token_uri_str;
str_init_create(&token_uri_str, token_uri, 0);
struct request_t request;
ci_init(&request, &token_uri_str, 0, NULL, complete_authuri.str, POST);
ci_request(&request);
if(curl_post_callback(state, &request))
goto init_fail;
ci_destroy(&request);
}
create_oauth_header(state);
gdi_get_file_list(state);
if(create_hash_table(state->num_files*4, state->head))
{
printf("create_hash failed\n");
goto init_fail;
}
func.func2 = destroy_hash_table;
fstack_push(estack, NULL, &func, 2);
goto init_success;
init_fail:
while(estack->size)
fstack_pop(estack);
while(gstack->size)
fstack_pop(gstack);
fstack_destroy(estack);
free(estack);
fstack_destroy(gstack);
free(gstack);
return 1;
init_success:
while(gstack->size)
fstack_pop(gstack);
fstack_destroy(gstack);
free(gstack);
return 0;
}
void gdi_destroy(struct gdi_state* state)
{
printf("Cleaning up...\n");
fflush(stdout);
struct gd_fs_entry_t *iter = state->head;
struct gd_fs_entry_t *tmp = iter;
while(iter != NULL)
{
gd_fs_entry_destroy(iter);
iter = iter->next;
free(tmp);
tmp = iter;
}
while(state->stack->size)
fstack_pop(state->stack);
fstack_destroy(state->stack);
free(state->stack);
}
size_t remove_newlines(char *str, size_t length)
{
size_t scan_iter, move_iter;
for(scan_iter = 0, move_iter = 0; scan_iter < length; ++scan_iter)
{
if(scan_iter == '\0')
{
str[move_iter] = 0;
break;
}
else if(scan_iter != '\n')
{
str[move_iter] = str[scan_iter];
++move_iter;
}
}
return move_iter+1;
}
/** Build linked list of files.
*
* Calls the XML parsing code to get gd_fs_entry_ts for creating a list of files.
*
* @xml a string containing the xml to parse, this is what we get from curl
* @len the length of xml
* @state the variable we store this mount's state in
*
* @returns the link to the next page of the directory listing, as found in xml
*/
struct str_t* xml_parse_file_list(struct str_t* xml, struct gdi_state *state)
{
char* iter = strstr(xml->str, "<feed");
xmlDocPtr xmldoc = xmlParseMemory(iter, xml->len - (iter - xml->str));
xmlNodePtr node;
size_t count = 0;
struct gd_fs_entry_t *tmp = state->tail;
struct str_t* next = NULL;
if(xmldoc == NULL || xmldoc->children == NULL || xmldoc->children->children == NULL)
return NULL;
for(node = xmldoc->children->children; node != NULL; node = node->next)
{
if(strcmp(node->name, "entry") == 0)
{
if(!tmp)
{
tmp = gd_fs_entry_from_xml(xmldoc, node);
state->head = tmp;
state->tail = tmp;
}
else
{
tmp->next = gd_fs_entry_from_xml(xmldoc, node);
tmp = tmp->next;
state->tail = tmp;
}
++count;
}
if(strcmp(node->name, "link") == 0)
{
char *prop = xmlGetProp(node, "rel");
if(prop != NULL)
{
if(strcmp(prop, "next") == 0)
{
next = (struct str_t*) malloc(sizeof(struct str_t));
str_init(next);
next->str = xmlGetProp(node, "href");
next->len = strlen(next->str);
}
}
}
}
xmlFreeDoc(xmldoc);
state->num_files += count;
return next;
}
/** Gets a listing of all the files for this mount.
*
* Calls curl repeatedly to get each page of xml from the directory-listing
* Google API. Then parses those pages into a list of gd_fs_entry_ts, stored
* in state.
*
* @state the state for this mount
*
* @returns void
*/
void gdi_get_file_list(struct gdi_state *state)
{
struct str_t resp;
resp.len = 0;
resp.str = NULL;
struct str_t uri;
str_init_create(&uri, "https://docs.google.com/feeds/default/private/full?v=3&showfolders=true&max-results=1000", 0);
struct str_t* next = NULL;
struct request_t request;
ci_init(&request, &uri, 1, &state->oauth_header, NULL, GET);
do
{
ci_request(&request);
str_destroy(next);
free(next);
next = xml_parse_file_list(&request.response.body, state);
if(next)
ci_set_uri(&request, next);
ci_clear_response(&request);
} while(next);
ci_destroy(&request);
str_destroy(&uri);
}
const char* gdi_strip_path(const char* path)
{
char *filename = strrchr(path, '/');
++filename;
return filename;
}
int gdi_check_update(struct gdi_state* state, struct gd_fs_entry_t* entry)
{
int ret = 0;
if(entry->md5set)
{
struct request_t request;
ci_init(&request, &entry->feed, 1, &state->oauth_header, NULL, GET);
ci_request(&request);
struct str_t* md5 = xml_get_md5sum(&request.response.body);
if(md5 == NULL)
ret = -1;
if(!ret && strcmp(md5->str, entry->md5.str))
ret = 1;
ci_destroy(&request);
}
return ret;
}
int gdi_load(struct gdi_state* state, struct gd_fs_entry_t* entry)
{
int ret = 0;
if(entry->cached)
{
int updated = gdi_check_update(state, entry);
struct request_t request;
switch(updated)
{
case -1:
ret = 1;
break;
case 0:
ret = 0;
break;
case 1:
ci_init(&request, &entry->src, 1, &state->oauth_header, NULL, GET);
ci_request(&request);
str_swap(&request.response.body, &entry->cache);
ci_destroy(&request);
break;
}
}
else
{
struct request_t request;
ci_init(&request, &entry->src, 1, &state->oauth_header, NULL, GET);
ci_request(&request);
str_swap(&request.response.body, &entry->cache);
ci_destroy(&request);
entry->cached = 1;
}
return ret;
}
const char* gdi_read(size_t *size, struct gd_fs_entry_t* entry, off_t offset)
{
size_t remaining = (entry->cache.len < offset) ? 0 : entry->cache.len - offset;
*size = (remaining < *size) ? remaining : *size;
if(*size == 0)
return NULL;
return entry->cache.str + offset;
}