In the following code:
#!/bin/bash
if [ ! -f "$file" ]
then
stat --printf="%s" "$file"
cat "$file"
else
echo -1
fi
$file
is the name of a binary file that could be deleted at any point.
My biggest fear is that the file could be deleted after [ ! -f "$file" ]
but before cat "$file"
is executed and the result would be incorrect.
But I also wonder what will happen if the file is deleted during the execution of Edit: https://stackoverflow.com/a/2031100/4503330cat "$file"
. Will it be fully/partially outputted, is there a risk to read unrelated characters if $file
get overwritten on the drive? man cat
does not explain that.
How can I guarantee that the output is either?
- The size of the file, followed by a new line and the content of the file
-1
Note: the size of the file could be up to 5MiB and making a copy of it would be too slow.
Edit: The file is created with ffmpeg ... -window_size 5 -extra_window_size 0 -min_seg_duration 2000000 -f dash ...
which in my case keep up to 5 files at the time in a particular directory, they never reuse the same name and they follow this cycle (entirely controlled by ffmpeg) : 1) created with .tmp extension 2) renamed without .tmp 3) (at least 10 seconds later) deleted