public int Put(Stream src, IActivityIOPath dst, Dev2CRUDOperationTO args, string whereToPut, List<string> filesToCleanup)
{
int result = -1;
using (src)
{
//2013.05.29: Ashley Lewis for bug 9507 - default destination to source directory when destination is left blank or if it is not a rooted path
if (!Path.IsPathRooted(dst.Path))
{
//get just the directory path to put into
if (whereToPut != null)
{
//Make the destination directory equal to that directory
dst = ActivityIOFactory.CreatePathFromString(whereToPut + "\\" + dst.Path, dst.Username, dst.Password,dst.PrivateKeyFile);
}
}
if (args.Overwrite || !args.Overwrite && !FileExist(dst))
{
_fileLock.EnterWriteLock();
try
{
if (!RequiresAuth(dst))
{
using (src)
{
File.WriteAllBytes(dst.Path, src.ToByteArray());
result = (int)src.Length;
}
}
else
{
// handle UNC path
SafeTokenHandle safeTokenHandle;
bool loginOk = LogonUser(ExtractUserName(dst), ExtractDomain(dst), dst.Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);
if (loginOk)
{
using (safeTokenHandle)
{
WindowsIdentity newID = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
using (WindowsImpersonationContext impersonatedUser = newID.Impersonate())
{
// Do the operation here
using (src)
{
File.WriteAllBytes(dst.Path, src.ToByteArray());
result = (int)src.Length;
}
// remove impersonation now
impersonatedUser.Undo();
}
}
}
else
{
// login failed
throw new Exception("Failed to authenticate with user [ " + dst.Username + " ] for resource [ " + dst.Path + " ] ");
}
}
}
finally
{
_fileLock.ExitWriteLock();
}
}
}
return result;
}