5 def initialize(manifest_text="")
6 @manifest_text = manifest_text
8 @root = CollectionRoot.new
9 manifest = Keep::Manifest.new(manifest_text)
10 manifest.each_line do |stream_root, locators, file_specs|
11 if stream_root.empty? or locators.empty? or file_specs.empty?
12 raise ArgumentError.new("manifest text includes malformed line")
14 loc_list = LocatorList.new(locators)
15 file_specs.map { |s| manifest.split_file_token(s) }.
16 each do |file_start, file_len, file_path|
17 @root.file_at(normalize_path(stream_root, file_path)).
18 add_segment(loc_list.segment(file_start, file_len))
24 @manifest_text ||= @root.manifest_text
37 @manifest_text = @root.manifest_text
41 def cp_r(source, target, source_collection=nil)
42 opts = {descend_target: !source.end_with?("/")}
43 copy(:merge, source.chomp("/"), target, source_collection, opts)
46 def each_file_path(&block)
47 @root.each_file_path(&block)
52 substream, item = find(path)
53 not (substream.leaf? or substream[item].nil?)
54 rescue Errno::ENOENT, Errno::ENOTDIR
59 def rename(source, target)
60 copy(:add_copy, source, target) { rm_r(source) }
68 remove(source, recursive: true)
74 @root.find(normalize_path(*parts))
85 def normalize_path(*parts)
86 path = File.join(*parts)
88 raise ArgumentError.new("empty path")
89 elsif (path == ".") or path.start_with?("./")
96 def copy(copy_method, source, target, source_collection=nil, opts={})
97 # Find the item at path `source` in `source_collection`, find the
98 # destination stream at path `target`, and use `copy_method` to copy
99 # the found object there. If a block is passed in, it will be called
100 # right before we do the actual copy, after we confirm that everything
101 # is found and can be copied.
102 source_collection = self if source_collection.nil?
103 src_stream, src_tail = source_collection.find(source)
104 dst_stream, dst_tail = find(target)
105 if (source_collection.equal?(self) and
106 (src_stream.path == dst_stream.path) and (src_tail == dst_tail))
109 src_item = src_stream[src_tail]
110 dst_tail ||= src_tail
111 check_method = "check_can_#{copy_method}".to_sym
113 if opts.fetch(:descend_target, true)
115 # Find out if `target` refers to a stream we should copy into.
116 tail_stream = dst_stream[dst_tail]
117 tail_stream.send(check_method, src_item, src_tail)
118 # Yes it does. Copy the item at `source` into it with the same name.
119 dst_stream = tail_stream
120 target_name = src_tail
121 rescue Errno::ENOENT, Errno::ENOTDIR
122 # It does not. We'll fall back to writing to `target` below.
126 dst_stream.send(check_method, src_item, dst_tail)
127 target_name = dst_tail
129 # At this point, we know the operation will work. Call any block as
133 # Re-find the destination stream, in case the block removed
134 # the original (that's how rename is implemented).
135 dst_stream = @root.stream_at(dst_stream.path)
137 dst_stream.send(copy_method, src_item, target_name)
141 def remove(path, opts={})
142 stream, name = find(path)
143 stream.delete(name, opts)
147 LocatorSegment = Struct.new(:locators, :start_pos, :length)
149 class LocatorRange < Range
152 def initialize(loc_s, start)
154 range_end = start + Keep::Locator.parse(loc_s).size.to_i
155 super(start, range_end, false)
160 # LocatorList efficiently builds LocatorSegments from a stream manifest.
161 def initialize(locators)
163 @ranges = locators.map do |loc_s|
164 new_range = LocatorRange.new(loc_s, next_start)
165 next_start = new_range.end
170 def segment(start_pos, length)
171 # Return a LocatorSegment that captures `length` bytes from `start_pos`.
172 start_index = search_for_byte(start_pos)
174 end_index = start_index
176 end_index = search_for_byte(start_pos + length - 1, start_index)
178 seg_ranges = @ranges[start_index..end_index]
179 LocatorSegment.new(seg_ranges.map(&:locator),
180 start_pos - seg_ranges.first.begin,
186 def search_for_byte(target, start_index=0)
187 # Do a binary search for byte `target` in the list of locators,
188 # starting from `start_index`. Return the index of the range in
189 # @ranges that contains the byte.
195 if range.include?(target)
198 raise RangeError.new("%i not in segment" % target)
199 elsif target < range.begin
209 attr_reader :path, :name
213 @name = File.basename(path)
217 class CollectionFile < CollectionItem
235 def add_segment(segment)
239 def each_segment(&block)
240 @segments.each(&block)
243 def check_can_add_copy(src_item, name)
244 raise Errno::ENOTDIR.new(path)
247 alias_method :check_can_merge, :check_can_add_copy
249 def copy_named(copy_path)
250 copy = self.class.new(copy_path)
251 each_segment { |segment| copy.add_segment(segment) }
256 class CollectionStream < CollectionItem
276 raise Errno::ENOENT.new("%p not found in %p" % [key, path])
279 def delete(name, opts={})
281 if item.file? or opts[:recursive]
284 raise Errno::EISDIR.new(path)
289 return to_enum(__method__) unless block_given?
290 items.each_value do |item|
294 item.each_file_path { |path| yield path }
300 # Given a POSIX-style path, return the CollectionStream that
301 # contains the object at that path, and the name of the object
303 components = find_path.split("/")
304 tail = components.pop
305 [components.reduce(self, :[]), tail]
308 def stream_at(find_path)
309 key, rest = find_path.split("/", 2)
310 next_stream = get_or_new(key, CollectionStream)
314 next_stream.stream_at(rest)
318 def file_at(find_path)
319 stream_path, _, file_name = find_path.rpartition("/")
320 if stream_path.empty?
321 get_or_new(file_name, CollectionFile)
323 stream_at(stream_path).file_at(file_name)
328 # Return a string with the normalized manifest text for this stream,
329 # including all substreams.
330 file_keys, stream_keys = items.keys.sort.partition do |key|
333 my_line = StreamManifest.new(path)
334 file_keys.each do |file_name|
335 my_line.add_file(items[file_name])
337 sub_lines = stream_keys.map do |sub_name|
338 items[sub_name].manifest_text
340 my_line.to_s + sub_lines.join("")
343 def check_can_add_copy(src_item, key)
344 if existing = check_can_merge(src_item, key) and not existing.leaf?
345 raise Errno::ENOTEMPTY.new(existing.path)
349 def check_can_merge(src_item, key)
350 if existing = items[key] and (existing.class != src_item.class)
351 raise Errno::ENOTDIR.new(existing.path)
356 def add_copy(src_item, key)
357 self[key] = src_item.copy_named("#{path}/#{key}")
360 def merge(src_item, key)
361 # Do a recursive copy of the collection item `src_item` to destination
362 # `key`. If a simple copy is safe, do that; otherwise, recursively
363 # merge the contents of the stream `src_item` into the stream at
366 check_can_add_copy(src_item, key)
367 add_copy(src_item, key)
368 rescue Errno::ENOTEMPTY
371 # Copy as much as possible, then raise any error encountered.
372 # Start with streams for a depth-first merge.
373 src_items = src_item.items.each_pair.sort_by do |_, sub_item|
374 (sub_item.file?) ? 1 : 0
376 src_items.each do |sub_key, sub_item|
378 dest.merge(sub_item, sub_key)
379 rescue Errno::ENOTDIR => error
382 raise error unless error.nil?
386 def copy_named(copy_path)
387 copy = self.class.new(copy_path)
388 items.each_pair do |key, item|
389 copy.add_copy(item, key)
404 def get_or_new(key, klass)
405 # Return the collection item at `key` and ensure that it's a `klass`.
406 # If `key` does not exist, create a new `klass` there.
407 # If the value for `key` is not a `klass`, raise an ArgumentError.
410 self[key] = klass.new("#{path}/#{key}")
411 elsif not item.is_a?(klass)
413 new("in stream %p, %p is a %s, not a %s" %
414 [path, key, items[key].class.human_name, klass.human_name])
421 class CollectionRoot < CollectionStream
427 def delete(name, opts={})
429 # If that didn't fail, it deleted the . stream. Recreate it.
433 def check_can_merge(src_item, key)
434 if items.include?(key)
437 raise_root_write_error(key)
444 items["."] = CollectionStream.new(".")
447 def raise_root_write_error(key)
448 raise ArgumentError.new("can't write to %p at collection root" % key)
452 raise_root_write_error(key)
457 # Build a manifest text for a single stream, without substreams.
458 # The manifest includes files in the order they're added. If you want
459 # a normalized manifest, add files in lexical order by name.
468 def add_file(coll_file)
469 coll_file.each_segment do |segment|
470 extend_locator_ranges(segment.locators)
471 extend_file_specs(coll_file.name, segment)
476 if @file_specs.empty?
479 "%s %s %s\n" % [escape_name(@name),
480 @loc_ranges.keys.join(" "),
481 @file_specs.join(" ")]
487 def extend_locator_ranges(locators)
489 select { |loc_s| not @loc_ranges.include?(loc_s) }.
491 @loc_ranges[loc_s] = LocatorRange.new(loc_s, @loc_range_start)
492 @loc_range_start = @loc_ranges[loc_s].end
496 def extend_file_specs(filename, segment)
497 # Given a filename and a LocatorSegment, add the smallest
498 # possible array of file spec strings to @file_specs that
499 # builds the file from available locators.
500 filename = escape_name(filename)
501 start_pos = segment.start_pos
502 length = segment.length
503 start_loc = segment.locators.first
505 # Build a list of file specs by iterating through the segment's
506 # locators and preparing a file spec for each contiguous range.
507 segment.locators[1..-1].each do |loc_s|
508 range = @loc_ranges[loc_s]
509 if range.begin != @loc_ranges[prev_loc].end
510 range_start, range_length =
511 start_and_length_at(start_loc, prev_loc, start_pos, length)
512 @file_specs << "#{range_start}:#{range_length}:#{filename}"
514 length -= range_length
519 range_start, range_length =
520 start_and_length_at(start_loc, prev_loc, start_pos, length)
521 @file_specs << "#{range_start}:#{range_length}:#{filename}"
524 def escape_name(name)
525 name.gsub(/\\/, "\\\\\\\\").gsub(/\s/) do |s|
526 s.each_byte.map { |c| "\\%03o" % c }.join("")
530 def start_and_length_at(start_key, end_key, start_pos, length)
531 range_begin = @loc_ranges[start_key].begin + start_pos
532 range_length = [@loc_ranges[end_key].end - range_begin, length].min
533 [range_begin, range_length]