Created
November 13, 2008 23:55
-
-
Save devyn/24691 to your computer and use it in GitHub Desktop.
RedFS is a mountable block system (not working yet)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# RedFS (Fuse) | |
# RedFS is a mountable block system | |
# To make a RedFS virtual system, use the following shell command: | |
# $ dd if=/dev/zero of=(yourfile) bs=4096 count=(number of blocks) | |
# To run: | |
# # ./redfs-fuse.rb (yourfile) (mountpoint) | |
# or: | |
# $ sudo ./redfs-fuse.rb (yourfile) (mountpoint) | |
# Also, please note that if when you write a block and it is larger than 4096 bytes, RedFS will cut off at that point. | |
require 'fusefs' | |
class RedFS < FuseFS::FuseDir | |
BYTES_IN_BLOCK = 4096 | |
def close_base | |
@file.close | |
end | |
def block_to_byte(n) | |
b = n * BYTES_IN_BLOCK | |
((@file.size - 1) > b) ? b : nil | |
end | |
def total_blocks | |
@file.size/BYTES_IN_BLOCK | |
end | |
def initialize | |
@file = File.open(ARGV.first, 'r+') | |
end | |
def directory?(path); false; end | |
def file?(path) | |
false unless File.basename(path) =~ /^(\d+)$/ | |
block_to_byte(File.basename(path).to_i) ? true : false | |
end | |
def can_delete?; false; end | |
def can_write?(path); file?(path); end | |
def contents(path) | |
(0..(total_blocks - 1)).to_a | |
end | |
def read_file(path) | |
@file.pos = block_to_byte File.basename(path).to_i | |
@file.readbytes BYTES_IN_BLOCK | |
end | |
def write_to(path, data) | |
@file.pos = block_to_byte File.basename(path).to_i | |
str = data[0..(BYTES_IN_BLOCK-1)] | |
@file.write str | |
@file.write "\0"*(BYTES_IN_BLOCK-str.size) | |
end | |
end | |
if __FILE__ == $0 | |
root = RedFS.new | |
FuseFS.set_root root | |
FuseFS.mount_under ARGV[1] | |
%w(INT TERM).each {|sig| trap(sig) do | |
root.close_base | |
FuseFS.unmount | |
end } | |
FuseFS.run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment