Class: Rednode::Bindings::Fs

Inherits:
Object
  • Object
show all
Includes:
Namespace
Defined in:
lib/rednode/bindings/fs.rb

Defined Under Namespace

Classes: Stats

Instance Method Summary collapse

Methods included from Namespace

#[]

Constructor Details

#initializeFs

Returns a new instance of Fs.



4
5
6
7
8
9
10
11
# File 'lib/rednode/bindings/fs.rb', line 4

def initialize
  @descriptors = {}
  @descriptors[STDOUT] = Class.new do
    def self.write(chars)
      $stdout.write(chars.pack("C*"))
    end
  end
end

Instance Method Details

#chmod(path, mode, callback = nil) ⇒ Object



13
14
15
16
17
# File 'lib/rednode/bindings/fs.rb', line 13

def chmod(path, mode, callback = nil)
  async(callback) do
    File.chmod(mode, path)
  end
end

#close(fd, callback = nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/rednode/bindings/fs.rb', line 28

def close(fd, callback = nil)
  async(callback) do
    file(fd) do |f|
      f.close()
      @descriptors.delete(fd)
    end
  end
end

#fstat(fd, callback = nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/rednode/bindings/fs.rb', line 75

def fstat(fd, callback = nil)
  async(callback) do
    file(fd) do |f|
      Stats.new(f.stat)
    end
  end
end

#fsync(fd, callback = nil) ⇒ Object Also known as: fdatasync



83
84
85
86
87
88
89
# File 'lib/rednode/bindings/fs.rb', line 83

def fsync(fd, callback = nil)
  async(callback) do
    file(fd) do |f|
      f.fsync
    end
  end
end


97
98
99
100
101
# File 'lib/rednode/bindings/fs.rb', line 97

def link(old_name, new_name, callback = nil)
  async(callback) do
    File.link(old_name, new_name)
  end
end

#lstat(path, callback = nil) ⇒ Object



69
70
71
72
73
# File 'lib/rednode/bindings/fs.rb', line 69

def lstat(path, callback = nil)
  async(callback) do
    Stats.new(File.lstat(path))
  end
end

#mkdir(name, mode, callback = nil) ⇒ Object



127
128
129
130
131
# File 'lib/rednode/bindings/fs.rb', line 127

def mkdir(name, mode, callback = nil)
  async(callback) do
    Dir.mkdir(name)
  end
end

#open(path, flags, mode, callback = nil) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/rednode/bindings/fs.rb', line 19

def open(path, flags, mode, callback = nil)
  async(callback) do
    file = File.new(path, flags, mode)
    file.fileno.tap do |fd|
      @descriptors[fd] = file
    end
  end
end

#read(fd, buffer, offset, length, position, callback = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rednode/bindings/fs.rb', line 37

def read(fd, buffer, offset, length, position, callback = nil)
  raise "Second argument needs to be a buffer" unless buffer.kind_of?(Buffer::Buffer)
  raise "Offset is out of bounds" unless offset <= buffer.length
  raise "Length is extends beyond buffer" unless (offset + length) <= buffer.length
  async(callback) do
    file(fd) do |f|
      f.seek(position) if position
      data = buffer.send(:data)
      bytes = f.read(length)
      data[offset, bytes.length] = bytes.unpack('C*')
      bytes.length
    end
  end
end

#readdir(path, callback = nil) ⇒ Object



133
134
135
136
137
# File 'lib/rednode/bindings/fs.rb', line 133

def readdir(path, callback = nil)
  async(callback) do
    Dir.entries(path).reject { |e| ['.', '..'].include?(e) }
  end
end


91
92
93
94
95
# File 'lib/rednode/bindings/fs.rb', line 91

def readlink(path, callback = nil)
  async(callback) do
    File.readlink(path)
  end
end

#rename(old_name, new_name, callback = nil) ⇒ Object



115
116
117
118
119
# File 'lib/rednode/bindings/fs.rb', line 115

def rename(old_name, new_name, callback = nil)
  async(callback) do
    File.rename(old_name, new_name)
  end
end

#rmdir(dir, callback = nil) ⇒ Object



121
122
123
124
125
# File 'lib/rednode/bindings/fs.rb', line 121

def rmdir(dir, callback = nil)
  async(callback) do
    Dir.rmdir(dir)
  end
end

#stat(path, callback = nil) ⇒ Object



63
64
65
66
67
# File 'lib/rednode/bindings/fs.rb', line 63

def stat(path, callback = nil)
  async(callback) do
    Stats.new(File.stat(path))
  end
end


103
104
105
106
107
# File 'lib/rednode/bindings/fs.rb', line 103

def symlink(old_name, new_name, callback = nil)
  async(callback) do
    File.symlink(old_name, new_name)
  end
end


109
110
111
112
113
# File 'lib/rednode/bindings/fs.rb', line 109

def unlink(filename, callback = nil)
  async(callback) do
    File.unlink(filename)
  end
end

#write(fd, buffer, offset, length, position, callback = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/rednode/bindings/fs.rb', line 52

def write(fd, buffer, offset, length, position, callback = nil)
  async(callback) do
    file(fd) do |f|
      f.seek(position) if position
      data = buffer.send(:data)
      f.write(data[offset, length])
    end
    length
  end
end