Class: FakeFS::File::Stat

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/fakefs/file.rb

Overview

FakeFS Stat class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, lstat = false) ⇒ Stat

Returns a new instance of Stat.

Raises:

  • (Errno::ENOENT)


347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/fakefs/file.rb', line 347

def initialize(file, lstat = false)
  raise(Errno::ENOENT, file.to_s) unless File.exist?(file)

  @file      = file
  @fake_file = FileSystem.find(@file)
  @__lstat   = lstat
  @ctime     = @fake_file.ctime
  @mtime     = @fake_file.mtime
  @atime     = @fake_file.atime
  @mode      = @fake_file.mode
  @uid       = @fake_file.uid
  @gid       = @fake_file.gid
  @inode     = @fake_file.inode

  @birthtime =
    if @fake_file.respond_to?(:birthtime)
      @fake_file.birthtime
    else
      @fake_file.ctime
    end
end

Instance Attribute Details

#atimeObject (readonly)

Returns the value of attribute atime.



344
345
346
# File 'lib/fakefs/file.rb', line 344

def atime
  @atime
end

#birthtimeObject (readonly)

Returns the value of attribute birthtime.



345
346
347
# File 'lib/fakefs/file.rb', line 345

def birthtime
  @birthtime
end

#ctimeObject (readonly)

Returns the value of attribute ctime.



344
345
346
# File 'lib/fakefs/file.rb', line 344

def ctime
  @ctime
end

#gidObject (readonly)

Returns the value of attribute gid.



344
345
346
# File 'lib/fakefs/file.rb', line 344

def gid
  @gid
end

#modeObject (readonly)

Returns the value of attribute mode.



344
345
346
# File 'lib/fakefs/file.rb', line 344

def mode
  @mode
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



344
345
346
# File 'lib/fakefs/file.rb', line 344

def mtime
  @mtime
end

#uidObject (readonly)

Returns the value of attribute uid.



344
345
346
# File 'lib/fakefs/file.rb', line 344

def uid
  @uid
end

Instance Method Details

#<=>(other) ⇒ Object



448
449
450
# File 'lib/fakefs/file.rb', line 448

def <=>(other)
  @mtime <=> other.mtime
end

#directory?Boolean

Returns:

  • (Boolean)


373
374
375
# File 'lib/fakefs/file.rb', line 373

def directory?
  File.directory?(@file)
end

#file?Boolean

Returns:

  • (Boolean)


377
378
379
# File 'lib/fakefs/file.rb', line 377

def file?
  File.file?(@file)
end

#ftypeObject



381
382
383
384
385
# File 'lib/fakefs/file.rb', line 381

def ftype
  return 'link' if symlink?
  return 'directory' if directory?
  'file'
end

#inoObject



442
443
444
# File 'lib/fakefs/file.rb', line 442

def ino
  @inode.inode_num
end


426
427
428
# File 'lib/fakefs/file.rb', line 426

def nlink
  @fake_file.links.size
end

#readable?Boolean

Returns:

  • (Boolean)


387
388
389
390
391
392
393
394
395
396
397
# File 'lib/fakefs/file.rb', line 387

def readable?
  # a file is readable if, and only if, it has the following bits:
  #   4 ( read permission )
  #   5 ( read + execute permission )
  #   6 ( read + write permission )
  #   7 ( read + write + execute permission )
  # for each group we will isolate the wanted numbers ( for owner, world, or group )
  # and see if the third bit is set ( as that is the bit for read )
  read_bit = 4
  check_if_bit_set(read_bit)
end

#sizeObject



430
431
432
433
434
435
436
# File 'lib/fakefs/file.rb', line 430

def size
  if @__lstat && symlink?
    @fake_file.target.size
  else
    File.size(@file)
  end
end

#sticky?Boolean

Assume nothing is sticky.

Returns:

  • (Boolean)


412
413
414
# File 'lib/fakefs/file.rb', line 412

def sticky?
  false
end

#symlink?Boolean

Returns:

  • (Boolean)


369
370
371
# File 'lib/fakefs/file.rb', line 369

def symlink?
  File.symlink?(@file)
end

#world_readable?Boolean

Returns:

  • (Boolean)


422
423
424
# File 'lib/fakefs/file.rb', line 422

def world_readable?
  0o777
end

#world_writable?Boolean

World_writable and readable are platform dependent usually comparing with S_IROTH defined on compilation (MRI)

Returns:

  • (Boolean)


418
419
420
# File 'lib/fakefs/file.rb', line 418

def world_writable?
  0o777
end

#writable?Boolean

Returns:

  • (Boolean)


399
400
401
402
403
404
405
406
407
408
409
# File 'lib/fakefs/file.rb', line 399

def writable?
  # a file is writable if, and only if, it has the following bits:
  #   2 ( write permission )
  #   3 ( write + execute permission )
  #   6 ( read + write permission )
  #   7 ( read + write + execute permission )
  # for each group we will isolate the wanted numbers ( for owner, world, or group )
  # and see if the second bit is set ( as that is the bit for write )
  write_bit = 2
  check_if_bit_set(write_bit)
end

#zero?Boolean

Returns:

  • (Boolean)


438
439
440
# File 'lib/fakefs/file.rb', line 438

def zero?
  size == 0
end