Class: Amp::Repositories::Stores::BasicStore

Inherits:
Object
  • Object
show all
Defined in:
lib/amp/repository/store.rb

Overview

BasicStore

This class is the one from which all other stores derive. It implements basic methods #walk, #join, #datafiles, and #copy_list which are the public methods for all stores. All others are basically internal.

Direct Known Subclasses

EncodedStore, FilenameCacheStore

Constant Summary collapse

BASIC_DATA_FILES =
%W(data 00manifest.d 00manifest.i 00changelog.d  00changelog.i)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, openerklass, pathjoiner) ⇒ BasicStore

Returns a new instance of BasicStore.



33
34
35
36
37
38
39
# File 'lib/amp/repository/store.rb', line 33

def initialize(path, openerklass, pathjoiner)
  @path_joiner, @path = pathjoiner, path
  @create_mode = calculate_mode path
  @opener = openerklass.new(@path)
  @opener.create_mode = @create_mode
  #@opener.default = :open_hg
end

Instance Attribute Details

#create_modeObject (readonly)

Returns the value of attribute create_mode.



31
32
33
# File 'lib/amp/repository/store.rb', line 31

def create_mode
  @create_mode
end

#openerObject (readonly)

Returns the value of attribute opener.



30
31
32
# File 'lib/amp/repository/store.rb', line 30

def opener
  @opener
end

#pathObject (readonly)

Returns the value of attribute path.



29
30
31
# File 'lib/amp/repository/store.rb', line 29

def path
  @path
end

#path_joinerObject

Returns the value of attribute path_joiner.



28
29
30
# File 'lib/amp/repository/store.rb', line 28

def path_joiner
  @path_joiner
end

Instance Method Details

#calculate_mode(path) ⇒ Fixnum

Calculates the mode for the user on the file at the given path. I guess this saves some wasted chmods.

Parameters:

  • path (String)

    the path to calculate the mode for

Returns:

  • (Fixnum)

    the mode to use for chmod. Octal, like 0777



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/amp/repository/store.rb', line 109

def calculate_mode(path)
  begin
    mode = File.stat(path).mode
    if (0777 & ~Amp::Support.UMASK) == (0777 & mode)
      mode = nil
    end
  rescue
    mode = nil
  end
  mode
end

#copy_listArray<String>

Returns the list of basic files that are crucial for the store to function.

Returns:

  • (Array<String>)

    the list of basic files crucial to this class



126
127
128
# File 'lib/amp/repository/store.rb', line 126

def copy_list
  ['requires'] + BASIC_DATA_FILES
end

#datafilesObject

Returns all the data files in the store.



68
69
70
# File 'lib/amp/repository/store.rb', line 68

def datafiles
  do_walk('data', true)
end

#do_walk(relpath, recurse = false) ⇒ (String, String, Fixnum)

Basic walker that is not very smart at all. It can recursively search for data files, but it actually uses a queue to do its searching.

Parameters:

  • relpath (String)

    the base path to search

  • recurse (Boolean) (defaults to: false)

    (false) whether or not to recursively search each discovered directory.

Returns:

  • ((String, String, Fixnum))

    Each entry is returned in the form

    filepath, filepath, filesize


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/amp/repository/store.rb', line 81

def do_walk(relpath, recurse=false)
  path = join relpath
  stripped_len = path.size + File::SEPARATOR.size - 1
  list = []
  if File.directory?(path)
    to_visit = [path]
    while to_visit.any?
      p = to_visit.shift
      Dir.stat_list(p, true) do |file, kind, stat|
        fp = join(file)
        if kind =~ /file/ && ['.d','.i'].include?(file[-2..-1])
          n = fp[stripped_len..-1]
          list << [n, n, stat.size]
        elsif kind =~ /directory/ && recurse
          to_visit << fp
        end
      end
    end
  end
  list.sort
end

#join(f) ⇒ Object

Joins the file f to the store’s base path using the path-joiner.

Parameters:

  • f (String)

    the filename to join to the store’s base path

Returns:

  • the combined base path and file path



46
47
48
# File 'lib/amp/repository/store.rb', line 46

def join(f)
  @path_joiner.call(@path, f)
end

#walk {|file| ... } ⇒ Object

Iterates over every file tracked in the store and yield it.

Yields:

  • (file)

    every file in the store

Yield Parameters:

  • file (String)

    the filepath to an entry in the store



55
56
57
58
59
60
61
62
63
64
# File 'lib/amp/repository/store.rb', line 55

def walk
  datafiles do |x|
    yield x
  end
  
  meta = do_walk '', false
  meta.reverse.each do |x|
    yield x
  end
end