Quantcast
Channel: sshnet Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 1026

Commented Unassigned: SftpClient.WriteAllText saving wrong text. [1783]

$
0
0
I am using SshNet to update a text file on a Raspberry Pi server, running Raspbian. The server is running vsftpd as the FTP server.

The user modifies some text in the application (which was downloaded using the ReadAllText method) and then clicks save, which should save the edited text in the specified file on the server, using the WriteAllText method.

The problem is that the text somehow gets corrupted by using the WriteAllText method. Here is what I observed:

- If trying to edit somewhere around the middle of the text, it saves the text fine.
- If trying to add text to the end, it works fine.
- If trying to delete text from the end, all hell breaks loose. Example, editing the end of a text file:

Before deleting:
"Kind regards,


Department management"

After deleting everything after "Kind regards," and saving, this is what gets saved:
"Kind regards,
Department management"

- If trying to replace text at the end:

Replacing "Department management" with "Senior management":

"Kind regards,

Senior managementt" [Notice the double t at the end, it was not a typo. For different text, worse things that a double t appear.]

I have managed to work around it by saving the text to a file in the temp path and uploading it using the UploadFile method, which seems to be working fine.

I have attached the adapter I use for this (stripped of the stuff irrelevant to this problem).
Comments: ** Comment from web user: drieseng **

The problem is that - in SftpClient.WriteAllText (and all other WriteAll* methods) - we use SftpClient.CreateText which does not truncate the files if it already exists.

I thnik we should modify SftpClient.CreateText to use SftpClient.Create instead of SftpClient.OpenWrite. This will cause the file to be truncated if it already exists.

I also would propose to modify SftpClient.OpenWrite to open the remote file in append mode.
It does not make sense to start writing to the beginning of a file if you do not truncate it first, as that would leave part of the original file there if the new content contains less characters than the content of the existing file.


Viewing all articles
Browse latest Browse all 1026

Trending Articles