3 # A Locator is used to parse and manipulate Keep locator strings.
5 # Locators obey the following syntax:
7 # locator ::= address hint*
8 # address ::= digest size-hint
9 # digest ::= <32 hexadecimal digits>
10 # size-hint ::= "+" [0-9]+
11 # hint ::= "+" hint-type hint-content
13 # hint-content ::= [A-Za-z0-9@_-]+
15 # Individual hints may have their own required format:
17 # sign-hint ::= "+A" <40 lowercase hex digits> "@" sign-timestamp
18 # sign-timestamp ::= <8 lowercase hex digits>
19 attr_reader :hash, :hints, :size
21 LOCATOR_REGEXP = /^([[:xdigit:]]{32})(\+([[:digit:]]+))?(\+([[:upper:]][[:alnum:]+@_-]*))?$/
23 def initialize(hasharg, sizearg, hintarg)
30 !!(LOCATOR_REGEXP.match tok)
33 # Locator.parse returns a Locator object parsed from the string tok.
34 # Returns nil if tok could not be parsed as a valid locator.
38 rescue ArgumentError => e
43 # Locator.parse! returns a Locator object parsed from the string tok,
44 # raising an ArgumentError if tok cannot be parsed.
46 if tok.nil? or tok.empty?
47 raise ArgumentError.new "locator is nil or empty"
50 m = LOCATOR_REGEXP.match(tok.strip)
52 raise ArgumentError.new "not a valid locator #{tok}"
55 tokhash, _, toksize, _, trailer = m[1..5]
58 trailer.split('+').each do |hint|
59 if hint =~ /^[[:upper:]][[:alnum:]@_-]+$/
62 raise ArgumentError.new "unknown hint #{hint}"
67 Locator.new(tokhash, toksize, tokhints)
70 # Returns the signature hint supplied with this locator,
71 # or nil if the locator was not signed.
73 @hints.grep(/^A/).first
76 # Returns an unsigned Locator.
78 Locator.new(@hash, @size, @hints.reject { |o| o.start_with?("A") })
82 Locator.new(@hash, @size, [])
92 [ @hash, @size, *@hints ].join('+')
94 [ @hash, *@hints ].join('+')
100 # Class to parse a manifest text and provide common views of that data.
101 def initialize(manifest_text)
102 @text = manifest_text
107 return to_enum(__method__) unless block_given?
108 @text.each_line do |line|
112 line.scan /\S+/ do |token|
114 stream_name = unescape token
115 elsif file_tokens.empty? and Locator.valid? token
116 block_tokens << token
118 file_tokens << unescape(token)
122 next if stream_name.nil?
123 yield [stream_name, block_tokens, file_tokens]
128 # Parse backslash escapes in a Keep manifest stream or file name.
129 s.gsub(/\\(\\|[0-7]{3})/) do |_|
139 def split_file_token token
140 start_pos, filesize, filename = token.split(':', 3)
142 raise ArgumentError.new "Invalid file token '#{token}'"
144 [start_pos.to_i, filesize.to_i, unescape(filename)]
148 return to_enum(__method__) unless block_given?
149 @text.each_line do |line|
151 in_file_tokens = false
152 line.scan /\S+/ do |token|
154 stream_name = unescape token
155 elsif in_file_tokens or not Locator.valid? token
156 in_file_tokens = true
157 yield [stream_name] + split_file_token(token)
166 file_sizes = Hash.new(0)
167 each_file_spec do |streamname, _, filesize, filename|
168 file_sizes[[streamname, filename]] += filesize
170 @files = file_sizes.each_pair.map do |(streamname, filename), size|
171 [streamname, filename, size]
177 def files_count(stop_after=nil)
178 # Return the number of files represented in this manifest.
179 # If stop_after is provided, files_count will read the manifest
180 # incrementally, and return immediately when it counts that number of
181 # files. This can help you avoid parsing the entire manifest if you
182 # just want to check if a small number of files are specified.
183 if stop_after.nil? or not @files.nil?
187 each_file_spec do |streamname, _, _, filename|
188 seen_files[[streamname, filename]] = true
189 return stop_after if (seen_files.size >= stop_after)
194 def exact_file_count?(want_count)
195 files_count(want_count + 1) == want_count
198 def minimum_file_count?(want_count)
199 files_count(want_count) >= want_count
202 def has_file?(want_stream, want_file=nil)
204 want_stream, want_file = File.split(want_stream)
206 each_file_spec do |streamname, _, _, name|
207 if streamname == want_stream and name == want_file