MacFuse and Ruby fusefs extension

Posted on March 09, 2007

Lately I've been playing with MacFuse project and all I could say is that this is a fantastic effort of bringing the well-known user-space(fuse) file system to the OSX land. The latest versions of fuse work well with FreeBSD and MacFuse extends this functionality to OSX platform.

My goal was to make Ruby's fusefs extension play along with it. The major stumbling point was that MacFuse requires using the new fuse API, so I had to make several modifications to fusefs in order to make it possible at least to compile it.

OK. If you are a braveheart type and not afraid that it might damage your computer, these are the steps you need to do in order to install this extension:

  1. Download the latest version of MacFuse and use the installer that it comes with.

  2. Check out my version of fusefs from http://svn.datanoise.com/fusefs-osx and use the standard procedure of installing Ruby extensions:

    $ svn co http://svn.datanoise.com/fusefs-osx
    $ cd fusefs-osx
    $ make
    $ sudo make install
    

That's about it. Now you can start playing with examples located in <fusefs-osx>/sample directory. When you are at it, I'd like you to take a look at one of my personal toys - the remote DRb-based file system. It is located in <fusefs-osx>/sample/drb directory and that's how you use it:

  1. On the remote side (a Windows box will do just fine) start the drb server:

    $ ruby ./sample/drb/drbfs_server.rb ~/shared
    druby://myhost:7777
    

    The script takes the name of a directory you want to share. When the script is started, it'll print the URL you have to use when you mount your DRb filesystem on the client side.

  2. On the client side, mount DRb file system:

    $ ruby ./sample/drb/drbfs.rb mount/ druby://myhost:7777
    $ ls mount
    

    The client script takes the name of a directory you want to mount to and the URL of the server from the step one.

Enjoy and don't forget to read <fusefs-osx>/OSX.txt for the important API changes.

Conditional tail

Posted on March 03, 2007

So many times while watching log files I wanted to find a command that combines tail with grep filtering functionally. The Unix command repository is huge and I'm pretty sure you can do it without resorting to a scripting language. But having spent several minutes googling for this functionality to no avail, I decided to bring Ruby to the rescue.

At first I thought that I had to port File::Tail Perl module to Ruby, but I found that it has been done already by Florian Frank. He has even provided a convenient way of installing it:

    $ sudo gem install file-tail

OK, the rest was as obvious as this:

    $ cat tailg
    #!/usr/bin/env ruby

    require 'rubygems'
    gem 'file-tail'
    require 'file/tail'


    if ARGV.size != 2
      $stderr.puts "Usage #$0 <regexp> <log-file>"
    end

    begin
      File.open(ARGV[1]) do |log|
        log.extend(File::Tail)
        log.interval = 2
        log.backward(10)
        log.tail { |line| puts line if line =~ /#{ARGV[0]}/ }
      end 
    rescue Exception
    end

    $./tailg 'products' /var/log/searchd_query.log
    [Sat Mar  3 13:39:40 2007] 0.000 sec [all/0/rel 3 (0,9)] [products] NOA
    [Sat Mar  3 13:39:44 2007] 0.000 sec [all/0/rel 22 (0,9)] [products] lauren
    [Sat Mar  3 13:40:09 2007] 0.000 sec [all/0/rel 22 (9,9)] [products] lauren

tailg command accepts two parameters: a regexp to filter for interesting lines and a log file itself.