This project is read-only.

SFTP upload throws an exception while uploading blank file

Feb 10, 2012 at 12:39 PM
Edited Feb 10, 2012 at 12:40 PM

Hi everyone,

First of all I want to say as far as I can follow this is a great project and you are doing a great job. Many thanks.

I have a project that I transfer files between two SFTP servers. I download the file to my local file system first and upload to the other SFTP server. I have the following code for uploading:

 

 

                using (SftpClient sFtpClient = new SftpClient(hostname, port, username, password))
                {
                    sFtpClient.Connect();
                    for (int i = 0; i < faturalar.Count; i++)
                    {
                        using (System.IO.Stream stream = new System.IO.FileStream(faturalar[i].ToString(), FileMode.Open))
                        {
                            sFtpClient.UploadFile(stream, ftpPath + "some file name", true);
                        }
                    } 
                }

 

It works very well except on one condition, when I want to upload a blank file (0 bytes) to the my client's SFTP server it uploads the file but throws an exception. Strange thing is that when I want to upload a blank file to my own SFTP server, it uploads without exception. I debugged the code and tracked down the exception to following function:

 

        internal void WaitHandle(WaitHandle waitHandle, TimeSpan operationTimeout)
        {
            var waitHandles = new WaitHandle[]
                {
                    this._errorOccuredWaitHandle,
                    waitHandle,
                };

            var index = EventWaitHandle.WaitAny(waitHandles, operationTimeout);

            if (index < 1)
            {
                throw this._exception;
            }
            else if (index > 1)
            {
                //  throw time out error
                throw new SshOperationTimeoutException(string.Format(CultureInfo.CurrentCulture, "Sftp operation has timed out."));
            }
        }

 

At the red line index gets the value 0 for faulty operation. I am using the 7th December version. (12774 I guess?) 

Any ideas?

Feb 10, 2012 at 4:00 PM

Hi,

 

This is strange and defiantly sounds like a bug.

 

Can you open an issue for this error so I dont forget to look at it later?

 

Thanks,

Oleg

Feb 10, 2012 at 6:30 PM

Sargas,

 

I just did following test and it works fine for me here.

            using (var sftp = new SftpClient(connectionInfo))
            {
                sftp.Connect();
                using (System.IO.Stream stream = new System.IO.FileStream(@"D:\empty.txt", FileMode.Open))
                {
                    sftp.UploadFile(stream, "empty", true);
                }
                sftp.Disconnect();
            }

Let me know if this test will work for you.

 

Thanks,

Oleg

Feb 13, 2012 at 4:14 PM

Hi Oleg,

I tried your code and still throws exception. Additionally I tried to upload to my own SFTP server and it uploads without any problems. I think that case may be specific to my client's SFTP server and I talked to her about any logs, but no help from there either. The exception message I get from dll is "Failure.". Unlucky for me I don't have any other server to test it upon. Since I shouldn't upload any blank files in this case it won't be a problem, but we are planning to use the dll more in the future so I hope we won't face it again.

Thanks again.

Feb 14, 2012 at 12:20 AM

Hi,

 

So it looks like there is a problem with server implementation of SSH or to be more exact of SFTP part of it.

 

Unfortunately there should be support for empty file since its completely legal to upload 0 byte file, it simple will create a new file.

 

Let me know if there is anything else I can help with, unfortunatly without access to this type of server will be hard for me to debug it and see if I causing this problem.

 

Thanks,

Oleg