Exemplo 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);
                    }
                }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Factory method to create a new <see cref="PurePath"/> instance
        /// based upon the current operating system.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="path"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public bool TryCreate(PathFactoryOptions options, string path, out IPath result)
        {
            result = null;
            switch (PlatformChooser.GetPlatform())
            {
            case Platform.Posix:
                PurePosixPath purePosixPath;
                if (PurePosixPath.TryParse(path, out purePosixPath))
                {
                    result = new PosixPath(purePosixPath);
                    break;
                }
                return(false);

            case Platform.Windows:
                PureWindowsPath pureWindowsPath;
                if (PureWindowsPath.TryParse(path, out pureWindowsPath))
                {
                    result = new WindowsPath(path);
                    break;
                }
                return(false);
            }
            result = ApplyOptions(result, options);
            return(true);
        }
Exemplo n.º 3
0
        public async Task Write(string path, WriteFileMode mode, IWritable writable, bool appendGuid, bool createDirectory, CancellationToken cancel)
        {
            if (appendGuid)
            {
                var p = new PosixPath(path);
                if (p.IsEmpty)
                {
                    path = Guid.NewGuid().ToString("N");
                }
                else
                {
                    path = p.FileNameWithoutExtension + Guid.NewGuid().ToString("N") + p.Extension;
                }
            }

            path = this.Graph.ResolvePath(path);

            if (createDirectory)
            {
                var directoryPath = Path.GetDirectoryName(path);
                Directory.CreateDirectory(directoryPath);
            }

            using (var file = Open(path, mode))
            {
                using (writable)
                {
                    await writable.WriteTo(file, cancel).ConfigureAwait(false);
                }
            }
        }
Exemplo n.º 4
0
    public void Stat_With_MissingFile_GivesError()
    {
        var path = new PosixPath(Path.Combine(_fixture.TempFolder, "does_not_exist"));

        Assert.False(path.Exists());

        Assert.Throws <FileNotFoundException>(() => path.Stat());
    }
        public void Path_RootDirectoryOnly()
        {
            var path = "/";

            var actual = PosixPath.GetFileName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual(string.Empty, actual);
        }
        public void Path_TrailingForwardSlash()
        {
            var path = "/abc/";

            var actual = PosixPath.GetFileName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual(string.Empty, actual);
        }
        public void Path_Empty()
        {
            var path = string.Empty;

            var actual = PosixPath.GetFileName(path);

            Assert.IsNotNull(actual);
            Assert.AreSame(path, actual);
        }
Exemplo n.º 8
0
    public void JoinPosixPath_WithStringByDiv_ReturnsPosixPath()
    {
        var path  = new PosixPath(@"/tmp");
        var other = @"/";

        var final = path / other;

        Assert.True(final is PosixPath);
    }
        public void Path_TrailingForwardSlash()
        {
            var path = "/abc/";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/abc", actual);
        }
        public void Path_Empty()
        {
            var path = string.Empty;

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual(".", actual);
        }
        public void Path_ColonIsNotConsideredPathSeparator()
        {
            var path = "/home:abc.log";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/", actual);
        }
        public void Path_FileInRootDirectory()
        {
            var path = "/abc.log";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/", actual);
        }
        public void Path_RootDirectoryOnly()
        {
            var path = "/";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/", actual);
        }
Exemplo n.º 14
0
    public void JoinIPath_WithAnotherPathByDiv_ReturnsWindowsPath()
    {
        IPath path  = new PosixPath(@"/tmp");
        IPath other = new PosixPath(@"/tmp");

        var final = path / other;

        Assert.True(final is PosixPath);
    }
        public void Path_FileWithoutNoDirectory()
        {
            var path = "abc.log";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual(".", actual);
        }
        public void Path_FileNameOnlyWhitespace()
        {
            var path = "/home/\t ";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/home", actual);
        }
Exemplo n.º 17
0
    public void ExpandUser_WithHomeDirSet_ReplacesPath()
    {
        var root     = Environment.GetEnvironmentVariable("HOME");
        var expected = new PosixPath(root, "tmp");

        var actual = new PosixPath("~/tmp").ExpandUser();

        Assert.Equal(expected, actual);
    }
        public void Path_OnlyWhitespace()
        {
            var path = " ";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual(".", actual);
        }
        public void Path_TrailingWhitespace()
        {
            var path = "/abc \t ";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/", actual);
        }
        public void Path_LeadingWhitespace()
        {
            var path = "  / \tabc";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("  ", actual);
        }
        public void Path_BackslashIsNotConsideredDirectorySeparator()
        {
            var path = "/home\\abc.log";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/", actual);
        }
        public void Path_FileInNonRootDirectory()
        {
            var path = "/home/sshnet/xyz";

            var actual = PosixPath.GetDirectoryName(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/home/sshnet", actual);
        }
Exemplo n.º 23
0
    public void FileType_WithFileNotExist_ReturnsFileNotExist()
    {
        var fname = Guid.NewGuid().ToString();
        var path  = Path.Combine(_fixture.TempFolder, fname);

        var fileType = new PosixPath(path).GetFileType();

        Assert.Equal(PathLib.Posix.FileType.DoesNotExist, fileType);
    }
Exemplo n.º 24
0
        public void Path_RootDirectoryOnly()
        {
            var path = "/";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual("/", actual.Directory);
            Assert.IsNull(actual.File);
        }
Exemplo n.º 25
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);
        }
Exemplo n.º 26
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);
        }
Exemplo n.º 27
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);
        }
Exemplo n.º 28
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);
        }
Exemplo n.º 29
0
        public void Path_OnlyWhitespace()
        {
            var path = " ";

            var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);

            Assert.IsNotNull(actual);
            Assert.AreEqual(".", actual.Directory);
            Assert.AreSame(path, actual.File);
        }
Exemplo n.º 30
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);
        }
Exemplo n.º 31
0
 /// <summary>
 /// Factory method to create a new <see cref="PurePath"/> instance
 /// based upon the current operating system.
 /// </summary>
 /// <param name="path"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public IPath Create(string path, PathFactoryOptions options)
 {
     IPath ret = null;
     switch (PlatformChooser.GetPlatform())
     {
         case Platform.Posix:
             ret = new PosixPath(path);
             break;
         case Platform.Windows:
             ret =  new WindowsPath(path);
             break;
     }
     return ApplyOptions(ret, options);
 }
Exemplo n.º 32
0
 /// <summary>
 /// Factory method to create a new <see cref="PurePath"/> instance
 /// based upon the current operating system.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="path"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public bool TryCreate(PathFactoryOptions options, string path, out IPath result)
 {
     result = null;
     switch (PlatformChooser.GetPlatform())
     {
         case Platform.Posix:
             PurePosixPath purePosixPath;
             if (PurePosixPath.TryParse(path, out purePosixPath))
             {
                 result = new PosixPath(purePosixPath);
                 break;
             }
             return false;
         case Platform.Windows:
             PureWindowsPath pureWindowsPath;
             if (PureWindowsPath.TryParse(path, out pureWindowsPath))
             {
                 result = new WindowsPath(path);
                 break;
             }
             return false;
     }
     result = ApplyOptions(result, options);
     return true;
 }