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
TYPE_MAPPING =

unknown

{
  'block device' => S_IFBLK,
  'character device' => S_IFCHR,
  'directory' => S_IFDIR,
  'FIFO/pipe' => S_IFIFO,
  'symlink' => S_IFLNK,
  'regular file' => S_IFREG,
  'socket' => S_IFSOCK
}
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.



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

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.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def atime
  @atime
end

#ctimeObject (readonly)

Returns the value of attribute ctime.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def ctime
  @ctime
end

#devObject (readonly)

Returns the value of attribute dev.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def dev
  @dev
end

#ftypeObject (readonly)

Returns the value of attribute ftype.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def ftype
  @ftype
end

#gidObject (readonly)

Returns the value of attribute gid.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def gid
  @gid
end

#inoObject (readonly)

Returns the value of attribute ino.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def ino
  @ino
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def mtime
  @mtime
end

Returns the value of attribute nlink.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def nlink
  @nlink
end

#pathObject (readonly)

Returns the value of attribute path.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

def size
  @size
end

#uidObject (readonly)

Returns the value of attribute uid.



86
87
88
# File 'lib/em-fs/file/stat.rb', line 86

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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/em-fs/file/stat.rb', line 66

def parse str
  if m = str.match(STAT_REGEX)
    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: (TYPE_MAPPING[m[3]] || S_UNKNOWN),
                       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)


104
105
106
# File 'lib/em-fs/file/stat.rb', line 104

def blockdev?
  ftype^S_IFBLK == 0
end

#chardev?Boolean

Returns:

  • (Boolean)


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

def chardev?
  ftype^S_IFCHR == 0
end

#directory?Boolean

Returns:

  • (Boolean)


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

def directory?
  ftype^S_IFDIR == 0
end

#executable?Boolean

Returns:

  • (Boolean)


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

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

#executable_real?Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/em-fs/file/stat.rb', line 128

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

#file?Boolean

Returns:

  • (Boolean)


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

def file?
  ftype^S_IFREG == 0
end

#grpowned?Boolean

Returns:

  • (Boolean)


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

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

#modeObject



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

def mode
  @mode.to_s(8)
end

#owned?Boolean

Returns:

  • (Boolean)


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

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

#pipe?Boolean

Returns:

  • (Boolean)


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

def pipe?
  ftype^S_IFIFO == 0
end

#readable?Boolean

Returns:

  • (Boolean)


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

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

#readable_real?Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
190
# File 'lib/em-fs/file/stat.rb', line 180

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

#rgrpowned?Boolean

Returns:

  • (Boolean)


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

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

#rowned?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/em-fs/file/stat.rb', line 160

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

#socket?Boolean

Returns:

  • (Boolean)


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

def socket?
  ftype^S_IFSOCK == 0
end

#symlink?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/em-fs/file/stat.rb', line 196

def symlink?
  ftype^S_IFLNK == 0
end

#world_readable?Boolean

Returns:

  • (Boolean)


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

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

#world_writable?Boolean

Returns:

  • (Boolean)


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

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

#writable?Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
218
# File 'lib/em-fs/file/stat.rb', line 208

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

#writable_real?Boolean

Returns:

  • (Boolean)


220
221
222
223
224
225
226
227
228
229
230
# File 'lib/em-fs/file/stat.rb', line 220

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

#zero?Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/em-fs/file/stat.rb', line 232

def zero?
  @size == 0
end