Class: Wardite::Wasi::DirentCache

Inherits:
Object
  • Object
show all
Defined in:
lib/wardite/wasi/dirent_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DirentCache

Returns a new instance of DirentCache.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wardite/wasi/dirent_cache.rb', line 27

def initialize(path)
  @entries = []
  Dir.entries(path).sort.each do |entry|
    case entry
    when "."
      @entries << Dirent.new(entry, File.stat(path).ino, FILETYPE_DIRECTORY)
    when ".."
      @entries << Dirent.new(entry, 0, FILETYPE_DIRECTORY)
    else
      full_path = File.join(path, entry)
      type = case File.ftype(full_path)
             when "directory" then FILETYPE_DIRECTORY
             when "file"      then FILETYPE_REGULAR_FILE
             when "link"      then FILETYPE_SYMBOLIC_LINK
             else FILETYPE_UNKNOWN
             end
      @entries << Dirent.new(entry, File.stat(full_path).ino, type)
    end
  end

  @eof = false
end

Instance Attribute Details

#entriesObject (readonly)

: Array



22
23
24
# File 'lib/wardite/wasi/dirent_cache.rb', line 22

def entries
  @entries
end

#eofObject

: bool



23
24
25
# File 'lib/wardite/wasi/dirent_cache.rb', line 23

def eof
  @eof
end

Instance Method Details

#fetch_entries_binary(buf_len, cookie) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wardite/wasi/dirent_cache.rb', line 53

def fetch_entries_binary(buf_len, cookie)
  # d_next is the index of the next file in the list, so it should
  # always be one higher than the requested cookie.
  d_next = cookie + 1
  buf = ""
  entries_slice = entries[cookie..-1]
  return "", false if entries_slice.nil? || entries_slice.empty?

  entries_slice.each do |entry|
    data = make_dirent_pack1(d_next, entry.ino, entry.path.size, entry.type, entry.path)
    if buf.size + data.size > buf_len
      # truncated
      return buf, true
    end
    buf += data
    d_next += 1
  end
  
  return buf, false
end

#make_dirent_pack1(d_next, ino, name_len, type, name) ⇒ Object



80
81
82
83
84
# File 'lib/wardite/wasi/dirent_cache.rb', line 80

def make_dirent_pack1(d_next, ino, name_len, type, name)
  data = [d_next, ino, name_len, type].pack("Q! Q! I! I!")
  data += name
  data
end