Class: Tep::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/tep/shell.rb

Constant Summary collapse

DEFAULT_MAX =
65535

Class Method Summary collapse

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 swallows fopen errors and returns the empty string – matches the prior sphttp_file_read behaviour).



42
43
44
# File 'lib/tep/shell.rb', line 42

def self.read(path)
  File.read(path)
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.



49
50
51
52
# File 'lib/tep/shell.rb', line 49

def self.read_limited(path, max_bytes)
  out = File.read(path)
  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.



57
58
59
60
# File 'lib/tep/shell.rb', line 57

def self.write(path, data)
  File.write(path, data)
  data.length
end