class FakeFS::File::Stat

FakeFS Stat class

Attributes

atime[R]
birthtime[R]
ctime[R]
gid[R]
mode[R]
mtime[R]
uid[R]

Public Class Methods

new(file, lstat = false) click to toggle source
# File lib/fakefs/file.rb, line 345
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

Public Instance Methods

<=>(other) click to toggle source
# File lib/fakefs/file.rb, line 446
def <=>(other)
  @mtime <=> other.mtime
end
directory?() click to toggle source
# File lib/fakefs/file.rb, line 371
def directory?
  File.directory?(@file)
end
file?() click to toggle source
# File lib/fakefs/file.rb, line 375
def file?
  File.file?(@file)
end
ftype() click to toggle source
# File lib/fakefs/file.rb, line 379
def ftype
  return 'link' if symlink?
  return 'directory' if directory?
  'file'
end
ino() click to toggle source
# File lib/fakefs/file.rb, line 440
def ino
  @inode.inode_num
end
readable?() click to toggle source
# File lib/fakefs/file.rb, line 385
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
size() click to toggle source
# File lib/fakefs/file.rb, line 428
def size
  if @__lstat && symlink?
    @fake_file.target.size
  else
    File.size(@file)
  end
end
sticky?() click to toggle source

Assume nothing is sticky.

# File lib/fakefs/file.rb, line 410
def sticky?
  false
end
world_readable?() click to toggle source
# File lib/fakefs/file.rb, line 420
def world_readable?
  0o777
end
world_writable?() click to toggle source

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

# File lib/fakefs/file.rb, line 416
def world_writable?
  0o777
end
writable?() click to toggle source
# File lib/fakefs/file.rb, line 397
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?() click to toggle source
# File lib/fakefs/file.rb, line 436
def zero?
  size == 0
end

Private Instance Methods

check_if_bit_set(bit) click to toggle source
# File lib/fakefs/file.rb, line 452
def check_if_bit_set(bit)
  # get user's group and user ids
  # NOTE: I am picking `Process` over `Etc` as we use `Process`
  # when instaniating file classes. It may be worth it to ensure
  # our Process/Group detection scheme is robust in all cases
  uid = Process.uid
  gid = Process.gid

  # check if bit set for owner
  owner_bits = (@mode >> 6) & 0o7
  if uid == @uid
    # the user is locked out of the file if they are owner of the file
    # but do not have the bit set at the user level
    return true if owner_bits & bit == bit
    return false
  end

  # check if bit set for group
  group_bits = (@mode >> 3) & 0o7
  if gid == @gid
    # the user is locked out of the file if they are in the group that
    # owns the file but do not have the bit set at the group level
    return true if group_bits & bit == bit
    return false
  end

  # check if bit set for world
  world_bits = @mode & 0o7
  return true if world_bits & bit == bit

  false
end