-
Notifications
You must be signed in to change notification settings - Fork 43
/
FileUtils.rb
542 lines (480 loc) · 17.9 KB
/
FileUtils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
334
335
336
337
338
339
340
341
342
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# encoding: utf-8
# ***************************************************************************
#
# Copyright (c) 2002 - 2012 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#
# ***************************************************************************
# File: modules/FileUtils.ycp
# Package: YaST2
# Authors: Lukas Ocilka <lukas.ocilka@suse.cz>
# Summary: Module for getting information about files and directories.
# Their types and sizes and functions for checking, creating and
# removing them.
# Flags: Stable
#
# $Id$
require "yast"
module Yast
class FileUtilsClass < Module
def main
textdomain "base"
Yast.import "Popup"
Yast.import "String"
@tmpfiles = []
end
# Function which determines if the requested file/directory exists.
#
# @return true if exists
# @param string file name
#
# @example
# FileUtils::Exists ("/tmp") -> true
# FileUtils::Exists ("/var/log/messages") -> true
# FileUtils::Exists ("/does-not-exist") -> false
def Exists(target)
info = Convert.to_map(SCR.Read(path(".target.stat"), target))
return true if info != {}
false
end
# Function which determines if the requested file/directory is a directory
# or it is a link to a directory.
#
# @return true if it is a directory, nil if doesn't exist
# @param string file name
#
# @example
# FileUtils::IsDirectory ("/var") -> true
# FileUtils::IsDirectory ("/var/log/messages") -> false
# FileUtils::IsDirectory ("/does-not-exist") -> nil
def IsDirectory(target)
info = Convert.to_map(SCR.Read(path(".target.stat"), target))
defaultv = info != {} ? false : nil
Ops.get_boolean(info, "isdir", defaultv)
end
# Function which determines if the requested file/directory is a regular file
# or it is a link to a regular file.
#
# @return true if it is a regular file, nil if doesn't exist
# @param string file name
#
# @example
# FileUtils::IsFile ("/var") -> false
# FileUtils::IsFile ("/var/log/messages") -> true
# FileUtils::IsFile ("/does-not-exist") -> nil
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 block file (device)
# or link to a block device.
#
# @return true if it is a block file, nil if doesn't exist
# @param string file name
#
# @example
# FileUtils::IsBlock ("/var") -> false
# FileUtils::IsBlock ("/dev/sda2") -> true
# FileUtils::IsBlock ("/dev/does-not-exist") -> nil
def IsBlock(target)
info = Convert.to_map(SCR.Read(path(".target.stat"), target))
defaultv = info != {} ? false : nil
Ops.get_boolean(info, "isblock", defaultv)
end
# Function which determines if the requested file/directory is a fifo
# or link to a fifo.
#
# @return true if it is a fifo, nil if doesn't exist
# @param string file name
def IsFifo(target)
info = Convert.to_map(SCR.Read(path(".target.stat"), target))
defaultv = info != {} ? false : nil
Ops.get_boolean(info, "isfifo", defaultv)
end
# Function which determines if the requested file/directory is a link.
#
# @return true if it is a link, nil if doesn't exist
# @param string file name
def IsLink(target)
info = Convert.to_map(SCR.Read(path(".target.lstat"), target))
defaultv = info != {} ? false : nil
Ops.get_boolean(info, "islink", defaultv)
end
# Function which determines if the requested file/directory is a socket
# or link to a socket.
#
# @return true if it is a socket, nil if doesn't exist
# @param string file name
def IsSocket(target)
info = Convert.to_map(SCR.Read(path(".target.stat"), target))
defaultv = info != {} ? false : nil
Ops.get_boolean(info, "issock", defaultv)
end
# Function which determines if the requested file/directory is
# a character device or link to a character device.
#
# @return true if it is a charcater device, nil if doesn't exist
# @param string file name
def IsCharacterDevice(target)
info = Convert.to_map(SCR.Read(path(".target.stat"), target))
defaultv = info != {} ? false : nil
Ops.get_boolean(info, "ischr", defaultv)
end
# Function returns the real type of requested file/directory.
# If the file is a link to any object, "link" is returned.
#
# @return [String] file type (directory|regular|block|fifo|link|socket|chr_device), nil if doesn't exist
# @param string file name
#
# @example
# FileUtils::GetFileRealType ("/var") -> "directory"
# FileUtils::GetFileRealType ("/etc/passwd") -> "file"
# FileUtils::GetFileRealType ("/does-not-exist") -> nil
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
# Function returns the type of requested file/directory.
# If the file is a link to any object, the object's type is returned.
#
# @return [String] fle type (directory|regular|block|fifo|link|socket|chr_device), nil if doesn't exist
# @param string file name
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
# Function which returns the size of requested file/directory.
#
# @return [Fixnum] file size, -1 if doesn't exist
# @param string file name
#
# @example
# FileUtils::GetSize ("/var/log/YaST2") -> 12348
# FileUtils::GetSize ("/does-not-exist") -> -1
def GetSize(target)
Convert.to_integer(SCR.Read(path(".target.size"), target))
end
# Function which determines the owner's user ID of requested file/directory.
#
# @return [Fixnum] UID, nil if doesn't exist
# @param string file name
#
# @example
# FileUtils::GetOwnerUserID ("/etc/passwd") -> 0
# FileUtils::GetOwnerUserID ("/does-not-exist") -> nil
def GetOwnerUserID(target)
info = Convert.to_map(SCR.Read(path(".target.stat"), target))
Ops.get_integer(info, "uid")
end
# Function which determines the owner's group ID of requested file/directory.
#
# @return [Fixnum] GID, nil if doesn't exist
# @param string file name
#
# @example
# FileUtils::GetOwnerGroupID ("/etc/passwd") -> 0
# FileUtils::GetOwnerGroupID ("/does-not-exist") -> nil
def GetOwnerGroupID(target)
info = Convert.to_map(SCR.Read(path(".target.stat"), target))
Ops.get_integer(info, "gid")
end
# 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).
#
# @param [String] pathvalue (directory)
# @return [Boolean] whether everything was OK or whether user decided to ignore eventual errors
#
# @note This is an unstable API function and may change in the future
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
# Function return the MD5 sum of the file.
#
# @return [String] MD5 sum of the file, nil if doesn't exist
# @param string file name
#
# @example
# FileUtils::MD5sum ("/etc/passwd") -> "74855f6ac9bf728fccec4792d1dba828"
# FileUtils::MD5sum ("/does-not-exist") -> nil
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
# Changes ownership of a file/directory
#
# @return [Boolean] true if succeeded
# @param string user and group name (in the form 'user:group')
# @param [String] file name
# @param [Boolean] recursive, true if file (2nd param) is a directory
#
# @example
# FileUtils::Chown ( "somebody:somegroup", "/etc/passwd", false) -> 'chown somebody:somegroup /etc/passwd'
# FileUtils::Chown ( "nobody:nogroup", "/tmp", true) -> 'chown nobody:nogroup -R /tmp'
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
# Changes access rights to a file/directory
#
# @return [Boolean] true if succeeded
# @param [String] modes ( e.g. '744' or 'u+x')
# @param [String] file name
# @param [Boolean] recursive, true if file (2nd param) is a directory
#
# @example
# FileUtils::Chmod ( "go-rwx", "/etc/passwd", false) -> 'chmod go-rwx /etc/passwd'
# FileUtils::Chmod ( "700", "/tmp", true) -> 'chmod 700 -R /tmp'
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
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
# Creates temporary file
#
# @return [String] resulting file name or nil on failure
# @param [String] template for file name e.g. 'tmp.XXXX'
# @param string tmp file owner in a form 'user:group'
# @param string tmp file access rights
#
# @example
# FileUtils::MkTempFile ( "/tmp/tmpfile.XXXX", "somebody:somegroup", "744")
def MkTempFile(template, usergroup, modes)
MkTempInternal(template, usergroup, modes, false)
end
# The same as MkTempFile, for directories ('mktemp -d ... '). See above
#
# @example
# FileUtils::MkTempDirectory ( "/tmp/tmpdir.XXXX", "nobody:nogroup", "go+x")
def MkTempDirectory(template, usergroup, modes)
MkTempInternal(template, usergroup, modes, true)
end
# Removes files and dirs created in all previous calls to MkTemp[File|Directory]
#
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
publish function: :Exists, type: "boolean (string)"
publish function: :IsDirectory, type: "boolean (string)"
publish function: :IsFile, type: "boolean (string)"
publish function: :IsBlock, type: "boolean (string)"
publish function: :IsFifo, type: "boolean (string)"
publish function: :IsLink, type: "boolean (string)"
publish function: :IsSocket, type: "boolean (string)"
publish function: :IsCharacterDevice, type: "boolean (string)"
publish function: :GetFileRealType, type: "string (string)"
publish function: :GetFileType, type: "string (string)"
publish function: :GetSize, type: "integer (string)"
publish function: :GetOwnerUserID, type: "integer (string)"
publish function: :GetOwnerGroupID, type: "integer (string)"
publish function: :CheckAndCreatePath, type: "boolean (string)"
publish function: :MD5sum, type: "string (string)"
publish function: :Chown, type: "boolean (string, string, boolean)"
publish function: :Chmod, type: "boolean (string, string, boolean)"
publish function: :MkTempFile, type: "string (string, string, string)"
publish function: :MkTempDirectory, type: "string (string, string, string)"
publish function: :CleanupTemp, type: "void ()"
end
FileUtils = FileUtilsClass.new
FileUtils.main
end