Skip to content

Commit 583a9ce

Browse files
wxtsxtWojciech Swieboda
and
Wojciech Swieboda
authored
Add interface to SftpFile #120 (#812)
* Create ISftpFile interface. SftpFile sealed. Return ISftpFile from SftpClient instead of SftpFile. Make ISftpClient interface disposable. Co-authored-by: Wojciech Swieboda <wswieboda@chathamfinancial.com>
1 parent f072c5f commit 583a9ce

File tree

11 files changed

+275
-22
lines changed

11 files changed

+275
-22
lines changed

src/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,9 @@
13581358
<Compile Include="..\Renci.SshNet\Sftp\SftpFile.cs">
13591359
<Link>Sftp\SftpFile.cs</Link>
13601360
</Compile>
1361+
<Compile Include="..\Renci.SshNet\Sftp\ISftpFile.cs">
1362+
<Link>Sftp\ISftpFile.cs</Link>
1363+
</Compile>
13611364
<Compile Include="..\Renci.SshNet\Sftp\SftpFileAttributes.cs">
13621365
<Link>Sftp\SftpFileAttributes.cs</Link>
13631366
</Compile>

src/Renci.SshNet.Silverlight5/Renci.SshNet.Silverlight5.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,9 @@
13641364
<Compile Include="..\Renci.SshNet\Sftp\SftpFile.cs">
13651365
<Link>Sftp\SftpFile.cs</Link>
13661366
</Compile>
1367+
<Compile Include="..\Renci.SshNet\Sftp\ISftpFile.cs">
1368+
<Link>Sftp\ISftpFile.cs</Link>
1369+
</Compile>
13671370
<Compile Include="..\Renci.SshNet\Sftp\SftpFileAttributes.cs">
13681371
<Link>Sftp\SftpFileAttributes.cs</Link>
13691372
</Compile>

src/Renci.SshNet.Tests/Classes/SftpClientTest.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ public void EndListDirectoryTest()
562562
ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
563563
SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
564564
IAsyncResult asyncResult = null; // TODO: Initialize to an appropriate value
565-
IEnumerable<SftpFile> expected = null; // TODO: Initialize to an appropriate value
566-
IEnumerable<SftpFile> actual;
565+
IEnumerable<ISftpFile> expected = null; // TODO: Initialize to an appropriate value
566+
IEnumerable<ISftpFile> actual;
567567
actual = target.EndListDirectory(asyncResult);
568568
Assert.AreEqual(expected, actual);
569569
Assert.Inconclusive("Verify the correctness of this test method.");
@@ -702,8 +702,8 @@ public void GetTest()
702702
ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
703703
SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
704704
string path = string.Empty; // TODO: Initialize to an appropriate value
705-
SftpFile expected = null; // TODO: Initialize to an appropriate value
706-
SftpFile actual;
705+
ISftpFile expected = null; // TODO: Initialize to an appropriate value
706+
ISftpFile actual;
707707
actual = target.Get(path);
708708
Assert.AreEqual(expected, actual);
709709
Assert.Inconclusive("Verify the correctness of this test method.");
@@ -802,8 +802,8 @@ public void ListDirectoryTest()
802802
SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
803803
string path = string.Empty; // TODO: Initialize to an appropriate value
804804
Action<int> listCallback = null; // TODO: Initialize to an appropriate value
805-
IEnumerable<SftpFile> expected = null; // TODO: Initialize to an appropriate value
806-
IEnumerable<SftpFile> actual;
805+
IEnumerable<ISftpFile> expected = null; // TODO: Initialize to an appropriate value
806+
IEnumerable<ISftpFile> actual;
807807
actual = target.ListDirectory(path, listCallback);
808808
Assert.AreEqual(expected, actual);
809809
Assert.Inconclusive("Verify the correctness of this test method.");

src/Renci.SshNet.UAP10/Renci.SshNet.UAP10.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,9 @@
14401440
<Compile Include="..\Renci.SshNet\Sftp\SftpFile.cs">
14411441
<Link>Sftp\SftpFile.cs</Link>
14421442
</Compile>
1443+
<Compile Include="..\Renci.SshNet\Sftp\ISftpFile.cs">
1444+
<Link>Sftp\ISftpFile.cs</Link>
1445+
</Compile>
14431446
<Compile Include="..\Renci.SshNet\Sftp\SftpFileAttributes.cs">
14441447
<Link>Sftp\SftpFileAttributes.cs</Link>
14451448
</Compile>

src/Renci.SshNet.WindowsPhone/Renci.SshNet.WindowsPhone.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,9 @@
13431343
<Compile Include="..\Renci.SshNet\Sftp\SftpFile.cs">
13441344
<Link>Sftp\SftpFile.cs</Link>
13451345
</Compile>
1346+
<Compile Include="..\Renci.SshNet\Sftp\ISftpFile.cs">
1347+
<Link>Sftp\ISftpFile.cs</Link>
1348+
</Compile>
13461349
<Compile Include="..\Renci.SshNet\Sftp\SftpFileAttributes.cs">
13471350
<Link>Sftp\SftpFileAttributes.cs</Link>
13481351
</Compile>

src/Renci.SshNet.WindowsPhone8/Renci.SshNet.WindowsPhone8.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,9 @@
13961396
<Compile Include="..\Renci.SshNet\Sftp\SftpFile.cs">
13971397
<Link>Sftp\SftpFile.cs</Link>
13981398
</Compile>
1399+
<Compile Include="..\Renci.SshNet\Sftp\ISftpFile.cs">
1400+
<Link>Sftp\ISftpFile.cs</Link>
1401+
</Compile>
13991402
<Compile Include="..\Renci.SshNet\Sftp\SftpFileAttributes.cs">
14001403
<Link>Sftp\SftpFileAttributes.cs</Link>
14011404
</Compile>

src/Renci.SshNet/ISftpClient.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Renci.SshNet
1010
/// <summary>
1111
/// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
1212
/// </summary>
13-
public interface ISftpClient
13+
public interface ISftpClient : IDisposable
1414
{
1515
/// <summary>
1616
/// Gets or sets the maximum size of the buffer in bytes.
@@ -525,7 +525,7 @@ public interface ISftpClient
525525
/// A list of files.
526526
/// </returns>
527527
/// <exception cref="ArgumentException">The <see cref="IAsyncResult"/> object did not come from the corresponding async method on this type.<para>-or-</para><see cref="EndListDirectory(IAsyncResult)"/> was called multiple times with the same <see cref="IAsyncResult"/>.</exception>
528-
IEnumerable<SftpFile> EndListDirectory(IAsyncResult asyncResult);
528+
IEnumerable<ISftpFile> EndListDirectory(IAsyncResult asyncResult);
529529

530530
/// <summary>
531531
/// Ends the synchronize directories.
@@ -568,13 +568,13 @@ public interface ISftpClient
568568
/// </summary>
569569
/// <param name="path">The path.</param>
570570
/// <returns>
571-
/// A reference to <see cref="SftpFile"/> file object.
571+
/// A reference to <see cref="ISftpFile"/> file object.
572572
/// </returns>
573573
/// <exception cref="SshConnectionException">Client is not connected.</exception>
574574
/// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
575575
/// <exception cref="ArgumentNullException"><paramref name="path" /> is <b>null</b>.</exception>
576576
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
577-
SftpFile Get(string path);
577+
ISftpFile Get(string path);
578578

579579
/// <summary>
580580
/// Gets the <see cref="SftpFileAttributes"/> of the file on the path.
@@ -666,7 +666,7 @@ public interface ISftpClient
666666
/// <exception cref="SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
667667
/// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
668668
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
669-
IEnumerable<SftpFile> ListDirectory(string path, Action<int> listCallback = null);
669+
IEnumerable<ISftpFile> ListDirectory(string path, Action<int> listCallback = null);
670670

671671
/// <summary>
672672
/// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access.

src/Renci.SshNet/Sftp/ISftpFile.cs

+233
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
using System;
2+
3+
namespace Renci.SshNet.Sftp
4+
{
5+
/// <summary>
6+
/// Represents SFTP file information
7+
/// </summary>
8+
public interface ISftpFile
9+
{
10+
/// <summary>
11+
/// Gets the file attributes.
12+
/// </summary>
13+
SftpFileAttributes Attributes { get; }
14+
15+
/// <summary>
16+
/// Gets the full path of the directory or file.
17+
/// </summary>
18+
string FullName { get; }
19+
20+
/// <summary>
21+
/// For files, gets the name of the file. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists.
22+
/// Otherwise, the Name property gets the name of the directory.
23+
/// </summary>
24+
string Name { get; }
25+
26+
/// <summary>
27+
/// Gets or sets the time the current file or directory was last accessed.
28+
/// </summary>
29+
/// <value>
30+
/// The time that the current file or directory was last accessed.
31+
/// </value>
32+
DateTime LastAccessTime { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the time when the current file or directory was last written to.
36+
/// </summary>
37+
/// <value>
38+
/// The time the current file was last written.
39+
/// </value>
40+
DateTime LastWriteTime { get; set; }
41+
42+
/// <summary>
43+
/// Gets or sets the time, in coordinated universal time (UTC), the current file or directory was last accessed.
44+
/// </summary>
45+
/// <value>
46+
/// The time that the current file or directory was last accessed.
47+
/// </value>
48+
DateTime LastAccessTimeUtc { get; set; }
49+
50+
/// <summary>
51+
/// Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.
52+
/// </summary>
53+
/// <value>
54+
/// The time the current file was last written.
55+
/// </value>
56+
DateTime LastWriteTimeUtc { get; set; }
57+
58+
/// <summary>
59+
/// Gets or sets the size, in bytes, of the current file.
60+
/// </summary>
61+
/// <value>
62+
/// The size of the current file in bytes.
63+
/// </value>
64+
long Length { get; }
65+
66+
/// <summary>
67+
/// Gets or sets file user id.
68+
/// </summary>
69+
/// <value>
70+
/// File user id.
71+
/// </value>
72+
int UserId { get; set; }
73+
74+
/// <summary>
75+
/// Gets or sets file group id.
76+
/// </summary>
77+
/// <value>
78+
/// File group id.
79+
/// </value>
80+
int GroupId { get; set; }
81+
82+
/// <summary>
83+
/// Gets a value indicating whether file represents a socket.
84+
/// </summary>
85+
/// <value>
86+
/// <c>true</c> if file represents a socket; otherwise, <c>false</c>.
87+
/// </value>
88+
bool IsSocket { get; }
89+
90+
/// <summary>
91+
/// Gets a value indicating whether file represents a symbolic link.
92+
/// </summary>
93+
/// <value>
94+
/// <c>true</c> if file represents a symbolic link; otherwise, <c>false</c>.
95+
/// </value>
96+
bool IsSymbolicLink { get; }
97+
98+
/// <summary>
99+
/// Gets a value indicating whether file represents a regular file.
100+
/// </summary>
101+
/// <value>
102+
/// <c>true</c> if file represents a regular file; otherwise, <c>false</c>.
103+
/// </value>
104+
bool IsRegularFile { get; }
105+
106+
/// <summary>
107+
/// Gets a value indicating whether file represents a block device.
108+
/// </summary>
109+
/// <value>
110+
/// <c>true</c> if file represents a block device; otherwise, <c>false</c>.
111+
/// </value>
112+
bool IsBlockDevice { get; }
113+
114+
/// <summary>
115+
/// Gets a value indicating whether file represents a directory.
116+
/// </summary>
117+
/// <value>
118+
/// <c>true</c> if file represents a directory; otherwise, <c>false</c>.
119+
/// </value>
120+
bool IsDirectory { get; }
121+
122+
/// <summary>
123+
/// Gets a value indicating whether file represents a character device.
124+
/// </summary>
125+
/// <value>
126+
/// <c>true</c> if file represents a character device; otherwise, <c>false</c>.
127+
/// </value>
128+
bool IsCharacterDevice { get; }
129+
130+
/// <summary>
131+
/// Gets a value indicating whether file represents a named pipe.
132+
/// </summary>
133+
/// <value>
134+
/// <c>true</c> if file represents a named pipe; otherwise, <c>false</c>.
135+
/// </value>
136+
bool IsNamedPipe { get; }
137+
138+
/// <summary>
139+
/// Gets or sets a value indicating whether the owner can read from this file.
140+
/// </summary>
141+
/// <value>
142+
/// <c>true</c> if owner can read from this file; otherwise, <c>false</c>.
143+
/// </value>
144+
bool OwnerCanRead { get; set; }
145+
146+
/// <summary>
147+
/// Gets or sets a value indicating whether the owner can write into this file.
148+
/// </summary>
149+
/// <value>
150+
/// <c>true</c> if owner can write into this file; otherwise, <c>false</c>.
151+
/// </value>
152+
bool OwnerCanWrite { get; set; }
153+
154+
/// <summary>
155+
/// Gets or sets a value indicating whether the owner can execute this file.
156+
/// </summary>
157+
/// <value>
158+
/// <c>true</c> if owner can execute this file; otherwise, <c>false</c>.
159+
/// </value>
160+
bool OwnerCanExecute { get; set; }
161+
162+
/// <summary>
163+
/// Gets or sets a value indicating whether the group members can read from this file.
164+
/// </summary>
165+
/// <value>
166+
/// <c>true</c> if group members can read from this file; otherwise, <c>false</c>.
167+
/// </value>
168+
bool GroupCanRead { get; set; }
169+
170+
/// <summary>
171+
/// Gets or sets a value indicating whether the group members can write into this file.
172+
/// </summary>
173+
/// <value>
174+
/// <c>true</c> if group members can write into this file; otherwise, <c>false</c>.
175+
/// </value>
176+
bool GroupCanWrite { get; set; }
177+
178+
/// <summary>
179+
/// Gets or sets a value indicating whether the group members can execute this file.
180+
/// </summary>
181+
/// <value>
182+
/// <c>true</c> if group members can execute this file; otherwise, <c>false</c>.
183+
/// </value>
184+
bool GroupCanExecute { get; set; }
185+
186+
/// <summary>
187+
/// Gets or sets a value indicating whether the others can read from this file.
188+
/// </summary>
189+
/// <value>
190+
/// <c>true</c> if others can read from this file; otherwise, <c>false</c>.
191+
/// </value>
192+
bool OthersCanRead { get; set; }
193+
194+
/// <summary>
195+
/// Gets or sets a value indicating whether the others can write into this file.
196+
/// </summary>
197+
/// <value>
198+
/// <c>true</c> if others can write into this file; otherwise, <c>false</c>.
199+
/// </value>
200+
bool OthersCanWrite { get; set; }
201+
202+
/// <summary>
203+
/// Gets or sets a value indicating whether the others can execute this file.
204+
/// </summary>
205+
/// <value>
206+
/// <c>true</c> if others can execute this file; otherwise, <c>false</c>.
207+
/// </value>
208+
bool OthersCanExecute { get; set; }
209+
210+
/// <summary>
211+
/// Sets file permissions.
212+
/// </summary>
213+
/// <param name="mode">The mode.</param>
214+
void SetPermissions(short mode);
215+
216+
/// <summary>
217+
/// Permanently deletes a file on remote machine.
218+
/// </summary>
219+
void Delete();
220+
221+
/// <summary>
222+
/// Moves a specified file to a new location on remote machine, providing the option to specify a new file name.
223+
/// </summary>
224+
/// <param name="destFileName">The path to move the file to, which can specify a different file name.</param>
225+
/// <exception cref="ArgumentNullException"><paramref name="destFileName"/> is <c>null</c>.</exception>
226+
void MoveTo(string destFileName);
227+
228+
/// <summary>
229+
/// Updates file status on the server.
230+
/// </summary>
231+
void UpdateStatus();
232+
}
233+
}

src/Renci.SshNet/Sftp/SftpFile.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Renci.SshNet.Sftp
77
/// <summary>
88
/// Represents SFTP file information
99
/// </summary>
10-
public class SftpFile
10+
public sealed class SftpFile : ISftpFile
1111
{
1212
private readonly ISftpSession _sftpSession;
1313

src/Renci.SshNet/Sftp/SftpListDirectoryAsyncResult.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Renci.SshNet.Sftp
77
/// <summary>
88
/// Encapsulates the results of an asynchronous directory list operation.
99
/// </summary>
10-
public class SftpListDirectoryAsyncResult : AsyncResult<IEnumerable<SftpFile>>
10+
public class SftpListDirectoryAsyncResult : AsyncResult<IEnumerable<ISftpFile>>
1111
{
1212
/// <summary>
1313
/// Gets the number of files read so far.

0 commit comments

Comments
 (0)