forked from ehmry/nim-fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuse.nim
1563 lines (1332 loc) · 43.6 KB
/
fuse.nim
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
#
# A FUSE binding for Nim
# (c) Copyright 2015 Akira Hayakawa
#
import os
import posix
import logging
import strutils
# ------------------------------------------------------------------------------
# Darwin doesn't allow version < 25
# It's recommended to set FUSE_USE_VERSION to 26 that's somehow 21 by default.
{.passC: "-DFUSE_USE_VERSION=26".}
{.passC: gorge("pkg-config --cflags fuse").}
{.passL: gorge("pkg-config --libs fuse").}
type
OptionKind = enum
kSome
kNone
Option*[T] = object
case kind: OptionKind
of kSome: v: T
of kNone: nil
proc `$`*[T](o: Option[T]): string =
case o.kind
of kSome:
"Some " & $o.v
of kNone:
"None"
proc Some[T](v: T): Option[T] =
Option[T](kind: kSome, v: v)
proc None[T](): Option[T] =
Option[T](kind: kNone)
proc isSome*[T](o: Option[T]): bool =
o.kind == kSome
proc isNone*[T](o: Option[T]): bool =
o.kind == kNone
proc unwrap*[T](o: Option[T]): T =
o.v
# ------------------------------------------------------------------------------
type Buf* = ref object
data: seq[char]
size*: int
pos*: int
proc mkBuf*(size: int): Buf =
## Make a buf object of `size` bytes
var data = newSeq[char](size)
Buf(
data: data,
size: size,
pos: 0,
)
proc extend*(self: Buf, size: int) =
if (self.size >= size):
return
var newData = newSeq[char](size)
copyMem(addr(newData[0]), addr(self.data[0]), self.size)
proc asPtr*(self: Buf, at: int): pointer =
addr(self.data[at])
proc asPtr*(self: Buf): pointer =
## Get the current pos as the pointer
addr(self.data[self.pos])
proc asBuf*(self: Buf): Buf =
## Get the [pos,] buffer like slicing
Buf(
data: self.data[self.pos..self.size-1],
size: self.size - self.pos,
pos: 0,
)
proc `$`(self: IOVec): string =
"IOVec(base:$1 len:$2)" % [$cast[ByteAddress](self.iov_base), $self.iov_len]
proc asIOVec*(self: Buf): IOVec =
IOVec(
iov_base: self.asPtr,
iov_len: (csize_t)self.size,
)
proc mkIOVecT*[T](o: var T): IOVec =
IOVec(
iov_base: addr(o),
iov_len: (csize_t)sizeof(T),
)
proc mkIOVecS*(s: var string): IOVec =
IOVec(
iov_base: addr(s[0]),
iov_len: (csize_t)len(s),
)
proc write*(self: Buf, p: pointer, size: int) =
copyMem(self.asPtr, p, size)
proc write*[T](self: Buf, obj: T) =
let sz = sizeof(T)
var v = obj
self.write(addr(v), sizeof(T))
proc mkBufT[T](o: T): Buf {.deprecated.} =
result = mkBuf(sizeof(T))
result.write(o)
proc nullTerminated*(s: string): string =
## Returns null terminated string of `s`
## The length is incremented
## e.g. mybuf.writeS("hoge".nullTerminated)
result = s
result.add(chr(0))
proc writeS*(self: Buf, s: string) =
## Write string `s` (Only the contents. Exclude null terminator)
var vs = s
self.write(addr(vs[0]), len(s))
proc parseS*(self: Buf): string =
## Parse a null-terminated string in the buffer
$cstring(addr(self.data[0]))
proc mkBufS(s: string): Buf {.deprecated.} =
## Make a buffer from a string `s`
result = mkBuf(len(s))
result.writeS(s)
proc read*[T](self: Buf): T =
## Read a value of type T from the buffer
cast[ptr T](self.asPtr)[]
proc pop*[T](self: Buf): T =
## Read and advance the position
result = read[T](self)
self.pos += sizeof(T)
# ------------------------------------------------------------------------------
let
FUSE_KERNEL_VERSION* = 7'i32
FUSE_KERNEL_MINOR_VERSION* = 8'i32
FUSE_ROOT_ID* = 1
type fuse_attr = object
ino: int64
size: int64
blocks: int64
atime: int64
mtime: int64
ctime: int64
when hostOS == "macosx":
crtime: int64
atimensec: int32
mtimensec: int32
ctimensec: int32
when hostOS == "macosx":
crtimensec: int32
mode: int32
nlink: int32
uid: int32
gid: int32
rdev: int32
when hostOS == "macosx":
flags: int32
type fuse_kstatfs* = object
blocks*: int64
bfree*: int64
bavail*: int64
files*: int64
ffree*: int64
bsize*: int32
namelen*: int32
frsize*: int32
padding: int32
spare: array[6, int32]
type fuse_file_lock* = object
start*: int64
theEnd*: int64
theType*: int32
pid*: int32
let
# Bitmasks for fuse_setattr_in.valid
FATTR_MODE = 1 shl 0
FATTR_UID = 1 shl 1
FATTR_GID = 1 shl 2
FATTR_SIZE = 1 shl 3
FATTR_ATIME = 1 shl 4
FATTR_MTIME = 1 shl 5
FATTR_FH = 1 shl 6
when hostOS == "macosx":
let
FATTR_CRTIME = 1 shl 28
FATTR_CHGTIME = 1 shl 29
FATTR_BKUPTIME = 1 shl 30
FATTR_FLAGS = 1 shl 31
let
# Flags returned by the OPEN request
FOPEN_DIRECT_IO* = 1 shl 0
FOPEN_KEEP_CACHE* = 1 shl 1
when hostOS == "macosx":
let
FOPEN_PURGE_ATTR* = 1 shl 30
FOPEN_PURGE_UBC* = 1 shl 31
let
# INIT request/reply flags
FUSE_ASYNC_READ* = 1 shl 0
FUSE_POSIX_LOCKS* = 1 shl 1
when hostOS == "macosx":
let
FUSE_CASE_INSENSITIVE* = 1 shl 29
FUSE_VOL_RENAME* = 1 shl 30
FUSE_XTIMES* = 1 shl 31
let
# Release flags
FUSE_RELEASE_FLUSH = 1 shl 0
type fuse_opcode = enum
FUSE_LOOKUP = 1
FUSE_FORGET = 2
FUSE_GETATTR = 3
FUSE_SETATTR = 4
FUSE_READLINK = 5
FUSE_SYMLINK = 6
FUSE_MKNOD = 8
FUSE_MKDIR = 9
FUSE_UNLINK = 10
FUSE_RMDIR = 11
FUSE_RENAME = 12
FUSE_LINK = 13
FUSE_OPEN = 14
FUSE_READ = 15
FUSE_WRITE = 16
FUSE_STATFS = 17
FUSE_RELEASE = 18
FUSE_FSYNC = 20
FUSE_SETXATTR = 21
FUSE_GETXATTR = 22
FUSE_LISTXATTR = 23
FUSE_REMOVEXATTR = 24
FUSE_FLUSH = 25
FUSE_INIT = 26
FUSE_OPENDIR = 27
FUSE_READDIR = 28
FUSE_RELEASEDIR = 29
FUSE_FSYNCDIR = 30
FUSE_GETLK = 31
FUSE_SETLK = 32
FUSE_SETLKW = 33
FUSE_ACCESS = 34
FUSE_CREATE = 35
FUSE_INTERRUPT = 36
FUSE_BMAP = 37
FUSE_DESTROY = 38
FUSE_SETVOLNAME = 61 # macosx
FUSE_GETXTIMES = 62 # macosx
FUSE_EXCHANGE = 63 # macosx
let
FUSE_MIN_READ_BUFFER = 8192
type fuse_entry_out = object
nodeid: int64
generation: int64
entry_valid: int64
attr_valid: int64
entry_valid_nsec: int32
attr_valid_nsec: int32
attr: fuse_attr
type fuse_forget_in = object
nlookup: int64
type fuse_attr_out = object
attr_valid: int64
attr_valid_nsec: int32
dummy: int32
attr: fuse_attr
when hostOS == "macosx":
type fuse_getxtimes_out = object
bkuptime: int64
crtime: int64
bkuptimensec: int32
crtimensec: int32
type fuse_mknod_in = object
mode: int32
rdev: int32
type fuse_mkdir_in = object
mode: int32
padding: int32
type fuse_rename_in = object
newdir: int64
when hostOS == "macosx":
type fuse_exchange_in = object
olddir: int64
newdir: int64
options: int64
type fuse_link_in = object
oldnodeid: int64
type fuse_setattr_in = object
valid: int32
padding: int32
fh: int64
size: int64
unused1: int64
atime: int64
mtime: int64
unused2: int64
atimensec: int32
mtimensec: int32
unused3: int32
mode: int32
unused4: int32
uid: int32
gid: int32
unused5: int32
when hostOS == "macosx":
bkuptime: int64
chgtime: int64
crtime: int64
bkuptimensec: int32
chgtimensec: int32
crtimensec: int32
flags: int32
type fuse_open_in = object
flags: int32
mode: int32
type fuse_open_out* = object
fh*: int64
open_flags*: int32
padding: int32
type fuse_release_in = object
fh: int64
flags: int32
release_flags: int32
lock_owner: int64
type fuse_flush_in = object
fh: int64
unused: int32
padding: int32
lock_owner: int64
type fuse_read_in = object
fh: int64
offset: int64
size: int32
padding: int32
type fuse_write_in = object
fh: int64
offset: int64
size: int32
write_flags: int32
type fuse_write_out* = object
size*: int32
padding: int32
type fuse_statfs_out = object
st: fuse_kstatfs
type fuse_fsync_in = object
fh: int64
fsync_flags: int32
padding: int32
type fuse_setxattr_in = object
size: int32
flags: int32
when hostOS == "macosx":
position: int32
padding: int32
type fuse_getxattr_in = object
size: int32
padding: int32
when hostOS == "macosx":
position: int32
padding2: int32
type fuse_getxattr_out* = object
size*: int32 ## request of in-kernel buffer size (byte)
padding: int32
type fuse_lk_in = object
fh: int64
owner: int64
lk: fuse_file_lock
type fuse_lk_out = object
lk: fuse_file_lock
type fuse_access_in = object
mask: int32
padding: int32
type fuse_init_in = object
major: int32
minor: int32
max_readahead: int32
flags: int32
type fuse_init_out = object
major: int32
minor: int32
max_readahead: int32
flags: int32
unused: int32
max_write: int32
type fuse_interrupt_in = object
unique: int64
type fuse_bmap_in = object
theBlock: int64
blocksize: int32
padding: int32
type fuse_bmap_out* = object
theBlock*: int64
type fuse_in_header* = object
len*: int32
opcode*: int32
unique*: int64
nodeid*: int64
uid*: int32
gid*: int32
pid*: int32
padding: int32
type fuse_out_header = object
len: int32
error: int32
unique: int64
type fuse_dirent = object
ino: int64
off: int64
namelen: int32
theType: int32
# ------------------------------------------------------------------------------
type Timespec* = object
sec*: int64
nsec*: int32
type FileAttr* = ref object
ino*: int64 # Tino?
size*: int64 # int? Tblksize?
blocks*: int64 # Tblkcnt?
atime*: Timespec
mtime*: Timespec
ctime*: Timespec
crtime*: Timespec # macosx
mode*: int32
nlink*: int32 ## number of hard links. TNlink?
uid*: int32 # T
gid*: int32 # TGid?
rdev*: int32 # TDev?
flags*: int32 # macosx
when hostOS == "macosx":
proc fuse_attr_of(at: FileAttr): fuse_attr =
result = fuse_attr (
ino: at.ino,
size: at.size,
blocks: at.blocks,
atime: at.atime.sec,
mtime: at.mtime.sec,
ctime: at.ctime.sec,
crtime: at.crtime.sec,
atimensec: at.atime.nsec,
mtimensec: at.mtime.nsec,
ctimensec: at.ctime.nsec,
crtimensec: at.crtime.nsec,
nlink: at.nlink,
uid: at.uid,
gid: at.gid,
rdev: at.rdev,
flags: at.flags,
)
debug("attr:$1", repr(result))
else:
proc fuse_attr_of(at: FileAttr): fuse_attr =
result = fuse_attr(
ino: at.ino,
size: at.size,
blocks: at.blocks,
atime: at.atime.sec,
mtime: at.mtime.sec,
ctime: at.ctime.sec,
atimensec: at.atime.nsec,
mtimensec: at.mtime.nsec,
ctimensec: at.ctime.nsec,
mode: at.mode,
nlink: at.nlink,
uid: at.uid,
gid: at.gid,
rdev: at.rdev,
)
debug("attr:$1", repr(result))
type Sender = ref object of RootObj
method send(self: Sender, iovs: var openArray[IOVec]): int =
debug("NULLSender.send")
0
type Raw = ref object
sender: Sender
unique: int64
proc newRaw(sender: Sender, unique: int64): Raw =
Raw(sender: sender, unique: unique)
proc send(self: Raw, err: int, iovs: openArray[IOVec]) =
assert(err <= 0)
var iovL = newSeq[IOVec](len(iovs) + 1)
var sumLen = sizeof(fuse_out_header)
for i, iov in iovs:
iovL[i+1] = iov
debug("iov[$1]:$2", i, iov)
sumLen += iov.iov_len.int
var outH: fuse_out_header
outH.unique = self.unique
outH.error = err.int32
outH.len = sumLen.int32
debug("COMMON OUT:$1", repr(outH))
iovL[0] = mkIOVecT(outH)
discard self.sender.send(iovL)
proc ok(self: Raw, iovs: openArray[IOVec]) =
self.send(0, iovs)
proc err(self: Raw, e: int) =
self.send(e, @[])
template defWrapper(typ: untyped) =
type `typ`* {. inject .} = ref object
raw: Raw
proc sendOk[T](self: `typ`, a: T) =
var aa = a
self.raw.ok(@[mkIOVecT(aa)])
template defOk(typ: typedesc) =
proc ok*(self: typ, iovs: openArray[IOVec]) =
self.raw.ok(iovs)
template defErr(typ: typedesc) =
proc err*(self: `typ`, e: int) =
assert(e <= 0)
self.raw.err(e)
type TEntryOut* = ref object
generation*: int64 ## (`ino`, `generation`) should be unique for the filesystem's lifetime.
entry_timeout*: Timespec ## Validity timeout for the name.
attr_timeout*: Timespec ## Validity timeout for the attributes.
attr*: FileAttr
proc fuse_entry_out_of(eout: TEntryOut): fuse_entry_out =
fuse_entry_out(
nodeid: eout.attr.ino,
generation: eout.generation,
entry_valid: eout.entry_timeout.sec,
entry_valid_nsec: eout.entry_timeout.nsec,
attr_valid: eout.attr_timeout.sec,
attr_valid_nsec: eout.attr_timeout.nsec,
attr: fuse_attr_of(eout.attr)
)
template defEntry(typ: typedesc) =
proc entry(self: `typ`, hd: fuse_entry_out) =
self.sendOk(hd)
proc entry*(self: typ, eout: TEntryOut) =
self.entry(fuse_entry_out_of(eout))
type fuse_create_out = object
hd0: fuse_entry_out
hd1: fuse_open_out
template defCreate(typ: typedesc) =
proc create(self: typ, hd0: fuse_entry_out, hd1: fuse_open_out) =
let hd = fuse_create_out(hd0: hd0, hd1:hd1)
# TODO self.raw.ok(@[hd0, hd1])?
self.sendOk(hd)
proc create*(self: typ, eout: TEntryOut, oout: fuse_open_out) =
self.create(fuse_entry_out_of(eout), oout)
template defAttr(typ: typedesc) =
proc attr(self: `typ`, hd: fuse_attr_out) =
self.sendOk(hd)
proc attr*(self: typ, timeout: Timespec, at: FileAttr) =
self.attr(
fuse_attr_out(
attr_valid: timeout.sec,
attr_valid_nsec: timeout.nsec,
attr: fuse_attr_of(at)))
template defReadlink(typ: typedesc) =
proc readlink*(self: typ, s: string) =
var ss = s
self.raw.ok(@[mkIOVecS(ss)])
template defOpen(typ: typedesc) =
proc open*(self: `typ`, hd: fuse_open_out) =
self.sendOk(hd)
template defWrite(typ: typedesc) =
proc write*(self: `typ`, hd: fuse_write_out) =
self.sendOk(hd)
template defBuf(typ: typedesc) =
proc buf*(self: `typ`, iov: IOVec) =
self.raw.ok(@[iov])
template defIov(typ: typedesc) =
proc iov*(self: typ, iovs: openArray[IOVec]) =
self.raw.ok(iovs)
template defStatfs(typ: typedesc) =
proc statfs(self: typ, hd: fuse_statfs_out) =
self.sendOk(hd)
proc statfs*(self: typ, hd: fuse_kstatfs) =
self.statfs(fuse_statfs_out(st:hd))
template defXAttr(typ: typedesc) =
proc xattr*(self: `typ`, hd: fuse_getxattr_out) =
self.sendOk(hd)
template defLock(typ: typedesc) =
proc lock(self: typ, hd: fuse_lk_out) =
self.sendOk(hd)
proc lock*(self: typ, hd: fuse_file_lock) =
lock(self, fuse_lk_out(lk: hd))
template defBmap(typ: typedesc) =
proc bmap*(self: typ, hd: fuse_bmap_out) =
self.sendOk(hd)
defWrapper(Any)
defOk(Any)
defErr(Any)
defWrapper(Lookup)
defEntry(Lookup)
defErr(Lookup)
defWrapper(Forget)
defWrapper(GetAttr)
defAttr(GetAttr)
defErr(GetAttr)
defWrapper(SetAttr)
defAttr(SetAttr)
defErr(SetAttr)
defWrapper(Readlink)
defReadlink(Readlink)
defErr(Readlink)
defWrapper(Mknod)
defEntry(Mknod)
defErr(Mknod)
defWrapper(Mkdir)
defEntry(Mkdir)
defErr(Mkdir)
defWrapper(Unlink)
defErr(Unlink)
defWrapper(Rmdir)
defErr(Rmdir)
defWrapper(Symlink)
defEntry(Symlink)
defErr(Symlink)
defWrapper(Rename)
defErr(Rename)
defWrapper(Link)
defEntry(Link)
defErr(Link)
defWrapper(Open)
defOpen(Open)
defErr(Open)
defWrapper(Read)
defBuf(Read)
defIov(Read)
defErr(Read)
defWrapper(Write)
defWrite(Write)
defErr(Write)
defWrapper(Flush)
defErr(Flush)
defWrapper(Release)
defErr(Release)
defWrapper(Fsync)
defErr(Fsync)
defWrapper(Opendir)
defOpen(Opendir)
defErr(Opendir)
type Readdir* = ref object
raw: Raw
data: Buf
proc tryAdd*(self: Readdir, ino: int64, off: int64, st_mode: int32, name: string): bool =
## Try to add the entry
## If the buffer is too small for the entry then it returns false
proc align(x:int): int =
let sz = sizeof(int64)
(x + sz - 1) and not(sz - 1)
let namelen = len(name)
let entlen = sizeof(fuse_dirent) + namelen
let entsize = align(entlen)
if self.data.pos + entsize > self.data.size:
return false
let pos0 = self.data.pos
let hd = fuse_dirent(
ino: ino,
off: off,
namelen: namelen.int32,
theType: (st_mode and 0170000) shr 12
)
write[fuse_dirent](self.data, hd)
self.data.pos += sizeof(fuse_dirent)
let pos1 = self.data.pos
self.data.writeS(name)
self.data.pos += len(name)
let pos2 = self.data.pos
let padlen = entsize - entlen
if padlen > 0:
zeroMem(self.data.asPtr(), padlen.int)
self.data.pos += padlen
let pos3 = self.data.pos
debug("try add dirent. name:$1 entlen:$2 entsize:$3 pos:$4->$5->$6->$7", name, entlen, entsize, pos0, pos1, pos2, pos3)
return true
proc ok*(self: Readdir) =
## Ack by the current buffer contents
## If nothing is in the buffer it notifies the end of the stream.
self.data.size = self.data.pos
self.data.pos = 0
self.raw.ok(@[self.data.asIOVec])
defErr(Readdir)
defWrapper(Releasedir)
defErr(Releasedir)
defWrapper(Fsyncdir)
defErr(Fsyncdir)
defWrapper(Statfs)
defStatfs(Statfs)
defErr(Statfs)
defWrapper(SetXAttr)
defErr(SetXAttr)
defWrapper(GetXAttr)
defXAttr(GetXAttr)
defErr(GetXAttr)
type GetXAttrData = ref object
raw: Raw
size: uint
proc ok*(self: GetXAttrData, data: IOVec) =
if self.size < data.iov_len:
self.raw.err(-ERANGE)
return
self.raw.ok(@[data])
defErr(GetXAttrData)
defWrapper(ListXAttr)
defXAttr(ListXAttr)
defErr(ListXAttr)
type ListXAttrData = ref object
raw: Raw
size: uint
proc ok*(self: ListXAttrData, keys: openArray[string]) =
var ss = newSeq[string](len(keys))
var size: uint
for i, k in keys:
ss[i] = k.nullTerminated
size += (uint)len(ss[i])
if self.size < size:
self.raw.err(-ERANGE)
return
var iovs = newSeq[IOVec](len(ss))
for i, s in ss:
iovs[i] = ss[i].mkIOVecS
self.raw.ok(iovs)
defErr(ListXAttrData)
defWrapper(RemoveXAttr)
defErr(RemoveXAttr)
defWrapper(Access)
defErr(Access)
defWrapper(Create)
defCreate(Create)
defErr(Create)
defWrapper(Getlk)
defLock(Getlk)
defErr(Getlk)
defWrapper(Setlk)
defErr(Setlk)
defWrapper(Bmap)
defBmap(Bmap)
defErr(Bmap)
when hostOS == "macosx":
defWrapper(SetVolname)
defErr(SetVolname)
defWrapper(Exchange)
defErr(Exchange)
defWrapper(GetXTimes)
discard """
ERROR: proc getxtimes() does not compile on OSX!
fuse.nim(911, 12) Error: type mismatch: got (Raw, fuse_getxtimes_out)
but expected one of:
fuse.ok(self: Raw, iovs: openarray[IOVec])
fuse.ok(self: Any, iovs: openarray[IOVec])
fuse.ok(self: Readdir)
fuse.ok(self: GetXAttrData, data: IOVec)
fuse.ok(self: ListXAttrData, keys: openarray[string])
"""
proc getxtimes(self: GetXTimes, bkuptime: Timespec, crtime: Timespec) =
self.sendOk(fuse_get_xtimes_out (
bkuptime: bkuptime.sec,
crtime: crtime.sec,
bkuptimensec: bkuptime.nsec,
crtimensec: crtime.nsec,
))
defErr(GetXTimes)
# ------------------------------------------------------------------------------
type fuse_args {. importc:"struct fuse_args", header:"<fuse.h>" .} = object
argc: cint
argv: cstringArray
allocated: cint
proc fuse_mount_compat25(mountpoint: cstring, args: ptr fuse_args): cint {. importc, header:"<fuse.h>" .}
proc fuse_unmount_compat22(mountpoint: cstring) {. importc, header: "<fuse.h>" .}
type Channel = ref object
mount_point: string
fd: cint
proc connect(mount_point: string, mount_options: openArray[string]): Channel =
var args = fuse_args(
argc: mount_options.len.cint,
argv: allocCStringArray(mount_options),
allocated: 0, # control freeing by ourselves
)
let fd = fuse_mount_compat25(mount_point, addr(args))
deallocCStringArray(args.argv)
Channel(mount_point:mount_point, fd:fd)
proc disconnect(chan: Channel) =
# FIXME only linux
fuse_unmount_compat22(chan.mount_point)
proc fetch(chan: Channel, buf: Buf): int =
assert(buf.pos == 0)
debug("---------- START FETHCING ----------")
let n = posix.read(chan.fd, buf.asPtr, buf.size)
if n > 0:
buf.size = n # drop remaining buffer
result = 0
else:
result = osLastError().int
debug("fetch result. fd:$1 err:$2", chan.fd, result)
type ChannelSender = ref object of Sender
chan: Channel
method send(self: ChannelSender, iovs: var openArray[IOVec]): int =
let n = iovs.len.cint
var sumLen: uint
for iov in iovs:
sumLen += iov.iov_len
let bytes = posix.writev(self.chan.fd, addr(iovs[0]), n)
if bytes.uint != sumLen:
debug("send NG. actual:$1(byte) expected:$2 error:$3 msg:$4", bytes, sumLen, osLastError(), osErrorMsg(osLastError()))
result = -posix.EIO
else:
debug("send OK")
result = 0
proc mkSender(self: Channel): ChannelSender =
ChannelSender(chan: self)
# ------------------------------------------------------------------------------
type Request* = ref object
header*: fuse_in_header
data: Buf
type FuseFs* = ref object of RootObj
## Base class for FUSE filesystem
## User needs to implement a subclass
## These methods corrospond to fuse_lowlevel_ops in libfuse. Reasonable default
## implementations are provided here to get a mountable filesystem that does
## nothing.
method init*(self: FuseFs, req: Request): int =
## Initialize filesystem
## Called before any other filesystem method.
0
method destroy*(self: FuseFs, req: Request) =
## Clean up filesystem
## Called on filesystem exit.
discard
method lookup*(self: FuseFs, req: Request, parent: int64, name: string, reply: Lookup) =
## Look up a directory entry by name and get its attributes.
reply.err(-ENOSYS)
method forget*(self: FuseFs, req: Request, ino: int64, nlookup: int64) =
## Forget about an inode
## The nlookup parameter indicates the number of lookups previously performed on
## this inode. If the filesystem implements inode lifetimes, it is recommended that
## inodes acquire a single reference on each lookup, and lose nlookup references on
## each forget. The filesystem may ignore forget calls, if the inodes don't need to
## have a limited lifetime. On unmount it is not guaranteed, that all referenced
## inodes will receive a forget message.
discard
method getattr*(self: FuseFs, req: Request, ino: int64, reply: GetAttr) =
## Get file attributes
reply.err(-ENOSYS)