Method: Yast::FileUtilsClass#CheckAndCreatePath

Defined in:
library/general/src/modules/FileUtils.rb

#CheckAndCreatePath(pathvalue) ⇒ Boolean

Note:

This is an unstable API function and may change in the future

Checks whether the path (directory) exists and return a boolean value whether everything is OK or user accepted the behavior as despite some errors. If the directory doesn't exist, it offers to create it (and eventually creates it).

Parameters:

  • pathvalue (String)

    (directory)

Returns:

  • (Boolean)

    whether everything was OK or whether user decided to ignore eventual errors



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'library/general/src/modules/FileUtils.rb', line 263

def CheckAndCreatePath(pathvalue)
  check_path = pathvalue

  # remove the final slash
  # but never the last one "/"
  # bugzilla #203363
  check_path = Builtins.regexpsub(check_path, "^(.*)/$", "\\1") if Builtins.regexpmatch(check_path, "/$") && check_path != "/"
  Builtins.y2milestone("Checking existency of %1 path", check_path)

  # Directory (path) already exists
  if Exists(check_path)
    Builtins.y2milestone("Path %1 exists", check_path)
    # Directory (path) is a type 'directory'
    return true if IsDirectory(check_path)

    # Directory (path) is not a valid 'directory'
    Builtins.y2warning("Path %1 is not a directory", check_path)
    # Continue despite the error?
    Popup.ContinueCancel(
      Builtins.sformat(
        # TRANSLATORS: popup question (with continue / cancel buttons)
        # %1 is the filesystem path
        _(
          "Although the path %1 exists, it is not a directory.\nContinue or cancel the operation?"
        ),
        pathvalue
      )
    )
  # Directory (path) doesn't exist, trying to create it if wanted
  else
    Builtins.y2milestone("Path %1 does not exist", check_path)
    if Popup.YesNo(
      Builtins.sformat(
        # TRANSLATORS: question popup (with yes / no buttons). A user entered non-existent path
        # for a share, %1 is entered path
        _("The path %1 does not exist.\nCreate it now?"),
        pathvalue
      )
    )
      # Directory creation successful
      if Convert.to_boolean(SCR.Execute(path(".target.mkdir"), check_path))
        Builtins.y2milestone(
          "Directory %1 successfully created",
          check_path
        )
        true
        # Failed to create the directory
      else
        Builtins.y2warning("Failed to create directory %1", check_path)
        # Continue despite the error?
        Popup.ContinueCancel(
          Builtins.sformat(
            # TRANSLATORS: popup question (with continue / cancel buttons)
            # %1 is the name (path) of the directory
            _(
              "Failed to create the directory %1.\nContinue or cancel the current operation?"
            ),
            pathvalue
          )
        )
      end
      # User doesn't want to create the directory
    else
      Builtins.y2warning(
        "User doesn't want to create the directory %1",
        check_path
      )
      true
    end
  end
end