Problem with uploading to Unix

Feb 19, 2013 at 6:28 AM
Edited Feb 19, 2013 at 6:29 AM
When I try to use SCPClient to copy several big files (1 gb) from windows machine to unix machine, I have problem
First target file have same size , but others don't uppload full. They have less size then source files.
It's code
using (var sftp = new SftpClient(Host, Port, Login, Password))
        {
            List<string> movedFiles = new List<string>();
            try
            {

                string[] SourceFiles = Directory.GetFiles(SourceDirectory, this.SourceFileName + this.SourceFileFormat);
                {
                    foreach (string sourceFile in SourceFiles)
                    {
                        string resultTargetFile = TargetName;
                        if (String.IsNullOrEmpty(TargetFileName) || isMask)
                        {
                            resultTargetFile = Path.Combine(TargetDirectory, Path.GetFileName(sourceFile)).Replace(@"\", "/");
                        }
                        movedFiles.Add(resultTargetFile);
                        sftp.Connect();
                        using (var streamFile = File.OpenRead(sourceFile))
                        {
                            sftp.UploadFile(streamFile, resultTargetFile);
                        }
                        sftp.Disconnect();
                        cntOperation++;
                    }
                }

            }

            catch (ThreadAbortException)
            {
                if (sftp.IsConnected)
                {
                    foreach (string _movedFile in movedFiles)
                    {
                        if (sftp.Exists(_movedFile))
                            sftp.Delete(_movedFile);
                    }

                    sftp.Disconnect();
                }
            }
            catch (Exception ex)
            {
                Logger.WriteMessage(MessageType.Error, ex.ToString());
            }
        }
Feb 20, 2013 at 8:32 AM
Well connect inside the loop... not really good thing. You should keep connection opened. You can call connect every time, because it will do nothing if connection is opened.

for the file size. You should check the real file size and not the file size on the HDD. Depend on the Sector size... each file will be bigger or smaller. So what is the difference between local and uploaded file?
Feb 20, 2013 at 1:08 PM
Edited Feb 20, 2013 at 1:17 PM
At start I connected from outside a loop. But later I changed as it is now. I hoped that this will be solve the problem.

Now I have found problem and have solved it.

namespace Renci.SshNet
{
/// <summary>
/// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
/// </summary>
public partial class SftpClient : BaseClient
{
.
.
private void InternalUploadFile(Stream input, string path, Flags flags, SftpUploadAsyncResult asyncResult, Action<ulong> uploadCallback)
{
.
.
                {
                    expectedResponses++;
                }

                offset += (uint)bytesRead;

                bytesRead = input.Read(buffer, 0, buffer.Length);
            }
            else if (expectedResponses > 0)
            {
                //  Wait for expectedResponses to change
                lock (expectedResponsesLock)
                {
                    Monitor.Wait(expectedResponsesLock);
                }
            }
        } while (bytesRead > 0 || expectedResponses > 0);

        this._sftpSession.RequestClose(handle);
Problem occurs when network write\read speed is faster than disk write\read speed. Then the value of expectedResponses becomes equals to 0, and the file isn’t being uploaded completely.
I added new condition in method InternalUploadFile bytesRead > 0 ||

I waiting patch with this correction...
Coordinator
Feb 20, 2013 at 1:33 PM
Hi,

I actually solved this problem already but it didst make it to release version yet.
You can also download latest source code where this fix is already available.

Thanks,
Oleg
Feb 20, 2013 at 1:48 PM
Hello, Oleg!

When will you plan upload new release?
Coordinator
Feb 20, 2013 at 2:13 PM
Hi,

I usually wait for a while to have few more fixes in, also busy with another project right now so dont have much time for this one at a moment :(:(.

Thanks,
Oleg