Hi,I have the following environment:- Win7 / 64- .NET 3.5- MPC-Target :OpenSSH_6.0p1, OpenSSL 1.0.0j 10 May 2012- SSH.NET commit 19813I want to upload a directory structure (recursive, depth = 2) from the PC to a MPC basedLinux remote system. The total amount of data is about 8MB with 23 files from55 bytes to 4MB.Calling mScp.Upload(new DirectoryInfo(localDir), remoteDir) does not result anyerror and the Uploading event shoes uploading all files. But after Upload() returnedThere are a random number of missing files on the target (between 1 and 5).It tried the same with a PC based Linux as target what resulted in the same behaviour(OpenSSH_5.8p1 Debian-7ubuntu1, OpenSSL 1.0.0e 6 Sep 2011). It seems theproblem is caused anywhere within SSH.NET.Does anyone have a solution?Extension:I tried to workaround by replacing Upload(new DirectoryInfo(...)) by several singlecalls of Upload(new FileInfo(...)). This worked but is too slow for my application.
Comments: ** Comment from web user: Schmid **
Gert,
Please apologize the delay. I did not have the time for providing a demo the last days.
The demo creates a temporary file structure an cyclically uploads it to the remote device.
I'm referencing Renci.SshNet.NET35.dll and running the release build.
I'm using revision 25006 in which I already integrated a patch from a SSH.NET discussion for
using absolute path strings (see attached file).
Thank you for reopening the issue.
Ulrich
```
using System;
using System.Text;
using Renci.SshNet;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string address = "169.254.177.104";
const string user = "root";
const string pass = "password";
const string mRemoteDir = "/tmp/test/";
static SshClient mSsh;
static ScpClient mScp;
//************************************************************************
static int Execute(string command)
{
return Execute(command, false);
}
//************************************************************************
static int Execute(string command, bool silent)
{
if (!silent)
{
Console.WriteLine(string.Format("SSH.Execute : {0}", command));
}
SshCommand cmd = mSsh.RunCommand(command);
if (!silent)
{
Console.WriteLine(String.Format("ExitCode: {0}", cmd.ExitStatus));
if (!String.IsNullOrEmpty(cmd.Result))
{
Console.WriteLine(string.Format("Result:\n{0}", cmd.Result));
}
if (!String.IsNullOrEmpty(cmd.Error))
{
Console.WriteLine(string.Format("Error:\n{0}", cmd.Error));
}
}
return cmd.ExitStatus;
}
//************************************************************************
static void Main(string[] args)
{
var localDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
try
{
Directory.CreateDirectory(localDir);
CreateTestData(localDir);
using (mSsh = new SshClient(address, user, pass))
{
mSsh.Connect();
using (mScp = new ScpClient(address, user, pass))
{
mScp.Connect();
mScp.ErrorOccurred += new EventHandler<Renci.SshNet.Common.ExceptionEventArgs>(mScp_ErrorOccurred);
mScp.Uploading += new EventHandler<Renci.SshNet.Common.ScpUploadEventArgs>(mScp_Uploading);
for (int i = 0; i < 10; ++i)
{
UploadDir(localDir, mRemoteDir);
}
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(String.Format("Exception:\n{0}", ex));
}
finally
{
if (Directory.Exists(localDir))
{
Directory.Delete(localDir);
}
}
Console.WriteLine("Done");
Console.ReadLine();
}
//*************************************************************************
static void mScp_ErrorOccurred(object sender, Renci.SshNet.Common.ExceptionEventArgs e)
{
Console.WriteLine(String.Format("Error: {0}", e));
}
//*************************************************************************
static void mScp_Uploading(object sender, Renci.SshNet.Common.ScpUploadEventArgs e)
{
//Console.WriteLine(String.Format("Uploading {0} ({1}/{2})"
// , e.Filename
// , e.Uploaded
// , e.Size));
}
//*************************************************************************
private static void UploadDir(string localDir, string remoteDir)
{
string remoteDirPath = remoteDir.TrimEnd('/');
Execute(String.Format("rm -rf {0}", remoteDirPath), true);
Console.WriteLine("Upload to " + remoteDir);
DateTime startTime = DateTime.Now;
mScp.Upload(new DirectoryInfo(localDir)
, remoteDirPath);
Console.WriteLine("{0}", DateTime.Now - startTime);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(String.Format("Valid : {0}"
, ValidateUpload(localDir, remoteDirPath)));
Console.ResetColor();
}
//*************************************************************************
private static void CreateTestData(string localDir)
{
CreateTestFiles(localDir);
for (int subDirIndex = 0; subDirIndex < 1; ++subDirIndex)
{
var subDir = Path.Combine(localDir, subDirIndex.ToString());
Directory.CreateDirectory(subDir);
CreateTestFiles(subDir);
}
}
//*************************************************************************
private static void CreateTestFiles(string dir)
{
var fileContent = new StringBuilder("HelloWorld\n");
for (int fileIndex = 1; fileIndex < 20; ++fileIndex)
{
var fileName = String.Format("{0}.txt", fileIndex);
var filePath = Path.Combine(dir, fileName);
fileContent.Append(fileContent.ToString());
File.WriteAllText(filePath, fileContent.ToString());
}
}
//*************************************************************************
private static bool DoRecursive( string localDir
, string remoteDir
, Func<string, string, bool> directoryDelegate
, Func<string, string, bool> fileDelegate)
{
bool result = true;
result &= directoryDelegate(localDir, remoteDir);
foreach (string localFile in Directory.GetFiles(localDir))
{
string remoteFile = String.Format("{0}/{1}"
, remoteDir
, Path.GetFileName(localFile));
result &= fileDelegate(localFile, remoteFile);
}
foreach (string localSubDir in Directory.GetDirectories(localDir))
{
string remoteSubDir = String.Format("{0}/{1}"
, remoteDir
, Path.GetFileName(localSubDir));
result &= DoRecursive( localSubDir
, remoteSubDir
, directoryDelegate
, fileDelegate);
}
return result;
}
//*************************************************************************
private static bool ValidateUpload(string localDir, string remoteDir)
{
return DoRecursive(localDir, remoteDir
, (locDir, rmtDir) =>
{
bool validDir= true;
if (0 != Execute(String.Format("[ -d {0} ]", rmtDir), true))
{
Console.WriteLine(String.Format("Missing remote directory {0}"
, rmtDir));
validDir = false;
}
return validDir;
}
, (locFile, rmtFile) =>
{
bool validFile = true;
if (0 != Execute(String.Format("[ -f {0} ]", rmtFile), true))
{
Console.WriteLine(String.Format("Missing remote file {0}"
, rmtFile));
validFile = false;
}
return validFile;
}
);
}
}
}
```
The output is completely curious:
```
Upload to /tmp/test/
00:00:09.0710000
Missing remote file /tmp/test/0/8.txt
Missing remote file /tmp/test/0/9.txt
Valid : False
Upload to /tmp/test/
00:00:09.0390000
Missing remote file /tmp/test/0/9.txt
Valid : False
Upload to /tmp/test/
00:00:09.0390000
Valid : True
Upload to /tmp/test/
00:00:09.0460000
Missing remote file /tmp/test/0/8.txt
Missing remote file /tmp/test/0/9.txt
Valid : False
```