Class: RStyx::Server::InMemoryFile
- Defined in:
- lib/rstyx/server.rb
Overview
An SFile whose underlying data are stored as a String in memory. This string may grow to arbitrary size.
Instance Attribute Summary collapse
-
#contents ⇒ Object
The contents of the file, as a string.
Attributes inherited from SFile
#atime, #gid, #mtime, #muid, #name, #parent, #permissions, #uid, #version
Instance Method Summary collapse
-
#initialize(name, argv = { :permissions => 0666, :apponly => false, :excl => false, :uid => ENV["USER"], :gid => ENV["GROUP"] }) ⇒ InMemoryFile
constructor
A new instance of InMemoryFile.
-
#read(client, offset, count) ⇒ Object
Read data from the file.
-
#write(client, offset, data, truncate) ⇒ Object
Writes data to this file.
Methods inherited from SFile
#add_changelistener, #add_client, #appendonly?, #auth?, #can_setlength?, #can_setmode?, #can_setmtime?, #can_setname?, #client, #client_connected, #client_disconnected, #contents_changed, #delete, #directory?, #exclusive?, #filetype, #full_path, #length, #length=, #mode, #mode=, #num_clients, #qid, #refresh, #remove, #remove_client, #remove_dead_clients, #rename, #reply_read, #reply_write, #set_mtime, #stat, #uuid, #version_incr
Constructor Details
#initialize(name, argv = { :permissions => 0666, :apponly => false, :excl => false, :uid => ENV["USER"], :gid => ENV["GROUP"] }) ⇒ InMemoryFile
Returns a new instance of InMemoryFile.
1831 1832 1833 1834 1835 1836 |
# File 'lib/rstyx/server.rb', line 1831 def initialize(name, argv={ :permissions => 0666, :apponly => false, :excl => false, :uid => ENV["USER"], :gid => ENV["GROUP"] }) super(name, argv) @contentslock = Mutex.new end |
Instance Attribute Details
#contents ⇒ Object
The contents of the file, as a string.
1829 1830 1831 |
# File 'lib/rstyx/server.rb', line 1829 def contents @contents end |
Instance Method Details
#read(client, offset, count) ⇒ Object
Read data from the file.
- client
-
the SFileClient object representing the client reading from this file.
- offset
-
the offset the client wants to read from
- count
-
the number of bytes that the client wishes to read
1846 1847 1848 1849 1850 |
# File 'lib/rstyx/server.rb', line 1846 def read(client, offset, count) data = @contents[offset..(offset+count)] data ||= "" return(reply_read(data)) end |
#write(client, offset, data, truncate) ⇒ Object
Writes data to this file. Raises a StyxException if the offset is past the end of the file.
- client
-
the SFileClient object representing the client writing to this file
- offset
-
the offset the client wants to write to
- data
-
the data that the client wishes to write
- truncate
-
true or false depending on whether the file is to be truncated.
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 |
# File 'lib/rstyx/server.rb', line 1863 def write(client, offset, data, truncate) @contentslock.synchronize do # First write to the file if @contents.nil? @contents = "" end if offset > @contents.length raise StyxException.new("attempt to write past the end of the file") end @contents[offset..(offset + data.length)] = data if (truncate) @contents = @contents[0..(offset+data.length)] = data end return(reply_write(data.length, client.session)) end end |