From bf33852a22394625818021e9b2ca7d603901f77e Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 16:50:35 +0800 Subject: [PATCH 01/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 3f0d64e..e7054ed 100644 --- a/src/session.cr +++ b/src/session.cr @@ -343,7 +343,7 @@ class SSH2::Session File.open(local_path, "w") do |f| buf = uninitialized UInt8[1024] while read_bytes < file_size - bytes_to_read = min.call(buf.length, file_size - read_bytes) + bytes_to_read = min.call(buf.size, file_size - read_bytes) len = ch.read(buf.to_slice, bytes_to_read).to_i32 f.write(buf.to_slice, len) break if len <= 0 From 82c0f5b5f281d286eed3955a0e4aaea5e1b6c79e Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 17:16:31 +0800 Subject: [PATCH 02/33] Update channel.cr --- src/channel.cr | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/channel.cr b/src/channel.cr index b1eab2a..1b00b9f 100644 --- a/src/channel.cr +++ b/src/channel.cr @@ -109,7 +109,15 @@ class SSH2::Channel < IO return 0 if eof? read(0, slice) end - + def read(slice : Slice(UInt32)) + return 0 if eof? + read(0, slice) + end + def read(slice : Slice(UInt64)) + return 0 if eof? + read(0, slice) + end + def write(slice : Slice(UInt8)) write(0, slice) end From 648f8d1405c8f7a6dca0fc82aa4b8e674e33bebc Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 17:18:41 +0800 Subject: [PATCH 03/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index e7054ed..c664883 100644 --- a/src/session.cr +++ b/src/session.cr @@ -341,7 +341,7 @@ class SSH2::Session file_size = stat.st_size read_bytes = 0 File.open(local_path, "w") do |f| - buf = uninitialized UInt8[1024] + buf = StaticArray(Int8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size bytes_to_read = min.call(buf.size, file_size - read_bytes) len = ch.read(buf.to_slice, bytes_to_read).to_i32 From 46f6c3c5d2eeaeff0193a99aa2ae162d26be7563 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 17:38:53 +0800 Subject: [PATCH 04/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index c664883..eb37a31 100644 --- a/src/session.cr +++ b/src/session.cr @@ -331,7 +331,7 @@ class SSH2::Session # Download a file from the remote host via SCP to the local filesystem. def scp_recv_file(path, local_path = path) - min = -> (x : Int32|Int64, y : Int32|Int64) { x < y ? x : y} + min = -> (x : Int32, y : Int32) { x < y ? x : y} # libssh2 scp_recv method has a bug where its channel's read method doesn't # return 0 value to indicate the end of file(EOF). The only way to find EOF From e13a387e0f3091c1edcda2d89e2a34e49b8057d6 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 17:40:44 +0800 Subject: [PATCH 05/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index eb37a31..42d68a6 100644 --- a/src/session.cr +++ b/src/session.cr @@ -331,7 +331,7 @@ class SSH2::Session # Download a file from the remote host via SCP to the local filesystem. def scp_recv_file(path, local_path = path) - min = -> (x : Int32, y : Int32) { x < y ? x : y} + min = -> (x : Int64, y : Int64) { x < y ? x : y} # libssh2 scp_recv method has a bug where its channel's read method doesn't # return 0 value to indicate the end of file(EOF). The only way to find EOF From ee95ee29d04e7e213c32c1c06f72067ae15c0349 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 17:52:21 +0800 Subject: [PATCH 06/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 42d68a6..8e7266e 100644 --- a/src/session.cr +++ b/src/session.cr @@ -343,7 +343,7 @@ class SSH2::Session File.open(local_path, "w") do |f| buf = StaticArray(Int8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size - bytes_to_read = min.call(buf.size, file_size - read_bytes) +bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) len = ch.read(buf.to_slice, bytes_to_read).to_i32 f.write(buf.to_slice, len) break if len <= 0 From 00c9f827d1a09025d04f7caeb2b068d04e9cca2a Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 17:54:17 +0800 Subject: [PATCH 07/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 8e7266e..06a3a08 100644 --- a/src/session.cr +++ b/src/session.cr @@ -331,7 +331,7 @@ class SSH2::Session # Download a file from the remote host via SCP to the local filesystem. def scp_recv_file(path, local_path = path) - min = -> (x : Int64, y : Int64) { x < y ? x : y} + min = -> (x : Int64|Int32, y : Int64|Int32) { x < y ? x : y} # libssh2 scp_recv method has a bug where its channel's read method doesn't # return 0 value to indicate the end of file(EOF). The only way to find EOF From 5bfaf07ae3d965c7a1dfcdd39b4b340f3c0050b5 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 18:01:24 +0800 Subject: [PATCH 08/33] Update session.cr --- src/session.cr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 06a3a08..e76bb21 100644 --- a/src/session.cr +++ b/src/session.cr @@ -344,7 +344,8 @@ class SSH2::Session buf = StaticArray(Int8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) - len = ch.read(buf.to_slice, bytes_to_read).to_i32 +buf2 = StaticArray(Int8, bytes_to_read).new(0) + len = ch.read(buf2.to_slice).to_i32 f.write(buf.to_slice, len) break if len <= 0 read_bytes += len From e61d3f87167fc43430449d5331a6edcec93c3cef Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 18:04:08 +0800 Subject: [PATCH 09/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index e76bb21..1e5c0d0 100644 --- a/src/session.cr +++ b/src/session.cr @@ -344,7 +344,7 @@ class SSH2::Session buf = StaticArray(Int8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -buf2 = StaticArray(Int8, bytes_to_read).new(0) +buf2 = Array.new(Int8, bytes_to_read) len = ch.read(buf2.to_slice).to_i32 f.write(buf.to_slice, len) break if len <= 0 From 87dbf680ad3ac456ab119f9fe20e725d3966c788 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 18:05:40 +0800 Subject: [PATCH 10/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 1e5c0d0..c236280 100644 --- a/src/session.cr +++ b/src/session.cr @@ -344,7 +344,7 @@ class SSH2::Session buf = StaticArray(Int8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -buf2 = Array.new(Int8, bytes_to_read) +buf2 = Array(Int8).new( bytes_to_read,0) len = ch.read(buf2.to_slice).to_i32 f.write(buf.to_slice, len) break if len <= 0 From bbf54afe4026f59eb7495433044c0ab47dd0a2fb Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 18:08:56 +0800 Subject: [PATCH 11/33] Update session.cr --- src/session.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/session.cr b/src/session.cr index c236280..da2cc52 100644 --- a/src/session.cr +++ b/src/session.cr @@ -344,8 +344,8 @@ class SSH2::Session buf = StaticArray(Int8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -buf2 = Array(Int8).new( bytes_to_read,0) - len = ch.read(buf2.to_slice).to_i32 +buf2 = Slice(Int8).new( bytes_to_read,0) + len = ch.read(buf2).to_i32 f.write(buf.to_slice, len) break if len <= 0 read_bytes += len From a341e63e95321c0fccea1dedd1427d90d42f804f Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 18:19:55 +0800 Subject: [PATCH 12/33] Update session.cr --- src/session.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/session.cr b/src/session.cr index da2cc52..2b55e1b 100644 --- a/src/session.cr +++ b/src/session.cr @@ -343,9 +343,9 @@ class SSH2::Session File.open(local_path, "w") do |f| buf = StaticArray(Int8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size -bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -buf2 = Slice(Int8).new( bytes_to_read,0) - len = ch.read(buf2).to_i32 +#bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) +#buf2 = Slice(Int8).new( bytes_to_read,0) + len = ch.read(buf.to_slice).to_i32 f.write(buf.to_slice, len) break if len <= 0 read_bytes += len From 4e0a5d1b0540c80ba8a88b8790730efb817d9a74 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 18:21:48 +0800 Subject: [PATCH 13/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 2b55e1b..e6ccae1 100644 --- a/src/session.cr +++ b/src/session.cr @@ -341,7 +341,7 @@ class SSH2::Session file_size = stat.st_size read_bytes = 0 File.open(local_path, "w") do |f| - buf = StaticArray(Int8, 1024).new(0) # => 42#uninitialized UInt8[1024] + buf = StaticArray(UInt8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size #bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) #buf2 = Slice(Int8).new( bytes_to_read,0) From 9438cfe9bf76e0ef29d7f5263a786ba9f9e31058 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 18:29:22 +0800 Subject: [PATCH 14/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index e6ccae1..fcf30c3 100644 --- a/src/session.cr +++ b/src/session.cr @@ -346,7 +346,7 @@ class SSH2::Session #bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) #buf2 = Slice(Int8).new( bytes_to_read,0) len = ch.read(buf.to_slice).to_i32 - f.write(buf.to_slice, len) + f.write buf.to_slice, len break if len <= 0 read_bytes += len end From c63453be2f7014c4bcec8b0a2c8c4d1b3d93d973 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 18:30:26 +0800 Subject: [PATCH 15/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index fcf30c3..c82d621 100644 --- a/src/session.cr +++ b/src/session.cr @@ -346,7 +346,7 @@ class SSH2::Session #bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) #buf2 = Slice(Int8).new( bytes_to_read,0) len = ch.read(buf.to_slice).to_i32 - f.write buf.to_slice, len + f.write buf.to_slice break if len <= 0 read_bytes += len end From 36aa4998eed5de4746f6c239bb48e9a97223a6fb Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 18:34:32 +0800 Subject: [PATCH 16/33] Update session.cr --- src/session.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/session.cr b/src/session.cr index c82d621..0107318 100644 --- a/src/session.cr +++ b/src/session.cr @@ -345,9 +345,9 @@ class SSH2::Session while read_bytes < file_size #bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) #buf2 = Slice(Int8).new( bytes_to_read,0) - len = ch.read(buf.to_slice).to_i32 - f.write buf.to_slice + len = ch.read(buf.to_slice).to_i32 break if len <= 0 + f.write buf.to_slice read_bytes += len end end From be89d3e33e322e89216fa347aeb9dd85583493c6 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 19:35:30 +0800 Subject: [PATCH 17/33] Update session.cr --- src/session.cr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/session.cr b/src/session.cr index 0107318..97ef505 100644 --- a/src/session.cr +++ b/src/session.cr @@ -343,11 +343,11 @@ class SSH2::Session File.open(local_path, "w") do |f| buf = StaticArray(UInt8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size -#bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -#buf2 = Slice(Int8).new( bytes_to_read,0) - len = ch.read(buf.to_slice).to_i32 +bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) +buf2 = Slice(Int8).new( bytes_to_read.as(Int),0) + len = ch.read(buf2).to_i32 break if len <= 0 - f.write buf.to_slice + f.write buf2.to_slice read_bytes += len end end From 8af0c91ae4c138e859f505ac5ac34530ec9ae0db Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 19:40:09 +0800 Subject: [PATCH 18/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 97ef505..7ecc7d3 100644 --- a/src/session.cr +++ b/src/session.cr @@ -344,7 +344,7 @@ class SSH2::Session buf = StaticArray(UInt8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -buf2 = Slice(Int8).new( bytes_to_read.as(Int),0) +buf2 = Slice(Int8).new( bytes_to_read.as(Int32)) len = ch.read(buf2).to_i32 break if len <= 0 f.write buf2.to_slice From 55d12f9add804096efecfb6e6d913c0fe81479e7 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 19:40:57 +0800 Subject: [PATCH 19/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 7ecc7d3..349dd49 100644 --- a/src/session.cr +++ b/src/session.cr @@ -344,7 +344,7 @@ class SSH2::Session buf = StaticArray(UInt8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -buf2 = Slice(Int8).new( bytes_to_read.as(Int32)) +buf2 = Slice(UInt8).new( bytes_to_read.as(Int32)) len = ch.read(buf2).to_i32 break if len <= 0 f.write buf2.to_slice From 3b5fca3b546a9bbe3433e1eb5994864e92bcb37c Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 20:37:14 +0800 Subject: [PATCH 20/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 349dd49..d6f3235 100644 --- a/src/session.cr +++ b/src/session.cr @@ -344,7 +344,7 @@ class SSH2::Session buf = StaticArray(UInt8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -buf2 = Slice(UInt8).new( bytes_to_read.as(Int32)) +buf2 = Slice(UInt8).new( bytes_to_read.to_int32) len = ch.read(buf2).to_i32 break if len <= 0 f.write buf2.to_slice From ad460b6fa392fa31c5499a04ef7cb18c91ad24b7 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 20:37:50 +0800 Subject: [PATCH 21/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index d6f3235..2dab540 100644 --- a/src/session.cr +++ b/src/session.cr @@ -344,7 +344,7 @@ class SSH2::Session buf = StaticArray(UInt8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -buf2 = Slice(UInt8).new( bytes_to_read.to_int32) +buf2 = Slice(UInt8).new( bytes_to_read.to_i32) len = ch.read(buf2).to_i32 break if len <= 0 f.write buf2.to_slice From feaf94e80f9df310f16f8a9c1879ad47cc98835e Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 20:39:38 +0800 Subject: [PATCH 22/33] Update session.cr --- src/session.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/session.cr b/src/session.cr index 2dab540..84325ce 100644 --- a/src/session.cr +++ b/src/session.cr @@ -343,8 +343,8 @@ class SSH2::Session File.open(local_path, "w") do |f| buf = StaticArray(UInt8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size -bytes_to_read = min.call(buf.size, file_size - read_bytes).as(Int64) -buf2 = Slice(UInt8).new( bytes_to_read.to_i32) +bytes_to_read = min.call(buf.size, file_size - read_bytes).to_i32 +buf2 = Slice(UInt8).new( bytes_to_read) len = ch.read(buf2).to_i32 break if len <= 0 f.write buf2.to_slice From 449a7df7cb4e4d85f7f982820c523bb254ef090e Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 20:46:10 +0800 Subject: [PATCH 23/33] Update session.cr --- src/session.cr | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/session.cr b/src/session.cr index 84325ce..b73f540 100644 --- a/src/session.cr +++ b/src/session.cr @@ -310,6 +310,18 @@ class SSH2::Session end end end + # Send a file from a local filesystem to the remote host via SCP. + def scp_send_file(path,localpath) + if LibC.stat(path, out stat) != 0 + raise Errno.new("Unable to get stat for '#{path}'") + end + scp_send(path, (stat.st_mode & 0x3ff).to_i32, stat.st_size.to_u64, + stat.st_mtimespec.tv_sec, stat.st_atimespec.tv_sec) do |ch| + File.open(localpath, "r") do |f| + IO.copy(f, ch) + end + end + end # Request a file from the remote host via SCP. def scp_recv(path) From 38854b9732e824b032c37f3ddb60bed6f8462905 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 20:49:42 +0800 Subject: [PATCH 24/33] Update session.cr --- src/session.cr | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/session.cr b/src/session.cr index b73f540..4dfa577 100644 --- a/src/session.cr +++ b/src/session.cr @@ -315,8 +315,7 @@ class SSH2::Session if LibC.stat(path, out stat) != 0 raise Errno.new("Unable to get stat for '#{path}'") end - scp_send(path, (stat.st_mode & 0x3ff).to_i32, stat.st_size.to_u64, - stat.st_mtimespec.tv_sec, stat.st_atimespec.tv_sec) do |ch| + scp_send(path, (stat.st_mode & 0x3ff).to_i32, stat.st_size.to_u64,stat.st_mtimespec.tv_sec, stat.st_atimespec.tv_sec) do |ch| File.open(localpath, "r") do |f| IO.copy(f, ch) end From 160c428c12f4a81ede17dce8b19e2a75ce168efe Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 21:41:56 +0800 Subject: [PATCH 25/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 4dfa577..340645d 100644 --- a/src/session.cr +++ b/src/session.cr @@ -315,7 +315,7 @@ class SSH2::Session if LibC.stat(path, out stat) != 0 raise Errno.new("Unable to get stat for '#{path}'") end - scp_send(path, (stat.st_mode & 0x3ff).to_i32, stat.st_size.to_u64,stat.st_mtimespec.tv_sec, stat.st_atimespec.tv_sec) do |ch| + scp_send(path, (stat.st_mode & 0x3ff).to_i32, stat.st_size.to_u64) do |ch| File.open(localpath, "r") do |f| IO.copy(f, ch) end From 2ce2cb62b8f404267bb3f371c272a35be5fe0b30 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 21:46:09 +0800 Subject: [PATCH 26/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 340645d..2214398 100644 --- a/src/session.cr +++ b/src/session.cr @@ -289,7 +289,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.now.epoch, atime = Time.now.epoch) + def scp_send(path, mode, size, mtime = Time.now.local, atime = Time.now.local) channel = scp_send(path, mode, size, mtime, atime) begin yield channel From f811a166f647433132584a491200e6f6a9f23626 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 21:49:08 +0800 Subject: [PATCH 27/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 2214398..97fe7fa 100644 --- a/src/session.cr +++ b/src/session.cr @@ -289,7 +289,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.now.local, atime = Time.now.local) + def scp_send(path, mode, size, mtime = Time.to_utc, atime = Time.to_utc) channel = scp_send(path, mode, size, mtime, atime) begin yield channel From ab2c47619cc09061abdbe152f36b9d8603fdf961 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 21:50:26 +0800 Subject: [PATCH 28/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 97fe7fa..d9310ae 100644 --- a/src/session.cr +++ b/src/session.cr @@ -289,7 +289,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.to_utc, atime = Time.to_utc) + def scp_send(path, mode, size, mtime = Time.now.to_utc, atime = Time.now.to_utc) channel = scp_send(path, mode, size, mtime, atime) begin yield channel From ceacd6d59f486a7d688155802b80149e5746b458 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 21:52:36 +0800 Subject: [PATCH 29/33] Update session.cr --- src/session.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/session.cr b/src/session.cr index d9310ae..8ff05e1 100644 --- a/src/session.cr +++ b/src/session.cr @@ -282,14 +282,14 @@ class SSH2::Session # Send a file to the remote host via SCP. def scp_send(path, mode, size, mtime, atime) handle = LibSSH2.scp_send(self, path, mode.to_i32, size.to_u64, - LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) +mtime,atime) #LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) check_error(LibSSH2.session_last_errno(self)) Channel.new self, handle end # 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.now.to_utc, atime = Time.now.to_utc) + def scp_send(path, mode, size, mtime = Time.now, atime = Time.now) channel = scp_send(path, mode, size, mtime, atime) begin yield channel From ef5394845e6863c21aeae0d5773d65f053366d52 Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 21:53:54 +0800 Subject: [PATCH 30/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index 8ff05e1..e9b0199 100644 --- a/src/session.cr +++ b/src/session.cr @@ -289,7 +289,7 @@ mtime,atime) #LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) # 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.now, atime = Time.now) + def scp_send(path, mode, size, mtime = Time.now.to_utc, atime = Time.now.to_utc) channel = scp_send(path, mode, size, mtime, atime) begin yield channel From 049ac8ee888c2e24375bf0c0cb306f5252155b9f Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 22:51:09 +0800 Subject: [PATCH 31/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index e9b0199..e416cd4 100644 --- a/src/session.cr +++ b/src/session.cr @@ -289,7 +289,7 @@ mtime,atime) #LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) # 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.now.to_utc, atime = Time.now.to_utc) + def scp_send(path, mode, size, mtime = Time.now.to_unix, atime = Time.now.to_unix) channel = scp_send(path, mode, size, mtime, atime) begin yield channel From d0494e5407d015d538b1bf8aa5faa75f41bbd3ad Mon Sep 17 00:00:00 2001 From: whidbey Date: Mon, 9 Sep 2019 23:03:04 +0800 Subject: [PATCH 32/33] Update session.cr --- src/session.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.cr b/src/session.cr index e416cd4..78861b8 100644 --- a/src/session.cr +++ b/src/session.cr @@ -312,7 +312,7 @@ mtime,atime) #LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) end # Send a file from a local filesystem to the remote host via SCP. def scp_send_file(path,localpath) - if LibC.stat(path, out stat) != 0 + if LibC.stat(localpath, out stat) != 0 raise Errno.new("Unable to get stat for '#{path}'") end scp_send(path, (stat.st_mode & 0x3ff).to_i32, stat.st_size.to_u64) do |ch| From b4daa154b75181bb1c654556adc53047a052d9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=9A=D0=BD?= =?UTF-8?q?=D1=8F=D0=B7=D1=8C=D0=BA=D0=BE=D0=B2?= Date: Sun, 14 Jun 2020 12:41:29 +0300 Subject: [PATCH 33/33] Compatibility with crystal 0.35 --- .gitignore | 1 + spec/ssh2_spec.cr | 8 ++++---- src/channel.cr | 20 ++++++++++---------- src/lib_ssh2.cr | 2 +- src/session.cr | 38 ++++++++++++++++++++------------------ src/sftp/attributes.cr | 4 ++-- 6 files changed, 38 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index d1f2bed..c40a619 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .crystal +.tool-versions diff --git a/spec/ssh2_spec.cr b/spec/ssh2_spec.cr index b01f1a8..4699550 100644 --- a/spec/ssh2_spec.cr +++ b/spec/ssh2_spec.cr @@ -3,7 +3,7 @@ require "spec" def connect_ssh SSH2::Session.open("localhost", 2222) do |session| - session.login_with_pubkey("root", "./spec/keys/id_rsa") + session.login_with_pubkey("root", "./spec/keys/id_rsa", "./spec/keys/id_rsa.pub") session.authenticated?.should be_true yield session end @@ -22,7 +22,7 @@ describe SSH2 do end it "should be able to scp transfer file" do - fn = "#{Time.now.epoch}.txt" + fn = "#{Time.utc.to_unix}.txt" connect_ssh do |session| session.scp_send(fn, 0o0644, 12) do |ch| ch.puts "hello world" @@ -59,7 +59,7 @@ describe SSH2::KnownHosts do known_hosts.size.should eq(2) known_hosts.map(&.name).includes?("localhost").should be_true known_hosts.write_file("known_hosts") - known_hosts.delete_if {|h| h.name == "localhost"} + known_hosts.delete_if { |h| h.name == "localhost" } known_hosts.size.should eq(1) end @@ -101,7 +101,7 @@ describe SSH2::SFTP do it "should be able to upload a file" do connect_ssh do |ssh| ssh.sftp_session do |sftp| - fn = "#{Time.now.epoch}_upload.txt" + fn = "#{Time.utc.to_unix}_upload.txt" file = sftp.open(fn, "wc", 0o644) file.puts "hello world!" attrs = file.fstat diff --git a/src/channel.cr b/src/channel.cr index 1b00b9f..be39bec 100644 --- a/src/channel.cr +++ b/src/channel.cr @@ -1,9 +1,8 @@ require "./session" class SSH2::Channel < IO - - PROCESS_SHELL = "shell" - PROCESS_EXEC = "exec" + PROCESS_SHELL = "shell" + PROCESS_EXEC = "exec" PROCESS_SUBSYSTEM = "subsystem" getter session : Session @@ -63,7 +62,7 @@ class SSH2::Channel < IO def process_startup(request, message) ret = LibSSH2.channel_process_startup(self, request, request.bytesize.to_u32, - message, message ? message.bytesize.to_u32 : 0_u32) + message, message ? message.bytesize.to_u32 : 0_u32) check_error(ret) end @@ -71,7 +70,7 @@ class SSH2::Channel < IO # leading "SIG"), and the second field populated with the error message. def exit_signal ret = LibSSH2.channel_get_exit_signal(self, out exitsignal, out exitsignal_len, - out errmsg, out errmsg_len, nil, nil) + out errmsg, out errmsg_len, nil, nil) check_error(ret) exitsignal_str = String.new(exitsignal, exitsignal_len) if exitsignal errmsg_str = String.new(errmsg, errmsg_len) if errmsg @@ -109,15 +108,17 @@ class SSH2::Channel < IO return 0 if eof? read(0, slice) end + def read(slice : Slice(UInt32)) return 0 if eof? read(0, slice) end - def read(slice : Slice(UInt64)) + + def read(slice : Slice(UInt64)) return 0 if eof? read(0, slice) end - + def write(slice : Slice(UInt8)) write(0, slice) end @@ -175,8 +176,8 @@ class SSH2::Channel < IO def request_pty(term, modes = nil, width = LibSSH2::TERM_WIDTH, height = LibSSH2::TERM_HEIGHT, width_px = LibSSH2::TERM_WIDTH_PX, height_px = LibSSH2::TERM_HEIGHT_PX) ret = LibSSH2.channel_request_pty(self, term, term.bytesize.to_u32, - modes, modes ? modes.bytesize.to_u32 : 0_u32, - width, height, width_px, height_px) + modes, modes ? modes.bytesize.to_u32 : 0_u32, + width, height, width_px, height_px) check_error(ret) end @@ -227,7 +228,6 @@ class SSH2::Channel < IO end class StreamIO < IO - getter channel : Channel getter stream_id : Int32 diff --git a/src/lib_ssh2.cr b/src/lib_ssh2.cr index 5f0c0f4..2433a62 100644 --- a/src/lib_ssh2.cr +++ b/src/lib_ssh2.cr @@ -10,7 +10,7 @@ lib LibSSH2 @[Flags] enum BlockDirections - Inbound, + Inbound Outbound end diff --git a/src/session.cr b/src/session.cr index 78861b8..4e991e1 100644 --- a/src/session.cr +++ b/src/session.cr @@ -35,23 +35,23 @@ class SSH2::Session # Login with username and password def login(username, password) ret = LibSSH2.userauth_password(self, username, username.bytesize.to_u32, - password, password.bytesize.to_u32, nil) + password, password.bytesize.to_u32, nil) check_error(ret) end # Login with username using pub/priv key values def login_with_data(username, privkey, pubkey, passphrase = nil) ret = LibSSH2.userauth_publickey_frommemory(self, username, username.bytesize.to_u32, - pubkey, LibC::SizeT.new(pubkey.bytesize), - privkey, LibC::SizeT.new(privkey.bytesize), - passphrase) + pubkey, LibC::SizeT.new(pubkey.bytesize), + privkey, LibC::SizeT.new(privkey.bytesize), + passphrase) check_error(ret) end # Login with username using pub/priv key files def login_with_pubkey(username, privkey, pubkey = nil, passphrase = nil) ret = LibSSH2.userauth_publickey_fromfile(self, username, username.bytesize.to_u32, - pubkey, privkey, passphrase) + pubkey, privkey, passphrase) check_error(ret) end @@ -242,8 +242,8 @@ class SSH2::Session # Allocate a new channel for exchanging data with the server. def open_channel(channel_type, window_size, packet_size, message) handle = LibSSH2.channel_open(self, channel_type, channel_type.bytesize.to_u32, - window_size.to_u32, packet_size.to_u32, - message, message ? message.bytesize.to_u32 : 0_u32) + window_size.to_u32, packet_size.to_u32, + message, message ? message.bytesize.to_u32 : 0_u32) Channel.new self, handle end @@ -280,16 +280,16 @@ class SSH2::Session end # Send a file to the remote host via SCP. + # LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) def scp_send(path, mode, size, mtime, atime) - handle = LibSSH2.scp_send(self, path, mode.to_i32, size.to_u64, -mtime,atime) #LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) + handle = LibSSH2.scp_send(self, path, mode.to_i32, size.to_u64, mtime, atime) check_error(LibSSH2.session_last_errno(self)) Channel.new self, handle end # 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.now.to_unix, atime = Time.now.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 @@ -304,14 +304,15 @@ mtime,atime) #LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) raise Errno.new("Unable to get stat for '#{path}'") end scp_send(path, (stat.st_mode & 0x3ff).to_i32, stat.st_size.to_u64, - stat.st_mtimespec.tv_sec, stat.st_atimespec.tv_sec) do |ch| + stat.st_mtimespec.tv_sec, stat.st_atimespec.tv_sec) do |ch| File.open(path, "r") do |f| IO.copy(f, ch) end end end + # Send a file from a local filesystem to the remote host via SCP. - def scp_send_file(path,localpath) + def scp_send_file(path, localpath) if LibC.stat(localpath, out stat) != 0 raise Errno.new("Unable to get stat for '#{path}'") end @@ -342,7 +343,7 @@ mtime,atime) #LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) # Download a file from the remote host via SCP to the local filesystem. def scp_recv_file(path, local_path = path) - min = -> (x : Int64|Int32, y : Int64|Int32) { x < y ? x : y} + min = ->(x : Int64 | Int32, y : Int64 | Int32) { x < y ? x : y } # libssh2 scp_recv method has a bug where its channel's read method doesn't # return 0 value to indicate the end of file(EOF). The only way to find EOF @@ -352,16 +353,17 @@ mtime,atime) #LibC::TimeT.new(mtime), LibC::TimeT.new(atime)) file_size = stat.st_size read_bytes = 0 File.open(local_path, "w") do |f| - buf = StaticArray(UInt8, 1024).new(0) # => 42#uninitialized UInt8[1024] + buf = StaticArray(UInt8, 1024).new(0) # => 42#uninitialized UInt8[1024] while read_bytes < file_size -bytes_to_read = min.call(buf.size, file_size - read_bytes).to_i32 -buf2 = Slice(UInt8).new( bytes_to_read) - len = ch.read(buf2).to_i32 + bytes_to_read = min.call(buf.size, file_size - read_bytes).to_i32 + buf2 = Slice(UInt8).new(bytes_to_read) + len = ch.read(buf2).to_i32 break if len <= 0 - f.write buf2.to_slice + f.write buf2.to_slice read_bytes += len end end + if file_size != read_bytes File.delete(local_path) raise SSH2Error.new "Premature end of file" diff --git a/src/sftp/attributes.cr b/src/sftp/attributes.cr index 3cd128f..133e923 100644 --- a/src/sftp/attributes.cr +++ b/src/sftp/attributes.cr @@ -52,7 +52,7 @@ class SSH2::SFTP::Attributes end def atime - Time.epoch(@stat.atime.to_i32) + Time.unix_ms(@stat.atime.to_i32) end def atime=(v : Time) @@ -60,7 +60,7 @@ class SSH2::SFTP::Attributes end def mtime - Time.epoch(@stat.mtime.to_i32) + Time.unix_ms(@stat.mtime.to_i32) end def mtime=(v : Time)