File: fusefs_lib.c

package info (click to toggle)
ruby-fusefs 0.7.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: ansic: 1,137; ruby: 760; sh: 8; makefile: 2
file content (1519 lines) | stat: -rw-r--r-- 38,877 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
/* ruby-fuse
 *
 * A Ruby module to interact with the FUSE userland filesystem in
 * a Rubyish way.
 */

/* #define DEBUG /* */

#define FUSE_USE_VERSION 26
#define _FILE_OFFSET_BITS 64

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ruby.h>
#include <unistd.h>
#include <ctype.h>

#ifdef DEBUG
#include <stdarg.h>
#endif

#include "fusefs_fuse.h"

/* init_time
 *
 * All files will have a modified time equal to this. */
time_t init_time;

/* opened_file
 *
 * FuseFS uses the opened_file list to keep files that are written to in
 * memory until they are closed before passing it to FuseRoot.write_to,
 * and file contents returned by FuseRoot.read_file until FUSE informs
 * us it is safe to close.
 */
typedef struct __opened_file_ {
  char   *path;
  char   *value;
  int    modified;
  long   writesize;
  long   size;
  long   zero_offset;
  int    raw;
  struct __opened_file_ *next;
} opened_file;

typedef opened_file editor_file;

static opened_file *opened_head = NULL;
static editor_file *editor_head = NULL;

static int
file_openedP(const char *path) {
  opened_file *ptr;
  for (ptr = opened_head;ptr; ptr = ptr->next)
    if (!strcmp(path,ptr->path)) return 1;
  return 0;
}

/* When a file is being written to, its value starts with this much
 * allocated and grows by this much when necessary. */
#define FILE_GROW_SIZE  1024

/* When a file is created, the OS will first mknod it, then attempt to
 *   fstat it immediately. We get around this by using a static path name
 *   for the most recently mknodd'd path. */
static char *created_file = NULL;

/* Ruby Constants constants */
VALUE cFuseFS      = Qnil; /* FuseFS class */
VALUE cFSException = Qnil; /* Our Exception. */
VALUE FuseRoot     = Qnil; /* The root object we call */

/* IDs for calling methods on objects. */

#define RMETHOD(name,cstr) \
  char *c_ ## name = cstr; \
  ID name;

RMETHOD(id_dir_contents,"contents");
RMETHOD(id_read_file,"read_file");
RMETHOD(id_write_to,"write_to");
RMETHOD(id_delete,"delete");
RMETHOD(id_mkdir,"mkdir");
RMETHOD(id_rmdir,"rmdir");
RMETHOD(id_touch,"touch");
RMETHOD(id_size,"size");

RMETHOD(is_directory,"directory?");
RMETHOD(is_file,"file?");
RMETHOD(is_executable,"executable?");
RMETHOD(can_write,"can_write?");
RMETHOD(can_delete,"can_delete?");
RMETHOD(can_mkdir,"can_mkdir?");
RMETHOD(can_rmdir,"can_rmdir?");

RMETHOD(id_raw_open,"raw_open");
RMETHOD(id_raw_close,"raw_close");
RMETHOD(id_raw_read,"raw_read");
RMETHOD(id_raw_write,"raw_write");

RMETHOD(id_dup,"dup");

typedef unsigned long int (*rbfunc)();

/* debug()
 *
 * If #define DEBUG is enabled, then this acts as a printf to stderr
 */
#ifdef DEBUG
static void
debug(char *msg,...) {
  va_list ap;
  va_start(ap,msg);
  vfprintf(stderr,msg,ap);
}
#else
// Make debug just comment out what's after it.
#define debug // debug
#endif

/* catch_editor_files
 *
 * If this is a true value, then FuseFS will attempt to capture
 * editor swap files and handle them itself, so the ruby filesystem
 * is not passed swap files it doesn't care about.
 */

int handle_editor = 1;
int which_editor  = 0;
#define EDITOR_VIM    1
#define EDITOR_EMACS  2

/* editor_fileP
 *
 * Passed a path, editor_fileP will return if it is likely to be a file
 * belonging to an editor.
 *
 * vim: /path/to/.somename.ext.sw*
 * emacs: /path/to/#somename.ext#
 */
static int
editor_fileP(const char *path) {
  char *filename;
  editor_file *ptr;

  if (!handle_editor)
    return 0;
  
  /* Already created one */
  for (ptr = editor_head ; ptr ; ptr = ptr->next) {
    if (strcasecmp(ptr->path,path) == 0) {
      return 2;
    }
  }

  /* Basic checks */
  filename = strrchr(path,'/');
  if (!filename) return 0; // No /.
  filename++;
  if (!*filename) return 0; // / is the last.

  /* vim */
  do {
    // vim uses: .filename.sw?
    char *ptr = filename;
    int len;
    if (*ptr != '.') break;

    // ends with .sw? 
    ptr = strrchr(ptr,'.');
    len = strlen(ptr);
    // .swp or .swpx
    if (len != 4 && len != 5) break;
    if (strncmp(ptr,".sw",3) == 0) {
      debug("  (%s is a vim file).\n", path);
      which_editor = EDITOR_VIM;
      return 1; // It's a vim file.
    }
  } while (0);

  /* emacs */
  do {
    char *ptr = filename;
    // Begins with a #
    if (*ptr != '#') break;

    // Ends with a #
    ptr = strrchr(ptr,'#');
    if (!ptr) break;
    // the # must be the end of the filename.
    ptr++;
    if (*ptr) break;
    debug("  (%s is an emacs file).\n", path);
    which_editor = EDITOR_EMACS;
    return 1;
  } while (0);
  return 0;
}

/* rf_protected and rf_call
 *
 * Used for: protection.
 *
 * This is called by rb_protect, and will make a call using
 * the above rb_path and to_call ID to call the method safely
 * on FuseRoot.
 *
 * We call rf_call(path,method_id), and rf_call will use rb_protect
 *   to call rf_protected, which makes the call on FuseRoot and returns
 *   whatever the call returns.
 */
static VALUE
rf_protected(VALUE args) {
  ID to_call = SYM2ID(rb_ary_shift(args));
  return rb_apply(FuseRoot,to_call,args);
}

#define rf_call(p,m,a) \
  rf_mcall(p,m, c_ ## m, a)

static VALUE
rf_mcall(const char *path, ID method, char *methname, VALUE arg) {
  int error;
  VALUE result;
  VALUE methargs;

  if (!rb_respond_to(FuseRoot,method)) {
    return Qnil;
  }

  if (arg == Qnil) {
    debug("    root.%s(%s)\n", methname, path );
  } else {
    debug("    root.%s(%s,...)\n", methname, path );
  }

  if (TYPE(arg) == T_ARRAY) {
    methargs = arg;
  } else if (arg != Qnil) {
    methargs = rb_ary_new();
    rb_ary_push(methargs,arg);
  } else {
    methargs = rb_ary_new();
  }

  rb_ary_unshift(methargs,rb_str_new2(path));
  rb_ary_unshift(methargs,ID2SYM(method));

  /* Set up the call and make it. */
  result = rb_protect(rf_protected, methargs, &error);
 
  /* Did it error? */
  if (error) return Qnil;

  return result;
}
/* rf_getattr
 *
 * Used when: 'ls', and before opening a file.
 *
 * FuseFS will call: directory? and file? on FuseRoot
 *   to determine if the path in question is pointing
 *   at a directory or file. The permissions attributes
 *   will be 777 (dirs) and 666 (files) xor'd with FuseFS.umask
 */
static int
rf_getattr(const char *path, struct stat *stbuf) {
  /* If it doesn't exist, it doesn't exist. Simple as that. */
  VALUE retval;
  char *value;
  size_t len;

  debug("rf_getattr(%s)\n", path );
  /* Zero out the stat buffer */
  memset(stbuf, 0, sizeof(struct stat));

  /* "/" is automatically a dir. */
  if (strcmp(path,"/") == 0) {
    stbuf->st_mode = S_IFDIR | 0755;
    stbuf->st_nlink = 3;
    stbuf->st_uid = getuid();
    stbuf->st_gid = getgid();
    stbuf->st_mtime = init_time;
    stbuf->st_atime = init_time;
    stbuf->st_ctime = init_time;
    return 0;
  }

  /* If we created it with mknod, then it "exists" */
  debug("  Checking for created file ...");
  if (created_file && (strcmp(created_file,path) == 0)) {
    /* It's created */
    debug(" created.\n");
    stbuf->st_mode = S_IFREG | 0666;
    stbuf->st_nlink = 1 + file_openedP(path);
    stbuf->st_size = 0;
    stbuf->st_uid = getuid();
    stbuf->st_gid = getgid();
    stbuf->st_mtime = init_time;
    stbuf->st_atime = init_time;
    stbuf->st_ctime = init_time;
    return 0;
  }
  debug(" no.\n");

  /* debug("  Checking file_opened ...");
  if (file_openedP(path)) {
    debug(" opened.\n");
    stbuf->st_mode = S_IFREG | 0666;
    stbuf->st_nlink = 1 + file_openedP(path);
    stbuf->st_size = 0;
    stbuf->st_uid = getuid();
    stbuf->st_gid = getgid();
    stbuf->st_mtime = init_time;
    stbuf->st_atime = init_time;
    stbuf->st_ctime = init_time;
    return 0;
  }
  debug(" no.\n");
  */

  debug("  Checking if editor file...");
  switch (editor_fileP(path)) {
  case 2:
    debug(" Yes, and does exist.\n");
    stbuf->st_mode = S_IFREG | 0444;
    stbuf->st_nlink = 1;
    stbuf->st_size = 0;
    stbuf->st_uid = getuid();
    stbuf->st_gid = getgid();
    stbuf->st_mtime = init_time;
    stbuf->st_atime = init_time;
    stbuf->st_ctime = init_time;
    return 0;
  case 1:
    debug(" Yes, but doesn't exist.\n");
    return -ENOENT;
  default:
    debug("No.\n");
  }

  /* If FuseRoot says the path is a directory, we set it 0555.
   * If FuseRoot says the path is a file, it's 0444.
   *
   * Otherwise, -ENOENT */
  debug("Checking filetype ...");
  if (RTEST(rf_call(path, is_directory,Qnil))) {
    debug(" directory.\n");
    stbuf->st_mode = S_IFDIR | 0555;
    stbuf->st_nlink = 1;
    stbuf->st_size = 4096;
    stbuf->st_uid = getuid();
    stbuf->st_gid = getgid();
    stbuf->st_mtime = init_time;
    stbuf->st_atime = init_time;
    stbuf->st_ctime = init_time;
    return 0;
  } else if (RTEST(rf_call(path, is_file,Qnil))) {
    VALUE rsize;
    debug(" file.\n");
    stbuf->st_mode = S_IFREG | 0444;
    if (RTEST(rf_call(path,can_write,Qnil))) {
      stbuf->st_mode |= 0666;
    }
    if (RTEST(rf_call(path,is_executable,Qnil))) {
      stbuf->st_mode |= 0111;
    }
    stbuf->st_nlink = 1 + file_openedP(path);
    if (RTEST(rsize = rf_call(path,id_size,Qnil)) && FIXNUM_P(rsize)) {
      stbuf->st_size = FIX2LONG(rsize);
    } else {
      stbuf->st_size = 0;
    }
    stbuf->st_uid = getuid();
    stbuf->st_gid = getgid();
    stbuf->st_mtime = init_time;
    stbuf->st_atime = init_time;
    stbuf->st_ctime = init_time;
    return 0;
  }
  debug(" nonexistant.\n");
  return -ENOENT;
}

/* rf_readdir
 *
 * Used when: 'ls'
 *
 * FuseFS will call: 'directory?' on FuseRoot with the given path
 *   as an argument. If the return value is true, then it will in turn
 *   call 'contents' and expects to receive an array of file contents.
 *
 * '.' and '..' are automatically added, so the programmer does not
 *   need to worry about those.
 */
static int
rf_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
           off_t offset, struct fuse_file_info *fi) {
  VALUE contents;
  VALUE cur_entry;
  VALUE retval;

  debug("rf_readdir(%s)\n", path );

  /* This is what fuse does to turn off 'unused' warnings. */
  (void) offset;
  (void) fi;

  /* FuseRoot must exist */
  if (FuseRoot == Qnil) {
    if (!strcmp(path,"/")) {
      filler(buf,".", NULL, 0);
      filler(buf,"..", NULL, 0);
      return 0;
    }
    return -ENOENT;
  }

  if (strcmp(path,"/") != 0) {
    debug("  Checking is_directory? ...");
    retval = rf_call(path, is_directory,Qnil);

    if (!RTEST(retval)) {
      debug(" no.\n");
      return -ENOENT;
    }
    debug(" yes.\n");
  }
 
  /* These two are Always in a directory */
  filler(buf,".", NULL, 0);
  filler(buf,"..", NULL, 0);

  retval = rf_call(path, id_dir_contents,Qnil);
  if (!RTEST(retval)) {
    return 0;
  }
  if (TYPE(retval) != T_ARRAY) {
    return 0;
  }

  /* Duplicate the array, just in case. */
  /* TODO: Do this better! */
  retval = rb_funcall(retval,id_dup,0);

  while ((cur_entry = rb_ary_shift(retval)) != Qnil) {

    if (TYPE(cur_entry) != T_STRING)
      continue;

    filler(buf,StringValuePtr(cur_entry),NULL,0);
  }
  return 0;
}

/* rf_mknod
 *
 * Used when: This is called when a file is created.
 *
 * Note that this is actually almost useless to FuseFS, so all we do is check
 *   if a path is writable? and if so, return true. The open() will do the
 *   actual work of creating the file.
 */
static int
rf_mknod(const char *path, mode_t umode, dev_t rdev) {
  opened_file *ptr;

  debug("rf_mknod(%s)\n", path);
  /* Make sure it's not already open. */
  
  debug("  Checking if it's opened ...");
  if (file_openedP(path)) {
    debug(" yes.\n");
    return -EACCES;
  }
  debug(" no.\n");

  /* We ONLY permit regular files. No blocks, characters, fifos, etc. */
  debug("  Checking if an IFREG is requested ...");
  if (!S_ISREG(umode)) {
    debug(" no.\n");
    return -EACCES;
  }
  debug(" yes.\n");

  debug("  Checking if it's an editor file ...");
  switch (editor_fileP(path)) {
  case 2:
    debug(" yes, and it exists.\n");
    return -EEXIST;
  case 1:
    debug(" yes, and it doesn't exist.\n");
    editor_file *eptr;
    eptr = ALLOC(editor_file);
    eptr->writesize = FILE_GROW_SIZE;
    eptr->value = ALLOC_N(char,eptr->writesize);
    eptr->path  = strdup(path);
    eptr->size  = 0;
    eptr->raw = 0;
    eptr->zero_offset = 0;
    eptr->modified = 0;
    *(eptr->value) = '\0';
    eptr->next = editor_head;
    editor_head = eptr;
    return 0;
  default:
    debug("no.\n");
  }

  debug("  Checking if it's a file ..." );
  if (RTEST(rf_call(path, is_file,Qnil))) {
    debug(" yes.\n");
    return -EEXIST;
  }
  debug(" no.\n");

  /* Is this writable to */
  debug("  Checking if it's writable to ...");
  if (!RTEST(rf_call(path,can_write,Qnil))) {
    debug(" no.\n");
    debug("  Checking if it looks like an editor tempfile...");
    if (editor_head && (which_editor == EDITOR_VIM)) {
      char *ptr = strrchr(path,'/');
      while (ptr && isdigit(*ptr)) ptr++;
      if (ptr && (*ptr == '\0')) {
        debug(" yes.\n");
        editor_file *eptr;
        eptr = ALLOC(editor_file);
        eptr->writesize = FILE_GROW_SIZE;
        eptr->value = ALLOC_N(char,eptr->writesize);
        eptr->path  = strdup(path);
        eptr->raw = 0;
        eptr->size  = 0;
        eptr->zero_offset = 0;
        eptr->modified = 0;
        *(eptr->value) = '\0';
        eptr->next = editor_head;
        editor_head = eptr;
        return 0;
      }
    }
    debug(" no.\n");
    return -EACCES;
  }
  debug(" yes.\n");

  if (created_file)
    free(created_file);

  created_file = strdup(path);

  return 0;
}

/* rf_open
 *
 * Used when: A file is opened for read or write.
 *
 * If called to open a file for reading, then FuseFS will call "read_file" on
 *   FuseRoot, and store the results into the linked list of "opened_file"
 *   structures, so as to provide the same file for mmap, all excutes of
 *   read(), and preventing more than one call to FuseRoot.
 *
 * If called on a file opened for writing, FuseFS will first double check
 *   if the file is writable to by calling "writable?" on FuseRoot, passing
 *   the path. If the return value is a truth value, it will create an entry
 *   into the opened_file list, flagged as for writing.
 *
 * If called with any other set of flags, this will return -ENOPERM, since
 *   FuseFS does not (currently) need to support anything other than direct
 *   read and write.
 */
static int
rf_open(const char *path, struct fuse_file_info *fi) {
  VALUE body;
  char *value;
  size_t len;
  char open_opts[4], *optr;
  opened_file *newfile;

  debug("rf_open(%s)\n", path);

  /* Make sure it's not already open. */
  debug("  Checking if it's already open ...");
  if (file_openedP(path)) {
    debug(" yes.\n");
    return -EACCES;
  }
  debug(" no.\n");
 
  debug("Checking if an editor file is requested...");
  switch (editor_fileP(path)) {
  case 2:
    debug(" yes, and it was created.\n");
    return 0;
  case 1:
    debug(" yes, but it was not created.\n");
    return -ENOENT;
  default:
    debug(" no.\n");
  }

  optr = open_opts;
  switch (fi->flags & 3) {
  case 0:
    *(optr++) = 'r';
    break;
  case 1:
    *(optr++) = 'w';
    break;
  case 2:
    *(optr++) = 'w';
    *(optr++) = 'r';
    break;
  default:
    debug("Opening a file with something other than rd, wr, or rdwr?");
  }
  if (fi->flags & O_APPEND)
    *(optr++) = 'a';
  *(optr) = '\0';

  debug("  Checking for a raw_opened file... ");
  if (RTEST(rf_call(path,id_raw_open,rb_str_new2(open_opts)))) {
    debug(" yes.\n");
    newfile = ALLOC(opened_file);
    newfile->size = 0;
    newfile->value = NULL;
    newfile->writesize = 0;
    newfile->zero_offset = 0;
    newfile->modified = 0;
    newfile->path  = strdup(path);
    newfile->raw = 1;

    newfile->next = opened_head;
    opened_head = newfile;
    return 0;
  }
  debug(" no.\n");

  debug("  Checking open type ...");
  if ((fi->flags & 3) == O_RDONLY) {
    debug(" RDONLY.\n");
    /* Open for read. */
    /* Make sure it exists. */
    if (!RTEST(rf_call(path,is_file,Qnil))) {
      return -ENOENT;
    }

    body = rf_call(path, id_read_file,Qnil);

    /* I don't wanna deal with non-strings :D. */
    if (TYPE(body) != T_STRING) {
      return -ENOENT;
    }

    /* We have the body, now save it the entire contents to our
     * opened_file lists. */
    newfile = ALLOC(opened_file);
    value = RSTRING_PTR(body);
    newfile->size = RSTRING_LEN(body);
    newfile->value = ALLOC_N(char,(newfile->size)+1);
    memcpy(newfile->value,value,newfile->size);
    newfile->value[newfile->size] = '\0';
    newfile->writesize = 0;
    newfile->zero_offset = 0;
    newfile->modified = 0;
    newfile->path  = strdup(path);
    newfile->raw = 0;

    newfile->next = opened_head;
    opened_head = newfile;
    return 0;

  } else if (((fi->flags & 3) == O_RDWR) ||
             (((fi->flags & 3) == O_WRONLY) && (fi->flags & O_APPEND))) {
    /* Can we write to it? */
    debug(" RDWR or Append.\n");
    debug("  Checking if created file ...");
    if (created_file && (strcmp(created_file,path) == 0)) {
      debug(" yes.\n");
      newfile = ALLOC(opened_file);
      newfile->writesize = FILE_GROW_SIZE;
      newfile->value = ALLOC_N(char,newfile->writesize);
      newfile->path  = strdup(path);
      newfile->size  = 0;
      newfile->raw = 0;
      newfile->zero_offset = 0;
      *(newfile->value) = '\0';
      newfile->modified = 0;
      newfile->next = opened_head;
      opened_head = newfile;
      return 0;
    }
    debug(" no\n");

    debug("  Checking if we can write to it...");
    if (!RTEST(rf_call(path,can_write,Qnil))) {
      debug(" yes.\n");
      return -EACCES;
    }
    debug(" no\n");

    /* Make sure it exists. */
    if (RTEST(rf_call(path,is_file,Qnil))) {
      body = rf_call(path, id_read_file,Qnil);

      /* I don't wanna deal with non-strings :D. */
      if (TYPE(body) != T_STRING) {
        return -ENOENT;
      }

      /* We have the body, now save it the entire contents to our
       * opened_file lists. */
      newfile = ALLOC(opened_file);
      value = RSTRING_PTR(body);
      newfile->size = RSTRING_LEN(body);
      newfile->value = ALLOC_N(char,(newfile->size)+1);
      memcpy(newfile->value,value,newfile->size);
      newfile->writesize = newfile->size+1;
      newfile->path  = strdup(path);
      newfile->raw = 0;
      newfile->zero_offset = 0;
    } else {
      newfile = ALLOC(opened_file);
      newfile->writesize = FILE_GROW_SIZE;
      newfile->value = ALLOC_N(char,newfile->writesize);
      newfile->path  = strdup(path);
      newfile->size  = 0;
      newfile->raw = 0;
      newfile->zero_offset = 0;
      *(newfile->value) = '\0';
    }
    newfile->modified = 0;

    if (fi->flags & O_APPEND) {
      newfile->zero_offset = newfile->size;
    }

    newfile->next = opened_head;
    opened_head = newfile;
    return 0;
  } else if ((fi->flags & 3) == O_WRONLY) {
    debug(" WRONLY.\n");
#ifdef DEBUG
    if (fi->flags & O_APPEND)
      debug("    It's opened for O_APPEND\n");
    if (fi->flags & O_ASYNC)
      debug("    It's opened for O_ASYNC\n");
    if (fi->flags & O_CREAT)
      debug("    It's opened for O_CREAT\n");
    if (fi->flags & O_EXCL)
      debug("    It's opened for O_EXCL\n");
    if (fi->flags & O_NOCTTY)
      debug("    It's opened for O_NOCTTY\n");
    if (fi->flags & O_NONBLOCK)
      debug("    It's opened for O_NONBLOCK\n");
    if (fi->flags & O_SYNC)
      debug("    It's opened for O_SYNC\n");
    if (fi->flags & O_TRUNC)
      debug("    It's opened for O_TRUNC\n");
#endif

    /* Open for write. */
    /* Can we write to it? */
    debug("  Checking if we can write to it ... ");
    if (!((created_file && (strcmp(created_file,path) == 0)) ||
        RTEST(rf_call(path,can_write,Qnil)))) {
      debug(" no.\n");
      return -EACCES;
    }
    debug(" yes.\n");

    /* We can write to it. Create an opened_write_file entry and initialize
     * it to a small size. */
    newfile = ALLOC(opened_file);
    newfile->writesize = FILE_GROW_SIZE;
    newfile->value = ALLOC_N(char,newfile->writesize);
    newfile->path  = strdup(path);
    newfile->size  = 0;
    newfile->zero_offset = 0;
    newfile->modified = 0;
    newfile->raw = 0;
    *(newfile->value) = '\0';

    newfile->next = opened_head;
    opened_head = newfile;

    if (created_file && (strcasecmp(created_file,path) == 0)) {
      free(created_file);
      created_file = NULL;
    }
    return 0;
  } else {
    debug(" Unknown...\n");
    return -ENOENT;
  }
}

/* rf_release
 *
 * Used when: A file is no longer being read or written to.
 *
 * If release is called on a written file, FuseFS will call 'write_to' on
 *   FuseRoot, passing the path and contents of the file. It will then
 *   clear the file information from the in-memory file storage that
 *   FuseFS uses to prevent FuseRoot from receiving incomplete files.
 *
 * If called on a file opened for reading, FuseFS will just clear the
 *   in-memory copy of the return value from rf_open.
 */
static int
rf_release(const char *path, struct fuse_file_info *fi) {

  opened_file *ptr,*prev;
  int is_editor = 0;

  debug("rf_release(%s)\n", path);

  debug("  Checking for opened file ...");
  /* Find the opened file. */
  for (ptr = opened_head, prev=NULL;ptr;prev = ptr,ptr = ptr->next)
    if (strcmp(ptr->path,path) == 0) break;

  /* If we don't have this open, it doesn't exist. */
  if (ptr == NULL) {
    debug(" no.\n");
    debug(" Checking for opened editor file ...");
    for (ptr = opened_head, prev=NULL;ptr;prev = ptr,ptr = ptr->next)
      if (strcmp(ptr->path,path) == 0) {
        is_editor = 1;
        break;
      }
  }
  if (ptr == NULL) {
    debug(" no.\n");
    return -ENOENT;
  }
  debug(" yes.\n");

  /* If it's opened for raw read/write, call raw_close */
  debug("  Checking if it's opened for raw write...");
  if (ptr->raw) {
    /* raw read */
    debug(" yes.\n");
    rf_call(path,id_raw_close,Qnil);
  } else {
    debug(" no.\n");

    /* Is this a file that was open for write?
     *
     * If so, call write_to. */
    debug("  Checking if it's for write ...\n");
    if ((!ptr->raw) && (ptr->writesize != 0) && !editor_fileP(path)) {
      debug(" yes ...");
      if (ptr->modified) {
        debug(" and modified.\n");
        rf_call(path,id_write_to,rb_str_new(ptr->value,ptr->size));
      } else {
        debug(" and not modified.\n");
        if (!handle_editor) {
          debug("  ... But calling write anyawy.");
          rf_call(path,id_write_to,rb_str_new(ptr->value,ptr->size));
        }
      }
    }
  }

  /* Free the file contents. */
  if (!is_editor) {
    if (prev == NULL) {
      opened_head = ptr->next;
    } else {
      prev->next = ptr->next;
    }
    if (ptr->value)
      free(ptr->value);
    free(ptr->path);
    free(ptr);
  }

  return 0;
}

/* rf_touch
 *
 * Used when: A program tries to modify the file's times.
 *
 * We use this for a neat side-effect thingy. When a file is touched, we
 * call the "touch" method. i.e: "touch button" would call
 * "FuseRoot.touch('/button')" and something *can* happen. =).
 */
static int
rf_touch(const char *path, struct utimbuf *ignore) {
  debug("rf_touch(%s)\n", path);
  rf_call(path,id_touch,Qnil);
  return 0;
}

/* rf_rename
 *
 * Used when: a file is renamed.
 *
 * When FuseFS receives a rename command, it really just removes the old file
 *   and creates the new file with the same contents.
 */
static int
rf_rename(const char *path, const char *dest) {
  /* Does it exist to be edited? */
  int iseditor = 0;
  if (editor_fileP(path) == 2) {
    iseditor = 1;
  } else {
    debug("rf_rename(%s,%s)\n", path,dest);
    debug("  Checking if %s is file ...", path);
    if (!RTEST(rf_call(path,is_file,Qnil))) {
      debug(" no.\n");
      return -ENOENT;
    }
    debug(" yes.\n");

    /* Can we remove the old one? */
    debug("  Checking if we can delete %s ...", path);
    if (!RTEST(rf_call(path,can_delete,Qnil))) {
      debug(" no.\n");
      return -EACCES;
    }
    debug(" yes.\n");
  }
 
  /* Can we create the new one? */
  debug("  Checking if we can write to %s ...", dest);
  if (!RTEST(rf_call(dest,can_write,Qnil))) {
    debug(" no.\n");
    return -EACCES;
  }
  debug(" yes.\n");

  /* Copy it over and then remove. */
  debug("  Copying.\n");
  if (iseditor) {
    editor_file *eptr,*prev;
    for (eptr=editor_head,prev=NULL;eptr;prev = eptr,eptr = eptr->next) {
      if (strcmp(path,eptr->path) == 0) {
        if (prev == NULL) {
          editor_head = eptr->next;
        } else {
          prev->next = eptr->next;
        }
        VALUE body = rb_str_new(eptr->value,eptr->size);
        rf_call(dest,id_write_to,body);
        free(eptr->value);
        free(eptr->path);
        free(eptr);
        break;
      }
    }
  } else {
    VALUE body = rf_call(path,id_read_file,Qnil);
    if (TYPE(body) != T_STRING) {
      /* We just write a null file, then. Ah well. */
      VALUE newstr = rb_str_new2("");
      rf_call(path,id_delete,Qnil);
      rf_call(dest,id_write_to,newstr);
    } else {
      rf_call(path,id_delete,Qnil);
      rf_call(dest,id_write_to,body);
    }
  }
  return 0;
}

/* rf_unlink
 *
 * Used when: a file is removed.
 *
 * This calls can_remove? and remove() on FuseRoot.
 */
static int
rf_unlink(const char *path) {
  editor_file *eptr,*prev;
  debug("rf_unlink(%s)\n",path);

  debug("  Checking if it's an editor file ...");
  switch (editor_fileP(path)) {
  case 2:
    debug(" yes. Removing.\n");
    for (eptr=editor_head,prev=NULL;eptr;prev = eptr,eptr = eptr->next) {
      if (strcmp(path,eptr->path) == 0) {
        if (prev == NULL) {
          editor_head = eptr->next;
        } else {
          prev->next = eptr->next;
        }
        free(eptr->value);
        free(eptr->path);
        free(eptr);
        return 0;
      }
    }
    return -ENOENT;
  case 1:
    debug(" yes, but it wasn't created.\n");
    return -ENOENT;
  }
  debug(" no.\n");

  /* Does it exist to be removed? */
  debug("  Checking if it exists...");
  if (!RTEST(rf_call(path,is_file,Qnil))) {
    debug(" no.\n");
    return -ENOENT;
  }
  debug(" yes.\n");

  /* Can we remove it? */
  debug("  Checking if we can remove it...");
  if (!RTEST(rf_call(path,can_delete,Qnil))) {
    debug(" yes.\n");
    return -EACCES;
  }
  debug(" no.\n");
 
  /* Ok, remove it! */
  debug("  Removing it.\n");
  rf_call(path,id_delete,Qnil);
  return 0;
}

/* rf_truncate
 *
 * Used when: a file is truncated.
 *
 * If this is an existing file?, that is writable? to, then FuseFS will
 *   read the file, truncate it, and call write_to with the new value.
 */
static int
rf_truncate(const char *path, off_t offset) {
  debug( "rf_truncate(%s,%d)\n", path, offset );

  debug("Checking if it's an editor file ... ");
  if (editor_fileP(path)) {
    debug(" Yes.\n");
    opened_file *ptr;
    for (ptr = opened_head;ptr;ptr = ptr->next) {
      if (!strcmp(ptr->path,path)) {
        ptr->size = offset;
        return 0;
      }
    }
    return 0;
  }

  /* Does it exist to be truncated? */
  if (!RTEST(rf_call(path,is_file,Qnil))) {
    return -ENOENT;
  }

  /* Can we write to it? */
  if (!RTEST(rf_call(path,can_delete,Qnil))) {
    return -EACCES;
  }
 
  /* If offset is 0, then we just overwrite it with an empty file. */
  if (offset > 0) {
    VALUE newstr = rb_str_new2("");
    rf_call(path,id_write_to,newstr);
  } else {
    VALUE body = rf_call(path,id_read_file,Qnil);
    if (TYPE(body) != T_STRING) {
      /* We just write a null file, then. Ah well. */
      VALUE newstr = rb_str_new2("");
      rf_call(path,id_write_to,newstr);
    } else {
      long size;
      char *str = RSTRING_PTR(body);
      size = RSTRING_LEN(body);

      /* Just in case offset is bigger than the file. */
      if (offset >= size) return 0;

      str[offset] = '\0';
      rf_call(path,id_write_to,rb_str_new2(str));
    }
  }
  return 0;
}

/* rf_mkdir
 *
 * Used when: A user calls 'mkdir'
 *
 * This calls can_mkdir? and mkdir() on FuseRoot.
 */
static int
rf_mkdir(const char *path, mode_t mode) {
  debug("rf_mkdir(%s)",path);
  /* Does it exist? */
  if (RTEST(rf_call(path,is_directory,Qnil)))
    return -EEXIST;

  if (RTEST(rf_call(path,is_file,Qnil)))
    return -EEXIST;

  /* Can we mkdir it? */
  if (!RTEST(rf_call(path,can_mkdir,Qnil)))
    return -EACCES;
 
  /* Ok, mkdir it! */
  rf_call(path,id_mkdir,Qnil);
  return 0;
}

/* rf_rmdir
 *
 * Used when: A user calls 'rmdir'
 *
 * This calls can_rmdir? and rmdir() on FuseRoot.
 */
static int
rf_rmdir(const char *path) {
  debug("rf_rmdir(%s)",path);
  /* Does it exist? */
  if (!RTEST(rf_call(path,is_directory,Qnil))) {
    if (RTEST(rf_call(path,is_file,Qnil))) {
      return -ENOTDIR;
    } else {
      return -ENOENT;
    }
  }

  /* Can we rmdir it? */
  if (!RTEST(rf_call(path,can_rmdir,Qnil)))
    return -EACCES;
 
  /* Ok, rmdir it! */
  rf_call(path,id_rmdir,Qnil);
  return 0;
}

/* rf_write
 *
 * Used when: a file is written to by the user.
 *
 * This does not access FuseRoot at all. Instead, it appends the written
 *   data to the opened_file entry, growing its memory usage if necessary.
 */
static int
rf_write(const char *path, const char *buf, size_t size, off_t offset,
         struct fuse_file_info *fi) {
  debug("rf_write(%s)",path);

  opened_file *ptr;

  debug( "  Offset is %d\n", offset );

  debug("  Checking if file is open... ");
  /* Find the opened file. */
  for (ptr = opened_head;ptr;ptr = ptr->next)
    if (strcmp(ptr->path,path) == 0) break;

  /* If we don't have this open, we can't write to it. */
  if (ptr == NULL) {
    for (ptr = editor_head;ptr;ptr = ptr->next)
      if (strcmp(ptr->path,path) == 0) break;
  }

  if (ptr == NULL) {
    debug(" no.\n");
    return 0;
  }
  debug(" yes.\n");

  /* Make sure it's open for write ... */
  /* If it's opened for raw read/write, call raw_write */
  debug("  Checking if it's opened for raw write...");
  if (ptr->raw) {
    /* raw read */
    VALUE args = rb_ary_new();
    debug(" yes.\n");
    rb_ary_push(args,INT2NUM(offset));
    rb_ary_push(args,INT2NUM(size));
    rb_ary_push(args,rb_str_new(buf,size));
    rf_call(path,id_raw_write,args);
    return size;
  }
  debug(" no.\n");
  debug("  Checking if it's open for write ...");
  if (ptr->writesize == 0) {
    debug(" no.\n");
    return 0;
  }
  debug(" yes.\n");

  /* Mark it modified. */
  ptr->modified = 1;

  /* We have it, so now we need to write to it. */
  offset += ptr->zero_offset;

  /* Grow memory if necessary. */
  if ((offset + size + 1) > ptr->writesize) {
    size_t newsize;
    newsize = offset + size + 1 + FILE_GROW_SIZE;
    newsize -= newsize % FILE_GROW_SIZE;
    ptr->writesize = newsize;
    ptr->value = REALLOC_N(ptr->value, char, newsize);
  }

  memcpy(ptr->value + offset, buf, size);

  /* I really don't know if a null bit is required, but this
   * also functions as a size bit I can pass to rb_string_new2
   * to allow binary data */
  if (offset+size > ptr->size)
    ptr->size = offset+size;
  ptr->value[ptr->size] = '\0';

  return size;
}

/* rf_read
 *
 * Used when: A file opened by rf_open is read.
 *
 * In most cases, this does not access FuseRoot at all. It merely reads from
 * the already-read 'file' that is saved in the opened_file list.
 *
 * For files opened with raw_open, it calls raw_read
 */
static int
rf_read(const char *path, char *buf, size_t size, off_t offset,
        struct fuse_file_info *fi) {
  opened_file *ptr;

  debug( "rf_read(%s)\n", path );
  /* Find the opened file. */
  for (ptr = opened_head;ptr;ptr = ptr->next)
    if (strcmp(ptr->path,path) == 0) break;

  /* If we don't have this open, it doesn't exist. */
  if (ptr == NULL)
    return -ENOENT;

  /* If it's opened for raw read/write, call raw_read */
  if (ptr->raw) {
    /* raw read */
    VALUE args = rb_ary_new();
    rb_ary_push(args,INT2NUM(offset));
    rb_ary_push(args,INT2NUM(size));
    VALUE ret = rf_call(path,id_raw_read,args);
    if (!RTEST(ret))
      return 0;
    if (TYPE(ret) != T_STRING)
      return 0;
    memcpy(buf, RSTRING_PTR(ret), RSTRING_LEN(ret));
    return RSTRING_LEN(ret);
  }

  /* Is there anything left to read? */
  if (offset < ptr->size) {
    if (offset + size > ptr->size)
      size = ptr->size - offset;
    memcpy(buf, ptr->value + offset, size);
    return size;
  }

  return 0;
}

static int
rf_fsyncdir(const char * path, int p, struct fuse_file_info *fi)
{
  return 0;
}

static int 
rf_utime(const char *path, struct utimbuf *ubuf)
{
  return 0;
}

static int
rf_statfs(const char *path, struct statvfs *buf)
{
  return 0;
}

/* rf_oper
 *
 * Used for: FUSE utilizes this to call operations at the appropriate time.
 *
 * This is utilized by rf_mount
 */
static struct fuse_operations rf_oper = {
    .getattr   = rf_getattr,
    .readdir   = rf_readdir,
    .mknod     = rf_mknod,
    .unlink    = rf_unlink,
    .mkdir     = rf_mkdir,
    .rmdir     = rf_rmdir,
    .truncate  = rf_truncate,
    .rename    = rf_rename,
    .open      = rf_open,
    .release   = rf_release,
    .utime     = rf_touch,
    .read      = rf_read,
    .write     = rf_write,
    .fsyncdir  = rf_fsyncdir,
    .utime     = rf_utime,
    .statfs    = rf_statfs,
};

/* rf_set_root
 *
 * Used by: FuseFS.set_root
 *
 * This defines FuseRoot, which is the crux of FuseFS. It is required to
 *   have the methods "directory?" "file?" "contents" "writable?" "read_file"
 *   and "write_to"
 */
VALUE
rf_set_root(VALUE self, VALUE rootval) {
  if (self != cFuseFS) {
    rb_raise(cFSException,"Error: 'set_root' called outside of FuseFS?!");
    return Qnil;
  }

  rb_iv_set(cFuseFS,"@root",rootval);
  FuseRoot = rootval;
  return Qtrue;
}

/* rf_handle_editor
 *
 * Used by: FuseFS.handle_editor <value>
 *
 * If passed a false value, then FuseFS will not attempt to handle editor
 * swap files on its own, instead passing them to the filesystem as
 * normal files.
 */
VALUE
rf_handle_editor(VALUE self, VALUE troo) {
  if (self != cFuseFS) {
    rb_raise(cFSException,"Error: 'set_root' called outside of FuseFS?!");
    return Qnil;
  }

  handle_editor = RTEST(troo);
  return Qtrue;
}

/* rf_mount_to
 *
 * Used by: FuseFS.mount_to(dir)
 *
 * FuseFS.mount_to(dir) calls FUSE to mount FuseFS under the given directory.
 */
VALUE
rf_mount_to(int argc, VALUE *argv, VALUE self) {
  struct fuse_args *opts;
  VALUE mountpoint;
  int i;
  char *cur;

  if (self != cFuseFS) {
    rb_raise(cFSException,"Error: 'mount_to' called outside of FuseFS?!");
    return Qnil;
  }

  if (argc == 0) {
    rb_raise(rb_eArgError,"mount_to requires at least 1 argument!");
    return Qnil;
  }

  mountpoint = argv[0];

  Check_Type(mountpoint, T_STRING);
  opts = ALLOC(struct fuse_args);
  opts->argc = argc;
  opts->argv = ALLOC_N(char *, opts->argc);
  opts->allocated = 1;
  
  opts->argv[0] = strdup("-odirect_io");

  for (i = 1; i < argc; i++) {
    cur = StringValuePtr(argv[i]);
    opts->argv[i] = ALLOC_N(char, RSTRING_LEN(argv[i]) + 2);
    sprintf(opts->argv[i], "-o%s", cur);
  }

  rb_iv_set(cFuseFS,"@mountpoint",mountpoint);
  fusefs_setup(StringValuePtr(mountpoint), &rf_oper, opts);
  return Qtrue;
}

/* rf_fd
 *
 * Used by: FuseFS.fuse_fd(dir)
 *
 * FuseFS.fuse_fd returns the file descriptor of the open handle on the
 *   /dev/fuse object that is utilized by FUSE. This is crucial for letting
 *   ruby keep control of the script, as it can now use IO.select, rather
 *   than turning control over to fuse_main.
 */
VALUE
rf_fd(VALUE self) {
  int fd = fusefs_fd();
  if (fd < 0)
    return Qnil;
  return INT2NUM(fd);
}

/* rf_process
 *
 * Used for: FuseFS.process
 *
 * rf_process, which calls fusefs_process, is the other crucial portion to
 *   keeping ruby in control of the script. fusefs_process will read and
 *   process exactly one command from the fuse_fd. If this is called when
 *   there is no incoming data waiting, it *will* hang until it receives a
 *   command on the fuse_fd
 */
VALUE
rf_process(VALUE self) {
  fusefs_process();
}


/* rf_uid and rf_gid
 *
 * Used by: FuseFS.reader_uid and FuseFS.reader_gid
 *
 * These return the UID and GID of the processes that are causing the
 *   separate Fuse methods to be called. This can be used for permissions
 *   checking, returning a different file for different users, etc.
 */
VALUE
rf_uid(VALUE self) {
  int fd = fusefs_uid();
  if (fd < 0)
    return Qnil;
  return INT2NUM(fd);
}

VALUE
rf_gid(VALUE self) {
  int fd = fusefs_gid();
  if (fd < 0)
    return Qnil;
  return INT2NUM(fd);
}

/* Init_fusefs_lib()
 *
 * Used by: Ruby, to initialize FuseFS.
 *
 * This is just stuff to set up and establish the Ruby module FuseFS and
 *   its methods.
 */
void
Init_fusefs_lib() {
  opened_head = NULL;
  init_time = time(NULL);

  /* module FuseFS */
  cFuseFS = rb_define_module("FuseFS");

  /* Our exception */
  cFSException = rb_define_class_under(cFuseFS,"FuseFSException",rb_eStandardError);

  /* def Fuse.run */
  rb_define_singleton_method(cFuseFS,"fuse_fd",     (rbfunc) rf_fd, 0);
  rb_define_singleton_method(cFuseFS,"reader_uid",  (rbfunc) rf_uid, 0);
  rb_define_singleton_method(cFuseFS,"uid",         (rbfunc) rf_uid, 0);
  rb_define_singleton_method(cFuseFS,"reader_gid",  (rbfunc) rf_gid, 0);
  rb_define_singleton_method(cFuseFS,"gid",         (rbfunc) rf_gid, 0);
  rb_define_singleton_method(cFuseFS,"process",     (rbfunc) rf_process, 0);
  rb_define_singleton_method(cFuseFS,"mount_to",    (rbfunc) rf_mount_to, -1);
  rb_define_singleton_method(cFuseFS,"mount_under", (rbfunc) rf_mount_to, -1);
  rb_define_singleton_method(cFuseFS,"mountpoint",  (rbfunc) rf_mount_to, -1);
  rb_define_singleton_method(cFuseFS,"set_root",    (rbfunc) rf_set_root, 1);
  rb_define_singleton_method(cFuseFS,"root=",       (rbfunc) rf_set_root, 1);
  rb_define_singleton_method(cFuseFS,"handle_editor",   (rbfunc) rf_handle_editor, 1);
  rb_define_singleton_method(cFuseFS,"handle_editor=",  (rbfunc) rf_handle_editor, 1);

#undef RMETHOD
#define RMETHOD(name,cstr) \
  name = rb_intern(cstr);

  RMETHOD(id_dir_contents,"contents");
  RMETHOD(id_read_file,"read_file");
  RMETHOD(id_write_to,"write_to");
  RMETHOD(id_delete,"delete");
  RMETHOD(id_mkdir,"mkdir");
  RMETHOD(id_rmdir,"rmdir");
  RMETHOD(id_touch,"touch");
  RMETHOD(id_size,"size");

  RMETHOD(is_directory,"directory?");
  RMETHOD(is_file,"file?");
  RMETHOD(is_executable,"executable?");
  RMETHOD(can_write,"can_write?");
  RMETHOD(can_delete,"can_delete?");
  RMETHOD(can_mkdir,"can_mkdir?");
  RMETHOD(can_rmdir,"can_rmdir?");

  RMETHOD(id_raw_open,"raw_open");
  RMETHOD(id_raw_close,"raw_close");
  RMETHOD(id_raw_read,"raw_read");
  RMETHOD(id_raw_write,"raw_write");

  RMETHOD(id_dup,"dup");
}