Remove beginning of file without rewriting the whole file

前端 未结 3 1018
栀梦
栀梦 2020-12-29 06:21

I have an embedded Linux system, that stores data in a very large file, appending new data to the end. As the file size grows near filling available storage space, I need to

相关标签:
3条回答
  • 2020-12-29 06:44

    You can achieve the goal with Linux kernel v3.15 above for ext4/xfs file system.

    int ret = fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, 4096);
    

    See here Truncating the first 100MB of a file in linux

    0 讨论(0)
  • 2020-12-29 06:55

    What about setting up a separate process that renames the output file when it reaches a predefined size (for instance by adding the linux time at the end of the file name).

    This would allow you to keep the old data and the main process will recreate the output file the next time it writes to it.

    Another cron job may remove the old file every now and then.

    0 讨论(0)
  • 2020-12-29 07:08

    The easiest solution for your old applications would be a FUSE filesystem which gives them access to the underlying file, but with the offset cyclically shifted. This would allow you to implement a ringbuffer at the physical level. The FUSE layer would be fairly trivial as it only needs to adjust all filepositions by a constant, modulo filesize.

    0 讨论(0)
提交回复
热议问题