Class: Gitlab::Ci::Build::Artifacts::Metadata::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/ci/build/artifacts/metadata/entry.rb

Overview

Class that represents an entry (path and metadata) to a file or directory in GitLab CI Build Artifacts binary file / archive

This is IO-operations safe class, that does similar job to Ruby’s Pathname but without the risk of accessing filesystem.

This class is working only with UTF-8 encoded paths.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, entries) ⇒ Entry

Returns a new instance of Entry.



21
22
23
24
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 21

def initialize(path, entries)
  @entries = entries
  @path = Artifacts::Path.new(path)
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



18
19
20
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 18

def entries
  @entries
end

#nameObject



56
57
58
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 56

def name
  @name || @path.name
end

Instance Method Details

#==(other) ⇒ Object



118
119
120
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 118

def ==(other)
  path == other.path && @entries == other.entries
end

#basenameObject



52
53
54
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 52

def basename
  directory? && !blank_node? ? name + '/' : name
end

#blank_node?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 93

def blank_node?
  @path.to_s.empty? # "" is considered to be './'
end

#blobObject



36
37
38
39
40
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 36

def blob
  return unless file?

  @blob ||= Blob.decorate(::Ci::ArtifactBlob.new(self), nil)
end

#childrenObject



60
61
62
63
64
65
66
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 60

def children
  return [] unless directory?
  return @children if @children

  child_pattern = %r{^#{Regexp.escape(@path.to_s)}[^/]+/?$}
  @children = select_entries { |path| path =~ child_pattern }
end

#directories(opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 68

def directories(opts = {})
  return [] unless directory?

  dirs = children.select(&:directory?)
  return dirs unless has_parent? && opts[:parent]

  dotted_parent = parent
  dotted_parent.name = '..'
  dirs.prepend(dotted_parent)
end

#directory?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 28

def directory?
  blank_node? || @path.directory?
end

#exists?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 97

def exists?
  blank_node? || @entries.include?(@path.to_s)
end

#file?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 32

def file?
  !directory?
end

#filesObject



79
80
81
82
83
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 79

def files
  return [] unless directory?

  children.select(&:file?)
end

#has_parent?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 42

def has_parent?
  nodes > 0
end

#inspectObject



122
123
124
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 122

def inspect
  "#{self.class.name}: #{self}"
end

#metadataObject



85
86
87
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 85

def 
  @entries[@path.to_s] || {}
end

#nodesObject



89
90
91
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 89

def nodes
  @path.nodes + (file? ? 1 : 0)
end

#parentObject



46
47
48
49
50
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 46

def parent
  return unless has_parent?

  self.class.new(@path.to_s.chomp(basename), @entries)
end

#pathObject

rubocop: enable CodeReuse/ActiveRecord



110
111
112
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 110

def path
  @path.to_s
end

#to_sObject



114
115
116
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 114

def to_s
  @path.to_s
end

#total_sizeObject

rubocop: disable CodeReuse/ActiveRecord



102
103
104
105
106
107
# File 'lib/gitlab/ci/build/artifacts/metadata/entry.rb', line 102

def total_size
  descendant_pattern = /^#{Regexp.escape(@path.to_s)}/
  entries.sum do |path, entry|
    (entry[:size] if descendant_pattern.match?(path)).to_i
  end
end