public void SftpPathNotFoundExceptionConstructorTest2()
 {
     string message = string.Empty; // TODO: Initialize to an appropriate value
     Exception innerException = null; // TODO: Initialize to an appropriate value
     SftpPathNotFoundException target = new SftpPathNotFoundException(message, innerException);
     Assert.Inconclusive("TODO: Implement code to verify target");
 }
Exemple #2
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(_localfile, out provider);
            var localfullPath = pathinfo[0];

            if (File.Exists(@localfullPath))
            {
                WriteVerbose("Uploading " + localfullPath);
                var fil = new FileInfo(@localfullPath);
                foreach (var sftpSession in ToProcess)
                {
                    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(RemotePath + " does not exist.");
                       ThrowTerminatingError(new ErrorRecord(
                                                ex,
                                                RemotePath + " does not exist.",
                                                ErrorCategory.InvalidOperation,
                                                sftpSession));
                    }
                }
            }
            else
            {
                var ex = new FileNotFoundException("File to upload " + localfullPath + " was not found.");

                ThrowTerminatingError(new ErrorRecord(
                                                ex,
                                                "File to upload " + localfullPath + " was not found.",
                                                ErrorCategory.InvalidOperation,
                                                localfullPath));
            }
        }
Exemple #3
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 SftpPathNotFoundExceptionConstructorTest()
 {
     SftpPathNotFoundException target = new SftpPathNotFoundException();
     Assert.Inconclusive("TODO: Implement code to verify target");
 }