Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sftp.get downloads only the last chunk of file #341

Open
mumische opened this issue Jun 24, 2022 · 1 comment · May be fixed by #638
Open

sftp.get downloads only the last chunk of file #341

mumische opened this issue Jun 24, 2022 · 1 comment · May be fixed by #638

Comments

@mumische
Copy link

SUMMARY

I'm trying to download configuration file from remote router and getting only last chunk of the file.

ISSUE TYPE
  • Bug Report
PYLISSH and LIBSSH VERSION
0.4.0
OS / ENVIRONMENT

Ubuntu 22.04 LTS
Python 3.10.4

STEPS TO REPRODUCE

Narrowing the problem I wrote a piece of code below.
You need a file on remote host to reproduce it.

from pylibsshext.session import Session, AutoAddPolicy

hssh = Session()
p = AutoAddPolicy()
hssh.set_missing_host_key_policy(p)

HOST = 'hostname'
USER = 'username'
PASSWORD = 'password'
TIMEOUT = 30
PORT = 22

hssh.connect(
    host=HOST,
    user=USER,
    password=PASSWORD,
    timeout=TIMEOUT,
    port=PORT,
)
sftp = hssh.sftp()
sftp.get('m8.rsc', 'm8.rsc')
EXPECTED RESULTS

File 'm8.rsc' in current directory, size of 16K

ACTUAL RESULTS

File 'm8.rsc', containing last 223b of source file.


As I found out, the problem lays in line #103 of sftp.pyx

           with open(local_file, 'wb+') as f:

In python3, "w" parameter truncates the file, so during reading the source file new data overwrites old.
Please check and consider changing open mode to 'ab'

@Jakuje
Copy link
Contributor

Jakuje commented May 21, 2024

The link to the actual code line:

with open(local_file, 'wb+') as f:

Reading through the python questions, this sounds like a common confusion and I think you are right that this should be ab instead of wb+:

https://stackoverflow.com/questions/1466000/difference-between-modes-a-a-w-w-and-r-in-built-in-open-function/30566011#30566011

Wondering why nobody noticed this earlier.

Jakuje added a commit to Jakuje/pylibssh that referenced this issue Aug 30, 2024
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Jakuje added a commit to Jakuje/pylibssh that referenced this issue Aug 30, 2024
Fixes: ansible#341
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
@Jakuje Jakuje linked a pull request Aug 30, 2024 that will close this issue
Jakuje added a commit to Jakuje/pylibssh that referenced this issue Aug 30, 2024
Fixes: ansible#341
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Jakuje added a commit to Jakuje/pylibssh that referenced this issue Aug 30, 2024
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Jakuje added a commit to Jakuje/pylibssh that referenced this issue Sep 11, 2024
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Jakuje added a commit to Jakuje/pylibssh that referenced this issue Sep 11, 2024
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Jakuje added a commit to Jakuje/pylibssh that referenced this issue Sep 11, 2024
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
webknjaz pushed a commit to Jakuje/pylibssh that referenced this issue Sep 12, 2024
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
webknjaz pushed a commit to Jakuje/pylibssh that referenced this issue Sep 12, 2024
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants