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.
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()orToDirectoryInfo()and writing the enumeration by hand. The library already exposes comparable information throughExists,IsFile,IsDirectoryandLastWriteTimeUtc, so these fit the existing shape.API Proposal
API Usage
Alternative Designs
Size.LengthmatchesFileInfo.Length, which is the more familiar name for .NET developers.long?so a missing file yieldsnullrather than throwing. Worth discussing, since the existingLastWriteTimeUtcsets 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
Lengthis requested, must be consistent withLastWriteTimeUtc.IsEmptyDirectoryon a non-existent path needs a defined answer too. Both properties hit the file system, so they should not be presented as cheap.