how to mock the deletion of a file? (linux)

In order to run some tests, I need to run an app that will delete a file

Since it's expensive for me to recreate this file (>40 GBs) every time I need to run another test, I want to mock the deletion of this file.

But this must be completly transparent to the app deleting it. That means creating a symlink is not an ok solution ([ -L symlink ] returns 0 while [ -L file ] returns 1, so this wouldn't be completly transparent for the app).


There are several solutions. dd like solution may take some time, however using seek

dd of=mybigfile bs=1 count=0 seek=40G

is instant. however the 40g space taken by the command is not seen in df...

So the best is probably

fallocate -l 40g mybigfile

that creates instantly mybigfile and the space is actually reserved from the FS (and is seen in df)

-- edit after comment --

What about a hard link

cd data 
make 40g file "myfile"

then for each test

cd ../test 
ln ../data/myfile testfile

command to unlink testfile

data and test dirs being on the same FS (different dirs to prevent mistakes etc..).

The hard link status is pretty transparent and is not likely to be tested by the application . The unlink operation will remove the name associated with the inode and then decrement the links counter in the inode, and should be very fast (the file and its data will be actually freed only when the inode counter goes down to zero - which will not happen unless you unlink also data/myfile).


Use a filesystem that supports snapshots. ZFS and btrfs come to mind. Just take a snap with the file present, and then restore that snapshot after each test.

Both of these filesystems take snapshots in such a way that restoring should be very fast.


For things like this, I prefer to use an LD_PRELOAD trick. Write a tiny C library that mocks unlink (and maybe stat64 if the test also checks for file existence) and preload it. Test it on a less important file :)