CopyOnWrite 0.1.10

There is a newer version of this package available.
See the version list below for details.
dotnet add package CopyOnWrite --version 0.1.10                
NuGet\Install-Package CopyOnWrite -Version 0.1.10                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CopyOnWrite" Version="0.1.10" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CopyOnWrite --version 0.1.10                
#r "nuget: CopyOnWrite, 0.1.10"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CopyOnWrite as a Cake Addin
#addin nuget:?package=CopyOnWrite&version=0.1.10

// Install CopyOnWrite as a Cake Tool
#tool nuget:?package=CopyOnWrite&version=0.1.10                

The CopyOnWrite library provides a .NET layer on top of OS-specific logic that provides copy-on-write linking for files (a.k.a. CoW, file cloning, or reflinking). CoW linking provides the ability to copy a file without actually copying the original file's bytes from one disk location to another. The filesystem is in charge of ensuring that if the original file is modified or deleted, the CoW linked files remain unmodified by lazily copying the original file's bytes into each link. Unlike symlinks or hardlinks, writes to CoW links do not write through to the original file, as the filesystem breaks the link and copies in a lazy fashion. This enables scenarios like file caches where a single copy of a file held in a content-addressable or other store is safely linked to many locations in a filesystem with low I/O overhead.

This library allows a .NET developer to:

  • Discover whether CoW links are allowed between two filesystem paths,
  • Discover whether CoW links are allowed for a directory tree based at a specific root directory,
  • Create CoW links,
  • Find filesystem CoW link limits.

Discovery is important, as different operating systems and different filesystems available for those operating systems provide varying levels of CoW link support:

  • Windows: The default NTFS filesystem does NOT support CoW, but the ReFS filesystem does.
  • Linux: Btrfs, Xfs, Zfs support CoW while ext4 does not.
  • Mac: AppleFS supports CoW by default.

When using this library you may need to create a wrapper that copies the file if CoW is not available.

Example

using Microsoft.CopyOnWrite;

ICopyOnWriteFilesystem cow = CopyOnWriteFilesystemFactory.GetInstance();
bool canCloneInCurrentDirectory = cow.CopyOnWriteLinkSupportedInDirectoryTree(Environment.CurrentDirectory);
if (canCloneInCurrentDirectory)
{
    cow.CloneFile(existingFile, cowLinkFilePath);
}

Release History

NuGet package:

  • 0.1.10 September 2022: Fix missing destination file failure detection.
  • 0.1.9 September 2022: Add explicit cache invalidation call to interface. Update Windows implementation to detect ReFS mount points that are not drive roots, e.g. mounting D:\ (ReFS volume) under C:\ReFS.
  • 0.1.8 April 2022: Add overload for CoW clone to allow bypassing some Windows filesystem feature checks
  • 0.1.7 April 2022: Perf improvement for Windows CoW link creation by reducing kernel round-trips
  • 0.1.6 April 2022: Perf improvement for all Windows APIs
  • 0.1.5 October 2021: Separate exception type for when link limit is exceeded. Mac and Linux throw NotSupportedException.
  • 0.1.4 October 2021: Fix doc XML naming. Mac and Linux throw NotSupportedException.
  • 0.1.3 October 2021: Bug fixes for Windows. Mac and Linux throw NotSupportedException.
  • 0.1.2 October 2021: Performance fixes for Windows. Mac and Linux throw NotSupportedException.
  • 0.1.1 October 2021: Bug fixes for Windows. Mac and Linux throw NotSupportedException.
  • 0.1.0 July 2021: Windows ReFS support. Mac and Linux throw NotSupportedException.

Contributing

This project welcomes contributions and suggestions. See CONTRIBUTING.md.

Running Unit Tests on Windows

If you have a local ReFS drive volume on which to run ReFS related tests, set the following user or system level environment variable:

CoW_Test_ReFS_Drive=D:\

(You may need to exit and restart VS, VSCode, or consoles after setting this.) When this env var is not available, unit tests create and mount a local ReFS VHD for testing. You must run tests elevated (as admin), e.g. by opening Visual Studio as an admin before opening the solution.

Performance Comparisons

Windows

CoW links on ReFS take approximately constant time, saving time over file copies except at file size zero. The savings is proportional to the file size, with 16MB files at about 35X performance, 1MB at 3.2X, and small sizes at about 1.3X.

Detailed numbers for a VHD formatted empty with ReFS for each iteration, comparing System.IO.File.Copy() with CloneFile(), 25 copies/clones of a single source file per measurement. See CoWComparisons.cs. Machine was an 8/16-core M.2 SSD, Win10 21H1 Enterprise.

Method FileSize Mean Error StdDev Median Ratio RatioSD
File.Copy 0 4.933 ms 0.3274 ms 0.9551 ms 4.799 ms 1.00 0.00
CoW 0 5.002 ms 0.2447 ms 0.7100 ms 4.885 ms 1.04 0.21
File.Copy 1 8.841 ms 0.4976 ms 1.4197 ms 8.515 ms 1.00 0.00
CoW 1 6.499 ms 0.3647 ms 1.0579 ms 6.169 ms 0.75 0.13
File.Copy 1024 8.614 ms 0.4982 ms 1.4374 ms 8.471 ms 1.00 0.00
CoW 1024 6.678 ms 0.3776 ms 1.0834 ms 6.269 ms 0.79 0.14
File.Copy 16384 8.950 ms 0.5148 ms 1.4770 ms 8.454 ms 1.00 0.00
CoW 16384 6.774 ms 0.3240 ms 0.9137 ms 6.622 ms 0.77 0.12
File.Copy 262144 12.700 ms 0.6236 ms 1.7993 ms 12.140 ms 1.00 0.00
CoW 262144 7.036 ms 0.3911 ms 1.1470 ms 6.882 ms 0.56 0.10
File.Copy 1048576 21.920 ms 0.4359 ms 1.1785 ms 21.690 ms 1.00 0.00
CoW 1048576 6.940 ms 0.3652 ms 1.0711 ms 6.743 ms 0.32 0.05
File.Copy 16777216 252.637 ms 2.9167 ms 2.5855 ms 251.895 ms 1.00 0.00
CoW 16777216 7.272 ms 0.2882 ms 0.7987 ms 7.136 ms 0.03 0.00

Same benchmark performed on a ReFS partition (no VHD) on an M.2 SSD:

Method FileSize Mean Error StdDev Median Ratio RatioSD
File.Copy 0 3.627 ms 0.2430 ms 0.7127 ms 3.453 ms 1.00 0.00
CoW 0 4.173 ms 0.1934 ms 0.5641 ms 4.067 ms 1.19 0.27
File.Copy 1 6.711 ms 0.2276 ms 0.6567 ms 6.652 ms 1.00 0.00
CoW 1 5.597 ms 0.1989 ms 0.5803 ms 5.471 ms 0.84 0.12
File.Copy 1024 6.476 ms 0.2533 ms 0.7146 ms 6.398 ms 1.00 0.00
CoW 1024 5.647 ms 0.2135 ms 0.6194 ms 5.614 ms 0.89 0.14
File.Copy 16384 6.514 ms 0.2421 ms 0.6946 ms 6.340 ms 1.00 0.00
CoW 16384 5.703 ms 0.2212 ms 0.6453 ms 5.636 ms 0.88 0.12
File.Copy 262144 9.579 ms 0.4325 ms 1.2751 ms 9.421 ms 1.00 0.00
CoW 262144 5.770 ms 0.2632 ms 0.7761 ms 5.543 ms 0.61 0.12
File.Copy 1048576 20.679 ms 0.8702 ms 2.4966 ms 20.426 ms 1.00 0.00
CoW 1048576 6.253 ms 0.2658 ms 0.7628 ms 6.152 ms 0.31 0.05
File.Copy 16777216 240.713 ms 4.7471 ms 6.6547 ms 240.357 ms 1.00 0.00
CoW 16777216 7.291 ms 0.3910 ms 1.1217 ms 7.070 ms 0.03 0.01
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CopyOnWrite:

Package Downloads
Microsoft.MSBuildCache.Common

A project cache plugin for MSBuild

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on CopyOnWrite:

Repository Stars
microsoft/MSBuildSdks
MSBuild project SDKs
Version Downloads Last updated
0.4.2 173 11/4/2024
0.4.1 127 10/28/2024
0.4.0 95 10/14/2024
0.3.12 153 10/14/2024
0.3.11 218 9/24/2024
0.3.10 133 9/23/2024
0.3.9 111 9/23/2024
0.3.8 12,025 3/25/2024
0.3.7 8,117 9/11/2023
0.3.6 7,923 7/17/2023
0.3.5 203 7/13/2023
0.3.4 223 7/12/2023
0.3.3 209 7/11/2023
0.3.2 9,024 2/9/2023
0.3.1 342 2/8/2023
0.3.0 1,090 1/30/2023
0.2.2 407 1/27/2023
0.2.1 5,911 9/27/2022
0.2.0 478 9/15/2022
0.1.13 475 9/15/2022
0.1.12 486 9/15/2022
0.1.11 470 9/6/2022
0.1.10 430 9/3/2022
0.1.9 411 9/3/2022
0.1.8 523 4/19/2022
0.1.7 505 4/19/2022
0.1.6 445 4/19/2022
0.1.5 377 10/15/2021
0.1.4 367 10/14/2021
0.1.3 358 10/13/2021
0.1.2 352 10/13/2021
0.1.1 390 10/11/2021
0.1.0 407 7/10/2021