Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch a write to a file and make a copy before written to disk?

I want my program to be able to make a copy of a file before another program changes it's contents. I looked into FileSystemWatcher, but I don't think that will be able to help me, as I didn't see any thing about being able to catch or prevent events.

One solution I thought of was detecting when a program open a file with write privileges, and making a copy in memory at that point. I don't know how to detect when a file is opened by another program though.

Incase my desired results are unclear, here is an example. Audio files are linked to the music folders of multiple users on a system to save disk space. If a user want to modify the metadata of a music file, or apply some sort audio effect to a file, the modification shouldn't effect the other user's copies. The user should automatically get his own copy upon making a change.

like image 586
drdrez Avatar asked Nov 20 '25 07:11

drdrez


1 Answers

Thanks to @RaymondChen for mentioning the Volume Shadow Copy service in the comments!

This is exactly what I need. Relating back to my example in my original post; after the audio files are linked to user's directories, my program can create a shadow copy of the volume so that after a change to a linked file, my program can restore the shadowed copy, and move the modified file to respective user's directory.

like image 177
drdrez Avatar answered Nov 23 '25 04:11

drdrez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!