hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

RailsFS After a Couple Minutes of Tooling With Fuse, Whoa #

by why in inspect

Hot cats, I’m already in love with FuseFS. And it was only dropped on ruby-talk a bushel of hours ago. See, here’s a script which mounts the ActiveRecord classes in your Rails app as a Linux filesystem.

 #!/usr/bin/env ruby 

 require 'fusefs'
 require File.dirname(__FILE__) + '/../config/environment'

 class RailsFS < FuseFS::FuseDir
   def initialize
     @classes = {}
     require 'find'
     Find.find( File.join(RAILS_ROOT, 'app/models') ) do |model|
       if /(\w+)\.rb$/ =~ model
         model = $1
         kls = Inflector.classify( model )
         ( @classes[model] = Kernel::const_get( kls ) ).
           find :first rescue @classes.delete( model )
       end
     end
   end
   def directory? path
     tname, key = scan_path path
     table = @classes[tname]
     if table.nil?; false  # /table
     elsif key;     false  # /table/id
     else; true end
   end
   def file? path
     tname, key = scan_path path
     table = @classes[tname]
     key and table and table.find( key )
   end
   def can_delete?; true end
   def can_write? path; file? path end
   def contents path
     tname, key = scan_path path
     table = @classes[tname]
     if tname.nil?; @classes.keys.sort  # /
     else; table.find( :all ).map { |o| o.id.to_s } end  # /table
   end         
   def write_to path, body
     obj = YAML::load( body )
     obj.save
   end
   def read_file path
     tname, key = scan_path path
     table = @classes[tname]
     YAML::dump( table.find( key ) )
   end 
 end

 if (File.basename($0) == File.basename(__FILE__))
   root = RailsFS.new
   FuseFS.set_root(root)
   FuseFS.mount_under(ARGV[0])
   FuseFS.run # This doesn't return until we're unmounted.
 end

Make sure you have Linux 2.4-2.6, FUSE and Ruby-FuseFS.

Save the above script as script/filesys in your Rails app. (If you’d rather not cut-and-paste the above, it’s here.)

Now, run mkdir ~/railsmnt. Then, script/filesys ~/railsmnt. The rules are as follows:

  • ls ~/railsmnt will give you a list of tables.
  • ls ~/railsmnt/table will list IDs from the table.
  • cat ~/railsmnt/table/id will display a record in YAML.
  • vim ~/railsmnt/table/id to edit the record in YAML!

See, we got your ObjFSDB right here. Now I dare you to set up an FTP site to let people upload YAML files right into your database. (Thanks to Greg Milliam for his brilliant work, more userspace filesystem ideas here.)

said on 21 Sep 2005 at 16:30

OMG . I am in love you with right now!


railsmnt]$ cat contact_type/1
--- !ruby/object:ContactType
attributes:
  name: Client
  id: "1" 

said on 21 Sep 2005 at 16:36

Oh, oh, better yet tar czvf railsapp-yaml.tar.gz ~/railsapp to make a tarball of all the data in YAML format.

I should allow .svn directories to be stored in RailsFS, so you could actually keep versions of your database in Subversion.

said on 21 Sep 2005 at 16:41

_why. Stop it. Please. Just stop it. I want to hurt you now. You are going to make me cry. Stop it! STOP ! :-)

said on 21 Sep 2005 at 16:53

Why, if you did that, I think a lot more people would be in love with you. :)

said on 21 Sep 2005 at 17:01

I just can’t believe it!

This is great stuff!

said on 21 Sep 2005 at 17:18

Look, I’m browsing my Rails app data in Konqueror.

said on 21 Sep 2005 at 17:26

Wait, how do I get the most people in love with me? Please, I just want to make sure I do it exactly right.

said on 21 Sep 2005 at 17:31

Thanks, just rub it in that I have to use windows here ;)

This looks like it could be really fun stuff.

said on 21 Sep 2005 at 17:31

I’m pretty sure that the only way to get love now is to build Bootable-RailsOS.

Yes, I demand that you build a linux distro that runs off of Rails tonight. Thanks. :-)

Then…maybe then… you will get some love.

said on 21 Sep 2005 at 17:41

Gahh!! And me on OS X .

said on 21 Sep 2005 at 17:48

HouseHole, hoodwink’d and now this! I hereby nominate _why for the 2006 OSCON /Google hacker of the year award! What will he do next?

said on 21 Sep 2005 at 17:50

For his next trick, _why will make the MouseHole proxy a mountable filesystem which will allow hoodwink’ing from the commandline.

said on 21 Sep 2005 at 17:52

Aww, sweet! So people are really using it for even neater things than I imagined =).

Of course, as is the nature with bugs, I found quite a few things wrong with it (notably the editor files) today, but that’s in progress.

Thanks for the compliment!

said on 21 Sep 2005 at 17:54

And for you OS X and Windows users: If it’s at all possible, I’m asking for pointers and ways. I’m getting an OS X pbook eventually, and will try my darnedest to get FuseFS working on there. (Likely not with FUSE itself, but a library that lets me do similar stuff)

said on 21 Sep 2005 at 18:08

You are a god, Greg.

said on 21 Sep 2005 at 18:11

Very cool code, and you are a god of Ruby, Greg, but I only can wish I were Linus Torvalds. :-)

said on 21 Sep 2005 at 18:11

Microsoft Vista will have VistaFS, which will allow you to have transparent Internet Explorer.

Nothing to see here. Move along.

said on 21 Sep 2005 at 18:26

OMFG . I just came in my pants.

said on 21 Sep 2005 at 18:26

Bill Gates, get off my blog.

said on 21 Sep 2005 at 18:29

[hurls chair across room]

said on 21 Sep 2005 at 18:30

Get off my turf, Greg. Seriously.

said on 21 Sep 2005 at 18:48
Hmm…
if /(\w+)\.rb$/ =~ model
Try:
if File.extname(model) == ".rb" 
said on 21 Sep 2005 at 19:10
rails + fuse + xcruise =

every hacker movie from the 1990’s

said on 21 Sep 2005 at 19:11

So that was unix on Jurassic Park!!

said on 21 Sep 2005 at 19:30

Greg:

From what I can glean, Apple recommends in cases like this to skip writing an actual VFS driver, and instead write an NFS server that you can mount from the same machine (this is, apparently, how they implement their FTP filesystem). I can see some obvious performance issues with this, but it would be pretty cross-platform, at the same time.

Does ruby have any NFS libraries we could build off of?

said on 21 Sep 2005 at 19:35

greasygreasy:

I don’t know of any Ruby libraries to provide NFS service, but does Apple provide any libraries?

I currently have fusefs_lib.so built from fusefs_lib.c and fusefs_fuse.c. I imagine eventually moving most of the FUSE -specific stuff to fusefs_fuse.c and do all the ruby work in fusefs_lib.c

said on 21 Sep 2005 at 20:31

You know, Google is going to BUY you… not your work, not any of your code, but YOU as a person.

said on 21 Sep 2005 at 21:26

Google is going to buy us all before this is done. The whole Ruby community lock, stock and barrel. Or perhaps it’ll be a merger and we’ll rename it Roogle, or maybe Googley, or Roogley. The ‘O’s will be Rubys with Matz’ image hovering above on the front page. This thinktank is unstopable; the ideas flowing here are worth $Billions!

greasygreasy: interesting idea. Why not? A cross-platform GreaseMonkey was born here, why not a cross-platform VFS to free the Windows prisoners (and the mostly unshackled OS X users)?

This just in: Google and Micro$oft employees have been ordered to spend 20% of their time reading RedHanded and ruby-talk.

said on 21 Sep 2005 at 21:37
if you want to follow Daniel’s prettifying (de-Perling?) advice, make sure to change the next line also, as in

11       if File.extname(model) == ".rb" 
12         model = File.basename(model, ".rb")
 
said on 21 Sep 2005 at 23:06

It was right in front of my face the whole damn time. WebDAV. I’ve got a drop-in replacement for FuseFS about 80% done, using webrick and a modified version of Tatsuki Sugiura’s WebDAV for that cross-platform feel. Greg, I’d love to make it part of FuseFS, if you don’t think it’s out of the scope of the project. I don’t want to tread on your toes.

Reads work perfectly right now without any modification to _why’s code above (except for the parent class). Writes are fine on Windows, but need some work on OS X because Apple took the safe route and won’t allow writing to a WebDAV share without locking the file first, which Sugiura’s code doesn’t support. Yet. I’m workin’ on it.

Is this something people would be interested in?

said on 21 Sep 2005 at 23:10

How about CIFS ? It is fairly well supported across the board.

said on 21 Sep 2005 at 23:16

lorewarden: WebDAV shares can be mounted on every platform I know of at this point, Windows, Linux, OS X , generic Unix, etc. CIFS would certainly be a good choice as well, but I know of no ruby library for implementing a SMB server.

said on 21 Sep 2005 at 23:17

greasygreasy: Yeah, I’d love to incorporate it into fusefs. I’ve never used webdav though so I have no idea how that works on windows or OS X . If you want to contact me outside of this blog, just email me: walker@deafcode.com, and I’m sometimes on #ruby-lang as lethalcode. AOL IM and GMail IM of captdeaf on both. We’ll work on putting them together and try for a cross-platform FuseFS. That’d be really sweet.

If it works on Linux too, could even make it drop FUSE if it’s not found and default to webdav (Or drop FUSE altogether, but I think it may be better to keep it around as it’s likely better performance than doing it all over the network.)

said on 22 Sep 2005 at 03:20

I moved to Debian from FreeBSD last month, and this kind of thing was the reason.

Greg and _why, you are terrifying.

said on 22 Sep 2005 at 03:54

i like your comment format!

said on 22 Sep 2005 at 04:37

Okay, it’s official: Rails has the permafrost now. Always when you think it can’t get much cooler than it is, people come up with new stuff like this.

Years and years ago, I heard people drooling about things like “cat /dev/tcp/hostname/servicename” being available one day; Lately, people have been buzzing about Reiser4 and how it’s going to Change the World… but looks like user-space stuff is frostier still!

...and then the Plan9 folks come and spoil all the fun. =(

said on 22 Sep 2005 at 12:34

Really, in an ideal world we’d have v9fs instead of fuse. But fuse is a close second.

SMB and NFS are both too nasty to bother implementing for user-space filesystems.

said on 22 Sep 2005 at 13:23

HELLO JOSEPH !!

said on 22 Sep 2005 at 16:03

MenTaLguY: I agree on SMB and NFS , that’s why I was hoping for a pre-existing ruby library.

said on 22 Sep 2005 at 17:55

WWW Wolf: /dev/tcp/host/port already works under bash. Not nearly as cool as FUSE , but still cool.

said on 23 Sep 2005 at 02:31

Oh man, I wish I could try this out on my OS X system. Greg, get crackin’ and make it work! :D

said on 23 Sep 2005 at 04:28

I don’t know what you guys are talking about! Seriously. I love the excitement and want to understand—but what!!??

said on 23 Sep 2005 at 06:27

After seeing FuseFS yesterday on ruby-forge I was impressed. This is just getting silly now though. I feel like I’m living some web developers dream. :)

said on 23 Sep 2005 at 07:57

Holy crap, I was just thinking about doing something like this a few weeks ago. Very cool. :-)

said on 23 Sep 2005 at 12:38

/me dreams of a ruby/FuseFS compatible api to write namespace shell extensions on win32

said on 23 Sep 2005 at 12:58

I can’t find much info on user-space filesystems. Anybody have any pointers to articles explaining it and/or reasons for/against? Happy end o’ week!

said on 25 Sep 2005 at 05:47

why: from jurassic park it really was a unix box. irix to be exact. and the 3d file system navigator was a real product created by Sun. FSN (File System Navigator) pronounced Fusion im sure was slightly modified for the movie, but it IS what was used:

http://www.sgi.com/fun/freeware/3d_navigator.html

said on 25 Sep 2005 at 06:00

has anyone considered an svn mount, and maybe have like ./trunk/somefile/HEAD ./trunk/somefile/

or ./ /trunk/somefile .

;) the random useless shit we can do now !

said on 26 Sep 2005 at 04:17

Too many years ago (18 now) I first started databases with a system called Pick – where the entire file system structure was itself a database and you could do exactly what you are now doing. Edit records through the file system and (vice-versa) query files off the file system using your normal database reporting tools. Now we are back there again. Like it :)

said on 26 Sep 2005 at 07:35

_why: I should allow .svn directories to be stored in RailsFS, so you could actually keep versions of your database in Subversion.

Or you could just use Darcs in the directory above ;)

said on 26 Sep 2005 at 12:36

neat. really I like it.

said on 26 Sep 2005 at 16:52

I’m gonna have to learn Ruby and Rails now.

said on 27 Sep 2005 at 14:24

Hey _why – mind if I stick your railsfs.rb in the samples/ dir for the 0.4 release?

said on 28 Sep 2005 at 13:17

Mmmm, this might just be a reason for me to try the new FreeBSD FUSE kernel module. bouncy

said on 28 Sep 2005 at 16:35

Zobbo: I’m STILL progamming in Pick and love it’s simplicity. I haven’t been able to tear myself away. Poked around with (very briefly) java, C, PHP but not until I found Ruby have I found something so streamlined & w/rapid devel capabilities. Just sit down and program! Finally make the move…

said on 04 Oct 2005 at 19:39

The GNU /Hurd OS has had stuff like this in it since the beginning. Take a look at our notion of translators.

said on 04 Oct 2005 at 22:05

Greg: YES YES PLEASE !

Freaky: Does it work??

OldSchool, please post from now on as NewSchool. Thankyou!

RMS, get off my blog!

said on 05 Oct 2005 at 09:45

I can’t wait for this WebDAV thing to have it available on OSX

said on 14 Oct 2005 at 04:21

odd, I can’t seem to write files…

said on 11 Nov 2005 at 01:27

I’ve heard of people using this to mount their samaba shares on fuse and backing it all up on nfs.

SMB for Fuse

With SMB for Fuse you can seamlessly browse your network neighbourhood as were it on your own filesystem.

It’s basically smbmount with a twist. Instead of mounting one Samba share at a time, you mount all workgroups, hosts and shares at once. Only when you’re accessing a share a connection is made to the remote computer.

said on 19 Nov 2005 at 23:13

This could easily be done in Windows as well! Windows also has user mode type file system extensions – out of the box! (Google webdrive for instance).

said on 26 Mar 2006 at 17:23

Bollocks, now I have to install Linux & Ruby again.

Comments are closed for this entry.