Score:8

Linux/ext4/sqlite: Can touching a file while it's being written to cause a race condition?

ph flag

For context: I need to be able to reliably update the timestamp on some sqlite DB files which get intermittent updates on an ext4 filesystem.

Can using the touch command (to update the last modification time) fail when the database is being written to, or even worse, cause data loss?

Score:11
ca flag

You can indeed touch an open file without corrupting its content, as the filesystem will serialize updates to inode metadata.

If this is safe from an application point of view is another matter entirely, but it should be safe simple because even reading a file can update its metadata (ie: when the last read is older than one day or when not using noatime, relatime or lazytime).

That said, if SQLite uses mtime for some internal bookkeeping it can be confused by your metadata changes. This is especially true if using touch for setting a past timestamp, which is generally a bad idea.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.