Skip to content

Commit 1fcfc0e

Browse files
pmeinhardtandreasknoepfle
authored andcommitted
Experiment with tar pipe for file transfer
Does not respect all the context options at the moment.
1 parent e54d1ec commit 1fcfc0e

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

examples/tarpipe.exs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{:ok, conn} = SSHKit.connect("127.0.0.1", port: 2222, user: "deploy", password: "deploy", silently_accept_hosts: true)
2+
3+
ctx =
4+
SSHKit.Context.new()
5+
|> SSHKit.Context.path("/tmp")
6+
|> SSHKit.Context.user("other")
7+
|> SSHKit.Context.group("other")
8+
9+
defmodule Xfer do
10+
# https://github.com/erlang/otp/blob/OTP-23.2.1/lib/ssh/src/ssh.hrl
11+
def to_binary(data) when is_list(data) do
12+
:erlang.iolist_to_binary(data)
13+
catch
14+
_ -> :unicode.characters_to_binary(data)
15+
end
16+
17+
def to_binary(data) when is_binary(data) do
18+
data
19+
end
20+
end
21+
22+
:ok =
23+
with {:ok, chan} <- SSHKit.Channel.open(conn, []) do
24+
# command = SSHKit.Context.build(ctx, "tar -x")
25+
command = "tar -x -C #{ctx.path}"
26+
IO.puts(command)
27+
28+
case SSHKit.Channel.exec(chan, command) do
29+
:success ->
30+
{:ok, tar} = :erl_tar.init(self(), :write, fn
31+
:position, {_, position} ->
32+
# IO.write("tar position: #{inspect(position)}")
33+
{:ok, 0}
34+
35+
:write, {_, data} ->
36+
:ok = SSHKit.Channel.send(chan, Xfer.to_binary(data))
37+
:ok
38+
39+
:close, _ ->
40+
:ok = SSHKit.Channel.eof(chan)
41+
:ok
42+
end)
43+
44+
source = "test/fixtures"
45+
# :ok = :erl_tar.add(tar, to_charlist(source), to_charlist(source))
46+
47+
with {:ok, names} <- File.ls(source) do
48+
Enum.each(names, fn name ->
49+
path = Path.join(source, name)
50+
51+
with {:ok, stat} <- File.stat(path, time: :posix) do
52+
IO.puts("#{stat.type}: #{path}")
53+
54+
:ok = :erl_tar.add(tar, to_charlist(path), to_charlist(path), atime: stat.atime, mtime: stat.mtime, ctime: stat.ctime)
55+
end
56+
end)
57+
end
58+
59+
:ok = :erl_tar.close(tar)
60+
61+
:failure ->
62+
{:error, :failure}
63+
64+
other ->
65+
other
66+
end
67+
end
68+
69+
:ok = SSHKit.close(conn)

0 commit comments

Comments
 (0)