# hx/backend/hobix - Hobix filesystem backend for Hx # # Copyright (c) 2009-2010 MenTaLguY # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require 'pathname' require 'hx' require 'find' module Hx module Backend class RawFiles include Hx::Filter def initialize(input, options) @entry_dir = Hx.get_pathname(options, :entry_dir) @postedit_cmd = options[:postedit_cmd] @postdelete_cmd = options[:postdelete_cmd] @postedit_cmd = @postedit_cmd.split(/\s+/) if @postedit_cmd @postdelete_cmd = @postdelete_cmd.split(/\s+/) if @postdelete_cmd end def path_to_pathname(path) ; @entry_dir + path ; end private :path_to_pathname def edit_entry(path, prototype=nil) pathname = path_to_pathname(path) begin text = pathname.read rescue Errno::ENOENT raise NoSuchEntryError, path unless prototype text = prototype['content'].to_s end text = yield text if text Hx.write_file(pathname, text) system *(@postedit_cmd + [pathname]) if @postedit_cmd else begin File.unlink(pathname) system *(@postdelete_cmd + [pathname]) if @postdelete_cmd rescue Errno::ENOENT end end self end def each_entry_path(selector) Find.find @entry_dir.to_s do |fs_path| next unless FileTest.file? fs_path path = Pathname.new(fs_path).relative_path_from(@entry_dir).to_s yield path if selector.accept_path? path end self end def get_entry(path) pathname = path_to_pathname(path) begin { 'updated' => pathname.mtime, 'executable' => pathname.executable?, 'content' => Hx::LazyContent.new { pathname.read } } rescue Errno::ENOENT raise Hx::NoSuchEntryError, path end end end end end