Class: EventMachine::File::Stat

Inherits:
Object
  • Object
show all
Defined in:
lib/em-fs/file/stat.rb

Constant Summary collapse

STAT_REGEX =
/(\d+) (\d+) '([\w\/ ]+)' (\d+) (\d+) (\d+) '(.+)' (\d+) (\d+) ([\d.]+) ([\d.]+) ([\d.]+)/.freeze
STAT_FORMAT =

access rights octal —number of blocks allocated —the size in bytes of each block reported by %b device number in decimal —raw mode in hex file type group id number of hardlinks inode number —mount point file name —optimal IO transfer size hint total size in bytes –major device type in hex –minor device type in hex user id –time of birth time of last access time of last mod time of last change

"%a %d '%F' %g %h %i '%n' %s %u %X %Y %Z"
S_IFBLK =

Types

0b00000001
S_IFCHR =

block device

0b00000010
S_IFDIR =

character device

0b00000100
S_IFIFO =

directory

0b00001000
S_IFLNK =

FIFO/pipe

0b00010000
S_IFREG =

symlink

0b00100000
S_IFSOCK =

regular file

0b01000000
S_UNKNOWN =

socket

0b10000000
S_IRUSR =

Mode Flags

0b100000000
S_IWUSR =
0b010000000
S_IXUSR =
0b001000000
S_IRGRP =
0b000100000
S_IWGRP =
0b000010000
S_IXGRP =
0b000001000
S_IROTH =
0b000000100
S_IWOTH =
0b000000010
S_IXOTH =
0b000000001

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val = {}) ⇒ Stat

Returns a new instance of Stat.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/em-fs/file/stat.rb', line 92

def initialize val = {}
  @path       = val[:path]
  @atime      = val[:atime]
  @ctime      = val[:ctime]
  @dev        = val[:dev]
  @ftype      = val[:ftype]
  @gid        = val[:gid]
  @ino        = val[:ino]
  @mode       = val[:mode]
  @mtime      = val[:mtime]
  @nlink      = val[:nlink]
  @size       = val[:size]
  @uid        = val[:uid]
end

Instance Attribute Details

#atimeObject (readonly)

Returns the value of attribute atime.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def atime
  @atime
end

#ctimeObject (readonly)

Returns the value of attribute ctime.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def ctime
  @ctime
end

#devObject (readonly)

Returns the value of attribute dev.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def dev
  @dev
end

#ftypeObject (readonly)

Returns the value of attribute ftype.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def ftype
  @ftype
end

#gidObject (readonly)

Returns the value of attribute gid.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def gid
  @gid
end

#inoObject (readonly)

Returns the value of attribute ino.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def ino
  @ino
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def mtime
  @mtime
end

Returns the value of attribute nlink.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def nlink
  @nlink
end

#pathObject (readonly)

Returns the value of attribute path.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def size
  @size
end

#uidObject (readonly)

Returns the value of attribute uid.



89
90
91
# File 'lib/em-fs/file/stat.rb', line 89

def uid
  @uid
end

Class Method Details

.parse(str) ⇒ EM::File::Stat

Parses a given string for file stat information.

Parameters:

  • string (String)

    The String to be parsed.

Returns:

  • (EM::File::Stat)

    The file stat object.



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
# File 'lib/em-fs/file/stat.rb', line 58

def parse str
  if m = str.match(STAT_REGEX)
    ftype = case m[3]
            when 'block device' then S_IFBLK
            when 'character device' then S_IFCHR
            when 'directory' then S_IFDIR
            when 'FIFO/pipe' then S_IFIFO
            when 'symlink' then S_IFLNK
            when 'regular file' then S_IFREG
            when 'socket' then S_IFSOCK
            else
              S_UNKNOWN
            end
    EM::File::Stat.new path:  m[7],
                       atime: Time.at(Integer(m[10].split('.')[0], 10)),
                       ctime: Time.at(Integer(m[12].split('.')[0], 10)),
                       dev:   Integer(m[2], 10),
                       ftype: ftype,
                       gid:   Integer(m[4], 10),
                       ino:   Integer(m[6], 10),
                       mode:  Integer(m[1], 8),
                       mtime: Time.at(Integer(m[11].split('.')[0], 10)),
                       nlink: Integer(m[5], 10),
                       size:  Integer(m[8], 10),
                       uid:   Integer(m[9], 10)
  else
    raise "Unable to parse stat string: #{str}"
  end
end

Instance Method Details

#blockdev?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/em-fs/file/stat.rb', line 107

def blockdev?
  ftype^S_IFBLK == 0
end

#chardev?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/em-fs/file/stat.rb', line 111

def chardev?
  ftype^S_IFCHR == 0
end

#directory?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/em-fs/file/stat.rb', line 115

def directory?
  ftype^S_IFDIR == 0
end

#executable?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
# File 'lib/em-fs/file/stat.rb', line 119

def executable?
  return true if Process::UID.rid == 0
  return @mode & S_IXUSR != 0 if rowned?
  return @mode & S_IXGRP != 0 if rgrpowned?
  @mode & S_IXOTH != 0
end

#executable_real?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
# File 'lib/em-fs/file/stat.rb', line 126

def executable_real?
  return true if Process::UID.rid == 0
  return @mode & S_IXUSR != 0 if rowned?
  return @mode & S_IXGRP != 0 if rgrpowned?
  @mode & S_IXOTH != 0
end

#file?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/em-fs/file/stat.rb', line 133

def file?
  ftype^S_IFREG == 0
end

#grpowned?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/em-fs/file/stat.rb', line 137

def grpowned?
  gid == Process::GID.eid
end

#modeObject



145
146
147
# File 'lib/em-fs/file/stat.rb', line 145

def mode
  @mode.to_s(8)
end

#owned?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/em-fs/file/stat.rb', line 149

def owned?
  uid == Process::UID.eid
end

#pipe?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/em-fs/file/stat.rb', line 157

def pipe?
  ftype^S_IFIFO == 0
end

#readable?Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
# File 'lib/em-fs/file/stat.rb', line 161

def readable?
  return true if Process::UID.eid == 0
  return @mode & S_IRUSR != 0 if owned?
  return @mode & S_IRGRP != 0 if grpowned?
  @mode & S_IROTH != 0
end

#readable_real?Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
# File 'lib/em-fs/file/stat.rb', line 168

def readable_real?
  return true if Process::UID.rid == 0
  return @mode & S_IRUSR != 0 if rowned?
  return @mode & S_IRGRP != 0 if rgrpowned?
  @mode & S_IROTH != 0
end

#rgrpowned?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/em-fs/file/stat.rb', line 141

def rgrpowned?
  gid == Process::GID.rid
end

#rowned?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/em-fs/file/stat.rb', line 153

def rowned?
  uid == Process::UID.rid
end

#socket?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/em-fs/file/stat.rb', line 175

def socket?
  ftype^S_IFSOCK == 0
end

#symlink?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/em-fs/file/stat.rb', line 179

def symlink?
  ftype^S_IFLNK == 0
end

#world_readable?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/em-fs/file/stat.rb', line 183

def world_readable?
  @mode if @mode & S_IROTH == S_IROTH
end

#world_writable?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/em-fs/file/stat.rb', line 187

def world_writable?
  @mode if @mode & S_IWOTH == S_IWOTH
end

#writable?Boolean

Returns:

  • (Boolean)


191
192
193
194
195
196
# File 'lib/em-fs/file/stat.rb', line 191

def writable?
  return true if Process::UID.rid == 0
  return @mode & S_IWUSR != 0 if owned?
  return @mode & S_IWGRP != 0 if grpowned?
  @mode & S_IWOTH != 0
end

#writable_real?Boolean

Returns:

  • (Boolean)


198
199
200
201
202
203
# File 'lib/em-fs/file/stat.rb', line 198

def writable_real?
  return true if Process::UID.rid == 0
  return @mode & S_IWUSR != 0 if rowned?
  return @mode & S_IWGRP != 0 if rgrpowned?
  @mode & S_IWOTH != 0
end

#zero?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/em-fs/file/stat.rb', line 205

def zero?
  @size == 0
end