• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yast / yast-installation / 16913068408

12 Aug 2025 03:13PM UTC coverage: 40.949% (+0.02%) from 40.928%
16913068408

push

github

web-flow
Merge pull request #1136 from yast/drop_initviocons

Drop initviocons

4531 of 11065 relevant lines covered (40.95%)

6.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.21
/src/modules/SystemFilesCopy.rb
1
# ------------------------------------------------------------------------------
2
# Copyright (c) 2006-2012 Novell, Inc. All Rights Reserved.
3
#
4
#
5
# This program is free software; you can redistribute it and/or modify it under
6
# the terms of version 2 of the GNU General Public License as published by the
7
# Free Software Foundation.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License along with
14
# this program; if not, contact Novell, Inc.
15
#
16
# To contact Novell about this file by physical or electronic mail, you may find
17
# current contact information at www.novell.com.
18
# ------------------------------------------------------------------------------
19

20
# File:  modules/SystemFilesCopy.ycp
21
# Package:  Installation
22
# Summary:  Functionality for copying files from another systems
23
# Authors:  Lukas Ocilka <locilka@suse.cz>
24
#
25
# $Id$
26
#
27
# Functionality for copying files from any not-mounted systems
28
# to inst-sys and from inst-sys to just-installed system.
29
require "yast"
1✔
30

31
module Yast
1✔
32
  class SystemFilesCopyClass < Module
1✔
33
    def main
1✔
34
      textdomain "installation"
1✔
35

36
      Yast.import "Directory"
1✔
37
      Yast.import "FileUtils"
1✔
38
      Yast.import "String"
1✔
39
      Yast.import "Installation"
1✔
40
      Yast.import "ProductFeatures"
1✔
41
      Yast.import "Stage"
1✔
42
      Yast.import "InstData"
1✔
43

44
      # --> Variables
45

46
      @already_initialized = false
1✔
47

48
      # max tries when creating a temporary mount-directory
49
      @counter_max = 10
1✔
50
    end
51

52
    # Checks whether the directory exists and creates it if it is missing
53
    # If the path exists but it is not a directory, it tries to create another
54
    # directory and returns its name. 'nil' is returned when everythig fails.
55
    #
56
    # @param create_directory [String] directory to create
57
    def CreateDirectoryIfMissing(create_directory)
1✔
58
      # path already exists
59
      if FileUtils.Exists(create_directory)
×
60
        # exists as a directory
61
        if FileUtils.IsDirectory(create_directory)
×
62
          Builtins.y2milestone("Directory %1 already exists", create_directory)
×
63
          create_directory
×
64
          # exists but it's not a directory
65
        else
66
          Builtins.y2warning("Path %1 is not a directory", create_directory)
×
67
          new_dir = nil
×
68

69
          while new_dir.nil? && Ops.greater_than(@counter_max, 0)
×
70
            @counter_max = Ops.subtract(@counter_max, 1)
×
71
            create_directory = Ops.add(create_directory, "x")
×
72
            new_dir = CreateDirectoryIfMissing(create_directory)
×
73
          end
74

75
          new_dir
×
76
        end
77

78
        # path doesn't exist
79
      else
80
        SCR.Execute(path(".target.mkdir"), create_directory)
×
81
        # created successfully
82
        if FileUtils.Exists(create_directory)
×
83
          Builtins.y2milestone("Directory %1 created", create_directory)
×
84
          create_directory
×
85
          # cannot create
86
        else
87
          Builtins.y2error("Cannot create path %1", create_directory)
×
88
          nil
×
89
        end
90
      end
91
    end
92

93
    # Sets and creates a temporary directory for files to lay
94
    # in inst-sys until they're copied to the installed system.
95
    # Sets and creates a temporary directory that is used for
96
    # mounting partitions when copying files from them.
97
    def Initialize
1✔
98
      return true if @already_initialized
×
99

100
      # everything is fine
101
      @already_initialized = true
×
102
      true
×
103
    end
104

105
    # internal functions for SaveInstSysContent -->
106

107
    def AdjustDirectoryPath(directory_path)
1✔
108
      dir_path_list = Builtins.splitstring(directory_path, "/")
×
109

110
      dir_path_list = Builtins.filter(dir_path_list) { |one_dir| one_dir != "" }
×
111

112
      directory_path = Builtins.mergestring(dir_path_list, "/")
×
113
      Builtins.sformat("/%1/", directory_path)
×
114
    end
115

116
    def CopyFilesFromDirToDir(dir_from, dir_to)
1✔
117
      cmd = Builtins.sformat(
×
118
        "mkdir -p '%2' && cp -ar '%1.' '%2'",
119
        String.Quote(dir_from),
120
        String.Quote(dir_to)
121
      )
122
      cmd_run = Convert.to_map(WFM.Execute(path(".local.bash_output"), cmd))
×
123

124
      if Ops.get_integer(cmd_run, "exit", -1) == 0
×
125
        Builtins.y2milestone("Command >%1< succeeded", cmd)
×
126
        true
×
127
      else
128
        Builtins.y2error("Command %1 failed %2", cmd, cmd_run)
×
129
        false
×
130
      end
131
    end
132

133
    # <-- internal functions for SaveInstSysContent
134

135
    # Function reads <globals><save_instsys_content /></globals>
136
    # from control file and copies all content from inst-sys to
137
    # the just installed system.
138
    #
139
    # This function needs to be called in the inst-sys (first stage)
140
    # just before the disk is unmounted.
141
    #
142
    # FATE #301937
143
    #
144
    #
145
    # **Structure:**
146
    #
147
    #
148
    #      <globals>
149
    #          <save_instsys_content config:type="list">
150
    #              <save_instsys_item>
151
    #                  <instsys_directory>/root/</instsys_directory>
152
    #                  <system_directory>/root/inst-sys/</system_directory>
153
    #              </save_instsys_item>
154
    #          </save_instsys_content>
155
    #      </globals>
156
    def SaveInstSysContent
1✔
157
      if !Stage.initial
21✔
158
        Builtins.y2error(
21✔
159
          "This function can be called in the initial stage only!"
160
        )
161
        return false
21✔
162
      end
163

164
      globals_features = ProductFeatures.GetSection("globals")
×
165

166
      if globals_features.nil?
×
167
        Builtins.y2warning("No <globals> defined")
×
168
        return false
×
169
      elsif Ops.get_list(globals_features, "save_instsys_content", []) == []
×
170
        Builtins.y2milestone("No items to copy from inst-sys")
×
171
        return true
×
172
      end
173

174
      save_content = Convert.convert(
×
175
        Ops.get(globals_features, "save_instsys_content"),
176
        from: "any",
177
        to:   "list <map <string, string>>"
178
      )
179
      if save_content.nil?
×
180
        Builtins.y2error(
×
181
          "Cannot save inst-sys content: %1",
182
          Ops.get(globals_features, "save_instsys_content")
183
        )
184
        return false
×
185
      end
186

187
      Builtins.y2milestone("Save inst-sys content: %1", save_content)
×
188
      Builtins.foreach(save_content) do |copy_item|
×
189
        if Ops.get(copy_item, "instsys_directory", "") == ""
×
190
          Builtins.y2error("Error: %1 is not defined", "instsys_directory")
×
191
          next
×
192
        elsif Ops.get(copy_item, "system_directory", "") == ""
×
193
          Builtins.y2error("Error: %1 is not defined", "system_directory")
×
194
          next
×
195
        end
196
        dir_from = Builtins.sformat(
×
197
          "/%1/",
198
          Ops.get(copy_item, "instsys_directory", "")
199
        )
200
        dir_to = Builtins.sformat(
×
201
          "/%1/%2/",
202
          Installation.destdir,
203
          Ops.get(copy_item, "system_directory", "")
204
        )
205
        dir_from = AdjustDirectoryPath(dir_from)
×
206
        dir_to = AdjustDirectoryPath(dir_to)
×
207
        if dir_from == dir_to
×
208
          Builtins.y2error(
×
209
            "Dir 'from (%1)' and 'to (%2)' mustn't be the same",
210
            dir_from,
211
            dir_to
212
          )
213
          next
×
214
        end
215
        # search ("/a", "/b") -> nil
216
        # search ("/a/b", "/a") -> 0
217
        # search ("/a/b/", "/b/") -> 2
218
        position_str_in_str = Builtins.search(dir_to, dir_from)
×
219
        if !position_str_in_str.nil? && position_str_in_str == 0
×
220
          Builtins.y2error(
×
221
            "Cannot copy a directory content to itself (%1 -> %2)",
222
            dir_from,
223
            dir_to
224
          )
225
          next
×
226
        end
227
        CopyFilesFromDirToDir(dir_from, dir_to)
×
228
      end
229

230
      true
×
231
    end
232

233
    publish function: :CreateDirectoryIfMissing, type: "string (string)"
1✔
234
    publish function: :SaveInstSysContent, type: "boolean ()"
1✔
235
  end
236

237
  SystemFilesCopy = SystemFilesCopyClass.new
1✔
238
  SystemFilesCopy.main
1✔
239
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc