`
Teok
  • 浏览: 147808 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Saving data safely

阅读更多
我们知道,在Android平台中,保存数据有3种方式:local file, preference和sqlite。
在任何系统中,无论是fat、ntfs、ext还是YAFFS等等文件系统,保存数据的操作总不能保证绝对的安全。单以android系统来说,在2.3之前的版本,几乎都是使用YAFFS文件系统的,因为YAFFS系统的buffer等设计,会导致一些即便是调用了write()方法,甚至于调用了close()方法之后,数据都没有被写道flash存储中去。所以从android2.3开始,文件系统将开始迁移到ext4。但是即便使用了ext4之后,还是会存在一些buffering的问题。2.3版本发布之后,会有一些列I/O相关的例子展示该怎么做。
如果你使用preferences和sqlite存储数据,就不用担心buffer的问题。当你需要在flash上存储文件的时候,就要考虑这个问题了。

原文:
The reason this should be of concern to Android developers is that with 2.3, an increasing proportion of devices, notably including the Nexus S, are going to be moving from YAFFS to the ext4 filesystem, which buffers much more aggressively; thus you need to be more assertive about making sure your data gets to permanent storage when you want it to.

If you just use SharedPreferences or SQLite, you can relax, because we’ve made sure they Do The Right Thing about buffering. But if you have your own on-disk format, keep in mind that your data doesn't actually consistently reach the flash chip when you write() it or even when you close() it. There are several layers of buffering between you and the hardware! And because of ext4 buffering policy, any POSIX guarantees that you thought you had before (but actually didn't), you especially don't have now.

Some Android devices are already running non-YAFFS filesystems, but as we brought up the Nexus S, buffering issues have actually bitten us a couple of times in framework code. When the Gingerbread source code becomes available, you’ll find lots of examples of how file I/O should be done.

To start with, for raw data consider using one of the synchronous modes of java.io.RandomAccessFile, which take care of calling fsync() for you in the appropriate way. If you can’t, you’ll want Java code that looks something like this.

     public static boolean sync(FileOutputStream stream) {
         try {
             if (stream != null) {
                 stream.getFD().sync();
             }
             return true;
         } catch (IOException e) {
         }
         return false;

In some applications, you might even want to check the return status of the close() call.

Now of course, there are two sides to this story. When you call fsync() and force the data onto storage, that can be slow; worse, unpredictably slow. So be sure to call it when you need it, but be careful not to call it carelessly.

Background reading if you want to know more:

ext4 and data loss by Jonathan Corbet.

ext4 vs fsync, my take by Alexander Larsson.

Don’t fear the fsync! by Ted Ts’o.
分享到:
评论

相关推荐

    Sharing.Big.Data.Safely.Managing.Data.Security.1491952121.epub

    Providing lock-down security while safely sharing data is a significant challenge for a growing number of organizations. With this book, you’ll discover new options to share data safely without ...

    sap术语表-SAP (Systems, Applications and Products in Data Processing)

    Glossary Information Technology has many key...A technique to input data safely. It ensures application data integrity for background interfaces. It pretends to be someone typing. See also direct input.

    USB.Safely.Remove.安全移除工具

    USB Safely Remove 是一个增强型USB移动设备安全移除工具,比Windows自带的USB设备删除工具功能更强大使用更方便,USB Safely Remove 可以以不同的名称及图标显示多个移动设备,支持自定义隐藏任意USB移动设备,更...

    USB Safely Remove 6.4.2.1298.zip

    USB Safely Remove Portable是一款用于毫不费力地快速断开任何可移动设备的工具。它不仅能节省时间,还能确保在拔掉设备时,所有数据都保留在设备上。Safely Remove是一个系统工具,旨在快速和安全地停止连接到您的...

    USB Safely Remove v6.1.5 USB安全删除(简体中文)+破解

    Not only does it save time, but it ensures that all data remains on a device when it is unplugged. Safely Remove is a system utility designed for quick and safely stopping USB Flash Drives connected ...

    USB Safely Remove 4.1.5.806 多国语言版

    USB Safely Remove是一个可以安全删除USB设备的系统工具. 它使用方便, 可以删除任何USB设备, 可以自动停止设备而不必将其拔出. 该软件还提供设置热键, 防止设备假死, 定制设备显示名称, 插入设备自启动程序等功能....

    USB Safely Remove 5.3.x KeyGen

    USB Safely Remove是一个可以将USB设备安全删除的辅助工具,对于频繁添加或者移除USB设备的用户非常有用,能够有效帮助您解决无法删除USB设备的困扰。USB Safely Remove加入了一个堪称“杀手级”的功能──招回装置...

    USB Safely Remove 5.x KEYmaker

    USB Safely Remove 5.x 注册机

    USB safely

    USB safely

    USB Safely Remove 5.2.4中文版(附注册码).rar

    如果你经常频繁使用USB设备,为了保护硬件避免出现意外情况,建议你安装一个USB安全移除工具USB Safely Remove,能够在设备停止后断开电源,开启该选项后系统会断开USB设备的电源,推荐使用外置硬盘的用户使用,...

    弹出U盘移动硬盘移除工具USB Safely Remove单文件

    有时U盘和移动硬盘无法弹出,这时候就需要这款USB Safely Remove,这里提供一个单文件的绿色版,方便的使用,随时可以调出来弹出usb设备用。

    USB Safely Remove

    另外,USB Safely Remove軟體也支援自訂鍵盤快速鍵的操作模式,我們可以自行設定一個常用的快速鍵如〔Win〕+〔S〕或〔Win〕+〔Z〕...之類的,如果移除清單中只有一個USB外接裝置,當我們按一下快速鍵時,它會直接將...

    USB_Safely_Remove

    如果USB设备经常卸载不了,请使用这个USB设备软件来管理,它功能非常强大。

    USB Safely Remove 5.3.x KeyGen将USB设备安全删除的辅助工具

    USB Safely Remove是一个可以将USB设备安全删除的辅助工具,对于频繁添加或者移除USB设备的用户非常有用,能够有效帮助您解决无法删除USB设备的困扰。USB Safely Remove加入了一个堪称“杀手级”的功能──招回装置...

    USB Safely Remove V4.1.5.806官方免费注册版

    USB Safely Remove是一个可以将USB设备安全删除的系统工具,能够有效帮助您解决无法删除USB设备的困扰。 USB Safely Remove是一个收费软件,网上有很多破解版,但有心的用户会发现,官方曾经举行了为期一周的免费...

    史上最强USB删除工具USB Safely Remove 4.2.5.879 -绿色汉化版(含注册码)

    USB Safely Remove是一个可以将USB设备安全删除的辅助工具,对于频繁添加或者移除USB设备的用户非常有用,能够有效帮助您解决无法删除USB设备的困扰。 USB Safely Remove 可以直接取代操作系统内建的「安全地移除...

    USB Safely Remove(破解版)

    USB Safely Remove v4.14版 让你轻松卸载usb硬盘

    safely unhook,minhook

    Support unhooking safely.

    USB Safely Remove安全删除硬件V5.3中文版.rar

    USB USB Safely Remove是一款增强的安全删除硬件软件,能完全系统自带的USB设备删除工具,实时显示当前连接的USB设备名称,可为每个设备添加停止热键。显示停止菜单中可见设备与隐藏设备。F5热键可扫描硬件改动,...

Global site tag (gtag.js) - Google Analytics