Class: E2B::Services::Filesystem
- Inherits:
-
BaseService
- Object
- BaseService
- E2B::Services::Filesystem
- Defined in:
- lib/e2b/services/filesystem.rb
Overview
Filesystem operations for E2B sandbox
Provides methods for reading, writing, and managing files in the sandbox. Uses envd RPC for filesystem operations and REST endpoints for file transfer.
Constant Summary
Constants inherited from BaseService
BaseService::DEFAULT_USERNAME, BaseService::ENVD_DEFAULT_USER_VERSION, BaseService::ENVD_PORT, BaseService::ENVD_RECURSIVE_WATCH_VERSION
Instance Method Summary collapse
-
#exists?(path, user: nil, request_timeout: 30) ⇒ Boolean
Check if a path exists.
-
#get_info(path, user: nil, request_timeout: 30) ⇒ Models::EntryInfo
Get file/directory information using filesystem RPC.
-
#list(path, depth: 1, user: nil, request_timeout: 60) ⇒ Array<Models::EntryInfo>
(also: #list_files)
List directory contents using filesystem RPC.
-
#make_dir(path, user: nil, request_timeout: 30) ⇒ Boolean
(also: #mkdir, #create_folder)
Create a directory.
-
#read(path, format: "text", user: nil, request_timeout: 120) ⇒ String
(also: #read_file)
Default username for file operations Read file content.
-
#remove(path, user: nil, request_timeout: 30) ⇒ Object
(also: #delete_file)
Remove a file or directory.
-
#rename(old_path, new_path, user: nil, request_timeout: 30) ⇒ Models::EntryInfo
(also: #move, #move_files)
Rename/move a file or directory.
-
#watch_dir(path, recursive: false, user: nil, request_timeout: 30) ⇒ WatchHandle
Watch a directory for filesystem changes.
-
#write(path, data, user: nil, request_timeout: 120) ⇒ Models::WriteInfo
(also: #write_file)
Write content to a file using REST upload.
-
#write_files(files, user: nil, request_timeout: 120) ⇒ Array
Write multiple files at once.
Methods inherited from BaseService
Constructor Details
This class inherits a constructor from E2B::Services::BaseService
Instance Method Details
#exists?(path, user: nil, request_timeout: 30) ⇒ Boolean
Check if a path exists
Only NotFoundError is treated as “does not exist”. Other errors (auth, network, server) propagate so callers can distinguish “file is gone” from “we couldn’t ask”.
120 121 122 123 124 125 |
# File 'lib/e2b/services/filesystem.rb', line 120 def exists?(path, user: nil, request_timeout: 30) get_info(path, user: user, request_timeout: request_timeout) true rescue E2B::NotFoundError false end |
#get_info(path, user: nil, request_timeout: 30) ⇒ Models::EntryInfo
Get file/directory information using filesystem RPC
133 134 135 136 137 138 139 140 141 |
# File 'lib/e2b/services/filesystem.rb', line 133 def get_info(path, user: nil, request_timeout: 30) response = envd_rpc("filesystem.Filesystem", "Stat", body: { path: path }, timeout: request_timeout, headers: user_auth_headers(user)) entry_data = extract_entry(response) Models::EntryInfo.from_hash(entry_data) end |
#list(path, depth: 1, user: nil, request_timeout: 60) ⇒ Array<Models::EntryInfo> Also known as: list_files
List directory contents using filesystem RPC
100 101 102 103 104 105 106 107 108 |
# File 'lib/e2b/services/filesystem.rb', line 100 def list(path, depth: 1, user: nil, request_timeout: 60) response = envd_rpc("filesystem.Filesystem", "ListDir", body: { path: path, depth: depth }, timeout: request_timeout, headers: user_auth_headers(user)) entries = extract_entries(response) entries.map { |e| Models::EntryInfo.from_hash(e) } end |
#make_dir(path, user: nil, request_timeout: 30) ⇒ Boolean Also known as: mkdir, create_folder
Create a directory
178 179 180 181 182 183 184 |
# File 'lib/e2b/services/filesystem.rb', line 178 def make_dir(path, user: nil, request_timeout: 30) envd_rpc("filesystem.Filesystem", "MakeDir", body: { path: path }, timeout: request_timeout, headers: user_auth_headers(user)) true end |
#read(path, format: "text", user: nil, request_timeout: 120) ⇒ String Also known as: read_file
Default username for file operations Read file content
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/e2b/services/filesystem.rb', line 38 def read(path, format: "text", user: nil, request_timeout: 120) url = build_file_url("/files", path: path, user: user) response = rest_get(url, timeout: request_timeout) case format when "text" response.dup.force_encoding("UTF-8") when "bytes" response.b when "stream" StringIO.new(response.b) else raise ArgumentError, "Unsupported read format '#{format}'" end end |
#remove(path, user: nil, request_timeout: 30) ⇒ Object Also known as: delete_file
Remove a file or directory
148 149 150 151 152 153 |
# File 'lib/e2b/services/filesystem.rb', line 148 def remove(path, user: nil, request_timeout: 30) envd_rpc("filesystem.Filesystem", "Remove", body: { path: path }, timeout: request_timeout, headers: user_auth_headers(user)) end |
#rename(old_path, new_path, user: nil, request_timeout: 30) ⇒ Models::EntryInfo Also known as: move, move_files
Rename/move a file or directory
162 163 164 165 166 167 168 169 170 |
# File 'lib/e2b/services/filesystem.rb', line 162 def rename(old_path, new_path, user: nil, request_timeout: 30) response = envd_rpc("filesystem.Filesystem", "Move", body: { source: old_path, destination: new_path }, timeout: request_timeout, headers: user_auth_headers(user)) entry_data = extract_entry(response) Models::EntryInfo.from_hash(entry_data) end |
#watch_dir(path, recursive: false, user: nil, request_timeout: 30) ⇒ WatchHandle
Watch a directory for filesystem changes
Uses the polling-based CreateWatcher/GetWatcherEvents/RemoveWatcher RPCs.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/e2b/services/filesystem.rb', line 202 def watch_dir(path, recursive: false, user: nil, request_timeout: 30) if recursive && !supports_recursive_watch? raise E2B::TemplateError, "You need to update the template to use recursive watching. You can do this by running `e2b template build` in the directory with the template." end response = envd_rpc("filesystem.Filesystem", "CreateWatcher", body: { path: path, recursive: recursive }, timeout: request_timeout, headers: user_auth_headers(user)) watcher_id = response[:events]&.first&.dig("watcherId") || response["watcherId"] || extract_watcher_id(response) raise E2B::E2BError, "Failed to create watcher: no watcher_id returned" unless watcher_id rpc_proc = method(:envd_rpc) WatchHandle.new( watcher_id: watcher_id, envd_rpc_proc: rpc_proc, headers: user_auth_headers(user) ) end |
#write(path, data, user: nil, request_timeout: 120) ⇒ Models::WriteInfo Also known as: write_file
Write content to a file using REST upload
64 65 66 67 68 69 |
# File 'lib/e2b/services/filesystem.rb', line 64 def write(path, data, user: nil, request_timeout: 120) url = build_file_url("/files", path: path, user: user) content = data.is_a?(IO) || data.respond_to?(:read) ? data.read : data.to_s result = rest_upload(url, content, timeout: request_timeout) build_write_info(result, default_path: path) end |
#write_files(files, user: nil, request_timeout: 120) ⇒ Array
Write multiple files at once
83 84 85 86 87 |
# File 'lib/e2b/services/filesystem.rb', line 83 def write_files(files, user: nil, request_timeout: 120) files.map do |file| write(file[:path], file[:data] || file[:content], user: user, request_timeout: request_timeout) end end |