Class: MiqFS

Inherits:
Object
  • Object
show all
Defined in:
lib/fs/MiqFS/MiqFS.rb

Direct Known Subclasses

MiqMountManager

Constant Summary collapse

GLOB_CHARS =
'*?[{'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fsm, dobj) ⇒ MiqFS

Returns a new instance of MiqFS.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fs/MiqFS/MiqFS.rb', line 33

def initialize(fsm, dobj)
  extend(fsm)
  @dobj = dobj
  @cwd = "/"
  @fsId = ''
  @volName = ''
  fs_init
  @fsId = @fsId.to_s

  @findEachPrune = false
  @findEachYield = false
end

Instance Attribute Details

#dobjObject

Returns the value of attribute dobj.



7
8
9
# File 'lib/fs/MiqFS/MiqFS.rb', line 7

def dobj
  @dobj
end

#fsIdObject

Returns the value of attribute fsId.



7
8
9
# File 'lib/fs/MiqFS/MiqFS.rb', line 7

def fsId
  @fsId
end

#fsTypeObject

Returns the value of attribute fsType.



7
8
9
# File 'lib/fs/MiqFS/MiqFS.rb', line 7

def fsType
  @fsType
end

#volNameObject

Returns the value of attribute volName.



7
8
9
# File 'lib/fs/MiqFS/MiqFS.rb', line 7

def volName
  @volName
end

Class Method Details

.getFS(dobj, probes = nil) ⇒ Object

Class method to instantiate a MiqFS object to handle the file system type detected on the given disk.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fs/MiqFS/MiqFS.rb', line 14

def self.getFS(dobj, probes = nil)
  return(nil)   if dobj.fs == :none
  return(dobj.fs) if dobj.fs

  if probes.nil? && dobj.dInfo.localDev
    require 'fs/modules/NativeFS'
    return(new(NativeFS, dobj)) if NativeFS.supported?(dobj)
    return(nil)
  end

  if (fsm = FsProbe.getFsMod(dobj, probes))
    fs = new(fsm, dobj)
    dobj.fs = fs
    return(fs)
  end
  dobj.fs = :none if probes.nil? # only if we performed a full probe
  (nil)
end

Instance Method Details

#chdir(dir) ⇒ Object

Directory instance methods



60
61
62
63
64
# File 'lib/fs/MiqFS/MiqFS.rb', line 60

def chdir(dir)
  twd = normalizePath(dir)
  raise "Directory not found: #{twd}" unless fileDirectory?(twd)
  @cwd = twd
end

#copyIn(from, to, recursive = false) ⇒ Object

Copy files and directories from the host to the VM.

FILE -> FILE FILE -> DIR DIR -> DIR (recursive = true)



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
# File 'lib/fs/MiqFS/MiqFS.rb', line 368

def copyIn(from, to, recursive = false)
  allTargets = []
  from = [from] unless from.kind_of?(Array)
  from.each { |t| allTargets.concat(Dir.glob(t)) }

  raise "copyIn: no source files matched" if allTargets.length == 0
  if allTargets.length > 1 || recursive
    raise "copyIn: destination directory does not exist" unless self.fileExists?(to)
    raise "copyIn: destination must be a directory for multi-file copy" unless self.fileDirectory?(to)
  end

  allTargets.each do |f|
    #
    # Copy plain files.
    #
    if File.file?(f)
      if self.fileDirectory?(to)
        tf = File.join(to, File.basename(f))
      else
        tf = to
      end
      copyInSingle(f, tf)
      next
    end

    #
    # If the recursive flag is not set, skip directories.
    #
    next unless recursive

    #
    # Recursively copy directory sub-tree.
    #
    owd = Dir.pwd
    Dir.chdir(f)
    td = File.join(to, f)
    dirMkdir(td) unless self.fileExists?(td)
    Find.find('.') do |ff|
      tf = File.join(td, ff)
      if File.directory?(ff)
        dirMkdir(tf) unless self.fileExists?(tf)
      elsif File.file?(ff)
        copyInSingle(ff, tf)
      end
    end # Find.find
    Dir.chdir(owd)
  end # allTargets.each
end

#copyInSingle(ff, tf) ⇒ Object



417
418
419
420
421
422
423
424
425
# File 'lib/fs/MiqFS/MiqFS.rb', line 417

def copyInSingle(ff, tf)
  File.open(ff) do |ffo|
    tfo = fileOpen(tf, "wb")
    while (buf = ffo.read(1024))
      tfo.write(buf)
    end
    tfo.close
  end
end

#copyOut(from, to, recursive = false) ⇒ Object

Copy files and directories from the VM to the host.

FILE -> FILE FILE -> DIR DIR -> DIR (recursive = true)



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
# File 'lib/fs/MiqFS/MiqFS.rb', line 302

def copyOut(from, to, recursive = false)
  allTargets = []
  from = [from] unless from.kind_of?(Array)
  from.each { |t| allTargets.concat(dirGlob(t)) }

  raise "copyOut: no source files matched" if allTargets.length == 0
  if allTargets.length > 1 || recursive
    raise "copyOut: destination directory does not exist" unless File.exist?(to)
    raise "copyOut: destination must be a directory for multi-file copy" unless File.directory?(to)
  end

  allTargets.each do |f|
    #
    # Copy plain files.
    #
    if fileFile?(f)
      if fileDirectory?(to)
        tf = File.join(to, File.basename(f))
      else
        tf = to
      end
      copyOutSingle(f, tf)
      next
    end

    #
    # If the recursive flag is not set, skip directories.
    #
    next unless recursive

    #
    # Recursively copy directory sub-tree.
    #
    owd = @cwd
    chdir(f)
    td = File.join(to, f)
    Dir.mkdir(td) unless File.exist?(td)
    findEach('.') do |ff|
      tf = File.join(td, ff)
      if fileDirectory?(ff)
        Dir.mkdir(tf)
      elsif fileFile?(ff)
        copyOutSingle(ff, tf)
      end
    end # findEach
    chdir(owd)
  end # allTargets.each
end

#copyOutSingle(ff, tf) ⇒ Object



351
352
353
354
355
356
357
358
359
# File 'lib/fs/MiqFS/MiqFS.rb', line 351

def copyOutSingle(ff, tf)
  fileOpen(ff) do |ffo|
    tfo = File.new(tf, "wb")
    while (buf = ffo.read(1024))
      tfo.write(buf)
    end
    tfo.close
  end
end

#dirEntries(*dir) ⇒ Object



66
67
68
# File 'lib/fs/MiqFS/MiqFS.rb', line 66

def dirEntries(*dir)
  fs_dirEntries(optDir(dir))
end

#dirForeach(*dir, &block) ⇒ Object



70
71
72
73
# File 'lib/fs/MiqFS/MiqFS.rb', line 70

def dirForeach(*dir, &block)
  ents = fs_dirEntries(optDir(dir))
  ents.each { |e| block.call(e) }
end

#dirGlob(glb, *flags, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fs/MiqFS/MiqFS.rb', line 84

def dirGlob(glb, *flags, &block)
  return([glb]) unless isGlob?(glb)

  if glb[0, 1] == '/'
    dir = '/'
    glb = glb[1..-1]
  else
    dir = @cwd
  end

  matches = doGlob(glb.split('/'), dir, flags)
  return(matches) unless block_given?

  matches.each do |e|
    block.call(e)
  end
  (false)
end

#dirMkdir(dir) ⇒ Object



127
128
129
# File 'lib/fs/MiqFS/MiqFS.rb', line 127

def dirMkdir(dir)
  fs_dirMkdir(normalizePath(dir))
end

#dirRmdir(dir) ⇒ Object



131
132
133
# File 'lib/fs/MiqFS/MiqFS.rb', line 131

def dirRmdir(dir)
  fs_dirRmdir(normalizePath(dir))
end

#doGlob(glbArr, dir, flags) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fs/MiqFS/MiqFS.rb', line 103

def doGlob(glbArr, dir, flags)
  return [] if !glbArr || glbArr.length == 0

  retArr = []
  glb = glbArr[0]

  dirForeach(dir) do |e|
    if flags.length == 0
      match = File.fnmatch(glb, e)
    else
      match = File.fnmatch(glb, e, flags)
    end
    if match
      if glbArr.length == 1
        retArr << File.join(dir, e)
      else
        next unless fileDirectory?(nf = File.join(dir, e))
        retArr.concat(doGlob(glbArr[1..-1], nf, flags))
      end
    end
  end
  (retArr)
end

#expandPath(path, dir) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/fs/MiqFS/MiqFS.rb', line 434

def expandPath(path, dir)
  if path[0, 1] == '/'
    tPath = path
  else
    tPath = File.join(dir, path)
  end

  tpa = tPath.slice('/')
  return(tPath) if tpa.empty?

  rpa = []
  tpa.each do |d|
    case d
    when ".." then rpa.pop
    when "."  then next
    when ""   then next
    else rpa << d
    end
  end
  ("/" + rpa.join("/"))
end

#fileAtime(f) ⇒ Object



217
218
219
# File 'lib/fs/MiqFS/MiqFS.rb', line 217

def fileAtime(f)
  fs_fileAtime(normalizePath(f))
end

#fileAtime_obj(fo) ⇒ Object



229
230
231
# File 'lib/fs/MiqFS/MiqFS.rb', line 229

def fileAtime_obj(fo)
  fs_fileAtime_obj(fo)
end

#fileBasename(f, *sfx) ⇒ Object



199
200
201
202
# File 'lib/fs/MiqFS/MiqFS.rb', line 199

def fileBasename(f, *sfx)
  return(File.basename(f)) if sfx.length == 0
  (File.basename(f, sfx[0]))
end

#fileClose(mfobj) ⇒ Object



187
188
189
# File 'lib/fs/MiqFS/MiqFS.rb', line 187

def fileClose(mfobj)
  fs_fileClose(mfobj.fobj)
end

#fileCtime(f) ⇒ Object



221
222
223
# File 'lib/fs/MiqFS/MiqFS.rb', line 221

def fileCtime(f)
  fs_fileCtime(normalizePath(f))
end

#fileCtime_obj(fo) ⇒ Object



233
234
235
# File 'lib/fs/MiqFS/MiqFS.rb', line 233

def fileCtime_obj(fo)
  fs_fileCtime_obj(fo)
end

#fileDelete(f) ⇒ Object



191
192
193
# File 'lib/fs/MiqFS/MiqFS.rb', line 191

def fileDelete(f)
  fs_fileDelete(normalizePath(f))
end

#fileDirectory?(f) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/fs/MiqFS/MiqFS.rb', line 147

def fileDirectory?(f)
  fs_fileDirectory?(normalizePath(f))
end

#fileDirname(f) ⇒ Object



204
205
206
# File 'lib/fs/MiqFS/MiqFS.rb', line 204

def fileDirname(f)
  File.dirname(f)
end

#fileExists?(f) ⇒ Boolean

File instance methods

Returns:

  • (Boolean)


139
140
141
# File 'lib/fs/MiqFS/MiqFS.rb', line 139

def fileExists?(f)
  fs_fileExists?(normalizePath(f))
end

#fileExtname(f) ⇒ Object



208
209
210
# File 'lib/fs/MiqFS/MiqFS.rb', line 208

def fileExtname(f)
  File.extname(f)
end

#fileFile?(f) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/fs/MiqFS/MiqFS.rb', line 143

def fileFile?(f)
  fs_fileFile?(normalizePath(f))
end

#fileFnmatch(glb, pth, *flags) ⇒ Object



212
213
214
215
# File 'lib/fs/MiqFS/MiqFS.rb', line 212

def fileFnmatch(glb, pth, *flags)
  return(File.fnmatch(glb, pth)) if flags.length == 0
  (File.fnmatch(glb, pth, flags))
end

#fileMtime(f) ⇒ Object



225
226
227
# File 'lib/fs/MiqFS/MiqFS.rb', line 225

def fileMtime(f)
  fs_fileMtime(normalizePath(f))
end

#fileMtime_obj(fo) ⇒ Object



237
238
239
# File 'lib/fs/MiqFS/MiqFS.rb', line 237

def fileMtime_obj(fo)
  fs_fileMtime_obj(fo)
end

#fileOpen(f, mode = "r", &block) ⇒ Object



156
157
158
159
160
# File 'lib/fs/MiqFS/MiqFS.rb', line 156

def fileOpen(f, mode = "r", &block)
  fpf = normalizePath(f)
  fo = fileOpenCommon(fpf, mode, &block)
  (fo)
end


162
163
164
165
# File 'lib/fs/MiqFS/MiqFS.rb', line 162

def fileOpenLink(f, mode = "r", &block)
  fpf = normalizePath(f)
  fileOpenCommon(fpf, mode, &block)
end

#fileSize(f) ⇒ Object



195
196
197
# File 'lib/fs/MiqFS/MiqFS.rb', line 195

def fileSize(f)
  fs_fileSize(normalizePath(f))
end

#fileSymLink?(f) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
# File 'lib/fs/MiqFS/MiqFS.rb', line 151

def fileSymLink?(f)
  p = normalizePath(f)
  (fs_isSymLink?(p))
end

#find(dir, depth = nil, level = 0) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/fs/MiqFS/MiqFS.rb', line 241

def find(dir, depth = nil, level = 0)
  return if depth && level > depth
  foundFiles = []

  dirEntries = self.dirEntries(dir)
  dirEntries.each do |de|
    next if de == '.' || de == '..'
    fp = File.join(dir, de)
    foundFiles << fp
    foundFiles.concat(find(fp, depth, level + 1)) if self.fileDirectory?(fp)
  end
  (foundFiles)
end

#findEach(dir, depth = nil, level = 0, &block) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/fs/MiqFS/MiqFS.rb', line 255

def findEach(dir, depth = nil, level = 0, &block)
  return if depth && level > depth
  return unless (dirEntries = self.dirEntries(dir))
  dirEntries.each do |de|
    next if de == '.' || de == '..'
    fp = File.join(dir, de)
    @findEachYield = true
    begin
      yield(fp)
    ensure
      @findEachYield = false
    end
    findEach(fp, depth, level + 1, &block) if self.fileDirectory?(fp) && !@findEachPrune
    @findEachPrune = false
  end
end

#findEachPruneObject



272
273
274
275
# File 'lib/fs/MiqFS/MiqFS.rb', line 272

def findEachPrune
  raise "MiqFS.findEachPrune: findEach not in progress" unless @findEachYield
  @findEachPrune = true
end

#freeBytesObject

Return free space in file system.



52
53
54
# File 'lib/fs/MiqFS/MiqFS.rb', line 52

def freeBytes
  fs_freeBytes
end

#fs_isSymLink?(_f) ⇒ Boolean

Returns:

  • (Boolean)


468
469
470
# File 'lib/fs/MiqFS/MiqFS.rb', line 468

def fs_isSymLink?(_f)
  false
end

Overridden by fs module if the fs supports symbolic links.



464
465
466
# File 'lib/fs/MiqFS/MiqFS.rb', line 464

def fs_supportsSymLinks
  false
end

#isGlob?(str) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/fs/MiqFS/MiqFS.rb', line 80

def isGlob?(str)
  str.count(GLOB_CHARS) != 0
end

#pwdObject



75
76
77
# File 'lib/fs/MiqFS/MiqFS.rb', line 75

def pwd
  @cwd
end

#rmBranch(dir) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/fs/MiqFS/MiqFS.rb', line 277

def rmBranch(dir)
  raise "rmBranch: #{dir} does not exist"   unless self.fileExists?(dir)
  raise "rmBranch: #{dir} is not a directory" unless self.fileDirectory?(dir)

  dirEntries = self.dirEntries(dir)

  dirEntries.each do |de|
    next if de == '.' || de == '..'
    fp = File.join(dir, de)
    if self.fileDirectory?(fp)
      rmBranch(fp)
    else
      fileDelete(fp)
    end
  end if dirEntries
  dirRmdir(dir)
end

#umountObject



46
47
48
49
# File 'lib/fs/MiqFS/MiqFS.rb', line 46

def umount
  fs_umount if self.respond_to?(:fs_umount)
  nil
end