Sha256: 202a66dba06a6a72bac3dd6ac74a951ffc1236164dbb64a2783a2d770a1190c0

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 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, extensions = [])
      if !block_given?
        return enum_for(:each_in, paths)
      end

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

      extensions = [extensions].flatten.compact

      @driver.each_in(paths) do |path, content|
        if extensions.any?
          if !extensions.include?(File.extname(path))
            next
          end
        end

        yield(path, content)
      end
    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

4 entries across 4 versions & 1 rubygems

Version Path
ahnnotate-0.5.1 lib/ahnnotate/vfs.rb
ahnnotate-0.5.0 lib/ahnnotate/vfs.rb
ahnnotate-0.4.0 lib/ahnnotate/vfs.rb
ahnnotate-0.3.0 lib/ahnnotate/vfs.rb