1

Closed

sftpClient.WriteAllText corrupts uploaded data

description

Reproduce:
     var sftpClient = new SftpClient( server, port, user, 
            new PrivateKeyFile( new MemoryStream( Encoding.ASCII.GetBytes( privateKey ) ) ) );

        // 4* 1024 - data will be corrupted, 1*1024 - data will be transferred OK

        var dataToUpload = new string( '0', 4 * 1024 ) + "Z"; 

        sftpClient.Connect();
        sftpClient.WriteAllText( "incoming/text.txt", dataToUpload, Encoding.ASCII );
Closed Jan 27, 2013 at 12:18 AM by olegkap
Fixed in 22675

comments

olegkap wrote Jan 25, 2013 at 12:16 AM

Hi,

Thanks for reporting it.
After analyzing it I am surprised nobody else reported it before.
It was actually a big miscalculation on my part.
May be not many people using this feature anyway since originally I created it to see if I can do it.
But anyway, I made a fix so please check out latest commit and let me know if it works.

Here is a test I used to check if it work, in case you interesting:
            using (var sftp = new SftpClient(connectionInfo))
            {
                var dataToUpload = new string('0', 4 * 1024) + "Z";
                sftp.Connect();

                sftp.WriteAllText("text.txt", dataToUpload, Encoding.ASCII);
                var downloadedDatat = sftp.ReadAllText("text.txt", Encoding.ASCII);

                var file = sftp.OpenRead("text.txt");
                file.Position = 1024;
                using (var reader = new StreamReader(file))
                {
                    var t1 = reader.ReadToEnd();
                }

                sftp.Disconnect();
            }
Please let me know if it fixes your problem.

Thanks,
Oleg

mbergal wrote Jan 26, 2013 at 8:54 PM

Thanks, this helped.

Can we get a Nuget package with a fix?

olegkap wrote Jan 26, 2013 at 11:35 PM

Yea , no problem,
As soon as I finish some other minor fixes and failed test I will publish it,
Probably within next day or two.

Thanks,
Oleg