Skip to content

Instantly share code, notes, and snippets.

@Papierkorb
Created January 2, 2017 21:39
Show Gist options
  • Save Papierkorb/0eb344194e1297cf3aeb44806e30abb1 to your computer and use it in GitHub Desktop.
Save Papierkorb/0eb344194e1297cf3aeb44806e30abb1 to your computer and use it in GitHub Desktop.
require "./fuse"
FILE_DATA = "Hello, World!\n"
operations = Fuse::Binding::Operations.new
operations.getattr = ->(path : LibC::Char*, stat : LibC::Stat*) do
stat.value = LibC::Stat.new
case String.new(path)
when "/"
stat.value.st_mode = LibC::S_IFDIR | 0o755
stat.value.st_nlink = 2
when "/file"
stat.value.st_mode = LibC::S_IFREG | 0o777
stat.value.st_nlink = 1
stat.value.st_size = FILE_DATA.size
else
return -LibC::ENOENT
end
0
end
operations.open = ->(path : LibC::Char*, fi : Fuse::Binding::FileInfo*){ 0 }
operations.read = ->(path : LibC::Char*, buf : LibC::Char*, size : LibC::SizeT, offset : LibC::OffT, fi : Fuse::Binding::FileInfo*) do
return -LibC::ENOENT unless String.new(path) == "/file"
len = FILE_DATA.size
return 0 if offset >= len
if offset + size > len
to_copy = len - offset
else
to_copy = size
end
buf.copy_from(FILE_DATA.to_unsafe + offset, to_copy)
to_copy.to_i32
end
operations.readdir = ->(path : LibC::Char*, buf : Void*, filler : Fuse::Binding::FillDir, offset : LibC::OffT, fi : Fuse::Binding::FileInfo*) do
filler.call buf, ".".to_unsafe, Pointer(LibC::Stat).null, 0i64
filler.call buf, "..".to_unsafe, Pointer(LibC::Stat).null, 0i64
filler.call buf, "file".to_unsafe, Pointer(LibC::Stat).null, 0i64
0
end
argv = ARGV.map(&.to_unsafe).to_unsafe
Fuse::Binding.main(ARGV.size, argv, pointerof(operations), sizeof(Fuse::Binding::Operations), nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment