stream.SetLength() not truncating file
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 4.4k
- Forks
- 993
- Avg merge
- 9d 21h
- Merged PRs (30d)
- 1
Description
I'm trying to truncate a file on a SFTP server by opening a stream and then setting the length. The docs state that the stream will be truncated to the provided length. But the file remains unchanged.
If the specified value is less than the current length of the stream, the stream is truncated and - if the current position is greater than the new length - the current position is moved to the last byte of the stream.
var _client = new SftpClient("host", "user", "pass");
try
{
_client.Connect();
using (var stream = _client.OpenWrite("\\file2.txt"))
{
stream.SetLength(9);
//stream.Flush(); //no change with or without
}
}
catch (Exception ex)
{
Log(ex.Message);
}
The stream is working when I append more data to the file or if I make changes, but it won't get smaller. Thus I can't remove text from the file.
How can I remove all bytes from a file past a certain length?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing SftpClient.OpenWrite and the returned stream's SetLength implementation. Reproduce the example against an SFTP server and inspect how truncation is sent or applied, then add coverage for reducing a file's length. Done means a file is shortened to the requested byte length, with or without Flush().
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100