Esempio n. 1
0
        /// <summary>
        /// Uploads the specified file to the remote host.
        /// </summary>
        /// <param name="fileInfo">The file system info.</param>
        /// <param name="path">A relative or absolute path for the remote file.</param>
        /// <exception cref="ArgumentNullException"><paramref name="fileInfo" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="path" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="path"/> is a zero-length <see cref="string"/>.</exception>
        /// <exception cref="ScpException">A directory with the specified path exists on the remote host.</exception>
        /// <exception cref="SshException">The secure copy execution request was rejected by the server.</exception>
        public void Upload(FileInfo fileInfo, string path)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException("fileInfo");
            }

            var posixPath = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            using (var input = ServiceFactory.CreatePipeStream())
                using (var channel = Session.CreateChannelSession())
                {
                    channel.DataReceived += (sender, e) => input.Write(e.Data, 0, e.Data.Length);
                    channel.Open();

                    // Pass only the directory part of the path to the server, and use the (hidden) -d option to signal
                    // that we expect the target to be a directory.
                    if (!channel.SendExecRequest(string.Format("scp -t -d {0}", _remotePathTransformation.Transform(posixPath.Directory))))
                    {
                        throw SecureExecutionRequestRejectedException();
                    }
                    CheckReturnCode(input);

                    using (var source = fileInfo.OpenRead())
                    {
                        UploadTimes(channel, input, fileInfo);
                        UploadFileModeAndName(channel, input, source.Length, posixPath.File);
                        UploadFileContent(channel, input, source, fileInfo.Name);
                    }
                }
        }
Esempio n. 2
0
        public void Path_RootDirectoryOnly()
        {
            var path = "/";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/", actual.Directory);
            Assert.IsNull(actual.File);
        }
Esempio n. 3
0
        public void Path_FileInNonRootDirectory()
        {
            var path = "/home/sshnet/xyz";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/home/sshnet", actual.Directory);
            Assert.AreEqual("xyz", actual.File);
        }
Esempio n. 4
0
        public void Path_TrailingForwardSlash()
        {
            var path = "/abc/";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/abc", actual.Directory);
            Assert.IsNull(actual.File);
        }
Esempio n. 5
0
        public void Path_FileWithoutNoDirectory()
        {
            var path = "abc.log";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual(".", actual.Directory);
            Assert.AreSame(path, actual.File);
        }
Esempio n. 6
0
        public void Path_FileNameOnlyWhitespace()
        {
            var path = "/home/\t ";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/home", actual.Directory);
            Assert.AreEqual("\t ", actual.File);
        }
Esempio n. 7
0
        public void Path_OnlyWhitespace()
        {
            var path = " ";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual(".", actual.Directory);
            Assert.AreSame(path, actual.File);
        }
Esempio n. 8
0
        public void Path_TrailingWhitespace()
        {
            var path = "/abc \t ";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/", actual.Directory);
            Assert.AreEqual("abc \t ", actual.File);
        }
Esempio n. 9
0
        public void Path_LeadingWhitespace()
        {
            var path = "  / \tabc";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("  ", actual.Directory);
            Assert.AreEqual(" \tabc", actual.File);
        }
Esempio n. 10
0
        public void Path_ColonIsNotConsideredPathSeparator()
        {
            var path = "/home:abc.log";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/", actual.Directory);
            Assert.AreEqual("home:abc.log", actual.File);
        }
Esempio n. 11
0
        public void Path_Null()
        {
            const string path = null;

            try
            {
                PosixPath.CreateAbsoluteOrRelativeFilePath(path);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.IsNull(ex.InnerException);
                Assert.AreEqual("path", ex.ParamName);
            }
        }
Esempio n. 12
0
        public void Path_Empty()
        {
            var path = string.Empty;

            try
            {
                PosixPath.CreateAbsoluteOrRelativeFilePath(path);
                Assert.Fail();
            }
            catch (ArgumentException ex)
            {
                Assert.IsNull(ex.InnerException);
                Assert.AreEqual(string.Format("The path is a zero-length string.{0}Parameter name: {1}", Environment.NewLine, ex.ParamName), ex.Message);
                Assert.AreEqual("path", ex.ParamName);
            }
        }