Skip to content

Commit

Permalink
fix: ameba warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Nov 12, 2024
1 parent 1111274 commit 889a593
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 54 deletions.
4 changes: 2 additions & 2 deletions examples/sftp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ require "../src/ssh2"
SSH2::Session.open("localhost", 2222) do |session|
session.login_with_pubkey("root", "./spec/keys/id_rsa")
session.sftp_session do |sftp|
sftp.open_dir(".").ll do |fn|
puts fn
sftp.open_dir(".").ll do |file_name|
puts file_name
end
file = sftp.open(".bashrc")
puts file.read
Expand Down
16 changes: 8 additions & 8 deletions examples/shell.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ require "../src/ssh2"

SSH2::Session.open("my_server") do |session|
session.login("username", "password")
session.open_session do |ch|
ch.request_pty("vt100")
ch.shell
session.open_session do |chan|
chan.request_pty("vt100")
chan.shell
session.blocking = false

buf_space = Bytes.new(1024)
buf = buf_space.to_slice
loop do
io = IO.select([STDIN, ch.socket]).first
io = IO.select([STDIN, chan.socket]).first
if io == STDIN
command = gets
if command
ch.write(command.to_slice)
chan.write(command.to_slice)
end
elsif io == ch.socket
len = ch.read(buf).to_i32
elsif io == chan.socket
len = chan.read(buf).to_i32
print! String.new buf[0, len]
break if ch.eof?
break if chan.eof?
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions spec/ssh2_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "spec"
SPEC_SSH_HOST = ENV["SPEC_SSH_HOST"]? || "localhost"
SPEC_SSH_PORT = ENV["CI"]? ? 22 : 10022

def connect_ssh
def connect_ssh(&)
SSH2::Session.open(SPEC_SSH_HOST, SPEC_SSH_PORT) do |session|
session.login("root", "somepassword")
session.authenticated?.should be_true
Expand Down Expand Up @@ -48,16 +48,16 @@ describe SSH2 do
it "should be able to scp transfer file" do
fn = "#{Time.utc.to_unix}.txt"
connect_ssh do |session|
session.scp_send(fn, 0o0644, 12) do |ch|
ch.puts "hello world"
session.scp_send(fn, 0o0644, 12) do |chan|
chan.puts "hello world"
end
session.open_session do |ch|
ch.command("ls -l")
ch.gets_to_end.includes?(fn).should be_true
session.open_session do |chan|
chan.command("ls -l")
chan.gets_to_end.includes?(fn).should be_true
end
session.scp_recv(fn) do |ch, st|
buf = Slice(UInt8).new(st.st_size.to_i32)
ch.read(buf)
session.scp_recv(fn) do |chan, stat|
buf = Slice(UInt8).new(stat.st_size.to_i32)
chan.read(buf)
String.new(buf).should eq("hello world\n")
end
end
Expand Down Expand Up @@ -95,7 +95,7 @@ describe SSH2::KnownHosts do
known_hosts.size.should eq(2)
known_hosts.map(&.name).includes?(SPEC_SSH_HOST).should be_true
known_hosts.write_file("known_hosts")
known_hosts.delete_if { |h| h.name == SPEC_SSH_HOST }
known_hosts.delete_if { |host| host.name == SPEC_SSH_HOST }
known_hosts.size.should eq(1)
end

Expand Down
4 changes: 2 additions & 2 deletions src/agent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ class SSH2::Agent
raise SSH2Error.new "Failed to authenticate username #{username} with SSH agent"
end

def each
def each(&)
each_unsafe do |key|
yield PublicKey.new(
Slice.new(key.value.blob, key.value.blob_len),
String.new(key.value.comment))
end
end

private def each_unsafe
private def each_unsafe(&)
prev = Pointer(LibSSH2::AgentPublicKey).null
until LibSSH2.agent_get_identity(self, out store, prev) == 1
yield store
Expand Down
2 changes: 1 addition & 1 deletion src/channel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SSH2::Channel < IO
end

def socket
@session.socket.not_nil!
@session.socket.as(TCPSocket)
end

# Close an active data channel. In practice this means sending an
Expand Down
6 changes: 3 additions & 3 deletions src/known_hosts.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SSH2::KnownHosts
LibSSH2.knownhost_checkp(self, host, port, key, key.size.to_u64, typemask, out store)
end

def delete_if
def delete_if(&)
each_unsafe do |known_host|
if yield conv_to_host(known_host)
ret = LibSSH2.knownhost_del(self, known_host)
Expand All @@ -53,7 +53,7 @@ class SSH2::KnownHosts
check_error(ret)
end

def each
def each(&)
each_unsafe do |known_host|
yield conv_to_host(known_host)
end
Expand All @@ -65,7 +65,7 @@ class SSH2::KnownHosts
Host.new(name, key, known_host.value.typemask)
end

private def each_unsafe
private def each_unsafe(&)
prev = Pointer(LibSSH2::KnownHost).null
until LibSSH2.knownhost_get(self, out store, prev) == 1
yield store
Expand Down
56 changes: 32 additions & 24 deletions src/session.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SSH2::Session
new(socket)
end

def self.open(host : String, port = 22)
def self.open(host : String, port = 22, &)
TCPSocket.open(host, port) do |socket|
session = new(socket)
begin
Expand All @@ -40,7 +40,7 @@ class SSH2::Session
@socket.wait_writable if flags.outbound?
end

def perform_nonblock
def perform_nonblock(&)
loop do
result = @request_lock.synchronize { yield }
code = if result.is_a?(Tuple)
Expand All @@ -57,7 +57,7 @@ class SSH2::Session
end
end

def nonblock_handle
def nonblock_handle(&)
loop do
result = @request_lock.synchronize { yield }
handle = if result.is_a?(Tuple)
Expand Down Expand Up @@ -142,10 +142,6 @@ class SSH2::Session
@@callbacks_lock.synchronize { @@callbacks.delete object_id }
end

private def password_cb(username : String, welcome : String) : String
@interactive_cb.not_nil!.call(username, welcome)
end

# Login with username using pub/priv key values
def login_with_data(username : String, privkey : String, pubkey : String, passphrase : String? = nil)
@socket.wait_writable
Expand Down Expand Up @@ -217,10 +213,10 @@ class SSH2::Session
# 64
end
slice = Slice.new(handle, len)
String.build do |o|
slice.each_with_index do |b, idx|
o << b.to_s(16)
o << ":" unless idx == length - 1
String.build do |outp|
slice.each_with_index do |byte, idx|
outp << byte.to_s(16)
outp << ":" unless idx == length - 1
end
end
end
Expand Down Expand Up @@ -287,14 +283,26 @@ class SSH2::Session
end

# If set, libssh2 will not attempt to block SIGPIPEs but will let them trigger from the underlying socket layer.
@[Deprecated("Use `#enable_sigpipe=` instead")]
# ameba:disable Naming/AccessorMethodName
def set_enable_sigpipe(value)
self.enable_sigpipe = value
end

def enable_sigpipe=(value)
perform_nonblock { LibSSH2.session_flag(self, LibSSH2::SessionFlag::SIGPIPE, value ? 1 : 0) }
end

# If set - before the connection negotiation is performed - libssh2 will try
# to negotiate compression enabling for this connection. By default libssh2
# will not attempt to use compression.
@[Deprecated("Use `#enable_compression=` instead")]
# ameba:disable Naming/AccessorMethodName
def set_enable_compression(value)
self.enable_compression = value
end

def enable_compression=(value)
perform_nonblock { LibSSH2.session_flag(self, LibSSH2::SessionFlag::COMPRESS, value ? 1 : 0) }
end

Expand Down Expand Up @@ -370,7 +378,7 @@ class SSH2::Session
open_channel("session", LibSSH2::CHANNEL_WINDOW_DEFAULT, LibSSH2::CHANNEL_PACKET_DEFAULT, nil)
end

def open_session
def open_session(&)
channel = open_session
begin
yield channel
Expand All @@ -388,7 +396,7 @@ class SSH2::Session
Channel.new self, handle
end

def direct_tcpip(host, port, source_host, source_port)
def direct_tcpip(host, port, source_host, source_port, &)
channel = direct_tcpip(host, port, source_host, source_port)
begin
yield channel
Expand All @@ -406,7 +414,7 @@ class SSH2::Session

# Send a file to the remote host via SCP.
# A new channel is passed to the block and closed afterwards.
def scp_send(path, mode, size, mtime = Time.utc.to_unix, atime = Time.utc.to_unix)
def scp_send(path, mode, size, mtime = Time.utc.to_unix, atime = Time.utc.to_unix, &)
channel = scp_send(path, mode, size, mtime, atime)
begin
yield channel
Expand All @@ -421,9 +429,9 @@ class SSH2::Session
raise SSH2Error.new("Unable to get stat for '#{path}'")
end
scp_send(remote_path, (stat.st_mode & 0x3ff).to_i32, stat.st_size.to_u64,
stat.st_mtim.tv_sec, stat.st_atim.tv_sec) do |ch|
File.open(path, "r") do |f|
IO.copy(f, ch)
stat.st_mtim.tv_sec, stat.st_atim.tv_sec) do |chan|
File.open(path, "r") do |file|
IO.copy(file, chan)
end
end
end
Expand All @@ -439,7 +447,7 @@ class SSH2::Session

# Request a file from the remote host via SCP.
# A new channel is passed to the block and closed afterwards.
def scp_recv(path)
def scp_recv(path, &)
channel, stat = scp_recv(path)
begin
yield channel, stat
Expand All @@ -450,11 +458,11 @@ class SSH2::Session

# Download a file from the remote host via SCP to the local filesystem.
def scp_recv_file(path, local_path = path)
File.open(local_path, "w") do |f|
scp_recv(path) do |ch, st|
buf = Slice(UInt8).new(st.st_size.to_i32)
ch.read(buf)
f.write(buf)
File.open(local_path, "w") do |file|
scp_recv(path) do |chan, stat|
buf = Slice(UInt8).new(stat.st_size.to_i32)
chan.read(buf)
file.write(buf)
end
end
end
Expand All @@ -466,7 +474,7 @@ class SSH2::Session
SFTP::Session.new(self, handle)
end

def sftp_session
def sftp_session(&)
sftp = sftp_session
begin
yield sftp
Expand Down
8 changes: 4 additions & 4 deletions src/sftp/dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module SSH2::SFTP
include Node

# Lists a current directory
def ls
def ls(&)
loop do
buf_space = uninitialized UInt8[512]
buf = buf_space.to_slice
Expand All @@ -18,11 +18,11 @@ module SSH2::SFTP
# Lists a current directory
def ls
ret = [] of String
ls { |fn| ret << fn }
ls { |file_name| ret << file_name }
ret
end

def ll
def ll(&)
loop do
buf_space = uninitialized UInt8[512]
buf = buf_space.to_slice
Expand All @@ -42,7 +42,7 @@ module SSH2::SFTP

def ll
ret = [] of String
ll { |fn| ret << fn }
ll { |file_name| ret << file_name }
ret
end
end
Expand Down

0 comments on commit 889a593

Please sign in to comment.