Class: Bsdkrun::FileSystem
- Inherits:
-
Object
- Object
- Bsdkrun::FileSystem
- Defined in:
- lib/bsdkrun/filesystem.rb
Overview
Files in a running sandbox, reached as Sandbox#fs.
Every call goes through the guest's exec agent, so the sandbox has to be running — there is no offline write.
Instance Method Summary collapse
-
#download(remote_path, local_path, recursive: false) ⇒ void
Copy a file or directory out of the guest onto the host.
-
#initialize(id) ⇒ FileSystem
constructor
A new instance of FileSystem.
-
#read_file(path) ⇒ String
Read
pathfrom the guest as bytes (ASCII-8BIT). -
#read_text(path, encoding: "UTF-8") ⇒ String
Read
pathfrom the guest and tag it withencoding. -
#upload(local_path, remote_path) ⇒ void
Copy a host file or directory into the guest.
-
#write_file(path, data) ⇒ void
Write
datatopathin the guest, creating parent directories.
Constructor Details
#initialize(id) ⇒ FileSystem
Returns a new instance of FileSystem.
16 17 18 |
# File 'lib/bsdkrun/filesystem.rb', line 16 def initialize(id) @id = id end |
Instance Method Details
#download(remote_path, local_path, recursive: false) ⇒ void
This method returns an undefined value.
Copy a file or directory out of the guest onto the host.
Pass recursive: true for a directory; unlike #upload it cannot be detected here, because the path lives in the guest and answering would cost an extra round trip.
86 87 88 89 90 91 92 |
# File 'lib/bsdkrun/filesystem.rb', line 86 def download(remote_path, local_path, recursive: false) args = ["cp"] args << "-r" if recursive args += ["#{@id}:#{remote_path}", local_path.to_s] check!(Process.run(args), remote_path) nil end |
#read_file(path) ⇒ String
Read path from the guest as bytes (ASCII-8BIT).
37 38 39 40 41 |
# File 'lib/bsdkrun/filesystem.rb', line 37 def read_file(path) res = Process.run(["cp", "#{@id}:#{path}", "-"], binary: true) check!(res, path) res.stdout end |
#read_text(path, encoding: "UTF-8") ⇒ String
Read path from the guest and tag it with encoding.
48 49 50 |
# File 'lib/bsdkrun/filesystem.rb', line 48 def read_text(path, encoding: "UTF-8") read_file(path).force_encoding(encoding) end |
#upload(local_path, remote_path) ⇒ void
This method returns an undefined value.
Copy a host file or directory into the guest.
A directory's contents land in remote_path, so
upload("./src", "/app/src") leaves the guest's /app/src holding
what ./src holds. Whether it recurses is decided by looking at the local
path, so callers do not have to say which kind of thing it is.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bsdkrun/filesystem.rb', line 63 def upload(local_path, remote_path) unless File.exist?(local_path) raise FileTransferFailed.new("cannot upload #{local_path}: no such file or directory", local_path) end args = ["cp"] args << "-r" if File.directory?(local_path) args += [local_path.to_s, "#{@id}:#{remote_path}"] check!(Process.run(args), local_path) nil end |
#write_file(path, data) ⇒ void
This method returns an undefined value.
Write data to path in the guest, creating parent directories.
26 27 28 29 30 |
# File 'lib/bsdkrun/filesystem.rb', line 26 def write_file(path, data) res = Process.run(["cp", "-", "#{@id}:#{path}"], stdin: data, binary: true) check!(res, path) nil end |