Class: Yast::FileUtilsClass

Inherits:
Module
  • Object
show all
Defined in:
library/general/src/modules/FileUtils.rb

Instance Method Summary collapse

Instance Method Details

#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

#Chmod(modes, file, recursive) ⇒ Boolean

Changes access rights to a file/directory

Examples:

FileUtils::Chmod ( "go-rwx", "/etc/passwd", false) -> 'chmod go-rwx /etc/passwd'
FileUtils::Chmod ( "700", "/tmp", true) -> 'chmod 700 -R /tmp'

Parameters:

  • modes (String)

    ( e.g. '744' or 'u+x')

  • file (String)

    name

  • recursive, (Boolean)

    true if file (2nd param) is a directory

Returns:

  • (Boolean)

    true if succeeded



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'library/general/src/modules/FileUtils.rb', line 417

def Chmod(modes, file, recursive)
  Builtins.y2milestone(
    "Setting access rights of file %1 to %2",
    file,
    modes
  )

  cmd = Builtins.sformat(
    "/usr/bin/chmod %1 %2 %3",
    modes.shellescape,
    recursive ? "-R" : "",
    file.shellescape
  )

  retval = Convert.to_integer(SCR.Execute(path(".target.bash"), cmd))

  Builtins.y2error("Cannot chmod %1", file) if retval != 0

  retval == 0
end

#Chown(usergroup, file, recursive) ⇒ Boolean

Changes ownership of a file/directory

Examples:

FileUtils::Chown ( "somebody:somegroup", "/etc/passwd", false) -> 'chown somebody:somegroup /etc/passwd'
FileUtils::Chown ( "nobody:nogroup", "/tmp", true) -> 'chown nobody:nogroup -R /tmp'

Parameters:

  • string

    user and group name (in the form 'user:group')

  • file (String)

    name

  • recursive, (Boolean)

    true if file (2nd param) is a directory

Returns:

  • (Boolean)

    true if succeeded



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'library/general/src/modules/FileUtils.rb', line 385

def Chown(usergroup, file, recursive)
  Builtins.y2milestone(
    "Setting ownership of file %1 to %2",
    file,
    usergroup
  )

  cmd = Builtins.sformat(
    "/usr/bin/chown %1 %2 %3",
    usergroup.shellescape,
    recursive ? "-R" : "",
    file.shellescape
  )

  retval = Convert.to_integer(SCR.Execute(path(".target.bash"), cmd))

  Builtins.y2error("Cannot chown %1", file) if retval != 0

  retval == 0
end

#CleanupTempObject

Removes files and dirs created in all previous calls to MkTemp[File|Directory]



498
499
500
501
502
503
504
505
506
507
508
# File 'library/general/src/modules/FileUtils.rb', line 498

def CleanupTemp
  Builtins.foreach(@tmpfiles) do |one_file|
    Builtins.y2milestone("Removing %1", one_file)
    SCR.Execute(
      path(".target.bash"),
      Builtins.sformat("/bin/rm -rf %1", one_file.shellescape)
    )
  end

  nil
end

#Exists(target) ⇒ Object

Function which determines if the requested file/directory exists.

Examples:

FileUtils::Exists ("/tmp") -> true
FileUtils::Exists ("/var/log/messages") -> true
FileUtils::Exists ("/does-not-exist") -> false

Parameters:

  • string

    file name

Returns:

  • true if exists



53
54
55
56
57
58
59
# File 'library/general/src/modules/FileUtils.rb', line 53

def Exists(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))

  return true if info != {}

  false
end

#GetFileRealType(target) ⇒ String

Function returns the real type of requested file/directory. If the file is a link to any object, "link" is returned.

Examples:

FileUtils::GetFileRealType ("/var") -> "directory"
FileUtils::GetFileRealType ("/etc/passwd") -> "file"
FileUtils::GetFileRealType ("/does-not-exist") -> nil

Parameters:

  • string

    file name

Returns:

  • (String)

    file type (directory|regular|block|fifo|link|socket|chr_device), nil if doesn't exist



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'library/general/src/modules/FileUtils.rb', line 169

def GetFileRealType(target)
  info = Convert.to_map(SCR.Read(path(".target.lstat"), target))

  if Ops.get_boolean(info, "islink", false) == true
    "link"
  elsif Ops.get_boolean(info, "isdir", false) == true
    "directory"
  elsif Ops.get_boolean(info, "isreg", false) == true
    "regular"
  elsif Ops.get_boolean(info, "isblock", false) == true
    "block"
  elsif Ops.get_boolean(info, "isfifo", false) == true
    "fifo"
  elsif Ops.get_boolean(info, "issock", false) == true
    "socket"
  elsif Ops.get_boolean(info, "ischr", false) == true
    "chr_device"
  end
end

#GetFileType(target) ⇒ String

Function returns the type of requested file/directory. If the file is a link to any object, the object's type is returned.

Parameters:

  • string

    file name

Returns:

  • (String)

    fle type (directory|regular|block|fifo|link|socket|chr_device), nil if doesn't exist



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'library/general/src/modules/FileUtils.rb', line 194

def GetFileType(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))

  if Ops.get_boolean(info, "isdir", false) == true
    "directory"
  elsif Ops.get_boolean(info, "isreg", false) == true
    "regular"
  elsif Ops.get_boolean(info, "isblock", false) == true
    "block"
  elsif Ops.get_boolean(info, "isfifo", false) == true
    "fifo"
  elsif Ops.get_boolean(info, "issock", false) == true
    "socket"
  elsif Ops.get_boolean(info, "islink", false) == true
    "link"
  elsif Ops.get_boolean(info, "ischr", false) == true
    "chr_device"
  end
end

#GetOwnerGroupID(target) ⇒ Fixnum

Function which determines the owner's group ID of requested file/directory.

Examples:

FileUtils::GetOwnerGroupID ("/etc/passwd") -> 0
FileUtils::GetOwnerGroupID ("/does-not-exist") -> nil

Parameters:

  • string

    file name

Returns:

  • (Fixnum)

    GID, nil if doesn't exist



248
249
250
251
252
# File 'library/general/src/modules/FileUtils.rb', line 248

def GetOwnerGroupID(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))

  Ops.get_integer(info, "gid")
end

#GetOwnerUserID(target) ⇒ Fixnum

Function which determines the owner's user ID of requested file/directory.

Examples:

FileUtils::GetOwnerUserID ("/etc/passwd") -> 0
FileUtils::GetOwnerUserID ("/does-not-exist") -> nil

Parameters:

  • string

    file name

Returns:

  • (Fixnum)

    UID, nil if doesn't exist



234
235
236
237
238
# File 'library/general/src/modules/FileUtils.rb', line 234

def GetOwnerUserID(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))

  Ops.get_integer(info, "uid")
end

#GetSize(target) ⇒ Fixnum

Function which returns the size of requested file/directory.

Examples:

FileUtils::GetSize ("/var/log/YaST2") -> 12348
FileUtils::GetSize ("/does-not-exist") -> -1

Parameters:

  • string

    file name

Returns:

  • (Fixnum)

    file size, -1 if doesn't exist



222
223
224
# File 'library/general/src/modules/FileUtils.rb', line 222

def GetSize(target)
  Convert.to_integer(SCR.Read(path(".target.size"), target))
end

#IsBlock(target) ⇒ Object

Function which determines if the requested file/directory is a block file (device) or link to a block device.

Examples:

FileUtils::IsBlock ("/var") -> false
FileUtils::IsBlock ("/dev/sda2") -> true
FileUtils::IsBlock ("/dev/does-not-exist") -> nil

Parameters:

  • string

    file name

Returns:

  • true if it is a block file, nil if doesn't exist



105
106
107
108
109
110
# File 'library/general/src/modules/FileUtils.rb', line 105

def IsBlock(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))
  defaultv = (info != {}) ? false : nil

  Ops.get_boolean(info, "isblock", defaultv)
end

#IsCharacterDevice(target) ⇒ Object

Function which determines if the requested file/directory is a character device or link to a character device.

Parameters:

  • string

    file name

Returns:

  • true if it is a charcater device, nil if doesn't exist



152
153
154
155
156
157
# File 'library/general/src/modules/FileUtils.rb', line 152

def IsCharacterDevice(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))
  defaultv = (info != {}) ? false : nil

  Ops.get_boolean(info, "ischr", defaultv)
end

#IsDirectory(target) ⇒ Object

Function which determines if the requested file/directory is a directory or it is a link to a directory.

Examples:

FileUtils::IsDirectory ("/var") -> true
FileUtils::IsDirectory ("/var/log/messages") -> false
FileUtils::IsDirectory ("/does-not-exist") -> nil

Parameters:

  • string

    file name

Returns:

  • true if it is a directory, nil if doesn't exist



71
72
73
74
75
76
# File 'library/general/src/modules/FileUtils.rb', line 71

def IsDirectory(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))
  defaultv = (info != {}) ? false : nil

  Ops.get_boolean(info, "isdir", defaultv)
end

#IsFifo(target) ⇒ Object

Function which determines if the requested file/directory is a fifo or link to a fifo.

Parameters:

  • string

    file name

Returns:

  • true if it is a fifo, nil if doesn't exist



117
118
119
120
121
122
# File 'library/general/src/modules/FileUtils.rb', line 117

def IsFifo(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))
  defaultv = (info != {}) ? false : nil

  Ops.get_boolean(info, "isfifo", defaultv)
end

#IsFile(target) ⇒ Object

Function which determines if the requested file/directory is a regular file or it is a link to a regular file.

Examples:

FileUtils::IsFile ("/var") -> false
FileUtils::IsFile ("/var/log/messages") -> true
FileUtils::IsFile ("/does-not-exist") -> nil

Parameters:

  • string

    file name

Returns:

  • true if it is a regular file, nil if doesn't exist



88
89
90
91
92
93
# File 'library/general/src/modules/FileUtils.rb', line 88

def IsFile(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))
  defaultv = (info != {}) ? false : nil

  Ops.get_boolean(info, "isreg", defaultv)
end

Function which determines if the requested file/directory is a link.

Parameters:

  • string

    file name

Returns:

  • true if it is a link, nil if doesn't exist



128
129
130
131
132
133
# File 'library/general/src/modules/FileUtils.rb', line 128

def IsLink(target)
  info = Convert.to_map(SCR.Read(path(".target.lstat"), target))
  defaultv = (info != {}) ? false : nil

  Ops.get_boolean(info, "islink", defaultv)
end

#IsSocket(target) ⇒ Object

Function which determines if the requested file/directory is a socket or link to a socket.

Parameters:

  • string

    file name

Returns:

  • true if it is a socket, nil if doesn't exist



140
141
142
143
144
145
# File 'library/general/src/modules/FileUtils.rb', line 140

def IsSocket(target)
  info = Convert.to_map(SCR.Read(path(".target.stat"), target))
  defaultv = (info != {}) ? false : nil

  Ops.get_boolean(info, "issock", defaultv)
end

#mainObject



36
37
38
39
40
41
42
# File 'library/general/src/modules/FileUtils.rb', line 36

def main
  textdomain "base"
  Yast.import "Popup"
  Yast.import "String"

  @tmpfiles = []
end

#MD5sum(target) ⇒ String

Function return the MD5 sum of the file.

Examples:

FileUtils::MD5sum ("/etc/passwd") -> "74855f6ac9bf728fccec4792d1dba828"
FileUtils::MD5sum ("/does-not-exist") -> nil

Parameters:

  • string

    file name

Returns:

  • (String)

    MD5 sum of the file, nil if doesn't exist



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'library/general/src/modules/FileUtils.rb', line 343

def MD5sum(target)
  if !Exists(target)
    Builtins.y2error("File %1 doesn't exist", target)
    return nil
  end

  if !IsFile(target)
    Builtins.y2error("Not a file %1", target)
    return nil
  end

  cmd = Builtins.sformat("/usr/bin/md5sum '%1'", String.Quote(target))
  cmd_out = Convert.to_map(SCR.Execute(path(".target.bash_output"), cmd))

  if Ops.get_integer(cmd_out, "exit", -1) != 0
    Builtins.y2error("Command >%1< returned %2", cmd, cmd_out)
    return nil
  end

  filemd5 = Ops.get_string(cmd_out, "stdout", "")
  if Builtins.regexpmatch(filemd5, "[^ \t]+[ \t]+.*$")
    # Format: '19ea7ea41de37314f71c6849ddd259d5 /the/file'
    filemd5 = Builtins.regexpsub(filemd5, "^([^ \t]+)[ \t]+.*$", "\\1")
  else
    Builtins.y2warning("Strange md5out: '%1'", filemd5)
    return nil
  end

  filemd5
end

#MkTempDirectory(template, usergroup, modes) ⇒ Object

The same as MkTempFile, for directories ('mktemp -d ... '). See above

Examples:

FileUtils::MkTempDirectory ( "/tmp/tmpdir.XXXX", "nobody:nogroup", "go+x")


492
493
494
# File 'library/general/src/modules/FileUtils.rb', line 492

def MkTempDirectory(template, usergroup, modes)
  MkTempInternal(template, usergroup, modes, true)
end

#MkTempFile(template, usergroup, modes) ⇒ String

Creates temporary file

Examples:

FileUtils::MkTempFile ( "/tmp/tmpfile.XXXX", "somebody:somegroup", "744")

Parameters:

  • template (String)

    for file name e.g. 'tmp.XXXX'

  • string

    tmp file owner in a form 'user:group'

  • string

    tmp file access rights

Returns:

  • (String)

    resulting file name or nil on failure



484
485
486
# File 'library/general/src/modules/FileUtils.rb', line 484

def MkTempFile(template, usergroup, modes)
  MkTempInternal(template, usergroup, modes, false)
end

#MkTempInternal(template, usergroup, modes, directory) ⇒ Object



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'library/general/src/modules/FileUtils.rb', line 438

def MkTempInternal(template, usergroup, modes, directory)
  mktemp = Builtins.sformat(
    "/bin/mktemp %1 %2",
    directory ? "-d" : "",
    template.shellescape
  )

  cmd_out = Convert.to_map(SCR.Execute(path(".target.bash_output"), mktemp))
  if Ops.get_integer(cmd_out, "exit", -1) != 0
    Builtins.y2error("Error creating temporary file: %1", cmd_out)
    return nil
  end

  tmpfile = Ops.get(
    Builtins.splitstring(Ops.get_string(cmd_out, "stdout", ""), "\n"),
    0,
    ""
  )

  if tmpfile.nil? || tmpfile == ""
    Builtins.y2error(
      "Error creating temporary file: %1 - empty output",
      cmd_out
    )
    return nil
  end

  if !Chown(usergroup, tmpfile, directory) ||
      !Chmod(modes, tmpfile, directory)
    return nil
  end

  @tmpfiles = Builtins.add(@tmpfiles, tmpfile)
  tmpfile
end