summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/build/artifacts/path.rb
blob: ec1cfc4f7b6e649e8ab1ca65b4fe13660c93d02b (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
52
53
# frozen_string_literal: true

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