using System;
using System.IO;
using System.Threading;
using EnterpriseDT.Util.Debug;
using EnterpriseDT.Net.Ftp;
using EnterpriseDT.Net.Ftp.Pro;
public class RecursiveOps {
private static Logger log = Logger.GetLogger(typeof(RecursiveOps));
public static void Main(string[] args)
{
if (args.Length != 4) {
System.Console.Out.WriteLine(
"Usage: RecursiveOps remote-host username password remote-dir");
System.Console.Out.WriteLine("See readme.html for instructions.");
System.Environment.Exit(1);
}
string host = args[0];
string user = args[1];
string password = args[2];
string remoteDir = args[3];
Logger.CurrentLevel = Level.INFO;
try {
FTPClient ftp = new FTPClient(host);
ftp.Login(user, password);
int id = new Random().Next();
string tmpDir1 = remoteDir + "_" + id + "_1";
RecursiveOperations recurse = new RecursiveOperations();
recurse.Get(ftp, tmpDir1, remoteDir, true);
log.Info("Retrieved remote directory " + remoteDir + " to " + tmpDir1);
recurse.Put(ftp, tmpDir1, tmpDir1, true);
log.Info("Put local directory " + tmpDir1 + " to " + tmpDir1);
string tmpDir2 = remoteDir + "_" + id + "_2";
recurse.Get(ftp, tmpDir2, tmpDir1, true);
log.Info("Retrieved remote directory " + tmpDir1 + " to " + tmpDir2);
recurse.Delete(ftp, tmpDir1);
log.Info("Deleted remote directory " + tmpDir1);
CompareAndDeleteDirectories(new DirectoryInfo(tmpDir1), new DirectoryInfo(tmpDir2));
log.Info("Directories are identical");
ftp.Quit();
}
catch (Exception e)
{
log.Error("Caught exception " + e.GetType().FullName + " " + e.Message, e);
}
}
private static void CompareAndDeleteDirectories(DirectoryInfo dir1, DirectoryInfo dir2)
{
DirectoryInfo[] dirs1 = dir1.GetDirectories();
DirectoryInfo[] dirs2 = dir2.GetDirectories();
if (dirs1.Length != dirs2.Length)
throw new ApplicationException ("Directory counts do not match");
for (int i = 0; i < dirs1.Length; i++)
{
if (!dirs1[i].Name.Equals(dirs2[i].Name))
throw new ApplicationException ("Directory names do not match");
CompareAndDeleteDirectories(dirs1[i], dirs2[i]);
}
FileInfo[] files1 = dir1.GetFiles();
FileInfo[] files2 = dir2.GetFiles();
if (files1.Length != files2.Length)
throw new ApplicationException ("Directory file counts do not match");
for (int i = 0; i < files1.Length; i++)
{
if (!files1[i].Name.Equals(files2[i].Name))
throw new ApplicationException ("File name do not match");
CompareAndDeleteFiles(files1[i], files2[i]);
}
log.Debug("Deleting local directory: " + dir1.Name);
dir1.Delete();
log.Debug("Deleting local directory: " + dir2.Name);
dir2.Delete();
}
private static void CompareAndDeleteFiles(FileInfo file1, FileInfo file2)
{
BufferedStream is1 = null;
BufferedStream is2 = null;
try
{
if (file1.Length != file2.Length)
throw new ApplicationException ("File lengths do not match");
is1 = new BufferedStream(new FileStream(file1.FullName, FileMode.Open, FileAccess.Read));
is2 = new BufferedStream(new FileStream(file2.FullName, FileMode.Open, FileAccess.Read));
int ch1 = 0;
int ch2 = 0;
while ((ch1 = is1.ReadByte()) != - 1 && (ch2 = is2.ReadByte()) != - 1)
{
if (ch1 != ch2)
throw new ApplicationException ("Contents not equal");
}
}
finally
{
if (is1 != null)
is1.Close();
if (is2 != null)
is2.Close();
}
log.Debug("Deleting local file: " + file1.Name);
file1.Delete();
log.Debug("Deleting local file: " + file2.Name);
file2.Delete();
}
}