public void SftpPermissionDeniedExceptionConstructorTest1()
        {
            string message = string.Empty; // TODO: Initialize to an appropriate value
            SftpPermissionDeniedException target = new SftpPermissionDeniedException(message);

            Assert.Inconclusive("TODO: Implement code to verify target");
        }
Esempio n. 2
0
        protected override void ProcessRecord()
        {
            foreach (var sftpSession in ToProcess)
            {
                // check if the folder specified actually exists.
                // Resolve the path even if a relative one is given.
                foreach (var localfolder in _localfolder)
                {
                    ProviderInfo provider;
                    var          pathinfo      = GetResolvedProviderPathFromPSPath(localfolder, out provider);
                    var          localfullPath = pathinfo[0];


                    if (Directory.Exists(@localfullPath))
                    {
                        WriteVerbose("Uploading " + localfullPath + " to " + RemotePath);
                        if (!sftpSession.Session.Exists(RemotePath))
                        {
                            sftpSession.Session.CreateDirectory(RemotePath);
                        }
                        else
                        {
                            if (!Overwrite)
                            {
                                var ex = new SftpPermissionDeniedException("Folder already exists on remote host.");
                                ThrowTerminatingError(new ErrorRecord(
                                                          ex,
                                                          "Folder already exists on remote host",
                                                          ErrorCategory.InvalidOperation,
                                                          sftpSession));
                            }
                        }

                        try
                        {
                            UploadDirectory(sftpSession.Session, localfullPath, RemotePath);
                        }
                        catch (Exception ex)
                        {
                            WriteError(new ErrorRecord(
                                           ex,
                                           "Error while Uploading",
                                           ErrorCategory.InvalidOperation,
                                           sftpSession));
                        }
                    }
                    else
                    {
                        var ex = new FileNotFoundException("Folder to upload " + localfullPath + " was not found.");

                        ThrowTerminatingError(new ErrorRecord(
                                                  ex,
                                                  "Folder to upload " + localfullPath + " was not found.",
                                                  ErrorCategory.InvalidOperation,
                                                  localfullPath));
                    } // check if folder exists.
                }     // foreach local folder
            }         // sftp session.
        }             // Process Record.
Esempio n. 3
0
        protected override void ProcessRecord()
        {
            foreach (var sftpSession in ToProcess)
            {
                // check if the file specified actually exists.
                // Resolve the path even if a relative one is given.
                foreach (var localfile in _localfile)
                {
                    ProviderInfo provider;
                    var          pathinfo      = GetResolvedProviderPathFromPSPath(localfile, out provider);
                    var          localfullPath = pathinfo[0];


                    if (File.Exists(@localfullPath))
                    {
                        WriteVerbose("Uploading " + localfullPath);
                        var fil            = new FileInfo(@localfullPath);
                        var remoteFullpath = RemotePath.TrimEnd(new[] { '/' }) + "/" + fil.Name;
                        WriteVerbose("Uploading to " + remoteFullpath + " on " + sftpSession.Host);

                        // Setup Action object for showing download progress.

                        var res = new Action <ulong>(rs =>
                        {
                            //if (!MyInvocation.BoundParameters.ContainsKey("Verbose")) return;
                            if (fil.Length > 1240000)
                            {
                                var percent = (int)((((double)rs) / fil.Length) * 100.0);
                                if (percent % 10 == 0)
                                {
                                    // This will prevent the progress message from being stuck on the screen.
                                    if (percent == 90 || percent > 90)
                                    {
                                        return;
                                    }

                                    var progressRecord = new ProgressRecord(1,
                                                                            "Uploading " + fil.Name,
                                                                            String.Format("{0} Bytes Uploaded of {1}", rs, fil.Length))
                                    {
                                        PercentComplete = percent
                                    };

                                    Host.UI.WriteProgress(1, progressRecord);
                                    //Host.UI.WriteVerboseLine(percent.ToString(CultureInfo.InvariantCulture) + "% Completed.");
                                }
                            }
                        });

                        // Check that the path we are uploading to actually exists on the target.
                        if (sftpSession.Session.Exists(RemotePath))
                        {
                            // Ensure the remote path is a directory.
                            var attribs = sftpSession.Session.GetAttributes(RemotePath);
                            if (!attribs.IsDirectory)
                            {
                                throw new SftpPathNotFoundException("Specified path is not a directory");
                            }
                            // Check if the file already exists on the target system.
                            var present = sftpSession.Session.Exists(remoteFullpath);
                            if ((present & _overwrite) || (!present))
                            {
                                var localstream = File.OpenRead(localfullPath);
                                try
                                {
                                    sftpSession.Session.UploadFile(localstream, remoteFullpath, res);
                                    localstream.Close();
                                }
                                catch (Exception ex)
                                {
                                    localstream.Close();
                                    WriteError(new ErrorRecord(
                                                   ex,
                                                   "Error while Uploading",
                                                   ErrorCategory.InvalidOperation,
                                                   sftpSession));
                                }
                            }
                            else
                            {
                                var ex = new SftpPermissionDeniedException("File already exists on remote host.");
                                WriteError(new ErrorRecord(
                                               ex,
                                               "File already exists on remote host",
                                               ErrorCategory.InvalidOperation,
                                               sftpSession));
                            }
                        }
                        else
                        {
                            var ex = new SftpPathNotFoundException("Directory " + RemotePath + " does not exist.");
                            WriteError(new ErrorRecord(
                                           ex,
                                           "Directory " + RemotePath + " does not exist.",
                                           ErrorCategory.InvalidOperation,
                                           sftpSession));
                        }
                    }
                    else
                    {
                        var ex = new FileNotFoundException("File to upload " + localfullPath + " was not found.");

                        WriteError(new ErrorRecord(
                                       ex,
                                       "File to upload " + localfullPath + " was not found.",
                                       ErrorCategory.InvalidOperation,
                                       localfullPath));
                    } // check if file exists.
                }     // foreach local file
            }         // sftp session.
        }             // Process Record.
Esempio n. 4
0
        protected override void ProcessRecord()
        {
            // check if the file specified actually exists.
            // Resolve the path even if a relative one is given.
            ProviderInfo provider;
            var          pathinfo      = GetResolvedProviderPathFromPSPath(_localpath, out provider);
            var          localfullPath = pathinfo[0];

            if (Directory.Exists(@localfullPath))
            {
                var filename = Path.GetFileName(_remotefile);

                var localfilefullpath = localfullPath + "/" + filename;
                var fil = new FileInfo(@localfilefullpath);


                foreach (var sftpSession in ToProcess)
                {
                    WriteVerbose("Downloading " + filename + " to " + localfilefullpath + " from " + sftpSession.Host);



                    // Check that the path we are downloading from actually exists on the target.
                    if (sftpSession.Session.Exists(_remotefile))
                    {
                        // Ensure the remote path is a directory.
                        var attribs = sftpSession.Session.GetAttributes(_remotefile);
                        if (!attribs.IsRegularFile)
                        {
                            throw new SftpPathNotFoundException("Specified path is not a file.");
                        }

                        // Setup Action object for showing download progress.

                        var res = new Action <ulong>(rs =>
                        {
                            //if (!MyInvocation.BoundParameters.ContainsKey("Verbose")) return;
                            if (attribs.Size != 0)
                            {
                                var percent = (int)((((double)rs) / attribs.Size) * 100.0);
                                if (percent % 10 == 0)
                                {
                                    // This will prevent the progress message from being stuck on the screen.
                                    if (percent == 100)
                                    {
                                        return;
                                    }

                                    var progressRecord = new ProgressRecord(1,
                                                                            "Downloading " + fil.Name,
                                                                            String.Format("{0} Bytes Downloaded of {1}", rs, attribs.Size))
                                    {
                                        PercentComplete = percent
                                    };

                                    Host.UI.WriteProgress(1, progressRecord);
                                }
                            }
                        });

                        var present = File.Exists(localfilefullpath);

                        if ((present & _overwrite) || (!present))
                        {
                            var localstream = File.Create(@localfilefullpath);
                            try
                            {
                                sftpSession.Session.DownloadFile(_remotefile, localstream, res);
                                localstream.Close();
                            }
                            catch
                            {
                                localstream.Close();
                                var ex = new SftpPermissionDeniedException("Unable to download file from host.");
                                ThrowTerminatingError(new ErrorRecord(
                                                          ex,
                                                          "Unable to download file from host.",
                                                          ErrorCategory.InvalidOperation,
                                                          sftpSession));
                            }
                        }
                        else
                        {
                            var ex = new SftpPermissionDeniedException("File already present on local host.");
                            WriteError(new ErrorRecord(
                                           ex,
                                           "File already present on local host.",
                                           ErrorCategory.InvalidOperation,
                                           sftpSession));
                        }
                    }
                    else
                    {
                        var ex = new SftpPathNotFoundException(RemoteFile + " does not exist.");
                        ThrowTerminatingError(new ErrorRecord(
                                                  ex,
                                                  RemoteFile + " does not exist.",
                                                  ErrorCategory.InvalidOperation,
                                                  sftpSession));
                    }
                }
            }
            else
            {
                var ex = new FileNotFoundException("Local path" + localfullPath + " was not found.");

                ThrowTerminatingError(new ErrorRecord(
                                          ex,
                                          "Local path" + localfullPath + " was not found.",
                                          ErrorCategory.InvalidOperation,
                                          localfullPath));
            }
        }
        public void SftpPermissionDeniedExceptionConstructorTest()
        {
            SftpPermissionDeniedException target = new SftpPermissionDeniedException();

            Assert.Inconclusive("TODO: Implement code to verify target");
        }
Esempio n. 6
0
        protected override void ProcessRecord()
        {
            foreach (var sftpSession in ToProcess)
            {
                // check if the file specified actually exists.
                // Resolve the path even if a relative one is given.
                foreach (var localitem in _localItem)
                {
                    ProviderInfo provider;
                    var          pathinfo      = GetResolvedProviderPathFromPSPath(localitem, out provider);
                    var          localfullPath = pathinfo[0];
                    var          filePresent   = File.Exists(@localfullPath);
                    var          dirPresent    = Directory.Exists(@localfullPath);

                    if (filePresent)
                    {
                        var fil            = new FileInfo(@localfullPath);
                        var remoteFullpath = _remotepath.TrimEnd(new[] { '/' }) + "/" + fil.Name;
                        WriteVerbose("Uploading " + localfullPath + " to " + _remotepath);

                        // Setup Action object for showing download progress.
                        var progressHelper = new OperationProgressHelper(this, "Upload", fil.Name, fil.Length, 1);

                        // Check that the path we are uploading to actually exists on the target.
                        if (sftpSession.Session.Exists(_remotepath))
                        {
                            // Ensure the remote path is a directory.
                            var attribs = sftpSession.Session.GetAttributes(_remotepath);
                            if (!attribs.IsDirectory)
                            {
                                throw new SftpPathNotFoundException("Specified path is not a directory");
                            }
                            // Check if the file already exists on the target system.
                            var present = sftpSession.Session.Exists(remoteFullpath);
                            if (!present || _overwrite)
                            {
                                using (var localstream = File.OpenRead(localfullPath))
                                {
                                    try
                                    {
                                        sftpSession.Session.UploadFile(localstream, remoteFullpath, progressHelper.Callback);
                                        progressHelper.Complete();
                                    }
                                    catch (Exception ex)
                                    {
                                        WriteError(new ErrorRecord(
                                                       ex,
                                                       "Error while Uploading",
                                                       ErrorCategory.InvalidOperation,
                                                       sftpSession));
                                    }
                                    finally
                                    {
                                        localstream.Close();
                                    }
                                }
                            }
                            else
                            {
                                var ex = new SftpPermissionDeniedException("File already exists on remote host.");
                                WriteError(new ErrorRecord(
                                               ex,
                                               "File already exists on remote host",
                                               ErrorCategory.InvalidOperation,
                                               sftpSession));
                            }
                        }
                        else
                        {
                            var ex = new SftpPathNotFoundException(_remotepath + " does not exist.");
                            WriteError(new ErrorRecord(
                                           ex,
                                           _remotepath + " does not exist.",
                                           ErrorCategory.InvalidOperation,
                                           sftpSession));
                        }
                    }
                    else if (dirPresent)
                    {
                        var dirName        = new DirectoryInfo(@localfullPath).Name;
                        var remoteFullpath = _remotepath.TrimEnd(new[] { '/' }) + "/" + dirName;

                        WriteVerbose("Uploading " + localfullPath + " to " + _remotepath);
                        if (!sftpSession.Session.Exists(remoteFullpath))
                        {
                            sftpSession.Session.CreateDirectory(remoteFullpath);
                        }
                        else
                        {
                            if (!_overwrite)
                            {
                                var ex = new SftpPermissionDeniedException("Folder already exists on remote host.");
                                ThrowTerminatingError(new ErrorRecord(
                                                          ex,
                                                          "Folder already exists on remote host",
                                                          ErrorCategory.InvalidOperation,
                                                          sftpSession));
                            }
                        }

                        try
                        {
                            UploadDirectory(sftpSession.Session, localfullPath, remoteFullpath);
                        }
                        catch (Exception ex)
                        {
                            WriteError(new ErrorRecord(
                                           ex,
                                           "Error while Uploading",
                                           ErrorCategory.InvalidOperation,
                                           sftpSession));
                        }
                    }
                    else
                    {
                        var ex = new FileNotFoundException("Item to upload " + localfullPath + " was not found.");

                        ThrowTerminatingError(new ErrorRecord(
                                                  ex,
                                                  "Item to upload " + localfullPath + " was not found.",
                                                  ErrorCategory.InvalidOperation,
                                                  localfullPath));
                    } // check if item exists.
                }     // foreach local item
            }         // sftp session.
        }             // Process Record.
Esempio n. 7
0
        protected override void ProcessRecord()
        {
            // check if the path specified actually exists.
            // Resolve the path even if a relative one is given for PowerShell.
            ProviderInfo provider;
            var          pathinfo      = GetResolvedProviderPathFromPSPath(_localpath, out provider);
            var          localfullPath = pathinfo[0];

            if (Directory.Exists(@localfullPath))
            {
                foreach (var sftpSession in ToProcess)
                {
                    foreach (string remotepath in _remotepath)
                    {
                        // Check that the path we are downloading from actually exists on the target.
                        if (sftpSession.Session.Exists(remotepath))
                        {
                            // Check if the remote path is a file or a directory to perform proper action.
                            var attribs = sftpSession.Session.GetAttributes(remotepath);

                            if (attribs.IsDirectory)
                            {
                                string dirName      = new DirectoryInfo(remotepath).Name;
                                var    fileFullPath = $"{@localfullPath}{System.IO.Path.DirectorySeparatorChar}{dirName}";

                                var present = Directory.Exists(fileFullPath);
                                if ((present & _overwrite) || (!present))
                                {
                                    var res = new Action <ulong>(rs =>
                                    {
                                        if (attribs.Size != 0)
                                        {
                                            var percent = (int)((((double)rs) / attribs.Size) * 100.0);
                                            if (percent % 10 == 0)
                                            {
                                                // This should prevent the progress message from being stuck on the screen.
                                                if (percent == 100)
                                                {
                                                    return;
                                                }

                                                var progressRecord = new ProgressRecord(1,
                                                                                        "Downloading " + remotepath,
                                                                                        String.Format("{0} Bytes Downloaded of {1}", rs, attribs.Size))
                                                {
                                                    PercentComplete = percent
                                                };

                                                Host.UI.WriteProgress(1, progressRecord);
                                            }
                                        }
                                    });
                                    Directory.CreateDirectory(fileFullPath);
                                    DownloadDirectory(sftpSession.Session, remotepath, fileFullPath, _skipsymlink, res);
                                }
                                else
                                {
                                    var ex = new SftpPermissionDeniedException($"Item {remotepath} already present on local host.");
                                    WriteError(new ErrorRecord(
                                                   ex,
                                                   $"Item {remotepath} already present on local host.",
                                                   ErrorCategory.InvalidOperation,
                                                   sftpSession));
                                }
                            }
                            else if (attribs.IsRegularFile)
                            {
                                var fileName = new FileInfo(remotepath).Name;
                                // Setup Action object for showing download progress.
                                var res = new Action <ulong>(rs =>
                                {
                                    if (attribs.Size != 0)
                                    {
                                        var percent = (int)((((double)rs) / attribs.Size) * 100.0);
                                        if (percent % 10 == 0)
                                        {
                                            // This should prevent the progress message from being stuck on the screen.
                                            if (percent == 100)
                                            {
                                                return;
                                            }

                                            var progressRecord = new ProgressRecord(1,
                                                                                    "Downloading: " + fileName,
                                                                                    String.Format("{0} Bytes Downloaded of {1}", rs, attribs.Size))
                                            {
                                                PercentComplete = percent
                                            };

                                            Host.UI.WriteProgress(1, progressRecord);
                                        }
                                    }
                                });

                                var fileFullPath = $"{@localfullPath}{System.IO.Path.DirectorySeparatorChar}{fileName}";

                                var present = File.Exists(fileFullPath);

                                if ((present & _overwrite) || (!present))
                                {
                                    var localstream = File.Create(fileFullPath);
                                    try
                                    {
                                        WriteVerbose($"Downloading: {remotepath}");
                                        sftpSession.Session.DownloadFile(remotepath, localstream, res);
                                        localstream.Close();
                                        var progressRecord = new ProgressRecord(1,
                                                                                "Downloading: " + remotepath,
                                                                                String.Format("{0} Bytes Downloaded of {1}", attribs.Size, attribs.Size))
                                        {
                                            PercentComplete = 100
                                        };

                                        WriteProgress(progressRecord);
                                    }
                                    catch
                                    {
                                        localstream.Close();
                                        var ex = new SftpPermissionDeniedException($"Unable to download {remotepath} from host.");
                                        WriteError(new ErrorRecord(
                                                       ex,
                                                       $"Unable to download {remotepath} from host.",
                                                       ErrorCategory.InvalidOperation,
                                                       sftpSession));
                                    }
                                }
                                else
                                {
                                    var ex = new SftpPermissionDeniedException($"Item {remotepath} already present on local host.");
                                    WriteError(new ErrorRecord(
                                                   ex,
                                                   $"Item {remotepath} already present on local host.",
                                                   ErrorCategory.InvalidOperation,
                                                   sftpSession));
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            var ex = new SftpPathNotFoundException(remotepath + " does not exist.");

                            WriteError(new ErrorRecord(
                                           ex,
                                           remotepath + " does not exist.",
                                           ErrorCategory.InvalidOperation,
                                           sftpSession));
                        }
                    }
                }
            }
            else
            {
                var ex = new FileNotFoundException("Local path" + localfullPath + " was not found.");

                ThrowTerminatingError(new ErrorRecord(
                                          ex,
                                          "Local path" + localfullPath + " was not found.",
                                          ErrorCategory.InvalidOperation,
                                          localfullPath));
            }
        }
Esempio n. 8
0
        protected override void ProcessRecord()
        {
            // check if the path specified actually exists.
            // Resolve the path even if a relative one is given for PowerShell.
            ProviderInfo provider;
            var          pathinfo      = GetResolvedProviderPathFromPSPath(_localpath, out provider);
            var          localfullPath = pathinfo[0];

            if (Directory.Exists(@localfullPath))
            {
                foreach (var sftpSession in ToProcess)
                {
                    foreach (string remotepath in _remotepath)
                    {
                        // Check that the path we are downloading from actually exists on the target.
                        if (sftpSession.Session.Exists(remotepath))
                        {
                            // Check if the remote path is a file or a directory to perform proper action.
                            var attribs = sftpSession.Session.GetAttributes(remotepath);

                            if (attribs.IsDirectory)
                            {
                                string dirName      = new DirectoryInfo(remotepath).Name;
                                var    fileFullPath = $"{@localfullPath}{System.IO.Path.DirectorySeparatorChar}{dirName}";

                                var present = Directory.Exists(fileFullPath);
                                if (!present || _overwrite)
                                {
                                    Directory.CreateDirectory(fileFullPath);
                                    DownloadDirectory(sftpSession.Session, remotepath, fileFullPath, _skipsymlink);
                                }
                                else
                                {
                                    var ex = new SftpPermissionDeniedException($"Item {remotepath} already present on local host.");
                                    WriteError(new ErrorRecord(
                                                   ex,
                                                   $"Item {remotepath} already present on local host.",
                                                   ErrorCategory.InvalidOperation,
                                                   sftpSession));
                                }
                            }
                            else if (attribs.IsRegularFile)
                            {
                                var fileName = new FileInfo(remotepath).Name;

                                var fileFullPath = $"{@localfullPath}{System.IO.Path.DirectorySeparatorChar}{fileName}";

                                var present = File.Exists(fileFullPath);

                                if (!present || _overwrite)
                                {
                                    using (var localstream = File.Create(fileFullPath))
                                    {
                                        try
                                        {
                                            WriteVerbose($"Downloading: {remotepath}");
                                            var progressHelper = new OperationProgressHelper(this, "Download", remotepath, attribs.Size, 1);
                                            sftpSession.Session.DownloadFile(remotepath, localstream, progressHelper.Callback);
                                            progressHelper.Complete();
                                        }
                                        catch
                                        {
                                            var ex = new SftpPermissionDeniedException($"Unable to download {remotepath} from host.");
                                            WriteError(new ErrorRecord(
                                                           ex,
                                                           $"Unable to download {remotepath} from host.",
                                                           ErrorCategory.InvalidOperation,
                                                           sftpSession));
                                        }
                                        finally
                                        {
                                            localstream.Close();
                                        }
                                    }
                                }
                                else
                                {
                                    var ex = new SftpPermissionDeniedException($"Item {remotepath} already present on local host.");
                                    WriteError(new ErrorRecord(
                                                   ex,
                                                   $"Item {remotepath} already present on local host.",
                                                   ErrorCategory.InvalidOperation,
                                                   sftpSession));
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            var ex = new SftpPathNotFoundException(remotepath + " does not exist.");

                            WriteError(new ErrorRecord(
                                           ex,
                                           remotepath + " does not exist.",
                                           ErrorCategory.InvalidOperation,
                                           sftpSession));
                        }
                    }
                }
            }
            else
            {
                var ex = new FileNotFoundException("Local path" + localfullPath + " was not found.");

                ThrowTerminatingError(new ErrorRecord(
                                          ex,
                                          "Local path" + localfullPath + " was not found.",
                                          ErrorCategory.InvalidOperation,
                                          localfullPath));
            }
        }