PBI : 1172 Status : New Purpose : To provide overwrite value to CRUD path operations
Inheritance: IPathOverwrite
 /// <summary>
 /// Renames a file or folder from src to dst as per the value of args
 /// </summary>
 /// <param name="src"></param>
 /// <param name="dst"></param>
 /// <param name="args"></param>
 public string Rename(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args)
 {
     Source = src;
     Destination = dst;
     Dev2CRUDOperationTO = args;
     return "Successful";
 }
        public void CopyFileWithPathsExpectedRecursiveCopy()
        {
            var innerDir = Guid.NewGuid().ToString();
            var tempPath = Path.GetTempPath();
            var tempFileName = Path.GetFileName(Path.GetTempFileName());
            const string TempData = "some string data";
            if(tempFileName != null)
            {
                var tempFile = Path.Combine(tempPath, innerDir, innerDir, tempFileName);
                string directoryName = Path.GetDirectoryName(tempFile);
                if(directoryName != null)
                {
                    Directory.CreateDirectory(directoryName);
                }
                var upperLevelDir = Path.Combine(tempPath, innerDir);
                File.WriteAllText(tempFile, TempData);
                var dst = Path.Combine(tempPath, Guid.NewGuid().ToString());
                var scrEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(ActivityIOFactory.CreatePathFromString(upperLevelDir, string.Empty, null, true));
                var dstEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(ActivityIOFactory.CreatePathFromString(dst, string.Empty, null, true));

                var moveTO = new Dev2CRUDOperationTO(true);
                ActivityIOFactory.CreateOperationsBroker().Copy(scrEndPoint, dstEndPoint, moveTO);
                var newFilePath = Path.Combine(dst, innerDir, tempFileName);
                Assert.IsTrue(File.Exists(newFilePath));
                Assert.IsTrue(File.Exists(tempFile));
            }
        }
 public string Create(IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args, bool createToFile)
 {
     CreateToFile = createToFile;
     Destination = dst;
     Dev2CRUDOperationTO = args;
     return "Successful";
 }
        protected override IList<OutputTO> ExecuteConcreteAction(IDSFDataObject dataObject, out ErrorResultTO allErrors, int update)
        {

            IList<OutputTO> outputs = new List<OutputTO>();

            allErrors = new ErrorResultTO();
            var colItr = new WarewolfListIterator();

            //get all the possible paths for all the string variables
            
            var outputItr = new WarewolfIterator(dataObject.Environment.Eval(OutputPath, update));
            colItr.AddVariableToIterateOn(outputItr);

            var unameItr = new WarewolfIterator(dataObject.Environment.Eval(Username, update));
            colItr.AddVariableToIterateOn(unameItr);

            var passItr = new WarewolfIterator(dataObject.Environment.Eval(DecryptedPassword,update));
            colItr.AddVariableToIterateOn(passItr);

            var privateKeyItr = new WarewolfIterator(dataObject.Environment.Eval(PrivateKeyFile, update));
            colItr.AddVariableToIterateOn(privateKeyItr);

            if(dataObject.IsDebugMode())
            {
                AddDebugInputItem(new DebugEvalResult(OutputPath, "File or Folder", dataObject.Environment, update));
                AddDebugInputItem(new DebugItemStaticDataParams(Overwrite.ToString(), "Overwrite"));
                AddDebugInputItemUserNamePassword(dataObject.Environment, update);
                if (!string.IsNullOrEmpty(PrivateKeyFile))
                {
                    AddDebugInputItem(PrivateKeyFile, "Destination Private Key File", dataObject.Environment, update);
                }
            }

            while(colItr.HasMoreData())
            {
                IActivityOperationsBroker broker = ActivityIOFactory.CreateOperationsBroker();
                Dev2CRUDOperationTO opTo = new Dev2CRUDOperationTO(Overwrite);

                try
                {
                    IActivityIOPath dst = ActivityIOFactory.CreatePathFromString(colItr.FetchNextValue(outputItr),
                                                                                colItr.FetchNextValue(unameItr),
                                                                                colItr.FetchNextValue(passItr),
                                                                                true, colItr.FetchNextValue(privateKeyItr));
                    IActivityIOOperationsEndPoint dstEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(dst);
                    string result = broker.Create(dstEndPoint, opTo, true);
                    outputs.Add(DataListFactory.CreateOutputTO(Result, result));
                }
                catch(Exception e)
                {
                    outputs.Add(DataListFactory.CreateOutputTO(Result, "Failure"));
                    allErrors.AddError(e.Message);
                    break;
                }
            }

            return outputs;
        }
        private string CreateEndPointAndWriteData(IActivityIOOperationsEndPoint dst, IDev2PutRawOperationTO args)
        {
            var newArgs = new Dev2CRUDOperationTO(true);

            var endPointCreated = _implementation.CreateEndPoint(dst, newArgs, true) == ActivityIOBrokerBaseDriver.ResultOk;

            if (endPointCreated)
            {
                return(_implementation.WriteDataToFile(args, dst)
                    ? ActivityIOBrokerBaseDriver.ResultOk
                    : ActivityIOBrokerBaseDriver.ResultBad);
            }

            return(ActivityIOBrokerBaseDriver.ResultBad);
        }
Exemple #6
0
        string MoveTmpFileToDestination(IActivityIOOperationsEndPoint dst, string tmp, string result)
        {
            using (Stream s = new MemoryStream(_fileWrapper.ReadAllBytes(tmp)))
            {
                var newArgs = new Dev2CRUDOperationTO(true);

                if (!dst.PathExist(dst.IOPath))
                {
                    CreateEndPoint(dst, newArgs, true);
                }
                if (dst.Put(s, dst.IOPath, newArgs, null, _filesToDelete) < 0)
                {
                    result = ResultBad;
                }
            }
            return(result);
        }
Exemple #7
0
        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);
        }
Exemple #8
0
        public string PutRaw(IActivityIOOperationsEndPoint dst, IDev2PutRawOperationTO args)
        {
            var result = ResultOk;

            try
            {
                FileLock.EnterWriteLock();
                if (dst.RequiresLocalTmpStorage())
                {
                    var tmp = CreateTmpFile();
                    WriteToLocalTempStorage(dst, args, tmp);
                    result = MoveTmpFileToDestination(dst, tmp, result);
                }
                else
                {
                    if (dst.PathExist(dst.IOPath))
                    {
                        var tmp = CreateTmpFile();
                        result = WriteToRemoteTempStorage(dst, args, result, tmp);
                        RemoveTmpFile(tmp);
                    }
                    else
                    {
                        var newArgs = new Dev2CRUDOperationTO(true);
                        CreateEndPoint(dst, newArgs, true);
                        WriteDataToFile(args, dst);
                    }
                }
            }
            finally
            {
                FileLock.ExitWriteLock();
                for (var index = _filesToDelete.Count - 1; index > 0; index--)
                {
                    var name = _filesToDelete[index];
                    RemoveTmpFile(name);
                }
            }
            return(result);
        }
        public void CreateDirectoryUNCValidUser_WithOverwriteFalse_Present_Expected_DirectoryNotCreated()
        {
            Dev2CRUDOperationTO opTO = new Dev2CRUDOperationTO(false);
            string basePath = TestResource.PathOperations_UNC_Path_Secure + Guid.NewGuid();
            PathIOTestingUtils.CreateAuthedUNCPath(basePath, true, _inDomain);
            IActivityIOPath path = ActivityIOFactory.CreatePathFromString(basePath, (_inDomain ? "DEV2\\" : ".\\") + TestResource.PathOperations_Correct_Username, TestResource.PathOperations_Correct_Password);
            IActivityIOOperationsEndPoint FileSystemPro = ActivityIOFactory.CreateOperationEndPointFromIOPath(path);
            bool result = FileSystemPro.CreateDirectory(path, opTO);

            PathIOTestingUtils.DeleteAuthedUNCPath(basePath, _inDomain);

            Assert.IsFalse(result);
        }
        public void CreateDirectoryWithOverwriteTrue_NotPresent_Expected_DirectoryCreated()
        {
            Dev2CRUDOperationTO opTO = new Dev2CRUDOperationTO(true);
            string dir = Path.GetTempPath() + Path.GetRandomFileName();
            IActivityIOPath path = ActivityIOFactory.CreatePathFromString(dir, "", "");
            IActivityIOOperationsEndPoint FileSystemPro = ActivityIOFactory.CreateOperationEndPointFromIOPath(path);
            bool ok = FileSystemPro.CreateDirectory(path, opTO);

            Directory.Delete(dir);

            Assert.IsTrue(ok);
        }
        public void PutWithOverwriteTrue_UNCPathValidUser_FileNot_Present_Expected_NewFileCreated()
        {
            Dev2CRUDOperationTO opTO = new Dev2CRUDOperationTO(true);
            string tmp = TestResource.PathOperations_UNC_Path_Secure + Guid.NewGuid() + ".test";
            IActivityIOPath dst = ActivityIOFactory.CreatePathFromString(tmp, (_inDomain ? "DEV2\\" : ".\\") + TestResource.PathOperations_Correct_Username, TestResource.PathOperations_Correct_Password);
            IActivityIOOperationsEndPoint FileSystemPro = ActivityIOFactory.CreateOperationEndPointFromIOPath(dst);
            Stream stream = new MemoryStream(File.ReadAllBytes(_tmpfile1));
            int len = FileSystemPro.Put(stream, dst, opTO, null, new List<string>());
            stream.Close();

            PathIOTestingUtils.DeleteAuthedUNCPath(tmp);

            Assert.IsTrue(len > 0);
        }
        public void PutWithOverwriteFalse_UNCPath_File_Not_Present_FileDataReadIntoStream()
        {
            Dev2CRUDOperationTO opTO = new Dev2CRUDOperationTO(false);
            File.Delete(_uncfile1); // remove it is not there
            IActivityIOPath dst = ActivityIOFactory.CreatePathFromString(_uncfile1, "", "");
            IActivityIOPath src = ActivityIOFactory.CreatePathFromString(_tmpfile2, "", "");
            IActivityIOOperationsEndPoint FileSystemPro = ActivityIOFactory.CreateOperationEndPointFromIOPath(dst);
            Stream stream = FileSystemPro.Get(src, new List<string>());
            var directoryInfo = new FileInfo(src.Path).Directory;
            if(directoryInfo != null)
            {
                int len = FileSystemPro.Put(stream, dst, opTO, directoryInfo.ToString(), new List<string>());
                stream.Close();
                File.Delete(_uncfile1);

                Assert.IsTrue(len > 0);
            }
            else
            {
                Assert.Fail();
            }
        }
        protected override string ExecuteBroker(IActivityOperationsBroker broker, IActivityIOOperationsEndPoint scrEndPoint, IActivityIOOperationsEndPoint dstEndPoint)
        {

            Dev2CRUDOperationTO opTO = new Dev2CRUDOperationTO(Overwrite);
            return broker.Copy(scrEndPoint, dstEndPoint, opTO);
        }
        bool CreateFile(IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args)
        {

            var result = true;

            var tmp = CreateTmpFile();
            using(Stream s = new MemoryStream(File.ReadAllBytes(tmp)))
            {

                if(dst.Put(s, dst.IOPath, args, null, _filesToDelete) < 0)
                {
                    result = false;
                }

                s.Close();
            }

            return result;
        }
 bool CreateDirectory(IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args)
 {
     var result = dst.CreateDirectory(dst.IOPath, args);
     return result;
 }
        string CreateEndPoint(IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args, bool createToFile)
        {
            var result = ResultOk;
            // check the the dir strucutre exist
            var activityIOPath = dst.IOPath;
            var dirParts = MakeDirectoryParts(activityIOPath, dst.PathSeperator());

            // check from lowest path part up
            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;
                    }
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch(Exception)
                // ReSharper restore EmptyGeneralCatchClause
                {
                    //Note that we doing a recursive create should the directory not exists
                }
                finally
                {
                    pos--;
                }
            }

            // now create all the directories we need ;)
            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;

            // dir create failed
            if(!ok)
            {
                result = ResultBad;
            }
            else if(dst.PathIs(dst.IOPath) == enPathType.File && createToFile)
            {
                if(!CreateFile(dst, args))
                {
                    result = ResultBad;
                }
            }

            return result;
        }
        public string Move(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst,
                           Dev2CRUDOperationTO args)
        {
            string result;

            try
            {
                result = Copy(src, dst, args);

                if(result.Equals("Success"))
                {
                    src.Delete(src.IOPath);
                }
            }
            finally
            {
                _filesToDelete.ForEach(RemoveTmpFile);
            }

            return result;
        }
        public string Copy(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst,
                           Dev2CRUDOperationTO args)
        {
            string status;
            try
            {
                status = ValidateCopySourceDestinationFileOperation(src, dst, args, () =>
                    {
                        if(src.RequiresLocalTmpStorage())
                        {
                            if(dst.PathIs(dst.IOPath) == enPathType.Directory)
                            {
                                dst.IOPath.Path = dst.Combine(GetFileNameFromEndPoint(src));
                            }

                            using(var s = src.Get(src.IOPath, _filesToDelete))
                            {

                                // for flips sake quite putting short-hand notation in-line it causes bugs!!! ;)
                                dst.Put(s, dst.IOPath, args, Path.IsPathRooted(src.IOPath.Path) ? Path.GetDirectoryName(src.IOPath.Path) : null, _filesToDelete);
                                s.Close();
                                s.Dispose();
                            }
                        }
                        else
                        {
                            var sourceFile = new FileInfo(src.IOPath.Path);
                            if(dst.PathIs(dst.IOPath) == enPathType.Directory)
                            {
                                dst.IOPath.Path = dst.Combine(sourceFile.Name);
                            }

                            using(var s = src.Get(src.IOPath, _filesToDelete))
                            {
                                if(sourceFile.Directory != null)
                                {
                                    dst.Put(s, dst.IOPath, args, sourceFile.Directory.ToString(), _filesToDelete);
                                }
                            }
                        }
                        return ResultOk;
                    });
            }
            finally
            {
                _filesToDelete.ForEach(RemoveTmpFile);
            }
            return status;
        }
 public string Rename(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst,
                      Dev2CRUDOperationTO args)
 {
     string result;
     try
     {
         result = ValidateRenameSourceAndDesinationTypes(src, dst, args);
     }
     finally
     {
         _filesToDelete.ForEach(RemoveTmpFile);
     }
     return result;
 }
 public string Create(IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args, bool createToFile)
 {
     string result;
     try
     {
         ValidateEndPoint(dst, args);
         result = CreateEndPoint(dst, args, createToFile);
     }
     finally
     {
         _filesToDelete.ForEach(RemoveTmpFile);
     }
     return result;
 }
        string MoveTmpFileToDestination(IActivityIOOperationsEndPoint dst, string tmp, string result)
        {
            using(Stream s = new MemoryStream(File.ReadAllBytes(tmp)))
            {
                Dev2CRUDOperationTO newArgs = new Dev2CRUDOperationTO(true);

                //MO : 22-05-2012 : If the file doesnt exist then create the file
                if(!dst.PathExist(dst.IOPath))
                {
                    CreateEndPoint(dst, newArgs, true);
                }
                if(dst.Put(s, dst.IOPath, newArgs, null, _filesToDelete) < 0)
                {
                    result = ResultBad;
                }
            }
            return result;
        }
Exemple #22
0
        public string PutRaw(IActivityIOOperationsEndPoint dst, IDev2PutRawOperationTO args)
        {
            var result = ResultOk;

            try
            {
                FileLock.EnterWriteLock();
                if (dst.RequiresLocalTmpStorage())
                {
                    var tmp = CreateTmpFile();
                    switch (args.WriteType)
                    {
                    case WriteType.AppendBottom:
                        using (var s = dst.Get(dst.IOPath, _filesToDelete))
                        {
                            _fileWrapper.WriteAllBytes(tmp, s.ToByteArray());
                            _fileWrapper.AppendAllText(tmp, args.FileContents);
                        }
                        break;

                    case WriteType.AppendTop:
                        using (var s = dst.Get(dst.IOPath, _filesToDelete))
                        {
                            _fileWrapper.WriteAllText(tmp, args.FileContents);
                            _common.AppendToTemp(s, tmp);
                        }
                        break;

                    default:
                        WriteDataToFile(args, tmp);
                        break;
                    }
                    result = MoveTmpFileToDestination(dst, tmp, result);
                }
                else
                {
                    if (_fileWrapper.Exists(dst.IOPath.Path))
                    {
                        var tmp = CreateTmpFile();
                        switch (args.WriteType)
                        {
                        case WriteType.AppendBottom:
                            _fileWrapper.AppendAllText(dst.IOPath.Path, args.FileContents);
                            result = ResultOk;
                            break;

                        case WriteType.AppendTop:
                            using (var s = dst.Get(dst.IOPath, _filesToDelete))
                            {
                                _fileWrapper.WriteAllText(tmp, args.FileContents);
                                _common.AppendToTemp(s, tmp);
                                result = MoveTmpFileToDestination(dst, tmp, result);
                                RemoveTmpFile(tmp);
                            }
                            break;

                        default:
                            WriteDataToFile(args, tmp);
                            result = MoveTmpFileToDestination(dst, tmp, result);
                            RemoveTmpFile(tmp);
                            break;
                        }
                    }
                    else
                    {
                        Dev2CRUDOperationTO newArgs = new Dev2CRUDOperationTO(true);
                        CreateEndPoint(dst, newArgs, true);
                        var path = dst.IOPath.Path;
                        WriteDataToFile(args, path);
                    }
                }
            }
            finally
            {
                FileLock.ExitWriteLock();
                for (var index = _filesToDelete.Count - 1; index > 0; index--)
                {
                    var name = _filesToDelete[index];
                    RemoveTmpFile(name);
                }
            }
            return(result);
        }
        /// <summary>
        /// Transfer the contents of the directory
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dst"></param>
        /// <param name="args"></param>
        bool TransferDirectoryContents(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst,
                                               Dev2CRUDOperationTO args)
        {
            ValidateSourceAndDestinationContents(src, dst, args);

            if(args.DoRecursiveCopy)
            {
                RecursiveCopy(src, dst, args);
            }

            var srcContents = src.ListFilesInDirectory(src.IOPath);
            var result = true;
            var origDstPath = Dev2ActivityIOPathUtils.ExtractFullDirectoryPath(dst.IOPath.Path);

            if(!dst.PathExist(dst.IOPath))
            {
                CreateDirectory(dst, args);
            }

            // get each file, then put it to the correct location
            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach(var p in srcContents)
            {
                result = PerformTransfer(src, dst, args, origDstPath, p, result);
            }
            Dev2Logger.Log.Debug(string.Format("Transfered: {0}", src.IOPath.Path));
            return result;
        }
        public bool CreateDirectory(IActivityIOPath dst, Dev2CRUDOperationTO args)
        {
            bool result = false;

            if (args.Overwrite)
            {
                if (!RequiresAuth(dst))
                {
                    if (DirectoryExist(dst))
                    {
                        Delete(dst);
                    }
                    Directory.CreateDirectory(dst.Path);
                    result = true;
                }
                else
                {
                    try
                    {
                        // 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

                                    if (DirectoryExist(dst))
                                    {
                                        Delete(dst);
                                    }
                                    Directory.CreateDirectory(dst.Path);
                                    result = true;

                                    // remove impersonation now
                                    impersonatedUser.Undo();
                                }
                            }
                        }
                        else
                        {
                            // login failed, oh no!
                            throw new Exception("Failed to authenticate with user [ " + dst.Username + " ] for resource [ " + dst.Path + " ] ");
                        }
                    }
                    catch (Exception ex)
                    {
                        Dev2Logger.Log.Error(ex);
                        throw;
                    }
                }
            }
            else if (!args.Overwrite && !DirectoryExist(dst))
            {
                if (!RequiresAuth(dst))
                {
                    Directory.CreateDirectory(dst.Path);
                    result = true;
                }
                else
                {
                    try
                    {
                        // 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

                                    Directory.CreateDirectory(dst.Path);
                                    result = true;

                                    // remove impersonation now
                                    impersonatedUser.Undo();
                                }
                                newID.Dispose();
                            }
                        }
                        else
                        {
                            // login failed
                            throw new Exception("Failed to authenticate with user [ " + dst.Username + " ] for resource [ " + dst.Path + " ] ");
                        }
                    }
                    catch (Exception ex)
                    {
                        Dev2Logger.Log.Error(ex);
                        throw;
                    }
                }
            }

            return(result);
        }
 static bool PerformTransfer(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args, string origDstPath, IActivityIOPath p, bool result)
 {
     try
     {
         if(dst.PathIs(dst.IOPath) == enPathType.Directory)
         {
             var cpPath =
                 ActivityIOFactory.CreatePathFromString(
                     string.Format("{0}{1}{2}", 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.Log.Error(ex);
     }
     return result;
 }
        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);
        }
 static void DoFileTransfer(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args, IActivityIOPath dstPath, IActivityIOPath p, string path, ref bool result)
 {
     if(args.Overwrite || !dst.PathExist(dstPath))
     {
         result = TransferFile(src, dst, args, path, p, result);
     }
 }
 static bool TransferFile(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO 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 ValidateCopySourceDestinationFileOperation(IActivityIOOperationsEndPoint src,
                                                                  IActivityIOOperationsEndPoint dst,
                                                                  Dev2CRUDOperationTO args,
                                                                  Func<string> performAfterValidation)
        {
            var result = ResultOk;
            ValidateSourceAndDestinationPaths(src, dst);

            //ensures destination folder structure exists
            var opStatus = CreateEndPoint(dst, args, dst.PathIs(dst.IOPath) == enPathType.Directory);
            if(!opStatus.Equals("Success"))
            {
                throw new Exception("Recursive Directory Create Failed For [ " + dst.IOPath.Path + " ]");
            }

            //transfer contents to destination when the source is a directory
            if(src.PathIs(src.IOPath) == enPathType.Directory)
            {
                if(!TransferDirectoryContents(src, dst, args))
                {
                    result = ResultBad;
                }
            }
            else
            {
                if(!args.Overwrite)
                {
                    EnsureFilesDontExists(src, dst);
                }

                if(!Dev2ActivityIOPathUtils.IsStarWildCard(src.IOPath.Path))
                {
                    return performAfterValidation();
                }

                // we have star wild cards to deal with
                if(!TransferDirectoryContents(src, dst, args))
                {
                    result = ResultBad;
                }
            }

            return result;
        }
        public void PutWithOverwriteTrue_UNCPath_File_Present_Expected_FileReadIntoStream()
        {
            Dev2CRUDOperationTO opTO = new Dev2CRUDOperationTO(true);
            string tmp = Path.GetTempFileName();
            File.Delete(tmp);
            IActivityIOPath dst = ActivityIOFactory.CreatePathFromString(tmp, "", "");
            IActivityIOPath src = ActivityIOFactory.CreatePathFromString(_tmpfile2, "", "");
            IActivityIOOperationsEndPoint FileSystemPro = ActivityIOFactory.CreateOperationEndPointFromIOPath(dst);
            Stream stream = FileSystemPro.Get(src, new List<string>());
            var directoryInfo = new FileInfo(src.Path).Directory;
            if(directoryInfo != null)
            {
                int len = FileSystemPro.Put(stream, dst, opTO, directoryInfo.ToString(), new List<string>());
                File.Delete(tmp);

                Assert.IsTrue(len > 0);
            }
            else
            {
                Assert.Fail();
            }
        }
        public string PutRaw(IActivityIOOperationsEndPoint dst, Dev2PutRawOperationTO args)
        {
            var result = ResultOk;

            // directory put?
            // wild char put?
            try
            {
                _fileLock.EnterWriteLock();
                if(dst.RequiresLocalTmpStorage())
                {
                    var tmp = CreateTmpFile();
                    switch(args.WriteType)
                    {
                        case WriteType.AppendBottom:
                            using(var s = dst.Get(dst.IOPath, _filesToDelete))
                            {
                                File.WriteAllBytes(tmp, s.ToByteArray());
                                File.AppendAllText(tmp, args.FileContents);
                            }
                            break;
                        case WriteType.AppendTop:
                            using(var s = dst.Get(dst.IOPath, _filesToDelete))
                            {
                                File.WriteAllText(tmp, args.FileContents);
                                AppendToTemp(s, tmp);
                            }
                            break;
                        default:
                            if(IsBase64(args.FileContents))
                            {
                                var data = Convert.FromBase64String(args.FileContents.Replace("Content-Type:BASE64", ""));
                                File.WriteAllBytes(tmp, data);
                            }
                            else
                            {
                                File.WriteAllText(tmp, args.FileContents);
                            }
                            break;
                    }
                    result = MoveTmpFileToDestination(dst, tmp, result);
                }
                else
                {
                    if(File.Exists(dst.IOPath.Path))
                    {

                        var tmp = CreateTmpFile();
                        switch(args.WriteType)
                        {
                            case WriteType.AppendBottom:
                                File.AppendAllText(dst.IOPath.Path, args.FileContents);
                                result = ResultOk;
                                break;
                            case WriteType.AppendTop:
                                using(var s = dst.Get(dst.IOPath, _filesToDelete))
                                {
                                    File.WriteAllText(tmp, args.FileContents);

                                    AppendToTemp(s, tmp);
                                    result = MoveTmpFileToDestination(dst, tmp, result);
                                    RemoveTmpFile(tmp);
                                }
                                break;
                            default:
                                if(IsBase64(args.FileContents))
                                {
                                    var data = Convert.FromBase64String(args.FileContents.Replace("Content-Type:BASE64", ""));
                                    File.WriteAllBytes(tmp, data);
                                }
                                else
                                {
                                    File.WriteAllText(tmp, args.FileContents);
                                }
                                result = MoveTmpFileToDestination(dst, tmp, result);
                                RemoveTmpFile(tmp);
                                break;
                        }
                    }
                    else
                    {
                        // we can write directly to the file
                        Dev2CRUDOperationTO newArgs = new Dev2CRUDOperationTO(true);

                        CreateEndPoint(dst, newArgs, true);

                        if(IsBase64(args.FileContents))
                        {
                            var data = Convert.FromBase64String(args.FileContents.Replace("Content-Type:BASE64", ""));
                            File.WriteAllBytes(dst.IOPath.Path, data);
                        }
                        else
                        {
                            File.WriteAllText(dst.IOPath.Path, args.FileContents);
                        }
                    }
                }
            }
            finally
            {
                _fileLock.ExitWriteLock();
                for(var index = _filesToDelete.Count-1; index > 0; index--)
                {
                    var name = _filesToDelete[index];
                    RemoveTmpFile(name);
                }
            }
            return result;
        }
        public void PutWithOverwriteTrue_UNCPathValidUser_File_Present_FileInStream()
        {
            Dev2CRUDOperationTO opTO = new Dev2CRUDOperationTO(true);
            string tmp = TestResource.PathOperations_UNC_Path_Secure + Guid.NewGuid() + ".test";
            string tmp2 = TestResource.PathOperations_UNC_Path_Secure + Guid.NewGuid() + ".test";
            PathIOTestingUtils.CreateAuthedUNCPath(tmp, false, _inDomain);
            IActivityIOPath dst = ActivityIOFactory.CreatePathFromString(tmp, (_inDomain ? "DEV2\\" : ".\\") + TestResource.PathOperations_Correct_Username, TestResource.PathOperations_Correct_Password);
            IActivityIOPath src = ActivityIOFactory.CreatePathFromString(tmp2, (_inDomain ? "DEV2\\" : ".\\") + TestResource.PathOperations_Correct_Username, TestResource.PathOperations_Correct_Password);
            IActivityIOOperationsEndPoint FileSystemPro = ActivityIOFactory.CreateOperationEndPointFromIOPath(dst);
            Stream stream = new MemoryStream(File.ReadAllBytes(_tmpfile1));
            var directoryInfo = new FileInfo(src.Path).Directory;
            if(directoryInfo != null)
            {
                int len = FileSystemPro.Put(stream, dst, opTO, directoryInfo.ToString(), new List<string>());
                stream.Close();

                PathIOTestingUtils.DeleteAuthedUNCPath(tmp);
                PathIOTestingUtils.DeleteAuthedUNCPath(tmp2);

                Assert.IsTrue(len > 0);
            }
            else
            {
                Assert.Fail();
            }
        }
        /// <summary>
        /// Transfer the contents of the directory
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dst"></param>
        /// <param name="args"></param>
        void ValidateSourceAndDestinationContents(IActivityIOOperationsEndPoint src,
                                                          IActivityIOOperationsEndPoint dst,
                                                          Dev2CRUDOperationTO 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(
                            "The following file(s) exist in the destination folder and overwrite is set to false:- " +
                            fileNames);
                    }
                }
            }
        }
        public void CreateDirectoryWithOverwriteTrue_Present_Expected_DirectoryCreated()
        {
            Dev2CRUDOperationTO opTO = new Dev2CRUDOperationTO(true);
            IActivityIOPath path = ActivityIOFactory.CreatePathFromString(_tmpdir1, "", "");
            IActivityIOOperationsEndPoint FileSystemPro = ActivityIOFactory.CreateOperationEndPointFromIOPath(path);
            bool ok = FileSystemPro.CreateDirectory(path, opTO);

            Assert.IsTrue(ok);
        }
Exemple #35
0
 protected override string ExecuteBroker(IActivityOperationsBroker broker, IActivityIOOperationsEndPoint scrEndPoint, IActivityIOOperationsEndPoint dstEndPoint)
 {
     Dev2CRUDOperationTO opTo = new Dev2CRUDOperationTO(Overwrite);
     var result = broker.Rename(scrEndPoint, dstEndPoint, opTo);
     return result.Replace("Move", "Rename");
 }
 void RecursiveCopy(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2CRUDOperationTO args)
 {
     try
     {
         // List directory contents
         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 => exception != null && !string.IsNullOrEmpty(exception.Message)).Aggregate("", (current, exception) => current + exception.Message + "\r\n");
         throw new Exception(message, e);
     }
 }
        void ValidateEndPoint(IActivityIOOperationsEndPoint endPoint, Dev2CRUDOperationTO args)
        {
            if(endPoint.IOPath.Path.Trim().Length == 0)
            {
                throw new Exception("Source can not be an empty string");
            }

            if(endPoint.PathExist(endPoint.IOPath) && !args.Overwrite)
            {
                var type = endPoint.PathIs(endPoint.IOPath) == enPathType.Directory ? "Directory" : "File";
                throw new Exception(string.Format("Destination {0} already exists and overwrite is set to false",
                                                  type));
            }
        }