summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/build/artifacts/path.rb
blob: 9cd9b36c5f8d51f81b1769f7bddc9d5c7f3bd00a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module Gitlab
  module Ci
    module Build
      module Artifacts
        class Path
          def initialize(path)
            @path = path.dup.force_encoding('UTF-8')
          end

          def valid?
            nonzero? && utf8?
          end

          def directory?
            @path.end_with?('/')
          end

          def name
            @path.split('/').last.to_s
          end

          def nodes
            @path.count('/')
          end

          def to_s
            @path.tap do |path|
              unless nonzero?
                raise ArgumentError, 'Path contains zero byte character!'
              end

              unless utf8?
                raise ArgumentError, 'Path contains non-UTF-8 byte sequence!'
              end
            end
          end

          private

          def nonzero?
            @path.exclude?("\0")
          end

          def utf8?
            @path.valid_encoding?
          end
        end
      end
    end
  end
end