突然断电后文件中的垃圾数据
我使用的是FAT32系统的闪存。我使用 rtos(SMX) 的文件系统 API 不断将数据写入文件。但是,突然断电后,该文件会在系统重新启动时在第一个文件条目上方包含垃圾值。
我运行 chkdsk 实用程序,但它没有解决任何问题。
知道如何在不干净的断电情况下清除这些垃圾条目吗?
I am using a flash with FAT32 system. I am continuosly writing data to a file using file system APIs from rtos(SMX). However, after sudden poweroffs, the file contains garbage values just above the first file entry on system reboot.
I run chkdsk utility, but it doesn't fix any problem.
Any idea how can i get rid of these garbage entries even on unclean power offs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您预计会突然断电,则需要禁用文件写入时的所有缓存/缓冲。当然,您还需要处理部分写入的文件,但这至少应该防止尾随垃圾。
我不知道您正在使用的 API,但这可以通过“同步”安装驱动器(例如,Linux 中的
mount -osync
)或通过使用特定选项打开单个文件来完成。但是,如果您在单个文件写入时禁用缓冲,您仍然可能面临损坏 FAT 并丢失所有文件的风险。If you expect sudden power loss, you'll need to disable all caching/buffering on file writes. Of course you'll also need to deal with partially-written files, but that should at least prevent trailing garbage.
I don't know the API you're using, but this might be done by mounting the drive "synchronously" (e.g.,
mount -o sync
in Linux) or by opening individual files with specific options. If you do disable buffering on individual file writes, you may still run the risk of corrupting the FAT, however, and losing all the files.