Class: Yast::FileUtilsClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) CheckAndCreatePath(pathvalue)

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



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
334
335
336
337
338
339
340
341
342
343
344
# File '../../library/general/src/modules/FileUtils.rb', line 270

def CheckAndCreatePath(pathvalue)
  check_path = pathvalue

  # remove the final slash
  # but never the last one "/"
  # bugzilla #203363
  if Builtins.regexpmatch(check_path, "/$") && check_path != "/"
    check_path = Builtins.regexpsub(check_path, "^(.*)/$", "\\1")
  end
  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'
    if IsDirectory(check_path)
      return true 
      # Directory (path) is not a valid 'directory'
    else
      Builtins.y2warning("Path %1 is not a directory", check_path)
      # Continue despite the error?
      return 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?\n"
          ),
          pathvalue
        )
      )
    end 
    # 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?\n"),
          pathvalue
        )
      )
      # Directory creation successful
      if Convert.to_boolean(SCR.Execute(path(".target.mkdir"), check_path))
        Builtins.y2milestone(
          "Directory %1 successfully created",
          check_path
        )
        return true 
        # Failed to create the directory
      else
        Builtins.y2warning("Failed to create directory %1", check_path)
        # Continue despite the error?
        return 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?\n"
            ),
            pathvalue
          )
        )
      end 
      # User doesn't want to create the directory
    else
      Builtins.y2warning(
        "User doesn't want to create the directory %1",
        check_path
      )
      return true
    end
  end
end

- (Boolean) Chmod(modes, file, recursive)

Changes access rights to a file/directory

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



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File '../../library/general/src/modules/FileUtils.rb', line 428

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

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

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

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

  retval == 0
end

- (Boolean) Chown(usergroup, file, recursive)

Changes ownership of a file/directory

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



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File '../../library/general/src/modules/FileUtils.rb', line 396

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

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

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

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

  retval == 0
end

- (Object) CleanupTemp

Removes files and dirs created in all previous calls to MkTemp



509
510
511
512
513
514
515
516
517
518
519
# File '../../library/general/src/modules/FileUtils.rb', line 509

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)
    )
  end

  nil
end

- (Object) Exists(target)

Function which determines if the requested file/directory exists.

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

Parameters:

  • string

    file name

Returns:

  • true if exists



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

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

  return true if info != {}
  false
end

- (String) GetFileRealType(target)

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

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



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

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

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

- (String) GetFileType(target)

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File '../../library/general/src/modules/FileUtils.rb', line 197

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


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

- (Fixnum) GetOwnerGroupID(target)

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

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

Parameters:

  • string

    file name

Returns:

  • (Fixnum)

    GID, nil if doesn't exist



254
255
256
257
258
# File '../../library/general/src/modules/FileUtils.rb', line 254

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

  Ops.get_integer(info, "gid")
end

- (Fixnum) GetOwnerUserID(target)

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

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

Parameters:

  • string

    file name

Returns:

  • (Fixnum)

    UID, nil if doesn't exist



240
241
242
243
244
# File '../../library/general/src/modules/FileUtils.rb', line 240

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

  Ops.get_integer(info, "uid")
end

- (Fixnum) GetSize(target)

Function which returns the size of requested file/directory.

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



228
229
230
# File '../../library/general/src/modules/FileUtils.rb', line 228

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

- (Object) IsBlock(target)

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

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



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

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

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

- (Object) IsCharacterDevice(target)

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



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

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

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

- (Object) IsDirectory(target)

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

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



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

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

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

- (Object) IsFifo(target)

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



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

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

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

- (Object) IsFile(target)

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

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



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

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



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

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

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

- (Object) IsSocket(target)

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



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

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

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

- (Object) main



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

def main

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

  @tmpfiles = []
end

- (String) MD5sum(target)

Function return the MD5 sum of the file.

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



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File '../../library/general/src/modules/FileUtils.rb', line 354

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("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

- (Object) MkTempDirectory(template, usergroup, modes)

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

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



503
504
505
# File '../../library/general/src/modules/FileUtils.rb', line 503

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

- (String) MkTempFile(template, usergroup, modes)

Creates temporary file

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



495
496
497
# File '../../library/general/src/modules/FileUtils.rb', line 495

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

- (Object) MkTempInternal(template, usergroup, modes, directory)



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File '../../library/general/src/modules/FileUtils.rb', line 449

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

  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