Sha256: 9a4011547462f88255fba5fcfb6cfa92a92a2f9e8741143fa0db7ef2914ea920

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Ahnnotate
  class Vfs
    include Enumerable

    def initialize(driver)
      @driver = driver
    end

    def []=(path, content)
      if content.nil?
        return
      end

      if @driver.dir?(path)
        raise Error::VfsWriteError, "can't write to directory"
      end

      if !accessible_path?(path)
        raise Error::VfsOutsideOfRoot, "path seems to be outside of root"
      end

      @driver[path] = content
    end

    def [](path)
      if @driver.dir?(path)
        raise Error::VfsReadError, "can't read a directory"
      end

      if !accessible_path?(path)
        raise Error::VfsOutsideOfRoot, "path seems to be outside of root"
      end

      @driver[path]
    end

    def each
      if !block_given?
        return enum_for(:each)
      end

      @driver.each(&Proc.new)
    end

    def each_in(paths)
      if !block_given?
        return enum_for(:each_in, paths)
      end

      paths =
        if paths.is_a?(Array)
          paths
        else
          [paths]
        end

      @driver.each_in(paths, &Proc.new)
    end

    private

    def accessible_path?(path)
      random_safe_prefix = "/#{SecureRandom.hex}"
      expanded_path = File.expand_path(path, random_safe_prefix)

      /\A#{random_safe_prefix}\b/ === expanded_path
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ahnnotate-0.2.0 lib/ahnnotate/vfs.rb