Skip to content

[API Proposal]: Length and IsEmptyDirectory #133

Description

@dennisdoomen

Background and motivation

Two very common questions about a file system entry are not answerable through Pathy today: how big is this file, and does this directory contain anything. Both currently require dropping down to ToFileInfo() or ToDirectoryInfo() and writing the enumeration by hand. The library already exposes comparable information through Exists, IsFile, IsDirectory and LastWriteTimeUtc, so these fit the existing shape.

API Proposal

namespace Pathy
{
    public readonly struct ChainablePath
    {
        public long Length { get; }
        public bool IsEmptyDirectory { get; }
    }
}

API Usage

var artifact = artifactsDirectory / "Pathy.nupkg";

if (artifact.Length == 0)
{
    throw new InvalidOperationException("The packaging step produced an empty file.");
}

if ((ChainablePath.Current / "TestResults").IsEmptyDirectory)
{
    Console.WriteLine("No test results were produced.");
}

Alternative Designs

  • Name the size property Size. Length matches FileInfo.Length, which is the more familiar name for .NET developers.
  • Return long? so a missing file yields null rather than throwing. Worth discussing, since the existing LastWriteTimeUtc sets the precedent for how missing entries are handled.

Risks

The behaviour for a path that does not exist, or that points at a directory when Length is requested, must be consistent with LastWriteTimeUtc. IsEmptyDirectory on a non-existent path needs a defined answer too. Both properties hit the file system, so they should not be presented as cheap.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions