Class: Bsdkrun::FileSystem

Inherits:
Object
  • Object
show all
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.

Examples:

sbx.fs.write_file("/app/main.py", "print('hi')")
sbx.fs.read_text("/app/out.json")
sbx.fs.upload("./src", "/app/src")
sbx.fs.download("/app/dist", "./dist", recursive: true)

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ FileSystem

Returns a new instance of FileSystem.

Parameters:

  • id (String)

    the machine's id.



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.

Parameters:

  • remote_path (String)
  • local_path (String)
  • recursive (Boolean) (defaults to: false)

Raises:



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).

Parameters:

  • path (String)

Returns:

  • (String)

    binary string.

Raises:



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.

Parameters:

  • path (String)
  • encoding (String) (defaults to: "UTF-8")

Returns:

  • (String)


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.

Parameters:

  • local_path (String)
  • remote_path (String)

Raises:



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.

Parameters:

  • path (String)

    absolute path in the guest.

  • data (String)

    text or binary content.

Raises:



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