View Single Post
Old 11-08-2014, 07:32 AM   #95
ShellShock
Wizard
ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.
 
ShellShock's Avatar
 
Posts: 1,178
Karma: 2431850
Join Date: Sep 2008
Device: IPad Mini 2 Retina
Question Possible fix to "database disk image is malformed" error

Further to my previous post, I have been studying the Calibre library function, copy_from_idevice (in libimobiledevice.py) that copies a file from the idevice to your PC. This function is used by the IOS reader application (IOSRA) plugin to copy Marvin's SQLLite database, mainDb.sqlite, to your PC, so the plugin can query it to find out what books you have on your device.

A potential problem with copy_from_idevice is that it uses a read lock to read the remote file (Marvins's mainDb.sqlite on the idevice). That means Marvin could still be updating the mainDb.sqlite whilst IOSRA is merrily copying it, and thus you end up with a partial copy on your PC, and you get the "database disk image is malformed" error.

I have now rewritten copy_from_idevice to use a write lock. If Marvin still has mainDb.sqlite open, then copy_from_idevice will fail to open it, wait for a second, and then retry, up to 60 times. I have also added some safety code to ensure file handles are released if there is some other error whilst copying.

The fix should work if the idevice correctly obeys file locking with a write lock, and that SQLLite is also using a write lock.

I don't want to submit this code change into the Calibre GitHub until I know it fixes the bug, and as I have never been able to repro the bug myself, I would be grateful if someone here could test the fix for me. I have packaged up the fix into a replacement for the entire Calibre python code, pylib.zip. Please install as follows:
  1. Make sure you are on Calibre 2.8, preferably 64 bit (not sure if that matters). The fix may work with Calibre 2.9, but I have not tested it.
  2. Take a back up copy of C:\Program Files\Calibre\pylib.zip.
  3. Download my new version from my dropbox: https://www.dropbox.com/s/y03anlaxhh...pylib.zip?dl=0 (50 MB, too big to attach here).
  4. Copy the new version to C:\Program Files\Calibre.
  5. Preferably run Calibre with debugging on, and upload a tonne of books to Marvin!

If the fix does not work I have something else up my sleeve...

Many thanks
ShellShock is offline   Reply With Quote