Skip to content

[API Proposal]: Introduce RandomAccess.SetLength(SafeFileHandle handle) method #58376

Closed

Description

@adamsitnik

Background and motivation

Recently, we have introduced new RandomAccess type (#24847) which exposes some convenient methods for File IO. One of them is RandomAccess.GetLength(SafeFileHandle) that allows for reading file length.

When it comes to setting file length, users can:

  • use FileMode.Truncate to truncate given file when opening it.
  • specify preallocationSize to set initial size when opening the file.

The problem is that by using the new APIs, they can't set file length after the file has been opened.

FileStream exposes SetLength method, so should RandomAccess.

API Proposal

namespace System.IO
{
    public static class RandomAccess
    {
        public static void SetLength(SafeFileHandle handle, long length);
    }
}

API Usage

void WriteToFile(string path, string contents, Encoding encoding)
{
    long estimatedFileSize = encoding.GetMaxByteCount(contents.Length); // estimated file length
    using SafeFileHandle fileHandle = OpenHandle(path, FileMode.Create, FileAccess.Write, preallocationSize: estimatedFileSize);

    byte[] bytes = encoding.GetBytes(contents);
    RandomAccess.Write(fileHandle, bytes, fileOffset: 0);

    if (bytes.Length < estimatedFileSize) // shrink the size if initial estimation was wrong
    {
        RandomAccess.SetLength(fileHandle, bytes.Length);
    }
}

Risks

I can't see any.

Activity

  1. added this to the 7.0.0 milestone on Aug 30, 2021
  2. self-assigned this
    on Aug 30, 2021
  3. ghost added
    untriaged
    on Aug 30, 2021
  4. removed
    untriaged
    on Aug 30, 2021
  5. Thealexbarney commented on Sep 1, 2021

    @Thealexbarney

    Oh, I didn't realize this API was missing from RandomAccess until it was too late. I'd just assumed it would be included in a lower-level file operations API set.

    What's the best way to work around this in .NET 6 if one always needs to have the possibility of setting the file length available? Open a FileStream, get its SafeFileHandle, use that for reads/writes and call FileStream.SetLength() to set the file length?

  6. adamsitnik commented on Sep 13, 2021

    @adamsitnik
    MemberAuthor

    Open a FileStream, get its SafeFileHandle, use that for reads/writes and call FileStream.SetLength() to set the file length?

    Yes. The alternative it to specify the exact size as preallocationSize when opening file

  7. added
    api-suggestion
    api-ready-for-review
    on Jan 13, 2022
  8. bartonjs commented on Jan 18, 2022

    @bartonjs
    Member

    Video

    Looks good as proposed

    namespace System.IO
    {
        public partial static class RandomAccess
        {
            public static void SetLength(SafeFileHandle handle, long length);
        }
    }
  9. added
    api-approved
    and removed
    api-suggestion
    api-ready-for-review
    on Jan 18, 2022
  10. ghost added
    in-pr
    on Jan 19, 2022
  11. ghost removed
    in-pr
    on Jan 21, 2022
  12. ghost locked as resolved and limited conversation to collaborators on Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.IO

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions