0

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.

eocron
  • 6,885
  • 1
  • 21
  • 50
  • I don't think it is possible any other way if the source might be deleted at any time. – Jerodev Aug 21 '17 at 12:45
  • It's unclear what you are asking. You cannot copy a file "just for reading it". If you need to copy it, you will have to copy it completely or read it in-memory – Camilo Terevinto Aug 21 '17 at 12:45
  • If you want a real copy, of course, the whole file is copied. Do you need something lile hard or soft links? – T_D Aug 21 '17 at 12:46
  • Yes, I need some kind of link which point to constant file (it's content not changed). And persist as long as have references pointed to it. Basically, like objects in .NET - live as long as needed. – eocron Aug 21 '17 at 12:46
  • If the file can be deleted at any time and you just need to read it, why not just load it into memory and forget about it? – Camilo Terevinto Aug 21 '17 at 12:49
  • It can be quite large file - around 2gb or so. It will not fit into memory. – eocron Aug 21 '17 at 12:49
  • What type of file is it? Text? Binary? How do you use it? – AJ X. Aug 21 '17 at 12:52
  • You could create a hardlink. In this case, when the other process "deletes" the original "file" it is only deleting the junction to the file...the contents would remain as long as there is at least one hard link to the file contents. – Aron Aug 21 '17 at 12:54
  • Umm, how it is even matter what file it is? Just bytes, name is guid. – eocron Aug 21 '17 at 12:54
  • It matters because you could stream it without loading all of it's contents at once into memory. – AJ X. Aug 21 '17 at 12:55
  • Umm, no it doesn't. I already mentioned that file over 1 gb. – eocron Aug 21 '17 at 12:55
  • 1
    @eocron: Read the last comment again: "... **without loading all of its contents at once into memory.**" – Flater Aug 21 '17 at 12:58
  • I still don't get why it is matter. I can stream it, yes, but Im creating files because other applications can't read it from custom stream. Anyway, Aron has good point about using hard links from kernel. I will try it out and ask later. – eocron Aug 21 '17 at 12:59
  • Hard links are the way to go. Look [here](https://stackoverflow.com/questions/3387690/how-to-create-a-hardlink-in-c) for an example in C#. – Icemanind Aug 21 '17 at 13:02

1 Answers1

3

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.

Aron
  • 15,464
  • 3
  • 31
  • 64