Konubinix' opinionated web of thoughts

Recover Delete File Still in Use (Open)

Fleeting

Imagine a big video file is being processed (using ffmpeg), but the working directory has been deleted. The output will be lost because it will try to put it in the lost directory.

ps aux|grep ffmpeg
sam       411747  588  0.8 2422380 270804 pts/7  RLl+ 09:47 2104:51 ffmpeg -fix_sub_duration -i a.mp4 ... /tmp/tmp.vd3TbjmM22/out.webm
lsof -p 411747 | grep deleted
ffmpeg  411747  sam  cwd    DIR  259,2          0 28491977 somefolder/a (deleted)
ffmpeg  411747  sam    3r   REG  259,2 3812546616 28488892 somefolder/a/a.mp4 (deleted)
ls -la /proc/411747/fd
total 0
dr-x------ 2 sam sam  0 Aug 10 15:41 .
dr-xr-xr-x 9 sam sam  0 Aug 10 09:47 ..
lr-x------ 1 sam sam 64 Aug 10 15:41 0 -> /dev/null
lrwx------ 1 sam sam 64 Aug 10 15:41 1 -> /dev/pts/7
lrwx------ 1 sam sam 64 Aug 10 15:41 2 -> /dev/pts/7
lr-x------ 1 sam sam 64 Aug 10 15:41 3 -> somefolder/a/a.mp4 (deleted)
l-wx------ 1 sam sam 64 Aug 10 15:41 5 -> /tmp/tmp.vd3TbjmM22/out.webm

It looks like a broken symlink, but actually,

rsync -La /proc/411747/fd/3 newlocation

Gets the file back.

Also, I can open the output file to prevent it from being removed when the program finished.

In the python repl, simply run.

from pathlib import Path
f = Path("/tmp/tmp.vd3TbjmM22/out.webm").open()

Now, let the python repl open to prevent the file from being lost when ffmpeg ends.

ps aux|grep python
sam      2470611  0.5  0.1 295228 64976 pts/0    Sl+  15:47   0:01 /bin/python
ls -la /proc/2470611/fd
total 0
dr-x------ 2 sam sam  0 Aug 10 15:49 .
dr-xr-xr-x 9 sam sam  0 Aug 10 15:47 ..
lrwx------ 1 sam sam 64 Aug 10 15:49 0 -> /dev/pts/0
lrwx------ 1 sam sam 64 Aug 10 15:49 1 -> /dev/pts/0
lrwx------ 1 sam sam 64 Aug 10 15:49 10 -> socket:[5356603]
lr-x------ 1 sam sam 64 Aug 10 15:49 11 -> /tmp/tmp.vd3TbjmM22/out.webm
lrwx------ 1 sam sam 64 Aug 10 15:49 2 -> /dev/pts/0
lrwx------ 1 sam sam 64 Aug 10 15:49 5 -> anon_inode:[eventpoll]
lrwx------ 1 sam sam 64 Aug 10 15:49 6 -> socket:[5354825]
lrwx------ 1 sam sam 64 Aug 10 15:49 7 -> socket:[5354826]
lrwx------ 1 sam sam 64 Aug 10 15:49 8 -> anon_inode:[eventpoll]
lrwx------ 1 sam sam 64 Aug 10 15:49 9 -> socket:[5356602]

Now, when ffmpeg finishes, simply run

rsync -aL /proc/2470611/fd/11 output.webm