删除文件开头而不重写整个文件 [英] Remove beginning of file without rewriting the whole file

查看:93
本文介绍了删除文件开头而不重写整个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌入式Linux系统,该系统将数据存储在一个很大的文件中,并将新数据附加到末尾.随着文件大小的增加,快要接近可用存储空间,我需要删除最旧的数据.

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 remove oldest data.

问题是,我不能真正接受像往常一样将大量数据上移"文件所需要的中断-锁定文件一段较长的时间只是为了重写它(加上闪存介质,将对闪存造成不必要的磨损.

Problem is, I can't really accept the disruption it would take to move the massive bulk of data "up" the file, like normal - lock the file for an extended period of time just to rewrite it (plus this being a flash medium, it would cause unnecessary wear to the flash).

最简单的方法可能是将文件分割成多个较小的文件,但这与数据的处理和处理方式有关,这有几个缺点-所有客户端"软件都希望使用单个文件. OTOH可以处理将第一条记录切成两半的损坏",因此不需要在记录偏移处修剪文件,而只需在某处"修剪.释放了头几个iNode.无论如何,最旧的数据已经过时了,因此,只要尾巴"保持整洁,并且文件的开头部分可以更精确地删除,就可以完全接受文件开头的更严重损坏-大约几兆字节"就可以了,无需精确到第一个4096KB"精度.

Probably the easiest way would be to split the file into multiple smaller ones, but this has several downsides related to how the data is handled and processed - all the 'client end' software expects single file. OTOH it can handle 'corruption' of having the first record cut in half, so the file doesn't need to be trimmed at record offsets, just 'somewhere up there', e.g. first few iNodes freed. Oldest data is obsolete anyway so even more severe corruption of the beginning of the file is completely acceptable, as long as the 'tail' remains clean, and liberties can be taken with how much exactly is removed - 'roughly several first megabytes' is okay, no need for 'first 4096KB exactly' precision.

是否有一些方法,API,技巧,黑客来截断像这样的文件开头?

Is there some method, API, trick, hack to truncate beginning of file like that?

推荐答案

您可以使用ext4/xfs文件系统的上述Linux内核v3.15来实现这一目标.

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);

在这里看到 在Linux中截断文件的前100MB

这篇关于删除文件开头而不重写整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆