eocron
eocron

Reputation: 7546

Copy file for read only operations?

I want to copy some file to a bunch of other paths, so I doing this:

File.Copy(instancePath, destPath);

but the problem is that File.Copy is just too slow (I assume it copies file entirely). Also, I don't need to change destination files, so it is read-only references, but source can be deleted at any time after copy is completed.

Is there no other way than manually copy entire content for just read operations?

PS

Files quite large (> 1gb), so copying it in 10 places just for read - too expensive.

Upvotes: 0

Views: 141

Answers (1)

Aron
Aron

Reputation: 15772

You don't copy the file. You create a hard link to the file.

We tend to think of a file as an atomic entity on your storage device. This view is quite wrong. Files come in multiple parts, the metadata, and the contents.

In windows, in general, we only have 1 set of metadata (inode) per file content. However with NTFS it is quite possible to create multiple inodes resulting in the file being accessible in multiple places.

NTFS will keep track of how many inodes there are for each file, and will only garbage collect on files without any inodes. Therefore, your write process can delete it's inode for the file without affecting your read process, if you read process can create a hard link to the original file.

The process of creating a hard link is extremely quick as it is only needing to write a few kB of data.

Upvotes: 3

Related Questions