Class: Tep::Shell
- Inherits:
-
Object
- Object
- Tep::Shell
- Defined in:
- lib/tep/shell.rb
Constant Summary collapse
- DEFAULT_MAX =
65535
Class Method Summary collapse
-
.read(path) ⇒ Object
Read a file's contents.
-
.read_limited(path, max_bytes) ⇒ Object
Bounded read: slice after the fact.
-
.run(cmd) ⇒ Object
Run
cmdvia /bin/sh -c; return up to DEFAULT_MAX bytes of stdout as a string. -
.run_limited(cmd, max_bytes) ⇒ Object
As above but with a caller-chosen byte cap.
-
.write(path, data) ⇒ Object
Write
datatopath(truncate + rewrite).
Class Method Details
.read(path) ⇒ Object
Read a file's contents. Useful for /proc/loadavg, /proc/meminfo, /sys/class/thermal/.../temp, and similar small-text endpoints. Returns "" on open failure. Spinel's File.read now raises a CRuby-correct Errno on a missing/unreadable path (it used to silently return ""); we rescue here to preserve this helper's never-raise contract -- a handler reading /proc should get "" for an absent file, not a 502'd worker.
44 45 46 47 48 49 50 |
# File 'lib/tep/shell.rb', line 44 def self.read(path) begin File.read(path) rescue => e "" end end |
.read_limited(path, max_bytes) ⇒ Object
Bounded read: slice after the fact. The cap is mostly a defensive cue -- callers that need it should be reading bounded /proc files anyway. Same never-raise contract as #read.
55 56 57 58 59 60 61 62 63 |
# File 'lib/tep/shell.rb', line 55 def self.read_limited(path, max_bytes) out = "" begin out = File.read(path) rescue => e out = "" end out.length > max_bytes ? out[0, max_bytes] : out end |
.run(cmd) ⇒ Object
Run cmd via /bin/sh -c; return up to DEFAULT_MAX bytes of
stdout as a string. Stderr is inherited (visible on the
server's console / log). The command's exit status is
discarded -- callers that need it can append ; echo "EX=$?"
and parse the tail.
+ "" forces a Ruby-side copy of the static C buffer; see
read below.
26 27 28 |
# File 'lib/tep/shell.rb', line 26 def self.run(cmd) Sock.sphttp_shell_capture(cmd, DEFAULT_MAX) + "" end |
.run_limited(cmd, max_bytes) ⇒ Object
As above but with a caller-chosen byte cap. Lower caps are cheaper memory-wise; higher caps (up to the sphttp internal buffer of ~64KB) let longer outputs through.
33 34 35 |
# File 'lib/tep/shell.rb', line 33 def self.run_limited(cmd, max_bytes) Sock.sphttp_shell_capture(cmd, max_bytes) + "" end |
.write(path, data) ⇒ Object
Write data to path (truncate + rewrite). Returns the byte
count for symmetry with the old FFI shape; spinel's File.write
is void, so we recover it from data.length.
68 69 70 71 |
# File 'lib/tep/shell.rb', line 68 def self.write(path, data) File.write(path, data) data.length end |