Skip to content

Commit f28c9c4

Browse files
committed
Truncate existing file in CreateText
SftpClient.CreateText does not truncate the file if it exists, contrary to System.IO.File.CreateText which does. It is documented, but seems pretty unintuitive and more like a mistake than a concious decision. I considered leaving this 14 year old behaviour as it is, but turns out other people have hit it as well (it also affects WriteAll{Bytes/Text/Lines}).
1 parent 03ae6bf commit f28c9c4

File tree

4 files changed

+78
-358
lines changed

4 files changed

+78
-358
lines changed

src/Renci.SshNet/ISftpClient.cs

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,8 @@ public interface ISftpClient : IBaseClient
454454
/// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
455455
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
456456
/// <remarks>
457-
/// <para>
458-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
459-
/// </para>
460-
/// <para>
461-
/// If the target file does not exist, it is created.
462-
/// </para>
457+
/// If the file specified by <paramref name="path"/> does not exist, it is created.
458+
/// If the file does exist, its contents are replaced.
463459
/// </remarks>
464460
StreamWriter CreateText(string path);
465461

@@ -476,12 +472,8 @@ public interface ISftpClient : IBaseClient
476472
/// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
477473
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
478474
/// <remarks>
479-
/// <para>
480-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
481-
/// </para>
482-
/// <para>
483-
/// If the target file does not exist, it is created.
484-
/// </para>
475+
/// If the file specified by <paramref name="path"/> does not exist, it is created.
476+
/// If the file does exist, its contents are replaced.
485477
/// </remarks>
486478
StreamWriter CreateText(string path, Encoding encoding);
487479

@@ -1151,12 +1143,8 @@ public interface ISftpClient : IBaseClient
11511143
/// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
11521144
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
11531145
/// <remarks>
1154-
/// <para>
1155-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
1156-
/// </para>
1157-
/// <para>
1158-
/// If the target file does not exist, it is created.
1159-
/// </para>
1146+
/// If the file specified by <paramref name="path"/> does not exist, it is created.
1147+
/// If the file does exist, its contents are replaced.
11601148
/// </remarks>
11611149
void WriteAllBytes(string path, byte[] bytes);
11621150

@@ -1174,10 +1162,8 @@ public interface ISftpClient : IBaseClient
11741162
/// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).
11751163
/// </para>
11761164
/// <para>
1177-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
1178-
/// </para>
1179-
/// <para>
1180-
/// If the target file does not exist, it is created.
1165+
/// If the file specified by <paramref name="path"/> does not exist, it is created.
1166+
/// If the file does exist, its contents are replaced.
11811167
/// </para>
11821168
/// </remarks>
11831169
void WriteAllLines(string path, IEnumerable<string> contents);
@@ -1193,12 +1179,8 @@ public interface ISftpClient : IBaseClient
11931179
/// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
11941180
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
11951181
/// <remarks>
1196-
/// <para>
1197-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
1198-
/// </para>
1199-
/// <para>
1200-
/// If the target file does not exist, it is created.
1201-
/// </para>
1182+
/// If the file specified by <paramref name="path"/> does not exist, it is created.
1183+
/// If the file does exist, its contents are replaced.
12021184
/// </remarks>
12031185
void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding);
12041186

@@ -1216,10 +1198,8 @@ public interface ISftpClient : IBaseClient
12161198
/// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).
12171199
/// </para>
12181200
/// <para>
1219-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
1220-
/// </para>
1221-
/// <para>
1222-
/// If the target file does not exist, it is created.
1201+
/// If the file specified by <paramref name="path"/> does not exist, it is created.
1202+
/// If the file does exist, its contents are replaced.
12231203
/// </para>
12241204
/// </remarks>
12251205
void WriteAllLines(string path, string[] contents);
@@ -1235,12 +1215,8 @@ public interface ISftpClient : IBaseClient
12351215
/// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
12361216
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
12371217
/// <remarks>
1238-
/// <para>
1239-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
1240-
/// </para>
1241-
/// <para>
1242-
/// If the target file does not exist, it is created.
1243-
/// </para>
1218+
/// If the file specified by <paramref name="path"/> does not exist, it is created.
1219+
/// If the file does exist, its contents are replaced.
12441220
/// </remarks>
12451221
void WriteAllLines(string path, string[] contents, Encoding encoding);
12461222

@@ -1258,10 +1234,8 @@ public interface ISftpClient : IBaseClient
12581234
/// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).
12591235
/// </para>
12601236
/// <para>
1261-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
1262-
/// </para>
1263-
/// <para>
1264-
/// If the target file does not exist, it is created.
1237+
/// If the file specified by <paramref name="path"/> does not exist, it is created.
1238+
/// If the file does exist, its contents are replaced.
12651239
/// </para>
12661240
/// </remarks>
12671241
void WriteAllText(string path, string contents);
@@ -1277,12 +1251,8 @@ public interface ISftpClient : IBaseClient
12771251
/// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
12781252
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
12791253
/// <remarks>
1280-
/// <para>
1281-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
1282-
/// </para>
1283-
/// <para>
1284-
/// If the target file does not exist, it is created.
1285-
/// </para>
1254+
/// If the file specified by <paramref name="path"/> does not exist, it is created.
1255+
/// If the file does exist, its contents are replaced.
12861256
/// </remarks>
12871257
void WriteAllText(string path, string contents, Encoding encoding);
12881258
}

0 commit comments

Comments
 (0)