Class: Microsandbox::FS
- Inherits:
-
Object
- Object
- Microsandbox::FS
- Defined in:
- lib/microsandbox/fs.rb
Overview
Guest filesystem operations for a running sandbox. Obtain via Sandbox#fs. All paths are paths inside the guest VM.
Instance Method Summary collapse
-
#copy(src, dst) ⇒ nil
Copy a file within the guest.
-
#copy_from_host(host_path, guest_path) ⇒ nil
Copy a file from the host into the guest.
-
#copy_to_host(guest_path, host_path) ⇒ nil
Copy a file from the guest to the host.
-
#exists?(path) ⇒ Boolean
Whether the path exists in the guest.
-
#initialize(native) ⇒ FS
constructor
A new instance of FS.
-
#list(path) ⇒ Array<FsEntry>
List the entries of a directory.
-
#mkdir(path) ⇒ nil
Create a directory (and any missing parents).
-
#read(path) ⇒ String
Read a file as raw bytes (ASCII-8BIT).
-
#read_stream(path) ⇒ FsReadStream
Open a streaming reader over a guest file — for files too large to read into memory at once (unlike #read, which buffers the whole file).
-
#read_text(path) ⇒ String
Read a file as a UTF-8 string.
-
#remove(path) ⇒ nil
Remove a single file.
-
#remove_dir(path) ⇒ nil
Remove a directory recursively.
-
#rename(src, dst) ⇒ nil
Rename/move a file or directory within the guest.
-
#stat(path) ⇒ FsMetadata
Stat a path.
-
#write(path, data) ⇒ nil
Write data to a file, creating or truncating it.
-
#write_stream(path) {|sink| ... } ⇒ FsWriteSink, Object
Open a streaming writer to a guest file.
Constructor Details
#initialize(native) ⇒ FS
Returns a new instance of FS.
83 84 85 |
# File 'lib/microsandbox/fs.rb', line 83 def initialize(native) @native = native end |
Instance Method Details
#copy(src, dst) ⇒ nil
Copy a file within the guest.
139 140 141 142 |
# File 'lib/microsandbox/fs.rb', line 139 def copy(src, dst) @native.fs_copy(src.to_s, dst.to_s) nil end |
#copy_from_host(host_path, guest_path) ⇒ nil
Copy a file from the host into the guest.
164 165 166 167 |
# File 'lib/microsandbox/fs.rb', line 164 def copy_from_host(host_path, guest_path) @native.fs_copy_from_host(host_path.to_s, guest_path.to_s) nil end |
#copy_to_host(guest_path, host_path) ⇒ nil
Copy a file from the guest to the host.
171 172 173 174 |
# File 'lib/microsandbox/fs.rb', line 171 def copy_to_host(guest_path, host_path) @native.fs_copy_to_host(guest_path.to_s, host_path.to_s) nil end |
#exists?(path) ⇒ Boolean
Returns whether the path exists in the guest.
152 153 154 |
# File 'lib/microsandbox/fs.rb', line 152 def exists?(path) @native.fs_exists(path.to_s) end |
#list(path) ⇒ Array<FsEntry>
List the entries of a directory.
112 113 114 |
# File 'lib/microsandbox/fs.rb', line 112 def list(path) @native.fs_list(path.to_s).map { |entry| FsEntry.new(entry) } end |
#mkdir(path) ⇒ nil
Create a directory (and any missing parents).
118 119 120 121 |
# File 'lib/microsandbox/fs.rb', line 118 def mkdir(path) @native.fs_mkdir(path.to_s) nil end |
#read(path) ⇒ String
Read a file as raw bytes (ASCII-8BIT).
89 90 91 |
# File 'lib/microsandbox/fs.rb', line 89 def read(path) @native.fs_read(path.to_s) end |
#read_stream(path) ⇒ FsReadStream
Open a streaming reader over a guest file — for files too large to read into memory at once (unlike #read, which buffers the whole file).
179 180 181 |
# File 'lib/microsandbox/fs.rb', line 179 def read_stream(path) FsReadStream.new(@native.fs_read_stream(path.to_s)) end |
#read_text(path) ⇒ String
Read a file as a UTF-8 string.
95 96 97 |
# File 'lib/microsandbox/fs.rb', line 95 def read_text(path) @native.fs_read_text(path.to_s) end |
#remove(path) ⇒ nil
Remove a single file.
125 126 127 128 |
# File 'lib/microsandbox/fs.rb', line 125 def remove(path) @native.fs_remove(path.to_s) nil end |
#remove_dir(path) ⇒ nil
Remove a directory recursively.
132 133 134 135 |
# File 'lib/microsandbox/fs.rb', line 132 def remove_dir(path) @native.fs_remove_dir(path.to_s) nil end |
#rename(src, dst) ⇒ nil
Rename/move a file or directory within the guest.
146 147 148 149 |
# File 'lib/microsandbox/fs.rb', line 146 def rename(src, dst) @native.fs_rename(src.to_s, dst.to_s) nil end |
#stat(path) ⇒ FsMetadata
Stat a path.
158 159 160 |
# File 'lib/microsandbox/fs.rb', line 158 def stat(path) FsMetadata.new(@native.fs_stat(path.to_s)) end |
#write(path, data) ⇒ nil
Write data to a file, creating or truncating it.
104 105 106 107 108 |
# File 'lib/microsandbox/fs.rb', line 104 def write(path, data) bytes = Microsandbox.coerce_write_bytes(data) @native.fs_write(path.to_s, bytes) nil end |
#write_stream(path) {|sink| ... } ⇒ FsWriteSink, Object
Open a streaming writer to a guest file. With a block, the sink is yielded and closed (flushed) when the block returns.
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/microsandbox/fs.rb', line 187 def write_stream(path) sink = FsWriteSink.new(@native.fs_write_stream(path.to_s)) return sink unless block_given? begin yield sink ensure sink.close end end |