void ValidateSourceAndDestinationContents(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args) { if (!args.Overwrite) { var srcContentsFolders = src.ListFoldersInDirectory(src.IOPath); foreach (var sourcePath in srcContentsFolders) { var sourceEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(sourcePath); IList <string> dirParts = sourceEndPoint.IOPath.Path.Split(sourceEndPoint.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var directory = dirParts.Last(); var destinationPath = ActivityIOFactory.CreatePathFromString(dst.Combine(directory), dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); var destinationEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(destinationPath); if (destinationEndPoint.PathExist(destinationEndPoint.IOPath)) { ValidateSourceAndDestinationContents(sourceEndPoint, destinationEndPoint, args); } } var srcContents = src.ListFilesInDirectory(src.IOPath); var dstContents = dst.ListFilesInDirectory(dst.IOPath); var sourceFileNames = srcContents.Select(srcFile => GetFileNameFromEndPoint(src, srcFile)).ToList(); var destinationFileNames = dstContents.Select(dstFile => GetFileNameFromEndPoint(dst, dstFile)).ToList(); if (destinationFileNames.Count > 0) { var commonFiles = sourceFileNames.Where(destinationFileNames.Contains).ToList(); if (commonFiles.Count > 0) { var fileNames = commonFiles.Aggregate("", (current, commonFile) => current + "\r\n" + commonFile); throw new Exception(string.Format(ErrorResource.FileExistInDestinationFolder, fileNames)); } } } }
private bool CreateDirectoriesForPath(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, IList <string> dirParts) { var maxDepth = dirParts.Count - 1; var pos = 0; var origPath = dst.IOPath; try { while (pos <= maxDepth) { var toCreate = ActivityIOFactory.CreatePathFromString(dirParts[pos], dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); if (dst.PathExist(toCreate)) { pos++; continue; } dst.IOPath = toCreate; if (!CreateDirectory(dst, args)) { return(false); } pos++; } } finally { dst.IOPath = origPath; } return(true); }
bool PerformTransfer(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, string origDstPath, IActivityIOPath p, bool result) { try { if (dst.PathIs(dst.IOPath) == enPathType.Directory) { var cpPath = ActivityIOFactory.CreatePathFromString( $"{origDstPath}{dst.PathSeperator()}{Dev2ActivityIOPathUtils.ExtractFileName(p.Path)}", dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); var path = cpPath.Path; DoFileTransfer(src, dst, args, cpPath, p, path, ref result); } else { if (args.Overwrite || !dst.PathExist(dst.IOPath)) { var tmp = origDstPath + @"\\" + Dev2ActivityIOPathUtils.ExtractFileName(p.Path); var path = ActivityIOFactory.CreatePathFromString(tmp, dst.IOPath.Username, dst.IOPath.Password, dst.IOPath.PrivateKeyFile); DoFileTransfer(src, dst, args, path, p, path.Path, ref result); } } } catch (Exception ex) { Dev2Logger.Error(ex, GlobalConstants.WarewolfError); } return(result); }
public string UnZip(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2UnZipOperationTO args) { string status; try { status = ValidateUnzipSourceDestinationFileOperation(src, dst, args, () => { ZipFile zip; var tempFile = string.Empty; if (src.RequiresLocalTmpStorage()) { var tmpZip = CreateTmpFile(); using (var s = src.Get(src.IOPath, _filesToDelete)) { _fileWrapper.WriteAllBytes(tmpZip, s.ToByteArray()); } tempFile = tmpZip; zip = ZipFile.Read(tempFile); } else { zip = ZipFile.Read(src.Get(src.IOPath, _filesToDelete)); } if (dst.RequiresLocalTmpStorage()) { // unzip locally then Put the contents of the archive to the dst end-point var tempPath = _common.CreateTmpDirectory(); _common.ExtractFile(args, zip, tempPath); var endPointPath = ActivityIOFactory.CreatePathFromString(tempPath, string.Empty, string.Empty); var endPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(endPointPath); Move(endPoint, dst, new Dev2CRUDOperationTO(args.Overwrite)); } else { _common.ExtractFile(args, zip, dst.IOPath.Path); } if (src.RequiresLocalTmpStorage()) { _fileWrapper.Delete(tempFile); } return(ResultOk); }); } finally { _filesToDelete.ForEach(RemoveTmpFile); } return(status); }
public string UnZip(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2UnZipOperationTO args) { string status; try { status = _validator.ValidateUnzipSourceDestinationFileOperation(src, dst, args, () => { IIonicZipFileWrapper zip; var tempFile = string.Empty; if (src.RequiresLocalTmpStorage()) { var tmpZip = _implementation.CreateTmpFile(); using (var s = src.Get(src.IOPath, _filesToDelete)) { _fileWrapper.WriteAllBytes(tmpZip, s.ToByteArray()); } tempFile = tmpZip; zip = _zipFileFactory.Read(tempFile); } else { zip = _zipFileFactory.Read(src.Get(src.IOPath, _filesToDelete)); } if (dst.RequiresLocalTmpStorage()) { var tempPath = _common.CreateTmpDirectory(); _common.ExtractFile(args, zip, tempPath); var endPointPath = ActivityIOFactory.CreatePathFromString(tempPath, string.Empty, string.Empty); var endPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(endPointPath); Move(endPoint, dst, new Dev2CRUDOperationTO(args.Overwrite)); } else { _common.ExtractFile(args, zip, dst.IOPath.Path); } if (src.RequiresLocalTmpStorage()) { _fileWrapper.Delete(tempFile); } return(ActivityIOBrokerBaseDriver.ResultOk); }); } finally { RemoveAllTmpFiles(); } return(status); }
bool TransferFile(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, string path, IActivityIOPath p, bool result) { var tmpPath = ActivityIOFactory.CreatePathFromString(path, dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); var tmpEp = ActivityIOFactory.CreateOperationEndPointFromIOPath(tmpPath); var whereToPut = GetWhereToPut(src, dst); using (var s = src.Get(p, _filesToDelete)) { if (tmpEp.Put(s, tmpEp.IOPath, args, whereToPut, _filesToDelete) < 0) { result = false; } s.Close(); s.Dispose(); } return(result); }
string TransferTempZipFileToDestination(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2ZipOperationTO args, string tmpZip) { string result; using (Stream s2 = new MemoryStream(_fileWrapper.ReadAllBytes(tmpZip))) { dst = ActivityIOFactory.CreateOperationEndPointFromIOPath( ActivityIOFactory.CreatePathFromString(dst.IOPath.Path, dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile)); var zipTransferArgs = new Dev2CRUDOperationTO(args.Overwrite); result = ResultOk; if (src.RequiresLocalTmpStorage()) { if (dst.Put(s2, dst.IOPath, zipTransferArgs, null, _filesToDelete) < 0) { result = ResultBad; } } else { var fileInfo = new FileInfo(src.IOPath.Path); if (fileInfo.Directory != null && Path.IsPathRooted(fileInfo.Directory.ToString())) { if (dst.Put(s2, dst.IOPath, zipTransferArgs, fileInfo.Directory.ToString(), _filesToDelete) < 0) { result = ResultBad; } } else { if (dst.Put(s2, dst.IOPath, zipTransferArgs, null, _filesToDelete) < 0) { result = ResultBad; } } } } return(result); }
public string ZipDirectoryToALocalTempFile(IActivityIOOperationsEndPoint src, IDev2ZipOperationTO args) { var tmpDir = _common.CreateTmpDirectory(); var tempFilename = CreateTmpFile(); var tmpPath = ActivityIOFactory.CreatePathFromString(tmpDir, "", ""); var tmpEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(tmpPath); TransferDirectoryContents(src, tmpEndPoint, new Dev2CRUDOperationTO(true)); using (var zip = new ZipFile()) { zip.SaveProgress += (sender, eventArgs) => { if (eventArgs.CurrentEntry != null) { Dev2Logger.Debug($"Event Type: {eventArgs.EventType} Total Entries: {eventArgs.EntriesTotal} Entries Saved: {eventArgs.EntriesSaved} Current Entry: {eventArgs.CurrentEntry.FileName}", GlobalConstants.WarewolfDebug); } }; if (args.ArchivePassword != string.Empty) { zip.Password = args.ArchivePassword; } zip.CompressionLevel = _common.ExtractZipCompressionLevel(args.CompressionRatio); var toAdd = ListDirectory(tmpEndPoint, ReadTypes.FilesAndFolders); foreach (var p in toAdd) { if (tmpEndPoint.PathIs(p) == enPathType.Directory) { var directoryPathInArchive = p.Path.Replace(tmpPath.Path, ""); zip.AddDirectory(p.Path, directoryPathInArchive); } else { zip.AddFile(p.Path, "."); } } zip.Save(tempFilename); } var dir = new DirectoryWrapper(); dir.CleanUp(tmpDir); return(tempFilename); }
void RecursiveCopy(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args) { try { var srcContentsFolders = src.ListFoldersInDirectory(src.IOPath); Task.WaitAll(srcContentsFolders.Select(sourcePath => Task.Run(() => { var sourceEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(sourcePath); IList <string> dirParts = sourceEndPoint.IOPath.Path.Split(sourceEndPoint.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var destinationPath = ActivityIOFactory.CreatePathFromString(dst.Combine(dirParts.Last()), dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); var destinationEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(destinationPath); dst.CreateDirectory(destinationPath, args); TransferDirectoryContents(sourceEndPoint, destinationEndPoint, args); })).ToArray()); } catch (AggregateException e) { var message = e.InnerExceptions.Where(exception => !string.IsNullOrEmpty(exception?.Message)).Aggregate(string.Empty, (current, exception) => current + exception.Message + "\r\n"); throw new Exception(message, e); } }
string CreateEndPoint(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, bool createToFile) { var result = ResultOk; var activityIOPath = dst.IOPath; var dirParts = MakeDirectoryParts(activityIOPath, dst.PathSeperator()); var deepestIndex = -1; var startDepth = dirParts.Count - 1; var pos = startDepth; while (pos >= 0 && deepestIndex == -1) { var tmpPath = ActivityIOFactory.CreatePathFromString(dirParts[pos], activityIOPath.Username, activityIOPath.Password, true, activityIOPath.PrivateKeyFile); try { if (dst.ListDirectory(tmpPath) != null) { deepestIndex = pos; } } catch (Exception e) { Dev2Logger.Warn(e.Message, "Warewolf Warn"); } finally { pos--; } } pos = deepestIndex + 1; var ok = true; var origPath = dst.IOPath; while (pos <= startDepth && ok) { var toCreate = ActivityIOFactory.CreatePathFromString(dirParts[pos], dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile); dst.IOPath = toCreate; ok = CreateDirectory(dst, args); pos++; } dst.IOPath = origPath; if (!ok) { result = ResultBad; } else { if (dst.PathIs(dst.IOPath) == enPathType.File && createToFile && !CreateFile(dst, args)) { result = ResultBad; } } return(result); }
private IList <IActivityIOPath> ListDirectoriesAccordingToType(IActivityIOPath src, ReadTypes type) { IList <IActivityIOPath> result = new List <IActivityIOPath>(); string path = src.Path; if (!path.EndsWith("\\") && PathIs(src) == enPathType.Directory) { path += "\\"; } if (!RequiresAuth(src)) { try { IEnumerable <string> dirs; if (!Dev2ActivityIOPathUtils.IsStarWildCard(path)) { if (Directory.Exists(path)) { dirs = GetDirectoriesForType(path, string.Empty, type); } else { throw new Exception(string.Format(ErrorResource.DirectoryDoesNotExist, path)); } } else { // we have a wild-char path ;) string baseDir = Dev2ActivityIOPathUtils.ExtractFullDirectoryPath(path); string pattern = Dev2ActivityIOPathUtils.ExtractFileName(path); dirs = GetDirectoriesForType(baseDir, pattern, type); } if (dirs != null) { foreach (string d in dirs) { result.Add(ActivityIOFactory.CreatePathFromString(d, src.Username, src.Password, true, src.PrivateKeyFile)); } } } catch (Exception) { throw new Exception(string.Format(ErrorResource.DirectoryNotFound, src.Path)); } } else { try { // handle UNC path SafeTokenHandle safeTokenHandle; bool loginOk = LogonUser(ExtractUserName(src), ExtractDomain(src), src.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 try { IEnumerable <string> dirs; if (!Dev2ActivityIOPathUtils.IsStarWildCard(path)) { dirs = GetDirectoriesForType(path, string.Empty, type); } else { // we have a wild-char path ;) string baseDir = Dev2ActivityIOPathUtils.ExtractFullDirectoryPath(path); string pattern = Dev2ActivityIOPathUtils.ExtractFileName(path); dirs = GetDirectoriesForType(baseDir, pattern, type); } if (dirs != null) { foreach (string d in dirs) { result.Add(ActivityIOFactory.CreatePathFromString(d, src.Username, src.Password, src.PrivateKeyFile)); } } } catch (Exception) { throw new Exception(string.Format(ErrorResource.DirectoryNotFound, src.Path)); } // remove impersonation now impersonatedUser.Undo(); newID.Dispose(); } } } else { // login failed throw new Exception(string.Format(ErrorResource.FailedToAuthenticateUser, src.Username, src.Path)); } } catch (Exception ex) { Dev2Logger.Error(ex); throw; } } return(result); }
public int Put(Stream src, IActivityIOPath dst, IDev2CRUDOperationTO 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(string.Format(ErrorResource.FailedToAuthenticateUser, dst.Username, dst.Path)); } } } finally { _fileLock.ExitWriteLock(); } } } return(result); }