Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileStream.SetLength doesn't return file pointer to original value on failure #8307

Closed
azhmur opened this issue Jun 7, 2017 · 2 comments
Closed
Assignees
Milestone

Comments

@azhmur
Copy link
Contributor

azhmur commented Jun 7, 2017

In case someone need reserve disk space before writing to file obvious strategy is following:

  var fileStream = new FileStream();
  while (*)
  {
    try 
    {
       fileStream.SetLength();
       fileStream.Write();
    }
    catch ( out of space )
    {
    }
  }

But unfortunately on real out of disk space situation SetLength doesn't restore position, so on next interation succesfull SetLength restores incorrect position (EOF) and Write goes beyong supposed end of file.

I suppose to add try/finally statement for position restoration in SetLengthCore, to make this behaviour safer. I think moving position is uninteded and exists as work around for WinAPI incompleteness. I can make PR if this change is supposed to be approved.

https://github.com/dotnet/coreclr/blob/684784fdb929f2c7d0173a317cb6e3ac6dc29d1a/src/mscorlib/shared/System/IO/FileStream.Win32.cs#L444

PS Unix version seems to be calling unnecessary seeks. FTruncate doesn't need it. It seems like copy-pasting from Win32 version.
https://linux.die.net/man/2/truncate

@stephentoub
Copy link
Member

cc: @JeremyKuhne

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 26, 2020
@JeremyKuhne JeremyKuhne removed the untriaged New issue has not been triaged by the area owner label Mar 3, 2020
@carlossanlop carlossanlop changed the title FileStream.SetLength doesn't return file pointer to orginal value on failure FileStream.SetLength doesn't return file pointer to original value on failure Mar 6, 2020
@jozkee
Copy link
Member

jozkee commented Nov 4, 2020

Fixed by #44170 (Windows) and #44097 (Unix).

@jozkee jozkee closed this as completed Nov 4, 2020
@jozkee jozkee modified the milestones: Future, 6.0.0 Nov 5, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants