Class: Yast::FileUtilsClass
- Inherits:
-
Module
- Object
- Module
- Yast::FileUtilsClass
- Defined in:
- ../../library/general/src/modules/FileUtils.rb
Instance Method Summary (collapse)
-
- (Boolean) CheckAndCreatePath(pathvalue)
Checks whether the path (directory) exists and return a boolean value whether everything is OK or user accepted the behavior as despite some errors.
-
- (Boolean) Chmod(modes, file, recursive)
Changes access rights to a file/directory.
-
- (Boolean) Chown(usergroup, file, recursive)
Changes ownership of a file/directory.
-
- (Object) CleanupTemp
Removes files and dirs created in all previous calls to MkTemp.
-
- (Object) Exists(target)
Function which determines if the requested file/directory exists.
-
- (String) GetFileRealType(target)
Function returns the real type of requested file/directory.
-
- (String) GetFileType(target)
Function returns the type of requested file/directory.
-
- (Fixnum) GetOwnerGroupID(target)
Function which determines the owner's group ID of requested file/directory.
-
- (Fixnum) GetOwnerUserID(target)
Function which determines the owner's user ID of requested file/directory.
-
- (Fixnum) GetSize(target)
Function which returns the size of requested file/directory.
-
- (Object) IsBlock(target)
Function which determines if the requested file/directory is a block file (device) or link to a block device.
-
- (Object) IsCharacterDevice(target)
Function which determines if the requested file/directory is a character device or link to a character device.
-
- (Object) IsDirectory(target)
Function which determines if the requested file/directory is a directory or it is a link to a directory.
-
- (Object) IsFifo(target)
Function which determines if the requested file/directory is a fifo or link to a fifo.
-
- (Object) IsFile(target)
Function which determines if the requested file/directory is a regular file or it is a link to a regular file.
-
- (Object) IsLink(target)
Function which determines if the requested file/directory is a link.
-
- (Object) IsSocket(target)
Function which determines if the requested file/directory is a socket or link to a socket.
- - (Object) main
-
- (String) MD5sum(target)
Function return the MD5 sum of the file.
-
- (Object) MkTempDirectory(template, usergroup, modes)
The same as MkTempFile, for directories ('mktemp -d … ').
-
- (String) MkTempFile(template, usergroup, modes)
Creates temporary file.
- - (Object) MkTempInternal(template, usergroup, modes, directory)
Instance Method Details
- (Boolean) CheckAndCreatePath(pathvalue)
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).
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 334 335 336 337 338 339 340 341 |
# File '../../library/general/src/modules/FileUtils.rb', line 267 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'
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File '../../library/general/src/modules/FileUtils.rb', line 425 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'
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File '../../library/general/src/modules/FileUtils.rb', line 393 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
506 507 508 509 510 511 512 513 514 515 516 |
# File '../../library/general/src/modules/FileUtils.rb', line 506 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
54 55 56 57 58 59 |
# File '../../library/general/src/modules/FileUtils.rb', line 54 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# 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 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.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File '../../library/general/src/modules/FileUtils.rb', line 196 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
252 253 254 255 256 |
# File '../../library/general/src/modules/FileUtils.rb', line 252 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
238 239 240 241 242 |
# File '../../library/general/src/modules/FileUtils.rb', line 238 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
226 227 228 |
# File '../../library/general/src/modules/FileUtils.rb', line 226 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
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 |
- (Object) IsCharacterDevice(target)
Function which determines if the requested file/directory is a character device or link to a character device.
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 |
- (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
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 |
- (Object) IsFifo(target)
Function which determines if the requested file/directory is a fifo or link to a fifo.
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 |
- (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
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 |
- (Object) IsLink(target)
Function which determines if the requested file/directory is a link.
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 |
- (Object) IsSocket(target)
Function which determines if the requested file/directory is a socket or link to a socket.
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 |
- (Object) main
37 38 39 40 41 42 43 |
# 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
351 352 353 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 |
# File '../../library/general/src/modules/FileUtils.rb', line 351 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”)
500 501 502 |
# File '../../library/general/src/modules/FileUtils.rb', line 500 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”)
492 493 494 |
# File '../../library/general/src/modules/FileUtils.rb', line 492 def MkTempFile(template, usergroup, modes) MkTempInternal(template, usergroup, modes, false) end |
- (Object) MkTempInternal(template, usergroup, modes, directory)
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 473 474 475 476 477 478 479 480 |
# File '../../library/general/src/modules/FileUtils.rb', line 446 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 |