- Let's have any stream of binary data (like
/dev/random
,/dev/zero
, etc.). - Let's have a file which size can be N at maximum.
- Let's the N is in order of gigabytes.
Is there any elegant method/technology-to-use/hack in linux, to achieve such file continuously written is at maximum N bytes long and always contains only last written data (sequentially)? This means no large moves (file to file, memory to file) just little tweaking with the last/first data blocks.
The trick I'm looking for is the file beginning moving forward and effectively forgetting any too-old content (which would increase the file size over N) - content rotation.
The desired principle could be expressed as:
inp = fopen(stream, "r");
out = fopen(file, "wN"); // "Special open" with maximal size of N
while (is_reading)
{
if (rd = fread(buff, block_size, 1, inp))
{
fwrite(buff, rd, 1, out); // Old content forgotten
fflush(out); // Allow others to instantly see the current content
}
}
fclose(inp);
fclose(out);