0

assuming I have a (stock) android 4.x tablet connected to a microcontroller board via USB. The board is able to access the SD-card as USB mass storage and reads/writes to a file on the card. Now there should be an app on the tablet, which reads/writes the same file like the board does. So there is simultaneous read/write file-access from the board and the app.

To avoid inconsistent data etc. I would use exclusive FileLock when the app reads/writes to the file. Now my question is, if FileLock "is low-level enough" to also work in this case and prevent inconsistent data?

THX

2 Answers2

0

While the use of FileLock ensures that two applications cannot write to the file at the same time, application logic could foil the scheme. You still have to code correctly. In particular, if one app reads the file, makes changes and then "waits" to get a file lock to write the file -- then it might write "old" information back to the file.

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
  • thx, I assume that the coding and use of FileLock will be done correctly. The question is, if the FileLock mechanism will be aware of the access to the file via USB/microcontrollerboard? In other words: Is FileLock useless, if a file is accessed via USB attached device in parallel? – jOliver Mar 15 '14 at 16:21
0

Found this in the documentation of FileLock :

Locks are intended to be true platform operating system file locks, and therefore locks held by the VM will be visible to other operating system processes.

I guess the access to SDCard via the USB is governed by some Operating System's Process, so the lock should be visible to the micro-controller as well.

gkapagunta
  • 188
  • 1
  • 11
  • yes, also read this but was not quite convinced if the android usb driver could perhaps bypass the FileLock mechanism and write/read data from the usb to the file although my App holds the FileLock. Probably some android OS expert can dispel the doubt. – jOliver Mar 15 '14 at 19:48