-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdfuse.c
2018 lines (1659 loc) · 51.7 KB
/
dfuse.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Code not otherwise copyrighted is Copyright (C) 2010-2013 Dan Reif
*
* With the permission of Miklos Szeredi, the entirety of this file is exclusively licensed
* under the GNU GPL, version 2 or later:
*
* DFUSE, (C) Dan Reif, Miklos Szeredi, and others, based on the Hello FS template by Miklos
* Szeredi <[email protected]>. This code and any resultant executables or libraries are
* governed by the terms of the GPL published by the GNU project of the Free Software
* Foundation, either version 2, or (at your option) the latest version available at
* www.gnu.org at the time of its use or modification.
*/
#define FUSE_USE_VERSION 25
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <errmsg.h>
#include <fuse_opt.h>
#include <syslog.h>
/** options for fuse_opt.h */
struct options {
char *username;
char *password;
char *hostname;
char *database;
char *table;
char *prikey;
char *columns;
char *timestamp;
}options;
//Conservative, yes, but should be plenty. Also protects us from signedness issues.
#define MAX_STRING_LENGTH 2147483646
struct string_length {
char * string;
unsigned long length;
};
struct dfuse_nv_ll {
struct string_length * name;
struct string_length * value;
struct dfuse_nv_ll * nvll_value;
struct dfuse_nv_ll * next;
};
FILE *debug_fd( void );
#ifdef DEBUG
#define D(x,y) {fprintf(debug_fd(),(x),(y));fflush(debug_fd());}
#else
#define D(x,y) (void)0
#endif
// Stands for "DFuse Return Value". Implements --lazy-connect. Use instead of "return (somevalue);".
#define DFRV(v) { dfuse_maybe_close(); return (v); }
#define MYSQLSERVER options.hostname
#define MYSQLUSER options.username
#define MYSQLPASS options.password
#define MYSQLDB options.database
#define MAX_SQL_LENGTH 1500
//Doesn't like 'A', prefers 'a'. Makes the algorithm faster.
#define FROM_HEX(nbl) (((nbl)-'0')%('a'-'0'-10))
/** macro to define options */
#define DFUSE_OPT_KEY(t, p, v) { t, offsetof(struct options, p), v }
/** keys for FUSE_OPT_ options */
enum
{
KEY_VERSION,
KEY_HELP,
KEY_LAZY_CONNECT,
KEY_FOREGROUND,
KEY_JSON,
};
static struct fuse_opt dfuse_opts[] =
{
DFUSE_OPT_KEY("-u %s", username, 0),
DFUSE_OPT_KEY("-p %s", password, 0),
DFUSE_OPT_KEY("-H %s", hostname, 0),
DFUSE_OPT_KEY("-D %s", database, 0),
DFUSE_OPT_KEY("-t %s", table, 0),
DFUSE_OPT_KEY("-P %s", prikey, 0),
DFUSE_OPT_KEY("-c %s", columns, 0),
DFUSE_OPT_KEY("-T %s", timestamp, 0),
// #define FUSE_OPT_KEY(templ, key) { templ, -1U, key }
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("--lazy-connect", KEY_LAZY_CONNECT),
FUSE_OPT_KEY("--foreground", KEY_FOREGROUND),
FUSE_OPT_KEY("-f", KEY_FOREGROUND),
FUSE_OPT_KEY("--json", KEY_JSON),
FUSE_OPT_END
};
MYSQL *cached_sql = NULL;
MYSQL *dfuse_connect( char *host, char *user, char *pass, char *defaultdb );
FILE *cached_debug_fd = NULL;
void usage( char **argv );
unsigned short int lazy_conn = 0;
unsigned short int json = 0;
#ifdef VMALLOC
//No support for calloc, realloc... it's a hack, replace it with something better.
/* Compile with -DVMALLOC and -DDEBUG to enable. Try something like:
* grep -i alloc /tmp/fusedebug | awk '/Dealloc/ {print $2} /Alloc/ {print $7}' | sort | uniq -c | sort -rn
* to parse it, though really what you want is more like:
* grep -i alloc /tmp/fusedebug | awk '/Dealloc/ {print $2} /Alloc/ {print $7}' | sort | uniq -c | grep -vE '[02468] '
* though that's not tested. The first syntax found me the fact that I never free()'d encoded_prikey in
* dfuse_jsonify_row. Oops!
*/
void * dfuse_malloc( size_t s, long line )
{
void * p = NULL;
D("Allocating %d bytes at ", (s));
D("line %ld: ",line);
p = malloc(s);
D("%p\n",p);
return p;
}
void dfuse_free( void * p, long line )
{
D("Deallocating %p at ", (p));
D("line %ld.\n",line);
free(p);
}
#define DFUSE_MALLOC(s) dfuse_malloc((s),__LINE__)
#define DFUSE_FREE(p) dfuse_free((p),__LINE__)
#else
#define DFUSE_MALLOC(s) malloc((s))
#define DFUSE_FREE(p) free((p))
#endif
static char hex[] = "0123456789abcdef";
//I couldn't find a decent one, so I wrote my own. It's frankly proud of the fact that it ignores locales.
//I can't come up with a reason why that's not the right decision here, but if I'm wrong, feel free to correct it.
char *urlencode( const char *encodethis, unsigned long length )
{
unsigned long i, rvptr = 0;
char *rv;
if ( !length || !encodethis )
{
return NULL;
}
if ( length > MAX_STRING_LENGTH )
{
//Arguably should die here.
return NULL;
}
if ( !( rv = DFUSE_MALLOC(length*3+1) ) ) //Allocate for the malicious case: every single character needs to be escaped.
{
return NULL;
}
D("Encoding '%s': ",encodethis);
for ( i = 0; i < length; i++ )
{
if ( ( encodethis[i] >= 'A' && encodethis[i] <= 'Z' ) //[A-Z], aka isupper() but paranoid
|| ( encodethis[i] >= 'a' && encodethis[i] <= 'z' ) //[a-z], aka islower() but paranoid
|| ( encodethis[i] >= '0' && encodethis[i] <= '9' ) //[0-9], ala isdigit() but paranoid
|| ( encodethis[i] == '-' || encodethis[i] == '_' || encodethis[i] == '.' || encodethis[i] == ':' ) ) //Exceptions
{
rv[rvptr++] = encodethis[i];
continue;
}
rv[rvptr++] = '%';
rv[rvptr++] = hex[(encodethis[i] >> 4) & 0x0f];
rv[rvptr++] = hex[(encodethis[i] ) & 0x0f];
if ( rvptr >= MAX_STRING_LENGTH )
{
DFUSE_FREE(rv);
return NULL;
}
}
rv[rvptr] = '\0';
D("'%s'.\n",rv);
return rv;
}
struct string_length *htmldecode_n( const char *decodethis, struct string_length *rv_struct, unsigned long maxi )
{
unsigned long i, rvptr = 0;
char *rv;
D( "Decoding '%s'.\n", decodethis );
if ( !rv_struct || !rv_struct->string || !decodethis || maxi < 0 ) //We explicitly *do* handle zero-length strings.
{
return NULL;
}
if ( maxi >= MAX_STRING_LENGTH )
{
//Arguably should die here.
return NULL;
}
rv = rv_struct->string;
for ( i = 0; i <= maxi; i++ )
{
if ( decodethis[i] != '&' )
{
rv[rvptr++] = decodethis[i];
continue;
}
if ( decodethis[i+1] != 'x' )
{
D( "Failed 'x' assert at i=%ld.", i );
return NULL;
}
if ( decodethis[i+4] != ';' )
{
D( "Failed ';' assert at i=%ld.", i );
return NULL;
}
rv[rvptr++] = (FROM_HEX(decodethis[i+2])<<4) + FROM_HEX(decodethis[i+3]);
i += 4;
if ( rvptr >= MAX_STRING_LENGTH ) // This can never happen, since rvptr is always <= strlen(decodethis), but meh
{
return NULL;
}
}
rv[rvptr] = '\0';
rv_struct->length = rvptr;
return rv_struct;
}
char *htmlencode( const char *encodethis, unsigned long length )
{
unsigned long i, rvptr = 0;
char *rv;
if ( length < 0 || !encodethis )
{
return NULL;
}
if ( length > MAX_STRING_LENGTH )
{
//Arguably should die here.
return NULL;
}
if ( !( rv = DFUSE_MALLOC(length*5+1) ) ) //Allocate for the malicious case: every single character needs to be escaped.
{
return NULL;
}
D("Encoding '%s': ",encodethis);
for ( i = 0; i < length; i++ )
{
if ( ( encodethis[i] >= 'A' && encodethis[i] <= 'Z' ) //[A-Z], aka isupper() but paranoid
|| ( encodethis[i] >= 'a' && encodethis[i] <= 'z' ) //[a-z], aka islower() but paranoid
|| ( encodethis[i] >= '0' && encodethis[i] <= '9' ) //[0-9], ala isdigit() but paranoid
|| ( encodethis[i] == '-' || encodethis[i] == '_' || encodethis[i] == '.' || encodethis[i] == ':' ) || encodethis[i] == ' ' ) //Exceptions
{
rv[rvptr++] = encodethis[i];
continue;
}
rv[rvptr++] = '&';
rv[rvptr++] = 'x';
rv[rvptr++] = hex[(encodethis[i] >> 4) & 0x0f];
rv[rvptr++] = hex[(encodethis[i] ) & 0x0f];
rv[rvptr++] = ';';
if ( rvptr >= MAX_STRING_LENGTH )
{
DFUSE_FREE(rv);
return NULL;
}
}
rv[rvptr] = '\0';
D("'%s'.\n",rv);
return rv;
}
/*
* Doesn't handle some old-style silliness (like +), but meh. The real trick
* here is that, since I control what gets generated as a name in the first
* place, I can take some shortcuts. A significant security risk downside to
* that line of thinking is that it permits multiple representations of a
* given file's name; for example, you can call a file "cron_last", but you can
* reference that same file as "cro%6e_last", and perhaps most surprisingly, as
* "cro%]e_last". If that poses a security concern, it's easy enough to do
* something like urlencode(rv) and then strcmp it to what you were handed in
* the first place, but since the whole point of this approach is speed, I
* didn't see a need to do that. If you're exposing this dir via Apache with
* <File> directives governing access, you'll probably want to add that strcmp.
*/
struct string_length *urldecode( const char *decodethis )
{
unsigned long i, rvptr = 0;
char *rv;
struct string_length * rv_struct = NULL;
/*
* Zero-length strings freak me out here, but there's no reason to refuse to decode them
* that I can think of. Convince me otherwise.
*/
if ( !decodethis )
{
return NULL;
}
if ( strlen(decodethis) >= MAX_STRING_LENGTH )
{
//Arguably should die here.
return NULL;
}
if ( !( rv = DFUSE_MALLOC( strlen(decodethis)+1 ) ) )
{
return NULL;
}
for ( i = 0; i < strlen(decodethis); i++ )
{
if ( decodethis[i] != '%' )
{
rv[rvptr++] = decodethis[i];
continue;
}
rv[rvptr++] = (FROM_HEX(decodethis[i+1])<<4) + FROM_HEX(decodethis[i+2]);
i += 2;
if ( rvptr >= MAX_STRING_LENGTH ) // This can never happen, since rvptr is always <= strlen(decodethis), but meh
{
DFUSE_FREE( rv );
return NULL;
}
}
rv[rvptr] = '\0';
if ( !( rv_struct = DFUSE_MALLOC( sizeof( rv_struct ) ) ) )
{
DFUSE_FREE( rv );
return NULL;
}
rv_struct->length = rvptr;
rv_struct->string = rv;
return rv_struct;
}
void dfuse_maybe_close( void )
{
if ( !cached_sql || !lazy_conn )
{
return;
}
mysql_close( cached_sql );
cached_sql = NULL;
return;
}
char * dfuse_jsonify_row( MYSQL_ROW *sql_row, MYSQL_RES *sql_res, char *prikey, unsigned long prikey_len )
{
char **encoded_pieces, **encoded_fieldname, *encoded_prikey, *rv;
MYSQL_FIELD *sql_field;
unsigned long *lengths;
unsigned int num_fields, i;
unsigned long encoded_length = 0;
unsigned long long jsonified_length = 0;
static char jsonify_prepri[] = "{\n\t\"";
static char jsonify_postpri[] = "\": {\n";
static char jsonify_prerow[] = "\t\"";
static char jsonify_midrow[] = "\": \"";
static char jsonify_midrow_nq[] = "\": ";
static char jsonify_postrow[] = "\",\n";
static char jsonify_postrow_nq[] = ",\n";
static char jsonify_end[] = "\t}\n}";
if ( !sql_row || !prikey )
{
return NULL;
}
if ( prikey_len > MAX_STRING_LENGTH )
{
//Arguably should die here.
return NULL;
}
if ( !( num_fields = mysql_num_fields( sql_res ) ) )
{
return NULL;
}
if ( !( lengths = mysql_fetch_lengths( sql_res ) ) )
{
return NULL;
}
if ( !( encoded_prikey = htmlencode(prikey, prikey_len) ) )
{
return NULL;
}
if ( !( encoded_pieces = DFUSE_MALLOC(sizeof(encoded_pieces)*num_fields) ) )
{
return NULL;
}
if ( !( encoded_fieldname = DFUSE_MALLOC(sizeof(encoded_fieldname)*num_fields) ) )
{
return NULL;
}
for ( i = 0; i < num_fields; i++ )
{
sql_field = mysql_fetch_field( sql_res );
if ( sql_field && sql_field->name )
{
encoded_fieldname[i] = htmlencode(sql_field->name,sql_field->name_length);
}
else
{
encoded_fieldname[i] = "";
}
encoded_length += strlen(encoded_fieldname[i]);
D("Got an encoded fieldname: '%s'.\n", encoded_fieldname[i]);
if ( (*sql_row)[i] )
{
encoded_pieces[i] = htmlencode((*sql_row)[i],lengths[i]);
encoded_length += strlen(encoded_pieces[i]);
}
else
{
D("\tDecided to null-code: '%p', ", sql_row[i]);
D("'%p'\n", (*sql_row)[i]);
encoded_pieces[i] = NULL;
encoded_length += strlen("null")-
((strlen(jsonify_midrow)+strlen(jsonify_postrow))-
(strlen(jsonify_midrow_nq)+strlen(jsonify_postrow_nq))); // -2 for the start and end quotes.
}
D("\tBuilt an encoded_piece: '%s'.\n", encoded_pieces[i]);
}
jsonified_length = (
strlen( jsonify_prepri ) +
strlen( encoded_prikey ) +
strlen( jsonify_postpri ) +
( ( strlen( jsonify_prerow ) + strlen( jsonify_midrow ) + strlen( jsonify_postrow ) ) * num_fields ) +
encoded_length + // length of all the row data and field names, htmlencoded
strlen( jsonify_end ) +
1 ); // '\0'
if ( jsonified_length > MAX_STRING_LENGTH )
{
//Arguably should die noisily, since this means skullduggery is almost certainly afoot.
for ( i = 0; i < num_fields; i++ )
{
DFUSE_FREE( encoded_fieldname[i] );
DFUSE_FREE( encoded_pieces[i] );
}
DFUSE_FREE( encoded_fieldname );
DFUSE_FREE( encoded_pieces );
DFUSE_FREE( encoded_prikey);
return NULL;
}
//Opportunity to optimize: cache strlen()s via #define or summat
if ( !jsonified_length || !( rv = DFUSE_MALLOC( jsonified_length ) ) )
{
for ( i = 0; i < num_fields; i++ )
{
DFUSE_FREE( encoded_fieldname[i] );
DFUSE_FREE( encoded_pieces[i] );
}
DFUSE_FREE( encoded_fieldname );
DFUSE_FREE( encoded_pieces );
DFUSE_FREE( encoded_prikey);
return NULL;
}
// This part is stupidly easy to optimize; parts of the rv string are copied over themselves (row+1) times.
D("Got a rv ready to bundle with e_l = %ld.\n",encoded_length);
sprintf( rv, "%s%s%s",
jsonify_prepri,
encoded_prikey,
jsonify_postpri );
D("Starting off small: '%s'.\n", rv);
for ( i = 0; i < num_fields; i++ )
{
sprintf( rv, "%s%s%s%s%s%s",
rv,
jsonify_prerow,
encoded_fieldname[i],
encoded_pieces[i] ? jsonify_midrow : jsonify_midrow_nq,
encoded_pieces[i] ? encoded_pieces[i] : "null", //The irony that "null" goes in quotes here of
encoded_pieces[i] ? jsonify_postrow : jsonify_postrow_nq ); //all places is not lost on me.
DFUSE_FREE( encoded_fieldname[i] );
DFUSE_FREE( encoded_pieces[i] );
D("Gettin' fancier: '%s'.\n", rv);
}
sprintf( rv, "%s%s",
rv,
jsonify_end );
D("Slicker 'n a mayonaise sandwich: '%s'.\n", rv);
DFUSE_FREE( encoded_fieldname );
DFUSE_FREE( encoded_pieces );
DFUSE_FREE( encoded_prikey);
return rv;
/* Pseudocode to help me get things right.
$rv = "{\n\t" . prikey . ": {\n";
foreach( $row as $key => $val )
{
$rv .= "\t" . $key . ': "' . htmlencode($val) . '",' . "\n";
}
$rv .= "\t}\n}";
*/
}
struct dfuse_nv_ll * new_nvll( void )
{
struct dfuse_nv_ll *rv;
if ( !( rv = DFUSE_MALLOC( sizeof( struct dfuse_nv_ll ) ) ) )
{
return NULL;
}
rv->name = NULL;
rv->nvll_value = NULL;
rv->value = NULL;
rv->next = NULL;
return rv;
}
void free_nvll( struct dfuse_nv_ll *root )
{
if ( !root )
{
return;
}
if ( root->name ) { DFUSE_FREE(root->name); }
if ( root->value ) { DFUSE_FREE(root->value); }
if ( root->nvll_value ) { free_nvll( root->nvll_value ); }
if ( root->next ) { free_nvll( root->next ); }
DFUSE_FREE( root );
}
#define so_sl sizeof( struct string_length )
/*
* Parses some JSON into an array for you.
*
* @param json The string to parse. Must be complete and valid JSON, and cannot omit the
* root {} (that said, it might actually work, but I can't promise anything).
* @param recursed If it is non-null, we will return as soon as paren_deep <= 0 and set the
* unsigned long to which it points to the number of characters we disgested (intended
* for internal use; by default, we error out when the json is invalid, and this test is
* one of the ways we know that it is.
*
* @returns A linked list, next == null on the last element.
*/
struct dfuse_nv_ll * dfuse_parse_json( const char *json, size_t size, unsigned long *recursed )
{
struct dfuse_nv_ll *rootnvll = NULL;
struct dfuse_nv_ll *thisnvll = NULL;
unsigned long i;
unsigned long chars;
unsigned long toallocate;
int paren_deep = 0;
short int gotaname = 0;
char *decodedval;
struct string_length *decodedsl;
if ( !json )
{
D( "Null json with recursed='%p'.\n", recursed );
return NULL;
}
// We'll do this inside the loop, instead. Might be faster to do it here, though. Feel
// free to benchmark both approaches.
/* if ( strlen(json) > MAX_STRING_LENGTH )
{
return NULL;
}*/
for ( i = 0; json[i] != '\0' && i < size; i++ )
{
D( "\nParsing '%c'.\n", json[i] );
switch( json[i] )
{
case ' ':
case '\t':
case '\n':
case '\r':
case ',': //We skip it; arguably, we should parse through it, but that really only adds an extra error check.
case ':': //As above.
continue;
case '"':
if ( !rootnvll || !thisnvll )
{
D( "Reading a string without any rootnvll and/or thisnvll. Dying at %d.\n", __LINE__ );
return NULL;
}
// Read until endquote, this is our name if name==null, our value otherwise.
// If it's a value, when done reading it, set name=null.
toallocate=1; //sizeof("") = 1 due to '\0'.
for ( chars = 1; json[i+chars] != '"'; chars++ )
{
toallocate++;
if ( json[i+chars] == '\0' )
{
D( "Unexpected null while reading nameval at pos %ld.\n", i+chars );
free_nvll( rootnvll );
return NULL;
}
if ( json[i+chars] == '&' )
{
if ( json[i+chars+1] != 'x'
|| json[i+chars+4] != ';' )
{
D( "Bad '&' at position %ld.\n", i+chars );
free_nvll( rootnvll );
return NULL;
}
D( "\tRead an entity at i=%ld, ", i );
D( "chars=%ld.\n", chars );
chars += 4; // skip this character and the next four.
continue;
}
}
if ( !( decodedsl = DFUSE_MALLOC(sizeof(decodedsl)) ) )
{
D( "Failed to allocate %ld bytes for decodedsl.", sizeof(decodedsl) );
free_nvll(rootnvll);
return NULL;
}
if ( !( decodedval = DFUSE_MALLOC(toallocate) ) )
{
D( "Failed to allocate %ld bytes for toallocate.", toallocate );
free_nvll(rootnvll);
DFUSE_FREE(decodedsl);
return NULL;
}
decodedsl->string = decodedval;
decodedsl->length = 0;
if ( chars == 1 ) //In other words, if the first character we hit was a "...
{
decodedsl->string[0] = '\0';
}
else if ( !htmldecode_n( json+i+1, decodedsl, chars-2 ) )
{
D( "Chars = %ld", chars );
D( "Failed to htmldecode at line %d.", __LINE__ );
free_nvll(rootnvll);
DFUSE_FREE(decodedval);
DFUSE_FREE(decodedsl);
return NULL;
}
// -1 for the '\0'
if ( (toallocate-1) != decodedsl->length )
{
D( "Assert failed; my size estimation was wrong! They differ by %ld.", toallocate-1-decodedsl->length );
free_nvll(rootnvll);
DFUSE_FREE(decodedval);
DFUSE_FREE(decodedsl);
return NULL;
}
if ( gotaname )
{
if ( !( thisnvll->value = DFUSE_MALLOC( sizeof( thisnvll->value ) ) ) )
{
D( "Failed to malloc for the value stringlength at i=%ld.", i );
free_nvll(rootnvll);
DFUSE_FREE(decodedval);
DFUSE_FREE(decodedsl);
return NULL;
}
thisnvll->value->length = toallocate-1;
thisnvll->value->string = decodedsl->string;
DFUSE_FREE( decodedsl );
gotaname = 0;
}
else
{
struct dfuse_nv_ll *lastnvll = thisnvll;
if ( !( thisnvll = new_nvll() ) )
{
D( "Failed to malloc for new thisnvll at %d.", __LINE__ );
free_nvll(rootnvll);
DFUSE_FREE(decodedval);
DFUSE_FREE(decodedsl);
return NULL;
}
lastnvll->next = thisnvll;
if ( !( thisnvll->name = DFUSE_MALLOC( sizeof( thisnvll->name ) ) ) )
{
D( "Failed to malloc for the name stringlength at i=%ld.", i );
free_nvll(rootnvll);
DFUSE_FREE(decodedval);
DFUSE_FREE(decodedsl);
return NULL;
}
thisnvll->name->length = toallocate-1;
thisnvll->name->string = decodedsl->string;
DFUSE_FREE( decodedsl );
gotaname = 1;
}
i += chars;
break;
case '{':
paren_deep++;
if ( !rootnvll )
{
if ( paren_deep > 1 )
{
D( "Things that should never happen, part 6(b), subsection %d.", __LINE__ );
return NULL;
}
//Root brace. Or misformatted JSON. Either way, don't get too worked up over it.
if ( !( rootnvll = thisnvll = new_nvll() ) )
{
D( "Bailing due to inability to set rootnvll to newnvll at %d.", __LINE__ );
return NULL;
}
continue;
}
if ( !gotaname || !thisnvll->name )
{
D( "Things that should never happen, part 6(b), subsection %d.", __LINE__ );
free_nvll( rootnvll );
return NULL;
}
if ( thisnvll->value )
{
D( "Things that should never happen, part 6(b), subsection %d.", __LINE__ );
free_nvll( rootnvll );
return NULL;
}
if ( !( thisnvll->nvll_value = dfuse_parse_json( json+i, size-i, &chars ) ) )
{
D( "Things that should never happen, part 6(b), subsection %d.", __LINE__ );
free_nvll( rootnvll );
return NULL;
}
i += chars-1; //We want to be handed back the location of the next }, as we've incremented paren_deep.
break;
case '}':
paren_deep--;
if ( !recursed && paren_deep < 0 )
{
D( "The JSON dwarves delved too deep: %d!", __LINE__ );
free_nvll( rootnvll );
return NULL;
}
if ( recursed && paren_deep == 0 )
{
D( "Found the bottom of my stack. Returning at i=%ld.", i );
*recursed = i;
return rootnvll;
}
break;
//TODO: Dies on nulls at the moment. Fix that.
default:
D( "Unhandled character '%c' in json input. Bailing.\n", json[i] );
return NULL;
}
}
if ( paren_deep != 0 )
{
D( "Parse error: paren_deep is %d (and should be zero). Bailing.\n", paren_deep );
free_nvll( rootnvll );
return NULL;
}
D( "All's well: returning '%p'.\n", rootnvll );
D( "Total characters parsed: %ld.\n", i );
return rootnvll;
}
void print_nvll( struct dfuse_nv_ll *rootnvll, unsigned long deep )
{
struct dfuse_nv_ll *thisnvll;
int i;
if ( !rootnvll )
{
D( "Bailing early; asked to print an empty nvll at deep=%ld.\n", deep );
return;
}
for ( i = 0; i < deep; i++ ) { printf( "\t" ); }
printf( "{\n" );
for ( thisnvll = rootnvll; thisnvll; thisnvll = thisnvll->next )
{
if ( thisnvll->name )
{
for ( i = 0; i < deep; i++ ) { printf( "\t" ); }
//Blatantly not null-safe, but this is intended as a debugging function.
printf( "%s: ", thisnvll->name->string );
if ( thisnvll->nvll_value )
{
print_nvll( thisnvll->nvll_value, deep+1 );
}
else if ( thisnvll->value )
{
//Blatantly not null-safe, but this is intended as a debugging function.
printf( "\"%s\",\n", thisnvll->value->string );
}
else
{
printf( "null,\n" );
}
}
}
for ( i = 0; i < deep; i++ ) { printf( "\t" ); }
printf( "}\n" );
}
static int dfuse_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs)
{
switch( key )
{
case KEY_HELP:
case KEY_VERSION:
return -1;
break; //For thoroughness
case KEY_LAZY_CONNECT:
lazy_conn = 1;
return 0;
break; //Paranoia, it's all the rage this year.
case KEY_FOREGROUND:
if ( fuse_daemonize(1) )
{
printf( "Failed to forestall daemonization.\n" );
return -1;
}
return 0;
break;
case KEY_JSON:
json = 1;
return 0;
break;
default:
return 1; //"1" in this case means "not my problem": typically, the mountpoint. Possibly other gibberish, though.
}
D( "Fix your code at %ld.\n", __LINE__ );
exit(-1); //Now this, on the other hand, is indefensibly paranoid.
}
FILE *debug_fd( void )
{
// In the simple case, make debugging much easier.
if ( stdout ) { return stdout; }
if ( cached_debug_fd ) { return cached_debug_fd; }
if ( !( cached_debug_fd = fopen( "/tmp/fusedebug", "a" ) ) )
{
exit(-1);
}
return cached_debug_fd;
}
static int dfuse_readlink(const char *path, char *linkbuf, size_t bufsize )
{
//In the interest of saving ourselves a lot of time, if this is being called,
//it's a symlink, and if it is, it's always the same one.
//TAG: NULL_HANDLING
if ( bufsize <= 0 )
{
DFRV(-EINVAL);
}
if ( bufsize < strlen("/dev/null") + 1 ) //libc function says strlen(/dev/null), but FUSE API docs say +1.
{
DFRV(-ENOMEM);
}
strncpy(linkbuf,"/dev/null",bufsize);
DFRV(0);
}